Skip to content

Commit

Permalink
files2 - support overwrite readonly for local files
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Apr 10, 2019
1 parent 9319cc2 commit 4cce7c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/vs/workbench/services/textfile/common/textFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { IModeService } from 'vs/editor/common/services/modeService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { coalesce } from 'vs/base/common/arrays';
import { trim } from 'vs/base/common/strings';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';

/**
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
Expand Down Expand Up @@ -993,6 +992,4 @@ export class TextFileService extends Disposable implements ITextFileService {

super.dispose();
}
}

registerSingleton(ITextFileService, TextFileService);
}
34 changes: 34 additions & 0 deletions src/vs/workbench/services/textfile/node/textFileService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TextFileService } from 'vs/workbench/services/textfile/common/textFileService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { URI } from 'vs/base/common/uri';
import { ITextSnapshot, IWriteTextFileOptions, IFileStatWithMetadata } from 'vs/platform/files/common/files';
import { Schemas } from 'vs/base/common/network';
import { exists, stat, chmod } from 'vs/base/node/pfs';

export class NodeTextFileService extends TextFileService {

async write(resource: URI, value: string | ITextSnapshot, options?: IWriteTextFileOptions): Promise<IFileStatWithMetadata> {

// check for overwriteReadonly property (only supported for local file://)
try {
if (options && options.overwriteReadonly && resource.scheme === Schemas.file && await exists(resource.fsPath)) {
const fileStat = await stat(resource.fsPath);

// try to change mode to writeable
await chmod(resource.fsPath, fileStat.mode | 128);
}
} catch (error) {
// ignore and simply retry the operation
}

return super.write(resource, value, options);
}
}

registerSingleton(ITextFileService, NodeTextFileService);
2 changes: 1 addition & 1 deletion src/vs/workbench/workbench.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import 'vs/workbench/services/preferences/browser/preferencesService';
import 'vs/workbench/services/output/node/outputChannelModelService';
import 'vs/workbench/services/configuration/common/jsonEditingService';
import 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
import 'vs/workbench/services/textfile/common/textFileService';
import 'vs/workbench/services/textfile/node/textFileService';
import 'vs/workbench/services/dialogs/browser/fileDialogService';
import 'vs/workbench/services/dialogs/electron-browser/dialogService';
import 'vs/workbench/services/backup/node/backupFileService';
Expand Down
6 changes: 4 additions & 2 deletions src/vs/workbench/workbench.nodeless.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ import { ContextViewService } from 'vs/platform/contextview/browser/contextViewS
// import { RelayURLService } from 'vs/platform/url/electron-browser/urlService';
import { IHeapService, NullHeapService } from 'vs/workbench/services/heap/common/heap';
import { IBroadcastService, NullBroadcastService } from 'vs/workbench/services/broadcast/common/broadcast';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { TextFileService } from 'vs/workbench/services/textfile/common/textFileService';

import 'vs/workbench/browser/nodeless.simpleservices';
import 'vs/platform/dialogs/browser/dialogService';
Expand All @@ -112,7 +114,7 @@ import 'vs/workbench/services/preferences/browser/preferencesService';
import 'vs/workbench/services/output/common/outputChannelModelService';
import 'vs/workbench/services/configuration/common/jsonEditingService';
import 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
import 'vs/workbench/services/textfile/common/textFileService';
// import 'vs/workbench/services/textfile/node/textFileService';
import 'vs/workbench/services/dialogs/browser/fileDialogService';
// import 'vs/workbench/services/dialogs/electron-browser/dialogService';
// import 'vs/workbench/services/backup/node/backupFileService';
Expand Down Expand Up @@ -167,8 +169,8 @@ registerSingleton(IContextViewService, ContextViewService, true);
// registerSingleton(IURLService, RelayURLService);
registerSingleton(IHeapService, NullHeapService);
registerSingleton(IBroadcastService, NullBroadcastService);

registerSingleton(IContextMenuService, ContextMenuService);
registerSingleton(ITextFileService, TextFileService);

//#endregion

Expand Down

0 comments on commit 4cce7c0

Please sign in to comment.