1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isValidBasename } from 'vs/base/common/extpath';
import { Schemas } from 'vs/base/common/network';
import { IPath, win32, posix } from 'vs/base/common/path';
import { OperatingSystem, OS } from 'vs/base/common/platform';
import { basename } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { getVirtualWorkspaceScheme } from 'vs/platform/workspace/common/virtualWorkspace';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
export const IPathService = createDecorator<IPathService>('pathService');
/**
* Provides access to path related properties that will match the
* environment. If the environment is connected to a remote, the
* path properties will match that of the remotes operating system.
*/
export interface IPathService {
readonly _serviceBrand: undefined;
/**
* The correct path library to use for the target environment. If
* the environment is connected to a remote, this will be the
* path library of the remote file system. Otherwise it will be
* the local file system's path library depending on the OS.
*/
readonly path: Promise<IPath>;
/**
* Determines the best default URI scheme for the current workspace.
* It uses information about whether we're running remote, in browser,
* or native combined with information about the current workspace to
* find the best default scheme.
*/
readonly defaultUriScheme: string;
/**
* Converts the given path to a file URI to use for the target
* environment. If the environment is connected to a remote, it
* will use the path separators according to the remote file
* system. Otherwise it will use the local file system's path
* separators.
*/
fileURI(path: string): Promise<URI>;
/**
* Resolves the user-home directory for the target environment.
* If the envrionment is connected to a remote, this will be the
* remote's user home directory, otherwise the local one unless
* `preferLocal` is set to `true`.
*/
userHome(options?: { preferLocal: boolean }): Promise<URI>;
/**
* Figures out if the provided resource has a valid file name
* for the operating system the file is saved to.
*
* Note: this currently only supports `file` and `vscode-file`
* protocols where we know the limits of the file systems behind
* these OS. Other remotes are not supported and this method
* will always return `true` for them.
*/
hasValidBasename(resource: URI, basename?: string): Promise<boolean>;
hasValidBasename(resource: URI, os: OperatingSystem, basename?: string): boolean;
/**
* @deprecated use `userHome` instead.
*/
readonly resolvedUserHome: URI | undefined;
}
export abstract class AbstractPathService implements IPathService {
declare readonly _serviceBrand: undefined;
private resolveOS: Promise<OperatingSystem>;
private resolveUserHome: Promise<URI>;
private maybeUnresolvedUserHome: URI | undefined;
constructor(
private localUserHome: URI,
@IRemoteAgentService private readonly remoteAgentService: IRemoteAgentService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IWorkspaceContextService private contextService: IWorkspaceContextService
) {
// OS
this.resolveOS = (async () => {
const env = await this.remoteAgentService.getEnvironment();
return env?.os || OS;
})();
// User Home
this.resolveUserHome = (async () => {
const env = await this.remoteAgentService.getEnvironment();
const userHome = this.maybeUnresolvedUserHome = env?.userHome || localUserHome;
return userHome;
})();
}
hasValidBasename(resource: URI, basename?: string): Promise<boolean>;
hasValidBasename(resource: URI, os: OperatingSystem, basename?: string): boolean;
hasValidBasename(resource: URI, arg2?: string | OperatingSystem, basename?: string): boolean | Promise<boolean> {
// async version
if (typeof arg2 === 'string' || typeof arg2 === 'undefined') {
return this.resolveOS.then(os => this.doHasValidBasename(resource, os, arg2));
}
// sync version
return this.doHasValidBasename(resource, arg2, basename);
}
private doHasValidBasename(resource: URI, os: OperatingSystem, name?: string): boolean {
// Our `isValidBasename` method only works with our
// standard schemes for files on disk, either locally
// or remote.
if (resource.scheme === Schemas.file || resource.scheme === Schemas.vscodeRemote) {
return isValidBasename(name ?? basename(resource), os === OperatingSystem.Windows);
}
return true;
}
get defaultUriScheme(): string {
return AbstractPathService.findDefaultUriScheme(this.environmentService, this.contextService);
}
protected static findDefaultUriScheme(environmentService: IWorkbenchEnvironmentService, contextService: IWorkspaceContextService): string {
if (environmentService.remoteAuthority) {
return Schemas.vscodeRemote;
}
const virtualWorkspace = getVirtualWorkspaceScheme(contextService.getWorkspace());
if (virtualWorkspace) {
return virtualWorkspace;
}
const firstFolder = contextService.getWorkspace().folders[0];
if (firstFolder) {
return firstFolder.uri.scheme;
}
const configuration = contextService.getWorkspace().configuration;
if (configuration) {
return configuration.scheme;
}
return Schemas.file;
}
async userHome(options?: { preferLocal: boolean }): Promise<URI> {
return options?.preferLocal ? this.localUserHome : this.resolveUserHome;
}
get resolvedUserHome(): URI | undefined {
return this.maybeUnresolvedUserHome;
}
get path(): Promise<IPath> {
return this.resolveOS.then(os => {
return os === OperatingSystem.Windows ?
win32 :
posix;
});
}
async fileURI(_path: string): Promise<URI> {
let authority = '';
// normalize to fwd-slashes on windows,
// on other systems bwd-slashes are valid
// filename character, eg /f\oo/ba\r.txt
const os = await this.resolveOS;
if (os === OperatingSystem.Windows) {
_path = _path.replace(/\\/g, '/');
}
// check for authority as used in UNC shares
// or use the path as given
if (_path[0] === '/' && _path[1] === '/') {
const idx = _path.indexOf('/', 2);
if (idx === -1) {
authority = _path.substring(2);
_path = '/';
} else {
authority = _path.substring(2, idx);
_path = _path.substring(idx) || '/';
}
}
return URI.from({
scheme: Schemas.file,
authority,
path: _path,
query: '',
fragment: ''
});
}
}
|