From d36a62e23ff9cc46c7686b324af786e05163d7a1 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Thu, 5 Sep 2024 19:14:02 -0400 Subject: [PATCH] refactor: move checks into stat Signed-off-by: Trae Yelovich --- .../src/trees/dataset/DatasetFSProvider.ts | 50 ++++++++++--------- .../src/trees/uss/UssFSProvider.ts | 50 ++++++++++--------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts b/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts index 7ecf64dfcf..108caa0ec1 100644 --- a/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts +++ b/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts @@ -90,6 +90,7 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem */ public async stat(uri: vscode.Uri): Promise { ZoweLogger.trace(`[DatasetFSProvider] stat called with ${uri.toString()}`); + let entry: DsEntry | DirEntry; let isFetching = false; if (uri.query) { const queryParams = new URLSearchParams(uri.query); @@ -102,11 +103,33 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem } const uriInfo = FsAbstractUtils.getInfoForUri(uri, Profiles.getInstance()); - const entry = isFetching ? await this.remoteLookupForResource(uri) : this.lookup(uri, false); - // Do not perform remote lookup for profile or directory URIs; the code below is for change detection on PS or PDS members only + if (isFetching) { + entry = await this.remoteLookupForResource(uri); + } else { + try { + entry = this.lookup(uri, false) as DsEntry | DirEntry; + } catch (err) { + if (!(err instanceof vscode.FileSystemError) || err.code !== "FileNotFound") { + throw err; + } + + if (!uriInfo.isRoot) { + // we need to look up the resource since the parent folder doesn't exist + // (either user clicked on URI or it was left open in editor) + entry = await this.remoteLookupForResource(uri); + } else { + throw err; + } + } + } + + // Do not perform remote lookup for profile or directory URIs; the code below is for change detection on file entries only if (uriInfo.isRoot || FsAbstractUtils.isDirectoryEntry(entry)) { return entry; } + if (entry == null) { + throw vscode.FileSystemError.FileNotFound(uri); + } ZoweLogger.trace(`[DatasetFSProvider] stat is locating resource ${uri.toString()}`); @@ -382,28 +405,7 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem * @returns The data set's contents as an array of bytes */ public async readFile(uri: vscode.Uri): Promise { - let ds: DsEntry | DirEntry; - try { - ds = this._lookupAsFile(uri) as DsEntry; - } catch (err) { - if (!(err instanceof vscode.FileSystemError) || err.code !== "FileNotFound") { - throw err; - } - - // check if parent directory exists; if not, do a remote lookup - const parent = this._lookupParentDirectory(uri, true); - if (parent == null) { - ds = await this.remoteLookupForResource(uri); - } - } - - if (ds == null) { - throw vscode.FileSystemError.FileNotFound(uri); - } - if (FsAbstractUtils.isDirectoryEntry(ds)) { - throw vscode.FileSystemError.FileIsADirectory(uri); - } - + const ds = this._lookupAsFile(uri); const profInfo = this._getInfoFromUri(uri); if (profInfo.profile == null) { diff --git a/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts b/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts index 4f8f16b790..80824e07bc 100644 --- a/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts +++ b/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts @@ -62,6 +62,7 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv */ public async stat(uri: vscode.Uri): Promise { ZoweLogger.trace(`[UssFSProvider] stat called with ${uri.toString()}`); + let entry: UssFile | UssDirectory; let isFetching = false; if (uri.query) { const queryParams = new URLSearchParams(uri.query); @@ -73,12 +74,34 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv isFetching = queryParams.has("fetch") && queryParams.get("fetch") === "true"; } - const entry = isFetching ? await this.remoteLookupForResource(uri) : this.lookup(uri, false); const uriInfo = FsAbstractUtils.getInfoForUri(uri, Profiles.getInstance()); - // Do not perform remote lookup for profile or directory URIs; the code below is for change detection on USS files only + if (isFetching) { + entry = await this.remoteLookupForResource(uri); + } else { + try { + entry = this.lookup(uri, false) as UssFile | UssDirectory; + } catch (err) { + if (!(err instanceof vscode.FileSystemError) || err.code !== "FileNotFound") { + throw err; + } + + if (!uriInfo.isRoot) { + // we need to look up the resource since the parent folder doesn't exist + // (either user clicked on URI or it was left open in editor) + entry = await this.remoteLookupForResource(uri); + } else { + throw err; + } + } + } + + // Do not perform remote lookup for profile or directory URIs; the code below is for change detection on file entries only if (uriInfo.isRoot || FsAbstractUtils.isDirectoryEntry(entry)) { return entry; } + if (entry == null) { + throw vscode.FileSystemError.FileNotFound(uri); + } ZoweLogger.trace(`[UssFSProvider] stat is locating resource ${uri.toString()}`); @@ -306,28 +329,7 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv * @returns The file's contents as an array of bytes */ public async readFile(uri: vscode.Uri): Promise { - let file: UssFile | UssDirectory; - try { - file = this._lookupAsFile(uri) as UssFile; - } catch (err) { - if (!(err instanceof vscode.FileSystemError) || err.code !== "FileNotFound") { - throw err; - } - - // check if parent directory exists; if not, do a remote lookup - const parent = this._lookupParentDirectory(uri, true); - if (parent == null) { - file = await this.remoteLookupForResource(uri); - } - } - - if (file == null) { - throw vscode.FileSystemError.FileNotFound(uri); - } - if (FsAbstractUtils.isDirectoryEntry(file)) { - throw vscode.FileSystemError.FileIsADirectory(uri); - } - + const file = this._lookupAsFile(uri, { silent: false }); const profInfo = this._getInfoFromUri(uri); if (profInfo.profile == null) {