/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; import URI from 'vs/base/common/uri'; import { TPromise } from 'vs/base/common/winjs.base'; import { MainContext, IMainContext, ExtHostFileSystemShape, MainThreadFileSystemShape } from './extHost.protocol'; import * as vscode from 'vscode'; import { IStat } from 'vs/platform/files/common/files'; import { IDisposable } from 'vs/base/common/lifecycle'; import { asWinJsPromise } from 'vs/base/common/async'; export class ExtHostFileSystem implements ExtHostFileSystemShape { private readonly _proxy: MainThreadFileSystemShape; private readonly _provider = new Map(); private _handlePool: number = 0; constructor(mainContext: IMainContext) { this._proxy = mainContext.get(MainContext.MainThreadFileSystem); } registerFileSystemProvider(scheme: string, provider: vscode.FileSystemProvider) { const handle = this._handlePool++; this._provider.set(handle, provider); this._proxy.$registerFileSystemProvider(handle, scheme); this._proxy.$onDidAddFileSystemRoot(provider.root); let reg: IDisposable; if (provider.onDidChange) { reg = provider.onDidChange(event => this._proxy.$onFileSystemChange(handle, event)); } return { dispose: () => { if (reg) { reg.dispose(); } this._provider.delete(handle); this._proxy.$unregisterFileSystemProvider(handle); } }; } $utimes(handle: number, resource: URI, mtime: number, atime: number): TPromise { return asWinJsPromise(token => this._provider.get(handle).utimes(resource, mtime, atime)); } $stat(handle: number, resource: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).stat(resource)); } $read(handle: number, offset: number, count: number, resource: URI): TPromise { const progress = { report: chunk => { this._proxy.$reportFileChunk(handle, resource, [].slice.call(chunk)); } }; return asWinJsPromise(token => this._provider.get(handle).read(resource, offset, count, progress)); } $write(handle: number, resource: URI, content: number[]): TPromise { return asWinJsPromise(token => this._provider.get(handle).write(resource, Buffer.from(content))); } $unlink(handle: number, resource: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).unlink(resource)); } $move(handle: number, resource: URI, target: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).move(resource, target)); } $mkdir(handle: number, resource: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).mkdir(resource)); } $readdir(handle: number, resource: URI): TPromise<[URI, IStat][], any> { return asWinJsPromise(token => this._provider.get(handle).readdir(resource)); } $rmdir(handle: number, resource: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).rmdir(resource)); } }