Skip to content

Commit dbdb424

Browse files
author
Benjamin Pasero
committed
files - use proper flag for writing in extfs
1 parent f4eae9a commit dbdb424

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

src/vs/base/node/extfs.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,23 @@ export interface IWriteFileOptions {
366366
};
367367
}
368368

369+
interface IEnsuredWriteFileOptions extends IWriteFileOptions {
370+
mode: number;
371+
flag: string;
372+
}
373+
369374
let canFlush = true;
370375
export function writeFileAndFlush(path: string, data: string | Buffer | NodeJS.ReadableStream | Uint8Array, options: IWriteFileOptions, callback: (error?: Error) => void): void {
371-
options = ensureOptions(options);
376+
const ensuredOptions = ensureWriteOptions(options);
372377

373378
if (typeof data === 'string' || Buffer.isBuffer(data) || data instanceof Uint8Array) {
374-
doWriteFileAndFlush(path, data, options, callback);
379+
doWriteFileAndFlush(path, data, ensuredOptions, callback);
375380
} else {
376-
doWriteFileStreamAndFlush(path, data, options, callback);
381+
doWriteFileStreamAndFlush(path, data, ensuredOptions, callback);
377382
}
378383
}
379384

380-
function doWriteFileStreamAndFlush(path: string, reader: NodeJS.ReadableStream, options: IWriteFileOptions, callback: (error?: Error) => void): void {
385+
function doWriteFileStreamAndFlush(path: string, reader: NodeJS.ReadableStream, options: IEnsuredWriteFileOptions, callback: (error?: Error) => void): void {
381386

382387
// finish only once
383388
let finished = false;
@@ -468,7 +473,7 @@ function doWriteFileStreamAndFlush(path: string, reader: NodeJS.ReadableStream,
468473
// not in some cache.
469474
//
470475
// See https://github.com/nodejs/node/blob/v5.10.0/lib/fs.js#L1194
471-
function doWriteFileAndFlush(path: string, data: string | Buffer | Uint8Array, options: IWriteFileOptions, callback: (error?: Error) => void): void {
476+
function doWriteFileAndFlush(path: string, data: string | Buffer | Uint8Array, options: IEnsuredWriteFileOptions, callback: (error?: Error) => void): void {
472477
if (options.encoding) {
473478
data = encode(data instanceof Uint8Array ? Buffer.from(data) : data, options.encoding.charset, { addBOM: options.encoding.addBOM });
474479
}
@@ -478,7 +483,7 @@ function doWriteFileAndFlush(path: string, data: string | Buffer | Uint8Array, o
478483
}
479484

480485
// Open the file with same flags and mode as fs.writeFile()
481-
fs.open(path, typeof options.flag === 'string' ? options.flag : 'r', options.mode, (openError, fd) => {
486+
fs.open(path, options.flag, options.mode, (openError, fd) => {
482487
if (openError) {
483488
return callback(openError);
484489
}
@@ -506,18 +511,18 @@ function doWriteFileAndFlush(path: string, data: string | Buffer | Uint8Array, o
506511
}
507512

508513
export function writeFileAndFlushSync(path: string, data: string | Buffer, options?: IWriteFileOptions): void {
509-
options = ensureOptions(options);
514+
const ensuredOptions = ensureWriteOptions(options);
510515

511-
if (options.encoding) {
512-
data = encode(data, options.encoding.charset, { addBOM: options.encoding.addBOM });
516+
if (ensuredOptions.encoding) {
517+
data = encode(data, ensuredOptions.encoding.charset, { addBOM: ensuredOptions.encoding.addBOM });
513518
}
514519

515520
if (!canFlush) {
516-
return fs.writeFileSync(path, data, { mode: options.mode, flag: options.flag });
521+
return fs.writeFileSync(path, data, { mode: ensuredOptions.mode, flag: ensuredOptions.flag });
517522
}
518523

519524
// Open the file with same flags and mode as fs.writeFile()
520-
const fd = fs.openSync(path, typeof options.flag === 'string' ? options.flag : 'r', options.mode);
525+
const fd = fs.openSync(path, ensuredOptions.flag, ensuredOptions.mode);
521526

522527
try {
523528

@@ -536,22 +541,16 @@ export function writeFileAndFlushSync(path: string, data: string | Buffer, optio
536541
}
537542
}
538543

539-
function ensureOptions(options?: IWriteFileOptions): IWriteFileOptions {
544+
function ensureWriteOptions(options?: IWriteFileOptions): IEnsuredWriteFileOptions {
540545
if (!options) {
541546
return { mode: 0o666, flag: 'w' };
542547
}
543548

544-
const ensuredOptions: IWriteFileOptions = { mode: options.mode, flag: options.flag, encoding: options.encoding };
545-
546-
if (typeof ensuredOptions.mode !== 'number') {
547-
ensuredOptions.mode = 0o666;
548-
}
549-
550-
if (typeof ensuredOptions.flag !== 'string') {
551-
ensuredOptions.flag = 'w';
552-
}
553-
554-
return ensuredOptions;
549+
return {
550+
mode: typeof options.mode === 'number' ? options.mode : 0o666,
551+
flag: typeof options.flag === 'string' ? options.flag : 'w',
552+
encoding: options.encoding
553+
};
555554
}
556555

557556
/**

0 commit comments

Comments
 (0)