diff options
author | Axetroy <axetroy.dev@gmail.com> | 2019-12-18 22:45:19 +0800 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-12-18 09:45:19 -0500 |
commit | ef30d376db94bda24ed31947a1cc264b827705dc (patch) | |
tree | aba58a06c797efd513a1a83ba056487e18674d6f /std/fs/copy.ts | |
parent | 1c09cc63c8b9c33842ee302c93aebc6593be8b15 (diff) |
fix permission errors are swallowed by fs.copy() (#3504)
Diffstat (limited to 'std/fs/copy.ts')
-rw-r--r-- | std/fs/copy.ts | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/std/fs/copy.ts b/std/fs/copy.ts index f5e7f5078..783597882 100644 --- a/std/fs/copy.ts +++ b/std/fs/copy.ts @@ -23,21 +23,25 @@ async function ensureValidCopy( options: CopyOptions, isCopyFolder = false ): Promise<Deno.FileInfo> { - const destStat: Deno.FileInfo | null = await Deno.lstat(dest).catch( - (): Promise<null> => Promise.resolve(null) - ); - - if (destStat) { - if (isCopyFolder && !destStat.isDirectory()) { - throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.` - ); - } - if (!options.overwrite) { - throw new Error(`'${dest}' already exists.`); + let destStat: Deno.FileInfo | null; + + try { + destStat = await Deno.lstat(dest); + } catch (err) { + if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + return; } } + if (isCopyFolder && !destStat.isDirectory()) { + throw new Error( + `Cannot overwrite non-directory '${dest}' with directory '${src}'.` + ); + } + if (!options.overwrite) { + throw new Error(`'${dest}' already exists.`); + } + return destStat!; } @@ -51,19 +55,19 @@ function ensureValidCopySync( try { destStat = Deno.lstatSync(dest); - } catch { - // ignore error + } catch (err) { + if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + return; + } } - if (destStat!) { - if (isCopyFolder && !destStat!.isDirectory()) { - throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.` - ); - } - if (!options.overwrite) { - throw new Error(`'${dest}' already exists.`); - } + if (isCopyFolder && !destStat!.isDirectory()) { + throw new Error( + `Cannot overwrite non-directory '${dest}' with directory '${src}'.` + ); + } + if (!options.overwrite) { + throw new Error(`'${dest}' already exists.`); } return destStat!; @@ -186,6 +190,7 @@ function copyDirSync(src: string, dest: string, options: CopyOptions): void { /** * Copy a file or directory. The directory can have contents. Like `cp -r`. + * Requires the `--allow-read` and `--alow-write` flag. * @param src the file/directory path. * Note that if `src` is a directory it will copy everything inside * of this directory, not the entire directory itself @@ -224,6 +229,7 @@ export async function copy( /** * Copy a file or directory. The directory can have contents. Like `cp -r`. + * Requires the `--allow-read` and `--alow-write` flag. * @param src the file/directory path. * Note that if `src` is a directory it will copy everything inside * of this directory, not the entire directory itself |