Skip to content

Commit f28d4f1

Browse files
authored
feat(mode): support overwriteMode (#3)
1 parent 5208999 commit f28d4f1

File tree

3 files changed

+87
-8
lines changed

3 files changed

+87
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pnpm add @oomol-lab/sparse-file
3131
* `filepath`: string - Sparse file paths to create/resize
3232
* `size`: number - Sparse file size
3333
* `options?.safe`: boolean - Safe mode, default: true
34-
* `options?.mode`: number - File mode, default: 0o644
34+
* `options?.mode`: number - File mode. By default, the mode is only set when creating a file, unless overwriteMode is specified. default: 0o644
35+
* `options?.overwriteMode`: boolean - Overwrite mode. When the overwrite mode is allowed, the mode will be set even if the file exists. default: false
3536

3637
In safe mode, an error will occur if the size is less than 0 or greater than `Number.MAX_SAFE_INTEGER`. Additionally, it will be rejected if the passed size is larger than the current file size.
3738

src/index.test.ts

+71-6
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,79 @@ describe("create sparse without safe mode", () => {
121121
});
122122
});
123123

124-
it.concurrent<TestContext>("create sparse with custom mode", async (ctx) => {
125-
await createSparse(ctx.file, 1, {
126-
mode: 0o600,
124+
describe("custom mode", () => {
125+
it.concurrent<TestContext>("create sparse with custom mode", async (ctx) => {
126+
await createSparse(ctx.file, 1, {
127+
mode: 0o600,
128+
});
129+
130+
const stat = await fs.stat(ctx.file);
131+
132+
expect(stat.mode & 0o777).eq(0o600);
133+
});
134+
135+
it.concurrent<TestContext>("should overwrite mode", async (ctx) => {
136+
{
137+
const fh = await fs.open(ctx.file, "w", 0o644);
138+
await fh.writeFile(Buffer.alloc(1));
139+
await fh.sync();
140+
await fh.close();
141+
}
142+
143+
await createSparse(ctx.file, 10, {
144+
mode: 0o600,
145+
overwriteMode: true,
146+
});
147+
const stat = await fs.stat(ctx.file);
148+
expect(stat.mode & 0o777).eq(0o600);
149+
});
150+
151+
it.concurrent<TestContext>("should overwrite mode when size same", async (ctx) => {
152+
{
153+
const fh = await fs.open(ctx.file, "w", 0o644);
154+
await fh.writeFile(Buffer.alloc(1));
155+
await fh.sync();
156+
await fh.close();
157+
}
158+
159+
await createSparse(ctx.file, 1, {
160+
mode: 0o600,
161+
overwriteMode: true,
162+
});
163+
const stat = await fs.stat(ctx.file);
164+
expect(stat.mode & 0o777).eq(0o600);
127165
});
128166

129-
const stat = await fs.stat(ctx.file);
167+
it.concurrent<TestContext>("should maintain the original mode", async (ctx) => {
168+
{
169+
const fh = await fs.open(ctx.file, "w", 0o644);
170+
await fh.writeFile(Buffer.alloc(1));
171+
await fh.sync();
172+
await fh.close();
173+
}
174+
175+
await createSparse(ctx.file, 10, {
176+
mode: 0o600,
177+
overwriteMode: true,
178+
});
179+
const stat = await fs.stat(ctx.file);
180+
expect(stat.mode & 0o777).eq(0o600);
181+
});
130182

131-
expect(stat.mode & 0o777).eq(0o600);
183+
it.concurrent<TestContext>("should overwrite mode when use default mode", async (ctx) => {
184+
{
185+
const fh = await fs.open(ctx.file, "w", 0o600);
186+
await fh.writeFile(Buffer.alloc(1));
187+
await fh.sync();
188+
await fh.close();
189+
}
190+
191+
await createSparse(ctx.file, 1, {
192+
overwriteMode: true,
193+
});
194+
const stat = await fs.stat(ctx.file);
195+
expect(stat.mode & 0o777).eq(0o644);
196+
});
132197
});
133198

134199
describe.concurrent("physical file size", () => {
@@ -138,7 +203,7 @@ describe.concurrent("physical file size", () => {
138203
expect(await physicalFileSize(ctx.file)).eq(0);
139204
});
140205

141-
it<TestContext>("should return 0 when file is empty sparsefile", async (ctx) => {
206+
it<TestContext>("should return actual size when file not is empty sparsefile", async (ctx) => {
142207
const fh = await fs.open(ctx.file, "w");
143208
const { blksize } = await fh.stat();
144209
await fh.write(Buffer.alloc(1));

src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ export interface SparseOptions {
77
*/
88
safe?: boolean;
99
/**
10+
* By default, the mode is only set when creating a file, unless overwriteMode is specified.
1011
* @default 0o644
1112
*/
1213
mode?: number;
14+
/**
15+
* When the overwrite mode is allowed, the mode will be set even if the file exists.
16+
* @default false
17+
*/
18+
overwriteMode?: boolean;
1319
}
1420

1521
export const createSparse = async (filepath: string, size: number, options?: SparseOptions): Promise<void> => {
16-
const fh = await open(filepath, options?.mode || 0o644);
22+
const mode = options?.mode || 0o644;
23+
const fh = await open(filepath, mode);
1724

1825
const curState = await fs.stat(filepath);
1926
if (options?.safe !== false) {
@@ -33,6 +40,9 @@ export const createSparse = async (filepath: string, size: number, options?: Spa
3340
}
3441

3542
if (curState.size === size) {
43+
if (options?.overwriteMode) {
44+
await fh.chmod(mode);
45+
}
3646
await fh.close();
3747
return;
3848
}
@@ -45,6 +55,9 @@ export const createSparse = async (filepath: string, size: number, options?: Spa
4555
}
4656

4757
await fh.truncate(size);
58+
if (options?.overwriteMode) {
59+
await fh.chmod(mode);
60+
}
4861
await fh.sync();
4962
await fh.close();
5063
};

0 commit comments

Comments
 (0)