From f0f95e5b2612d011592f9dbe1885756c5219c70b Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 22 Mar 2021 11:35:55 -0700 Subject: [PATCH] On Windows, implement "Filesystem::copyFile()" with "copy()" Summary: Ref T13562. Currently, "Filesystem::copyFile()" uses "copy", which doesn't work now that we no longer invoke "cmd.exe" by default. Use "copy()" instead. Note that this whole function is probably nonsense, but I'll follow up on T13562. Test Plan: - Created a standalone script which runs "Filesystem::copyFile()". - Before: failed to copy any file. - After: succesfully copied normal files. - After: failed to copy a file over an existing directory with a reasonable error. - After: failed to copy a file over itself with a reasonable error. Maniphest Tasks: T13562 Differential Revision: https://secure.phabricator.com/D21643 --- src/filesystem/Filesystem.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/filesystem/Filesystem.php b/src/filesystem/Filesystem.php index 501faa5f..9cd52268 100644 --- a/src/filesystem/Filesystem.php +++ b/src/filesystem/Filesystem.php @@ -269,7 +269,29 @@ final class Filesystem extends Phobject { self::assertReadable($from); if (phutil_is_windows()) { - execx('copy /Y %s %s', $from, $to); + $trap = new PhutilErrorTrap(); + $ok = @copy($from, $to); + $err = $trap->getErrorsAsString(); + $trap->destroy(); + + if (!$ok) { + if (strlen($err)) { + throw new FilesystemException( + $to, + pht( + 'Failed to copy file from "%s" to "%s": %s', + $from, + $to, + $err)); + } else { + throw new FilesystemException( + $to, + pht( + 'Failed to copy file from "%s" to "%s".', + $from, + $to)); + } + } } else { execx('cp -p %s %s', $from, $to); }