Path: blob/main/src/vs/platform/files/browser/htmlFileSystemProvider.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { localize } from '../../../nls.js';6import { URI } from '../../../base/common/uri.js';7import { VSBuffer } from '../../../base/common/buffer.js';8import { CancellationToken } from '../../../base/common/cancellation.js';9import { Emitter, Event } from '../../../base/common/event.js';10import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';11import { Schemas } from '../../../base/common/network.js';12import { basename, extname, normalize } from '../../../base/common/path.js';13import { isLinux } from '../../../base/common/platform.js';14import { extUri, extUriIgnorePathCase, joinPath } from '../../../base/common/resources.js';15import { newWriteableStream, ReadableStreamEvents } from '../../../base/common/stream.js';16import { createFileSystemProviderError, IFileDeleteOptions, IFileOverwriteOptions, IFileReadStreamOptions, FileSystemProviderCapabilities, FileSystemProviderError, FileSystemProviderErrorCode, FileType, IFileWriteOptions, IFileSystemProviderWithFileReadStreamCapability, IFileSystemProviderWithFileReadWriteCapability, IStat, IWatchOptions, IFileChange, FileChangeType } from '../common/files.js';17import { FileSystemObserverRecord, WebFileSystemAccess, WebFileSystemObserver } from './webFileSystemAccess.js';18import { IndexedDB } from '../../../base/browser/indexedDB.js';19import { ILogService, LogLevel } from '../../log/common/log.js';2021export class HTMLFileSystemProvider extends Disposable implements IFileSystemProviderWithFileReadWriteCapability, IFileSystemProviderWithFileReadStreamCapability {2223//#region Events (unsupported)2425readonly onDidChangeCapabilities = Event.None;2627//#endregion2829//#region File Capabilities3031private extUri = isLinux ? extUri : extUriIgnorePathCase;3233private _capabilities: FileSystemProviderCapabilities | undefined;34get capabilities(): FileSystemProviderCapabilities {35if (!this._capabilities) {36this._capabilities =37FileSystemProviderCapabilities.FileReadWrite |38FileSystemProviderCapabilities.FileReadStream;3940if (isLinux) {41this._capabilities |= FileSystemProviderCapabilities.PathCaseSensitive;42}43}4445return this._capabilities;46}4748//#endregion495051constructor(52private indexedDB: IndexedDB | undefined,53private readonly store: string,54private logService: ILogService55) {56super();57}5859//#region File Metadata Resolving6061async stat(resource: URI): Promise<IStat> {62try {63const handle = await this.getHandle(resource);64if (!handle) {65throw this.createFileSystemProviderError(resource, 'No such file or directory, stat', FileSystemProviderErrorCode.FileNotFound);66}6768if (WebFileSystemAccess.isFileSystemFileHandle(handle)) {69const file = await handle.getFile();7071return {72type: FileType.File,73mtime: file.lastModified,74ctime: 0,75size: file.size76};77}7879return {80type: FileType.Directory,81mtime: 0,82ctime: 0,83size: 084};85} catch (error) {86throw this.toFileSystemProviderError(error);87}88}8990async readdir(resource: URI): Promise<[string, FileType][]> {91try {92const handle = await this.getDirectoryHandle(resource);93if (!handle) {94throw this.createFileSystemProviderError(resource, 'No such file or directory, readdir', FileSystemProviderErrorCode.FileNotFound);95}9697const result: [string, FileType][] = [];9899for await (const [name, child] of handle) {100result.push([name, WebFileSystemAccess.isFileSystemFileHandle(child) ? FileType.File : FileType.Directory]);101}102103return result;104} catch (error) {105throw this.toFileSystemProviderError(error);106}107}108109//#endregion110111//#region File Reading/Writing112113readFileStream(resource: URI, opts: IFileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> {114const stream = newWriteableStream<Uint8Array>(data => VSBuffer.concat(data.map(data => VSBuffer.wrap(data))).buffer, {115// Set a highWaterMark to prevent the stream116// for file upload to produce large buffers117// in-memory118highWaterMark: 10119});120121(async () => {122try {123const handle = await this.getFileHandle(resource);124if (!handle) {125throw this.createFileSystemProviderError(resource, 'No such file or directory, readFile', FileSystemProviderErrorCode.FileNotFound);126}127128const file = await handle.getFile();129130// Partial file: implemented simply via `readFile`131if (typeof opts.length === 'number' || typeof opts.position === 'number') {132let buffer = new Uint8Array(await file.arrayBuffer());133134if (typeof opts?.position === 'number') {135buffer = buffer.slice(opts.position);136}137138if (typeof opts?.length === 'number') {139buffer = buffer.slice(0, opts.length);140}141142stream.end(buffer);143}144145// Entire file146else {147const reader: ReadableStreamDefaultReader<Uint8Array> = file.stream().getReader();148149let res = await reader.read();150while (!res.done) {151if (token.isCancellationRequested) {152break;153}154155// Write buffer into stream but make sure to wait156// in case the `highWaterMark` is reached157await stream.write(res.value);158159if (token.isCancellationRequested) {160break;161}162163res = await reader.read();164}165stream.end(undefined);166}167} catch (error) {168stream.error(this.toFileSystemProviderError(error));169stream.end();170}171})();172173return stream;174}175176async readFile(resource: URI): Promise<Uint8Array> {177try {178const handle = await this.getFileHandle(resource);179if (!handle) {180throw this.createFileSystemProviderError(resource, 'No such file or directory, readFile', FileSystemProviderErrorCode.FileNotFound);181}182183const file = await handle.getFile();184185return new Uint8Array(await file.arrayBuffer());186} catch (error) {187throw this.toFileSystemProviderError(error);188}189}190191async writeFile(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void> {192try {193let handle = await this.getFileHandle(resource);194195// Validate target unless { create: true, overwrite: true }196if (!opts.create || !opts.overwrite) {197if (handle) {198if (!opts.overwrite) {199throw this.createFileSystemProviderError(resource, 'File already exists, writeFile', FileSystemProviderErrorCode.FileExists);200}201} else {202if (!opts.create) {203throw this.createFileSystemProviderError(resource, 'No such file, writeFile', FileSystemProviderErrorCode.FileNotFound);204}205}206}207208// Create target as needed209if (!handle) {210const parent = await this.getDirectoryHandle(this.extUri.dirname(resource));211if (!parent) {212throw this.createFileSystemProviderError(resource, 'No such parent directory, writeFile', FileSystemProviderErrorCode.FileNotFound);213}214215handle = await parent.getFileHandle(this.extUri.basename(resource), { create: true });216if (!handle) {217throw this.createFileSystemProviderError(resource, 'Unable to create file , writeFile', FileSystemProviderErrorCode.Unknown);218}219}220221// Write to target overwriting any existing contents222const writable = await handle.createWritable();223await writable.write(content as Uint8Array<ArrayBuffer>);224await writable.close();225} catch (error) {226throw this.toFileSystemProviderError(error);227}228}229230//#endregion231232//#region Move/Copy/Delete/Create Folder233234async mkdir(resource: URI): Promise<void> {235try {236const parent = await this.getDirectoryHandle(this.extUri.dirname(resource));237if (!parent) {238throw this.createFileSystemProviderError(resource, 'No such parent directory, mkdir', FileSystemProviderErrorCode.FileNotFound);239}240241await parent.getDirectoryHandle(this.extUri.basename(resource), { create: true });242} catch (error) {243throw this.toFileSystemProviderError(error);244}245}246247async delete(resource: URI, opts: IFileDeleteOptions): Promise<void> {248try {249const parent = await this.getDirectoryHandle(this.extUri.dirname(resource));250if (!parent) {251throw this.createFileSystemProviderError(resource, 'No such parent directory, delete', FileSystemProviderErrorCode.FileNotFound);252}253254return parent.removeEntry(this.extUri.basename(resource), { recursive: opts.recursive });255} catch (error) {256throw this.toFileSystemProviderError(error);257}258}259260async rename(from: URI, to: URI, opts: IFileOverwriteOptions): Promise<void> {261try {262if (this.extUri.isEqual(from, to)) {263return; // no-op if the paths are the same264}265266// Implement file rename by write + delete267const fileHandle = await this.getFileHandle(from);268if (fileHandle) {269const file = await fileHandle.getFile();270const contents = new Uint8Array(await file.arrayBuffer());271272await this.writeFile(to, contents, { create: true, overwrite: opts.overwrite, unlock: false, atomic: false });273await this.delete(from, { recursive: false, useTrash: false, atomic: false });274}275276// File API does not support any real rename otherwise277else {278throw this.createFileSystemProviderError(from, localize('fileSystemRenameError', "Rename is only supported for files."), FileSystemProviderErrorCode.Unavailable);279}280} catch (error) {281throw this.toFileSystemProviderError(error);282}283}284285//#endregion286287//#region File Watching (unsupported)288289private readonly _onDidChangeFileEmitter = this._register(new Emitter<readonly IFileChange[]>());290readonly onDidChangeFile = this._onDidChangeFileEmitter.event;291292watch(resource: URI, opts: IWatchOptions): IDisposable {293const disposables = new DisposableStore();294295this.doWatch(resource, opts, disposables).catch(error => this.logService.error(`[File Watcher ('FileSystemObserver')] Error: ${error} (${resource})`));296297return disposables;298}299300private async doWatch(resource: URI, opts: IWatchOptions, disposables: DisposableStore): Promise<void> {301if (!WebFileSystemObserver.supported(globalThis)) {302return;303}304305const handle = await this.getHandle(resource);306if (!handle || disposables.isDisposed) {307return;308}309310const observer = new (globalThis as any).FileSystemObserver((records: FileSystemObserverRecord[]) => {311if (disposables.isDisposed) {312return;313}314315const events: IFileChange[] = [];316for (const record of records) {317if (this.logService.getLevel() === LogLevel.Trace) {318this.logService.trace(`[File Watcher ('FileSystemObserver')] [${record.type}] ${joinPath(resource, ...record.relativePathComponents)}`);319}320321switch (record.type) {322case 'appeared':323events.push({ resource: joinPath(resource, ...record.relativePathComponents), type: FileChangeType.ADDED });324break;325case 'disappeared':326events.push({ resource: joinPath(resource, ...record.relativePathComponents), type: FileChangeType.DELETED });327break;328case 'modified':329events.push({ resource: joinPath(resource, ...record.relativePathComponents), type: FileChangeType.UPDATED });330break;331case 'errored':332this.logService.trace(`[File Watcher ('FileSystemObserver')] errored, disposing observer (${resource})`);333disposables.dispose();334}335}336337if (events.length) {338this._onDidChangeFileEmitter.fire(events);339}340});341342try {343await observer.observe(handle, opts.recursive ? { recursive: true } : undefined);344} finally {345if (disposables.isDisposed) {346observer.disconnect();347} else {348disposables.add(toDisposable(() => observer.disconnect()));349}350}351}352353//#endregion354355//#region File/Directoy Handle Registry356357private readonly _files = new Map<string, FileSystemFileHandle>();358private readonly _directories = new Map<string, FileSystemDirectoryHandle>();359360registerFileHandle(handle: FileSystemFileHandle): Promise<URI> {361return this.registerHandle(handle, this._files);362}363364registerDirectoryHandle(handle: FileSystemDirectoryHandle): Promise<URI> {365return this.registerHandle(handle, this._directories);366}367368get directories(): Iterable<FileSystemDirectoryHandle> {369return this._directories.values();370}371372private async registerHandle(handle: FileSystemHandle, map: Map<string, FileSystemHandle>): Promise<URI> {373let handleId = `/${handle.name}`;374375// Compute a valid handle ID in case this exists already376if (map.has(handleId) && !await map.get(handleId)?.isSameEntry(handle)) {377const fileExt = extname(handle.name);378const fileName = basename(handle.name, fileExt);379380let handleIdCounter = 1;381do {382handleId = `/${fileName}-${handleIdCounter++}${fileExt}`;383} while (map.has(handleId) && !await map.get(handleId)?.isSameEntry(handle));384}385386map.set(handleId, handle);387388// Remember in IndexDB for future lookup389try {390await this.indexedDB?.runInTransaction(this.store, 'readwrite', objectStore => objectStore.put(handle, handleId));391} catch (error) {392this.logService.error(error);393}394395return URI.from({ scheme: Schemas.file, path: handleId });396}397398async getHandle(resource: URI): Promise<FileSystemHandle | undefined> {399400// First: try to find a well known handle first401let handle = await this.doGetHandle(resource);402403// Second: walk up parent directories and resolve handle if possible404if (!handle) {405const parent = await this.getDirectoryHandle(this.extUri.dirname(resource));406if (parent) {407const name = extUri.basename(resource);408try {409handle = await parent.getFileHandle(name);410} catch (error) {411try {412handle = await parent.getDirectoryHandle(name);413} catch (error) {414// Ignore415}416}417}418}419420return handle;421}422423private async getFileHandle(resource: URI): Promise<FileSystemFileHandle | undefined> {424const handle = await this.doGetHandle(resource);425if (handle instanceof FileSystemFileHandle) {426return handle;427}428429const parent = await this.getDirectoryHandle(this.extUri.dirname(resource));430431try {432return await parent?.getFileHandle(extUri.basename(resource));433} catch (error) {434return undefined; // guard against possible DOMException435}436}437438private async getDirectoryHandle(resource: URI): Promise<FileSystemDirectoryHandle | undefined> {439const handle = await this.doGetHandle(resource);440if (handle instanceof FileSystemDirectoryHandle) {441return handle;442}443444const parentUri = this.extUri.dirname(resource);445if (this.extUri.isEqual(parentUri, resource)) {446return undefined; // return when root is reached to prevent infinite recursion447}448449const parent = await this.getDirectoryHandle(parentUri);450451try {452return await parent?.getDirectoryHandle(extUri.basename(resource));453} catch (error) {454return undefined; // guard against possible DOMException455}456}457458private async doGetHandle(resource: URI): Promise<FileSystemHandle | undefined> {459460// We store file system handles with the `handle.name`461// and as such require the resource to be on the root462if (this.extUri.dirname(resource).path !== '/') {463return undefined;464}465466const handleId = resource.path.replace(/\/$/, ''); // remove potential slash from the end of the path467468// First: check if we have a known handle stored in memory469const inMemoryHandle = this._files.get(handleId) ?? this._directories.get(handleId);470if (inMemoryHandle) {471return inMemoryHandle;472}473474// Second: check if we have a persisted handle in IndexedDB475const persistedHandle = await this.indexedDB?.runInTransaction(this.store, 'readonly', store => store.get(handleId));476if (WebFileSystemAccess.isFileSystemHandle(persistedHandle)) {477let hasPermissions = await persistedHandle.queryPermission() === 'granted';478try {479if (!hasPermissions) {480hasPermissions = await persistedHandle.requestPermission() === 'granted';481}482} catch (error) {483this.logService.error(error); // this can fail with a DOMException484}485486if (hasPermissions) {487if (WebFileSystemAccess.isFileSystemFileHandle(persistedHandle)) {488this._files.set(handleId, persistedHandle);489} else if (WebFileSystemAccess.isFileSystemDirectoryHandle(persistedHandle)) {490this._directories.set(handleId, persistedHandle);491}492493return persistedHandle;494}495}496497// Third: fail with an error498throw this.createFileSystemProviderError(resource, 'No file system handle registered', FileSystemProviderErrorCode.Unavailable);499}500501//#endregion502503private toFileSystemProviderError(error: Error): FileSystemProviderError {504if (error instanceof FileSystemProviderError) {505return error; // avoid double conversion506}507508let code = FileSystemProviderErrorCode.Unknown;509if (error.name === 'NotAllowedError') {510error = new Error(localize('fileSystemNotAllowedError', "Insufficient permissions. Please retry and allow the operation."));511code = FileSystemProviderErrorCode.Unavailable;512}513514return createFileSystemProviderError(error, code);515}516517private createFileSystemProviderError(resource: URI, msg: string, code: FileSystemProviderErrorCode): FileSystemProviderError {518return createFileSystemProviderError(new Error(`${msg} (${normalize(resource.path)})`), code);519}520}521522523