You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make File.Move(..., ..., overwrite: true) more efficient. (#47118)
The current implementation has an inefficiency. When moving a file across volumes, and when the target file doesn't exist, the "rename" syscall will be called twice (unsuccesfully) instead of once. By branching on "overwrite" from the beginning, we avoid calling the first "rename" in vain. Co-authored-by: Konstantin Gukov <kgukov@microsoft.com>
isDirectory:errorInfo.Error==Interop.Error.ENOENT&&!Directory.Exists(Path.GetDirectoryName(destFullPath))// The parent directory of destFile can't be found
185
164
);
@@ -190,6 +169,28 @@ public static void MoveFile(string sourceFullPath, string destFullPath, bool ove
190
169
return;
191
170
}
192
171
172
+
// The desired behavior for Move(source, dest) is to not overwrite the destination file
173
+
// if it exists. Since rename(source, dest) will replace the file at 'dest' if it exists,
174
+
// link/unlink are used instead. Rename is more efficient than link/unlink on file systems
175
+
// where hard links are not supported (such as FAT). Therefore, given that source file exists,
176
+
// rename is used in 2 cases: when dest file does not exist or when source path and dest
177
+
// path refer to the same file (on the same device). This is important for case-insensitive
178
+
// file systems (e.g. renaming a file in a way that just changes casing), so that we support
179
+
// changing the casing in the naming of the file. If this fails in any way (e.g. source file
180
+
// doesn't exist, dest file doesn't exist, rename fails, etc.), we just fall back to trying the
181
+
// link/unlink approach and generating any exceptional messages from there as necessary.
0 commit comments