Skip to content

Commit 4cce7c0

Browse files
committed
files2 - support overwrite readonly for local files
1 parent 9319cc2 commit 4cce7c0

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

src/vs/workbench/services/textfile/common/textFileService.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { IModeService } from 'vs/editor/common/services/modeService';
3737
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
3838
import { coalesce } from 'vs/base/common/arrays';
3939
import { trim } from 'vs/base/common/strings';
40-
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
4140

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

994993
super.dispose();
995994
}
996-
}
997-
998-
registerSingleton(ITextFileService, TextFileService);
995+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { TextFileService } from 'vs/workbench/services/textfile/common/textFileService';
7+
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
8+
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
9+
import { URI } from 'vs/base/common/uri';
10+
import { ITextSnapshot, IWriteTextFileOptions, IFileStatWithMetadata } from 'vs/platform/files/common/files';
11+
import { Schemas } from 'vs/base/common/network';
12+
import { exists, stat, chmod } from 'vs/base/node/pfs';
13+
14+
export class NodeTextFileService extends TextFileService {
15+
16+
async write(resource: URI, value: string | ITextSnapshot, options?: IWriteTextFileOptions): Promise<IFileStatWithMetadata> {
17+
18+
// check for overwriteReadonly property (only supported for local file://)
19+
try {
20+
if (options && options.overwriteReadonly && resource.scheme === Schemas.file && await exists(resource.fsPath)) {
21+
const fileStat = await stat(resource.fsPath);
22+
23+
// try to change mode to writeable
24+
await chmod(resource.fsPath, fileStat.mode | 128);
25+
}
26+
} catch (error) {
27+
// ignore and simply retry the operation
28+
}
29+
30+
return super.write(resource, value, options);
31+
}
32+
}
33+
34+
registerSingleton(ITextFileService, NodeTextFileService);

src/vs/workbench/workbench.main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ import 'vs/workbench/services/preferences/browser/preferencesService';
105105
import 'vs/workbench/services/output/node/outputChannelModelService';
106106
import 'vs/workbench/services/configuration/common/jsonEditingService';
107107
import 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
108-
import 'vs/workbench/services/textfile/common/textFileService';
108+
import 'vs/workbench/services/textfile/node/textFileService';
109109
import 'vs/workbench/services/dialogs/browser/fileDialogService';
110110
import 'vs/workbench/services/dialogs/electron-browser/dialogService';
111111
import 'vs/workbench/services/backup/node/backupFileService';

src/vs/workbench/workbench.nodeless.main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ import { ContextViewService } from 'vs/platform/contextview/browser/contextViewS
9191
// import { RelayURLService } from 'vs/platform/url/electron-browser/urlService';
9292
import { IHeapService, NullHeapService } from 'vs/workbench/services/heap/common/heap';
9393
import { IBroadcastService, NullBroadcastService } from 'vs/workbench/services/broadcast/common/broadcast';
94+
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
95+
import { TextFileService } from 'vs/workbench/services/textfile/common/textFileService';
9496

9597
import 'vs/workbench/browser/nodeless.simpleservices';
9698
import 'vs/platform/dialogs/browser/dialogService';
@@ -112,7 +114,7 @@ import 'vs/workbench/services/preferences/browser/preferencesService';
112114
import 'vs/workbench/services/output/common/outputChannelModelService';
113115
import 'vs/workbench/services/configuration/common/jsonEditingService';
114116
import 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
115-
import 'vs/workbench/services/textfile/common/textFileService';
117+
// import 'vs/workbench/services/textfile/node/textFileService';
116118
import 'vs/workbench/services/dialogs/browser/fileDialogService';
117119
// import 'vs/workbench/services/dialogs/electron-browser/dialogService';
118120
// import 'vs/workbench/services/backup/node/backupFileService';
@@ -167,8 +169,8 @@ registerSingleton(IContextViewService, ContextViewService, true);
167169
// registerSingleton(IURLService, RelayURLService);
168170
registerSingleton(IHeapService, NullHeapService);
169171
registerSingleton(IBroadcastService, NullBroadcastService);
170-
171172
registerSingleton(IContextMenuService, ContextMenuService);
173+
registerSingleton(ITextFileService, TextFileService);
172174

173175
//#endregion
174176

0 commit comments

Comments
 (0)