提交 38311adc 编写于 作者: B Benjamin Pasero

add and use writeFileAndFlushSync

上级 d691f07a
......@@ -2718,9 +2718,9 @@ declare module "fs" {
export function writeFile(filename: string | number, data: any, encoding: string, callback: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string | number, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string | number, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFileSync(filename: string, data: any, encoding: string): void;
export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void;
export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void;
export function writeFileSync(filename: string | number, data: any, encoding: string): void;
export function writeFileSync(filename: string | number, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void;
export function writeFileSync(filename: string | number, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void;
export function appendFile(filename: string, data: any, encoding: string, callback: (err: NodeJS.ErrnoException) => void): void;
export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
......
......@@ -363,6 +363,35 @@ export function writeFileAndFlush(path: string, data: string | NodeBuffer, optio
});
}
export function writeFileAndFlushSync(path: string, data: string | NodeBuffer, options?: { mode?: number; flag?: string; }): void {
if (!canFlush) {
return fs.writeFileSync(path, data, options);
}
if (!options) {
options = { mode: 0o666, flag: 'w' };
}
// Open the file with same flags and mode as fs.writeFile()
const fd = fs.openSync(path, options.flag, options.mode);
try {
// It is valid to pass a fd handle to fs.writeFile() and this will keep the handle open!
fs.writeFileSync(fd, data);
// Flush contents (not metadata) of the file to disk
try {
fs.fdatasyncSync(fd);
} catch (syncError) {
console.warn('[node.js fs] fdatasyncSync is now disabled for this session because it failed: ', syncError);
canFlush = false;
}
} finally {
fs.closeSync(fd);
}
}
/**
* Copied from: https://github.com/Microsoft/vscode-node-debug/blob/master/src/node/pathUtilities.ts#L83
*
......
......@@ -209,6 +209,31 @@ suite('Extfs', () => {
});
});
test('writeFileAndFlushSync', function (done: () => void) {
const id = uuid.generateUuid();
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
const newDir = path.join(parentDir, 'extfs', id);
const testFile = path.join(newDir, 'flushed.txt');
mkdirp(newDir, 493, error => {
if (error) {
return onError(error, done);
}
assert.ok(fs.existsSync(newDir));
extfs.writeFileAndFlushSync(testFile, 'Hello World', null);
assert.equal(fs.readFileSync(testFile), 'Hello World');
const largeString = (new Array(100 * 1024)).join('Large String\n');
extfs.writeFileAndFlushSync(testFile, largeString, null);
assert.equal(fs.readFileSync(testFile), largeString);
extfs.del(parentDir, os.tmpdir(), done, ignore);
});
});
test('realcase', (done) => {
const id = uuid.generateUuid();
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
......
......@@ -11,10 +11,10 @@ import { ParsedArgs } from 'vs/platform/environment/common/environment';
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
import * as fs from 'fs';
import * as paths from 'path';
import * as os from 'os';
import { whenDeleted } from 'vs/base/node/pfs';
import { writeFileAndFlushSync } from 'vs/base/node/extfs';
function shouldSpawnCliProcess(argv: ParsedArgs): boolean {
return !!argv['install-source']
......@@ -66,7 +66,7 @@ export function main(argv: string[]): TPromise<void> {
let waitMarkerError: Error;
const randomTmpFile = paths.join(os.tmpdir(), Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10));
try {
fs.writeFileSync(randomTmpFile, '');
writeFileAndFlushSync(randomTmpFile, '');
waitMarkerFilePath = randomTmpFile;
argv.push('--waitMarkerFilePath', waitMarkerFilePath);
} catch (error) {
......
......@@ -7,7 +7,6 @@ import { localize } from 'vs/nls';
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
import * as path from 'path';
import * as fs from 'fs';
import { TPromise } from 'vs/base/common/winjs.base';
import { sequence } from 'vs/base/common/async';
......@@ -30,7 +29,7 @@ import { RequestService } from 'vs/platform/request/node/requestService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender';
import { mkdirp } from 'vs/base/node/pfs';
import { mkdirp, writeFile } from 'vs/base/node/pfs';
import { IChoiceService } from 'vs/platform/message/common/message';
import { ChoiceCliService } from 'vs/platform/message/node/messageCli';
import { getBaseLabel } from 'vs/base/common/labels';
......@@ -77,10 +76,9 @@ class Main {
}
private setInstallSource(installSource: string): TPromise<any> {
return new TPromise<void>((c, e) => {
const path = getInstallSourcePath(this.environmentService.userDataPath);
fs.writeFile(path, installSource.slice(0, 30), 'utf8', err => err ? e(err) : c(null));
});
const path = getInstallSourcePath(this.environmentService.userDataPath);
return writeFile(path, installSource.slice(0, 30));
}
private listExtensions(showVersions: boolean): TPromise<any> {
......
......@@ -309,7 +309,7 @@ export class BackupMainService implements IBackupMainService {
fs.mkdirSync(this.backupHome);
}
fs.writeFileSync(this.workspacesJsonPath, JSON.stringify(this.backups));
extfs.writeFileAndFlushSync(this.workspacesJsonPath, JSON.stringify(this.backups));
} catch (ex) {
this.logService.error(`Backup: Could not save workspaces.json: ${ex.toString()}`);
}
......
......@@ -9,6 +9,7 @@ import * as path from 'path';
import * as fs from 'original-fs';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { writeFileAndFlushSync } from 'vs/base/node/extfs';
export const IStorageService = createDecorator<IStorageService>('storageService');
......@@ -84,7 +85,7 @@ export class StorageService implements IStorageService {
private save(): void {
try {
fs.writeFileSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here
writeFileAndFlushSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here
} catch (error) {
if (this.environmentService.verbose) {
console.error(error);
......
......@@ -11,9 +11,9 @@ import { isParent } from 'vs/platform/files/common/files';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { extname, join, dirname, isAbsolute, resolve } from 'path';
import { mkdirp, writeFile, readFile } from 'vs/base/node/pfs';
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { readFileSync, existsSync, mkdirSync } from 'fs';
import { isLinux, isMacintosh } from 'vs/base/common/platform';
import { delSync, readdirSync } from 'vs/base/node/extfs';
import { delSync, readdirSync, writeFileAndFlushSync } from 'vs/base/node/extfs';
import Event, { Emitter } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
import { isEqual } from 'vs/base/common/paths';
......@@ -136,7 +136,7 @@ export class WorkspacesMainService implements IWorkspacesMainService {
mkdirSync(configParent);
writeFileSync(workspace.configPath, JSON.stringify(storedWorkspace, null, '\t'));
writeFileAndFlushSync(workspace.configPath, JSON.stringify(storedWorkspace, null, '\t'));
return workspace;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册