From c899798be71d7f93ba1ad00f134f7b71ca6c3dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 14 Jan 2019 00:31:38 +0100 Subject: [PATCH 1/3] Avoid blocking copy in moveFile. --- source/vibe/core/file.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/vibe/core/file.d b/source/vibe/core/file.d index 83defc3..6530a1e 100644 --- a/source/vibe/core/file.d +++ b/source/vibe/core/file.d @@ -191,7 +191,7 @@ void moveFile(string from, string to, bool copy_fallback = false) try { std.file.rename(from, to); } catch (FileException e) { - std.file.copy(from, to); + copyFile(from, to); std.file.remove(from); } } From 9583df3c4414994dc36411e8d9e0af3ac7253d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 14 Jan 2019 00:32:27 +0100 Subject: [PATCH 2/3] Preserve times and attributes in copyFile. --- source/vibe/core/file.d | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/source/vibe/core/file.d b/source/vibe/core/file.d index 6530a1e..0ad3711 100644 --- a/source/vibe/core/file.d +++ b/source/vibe/core/file.d @@ -214,6 +214,17 @@ void moveFile(string from, string to, bool copy_fallback = false) */ void copyFile(NativePath from, NativePath to, bool overwrite = false) { + DirEntry info; + static if (__VERSION__ < 2078) { + () @trusted { + info = DirEntry(from.toString); + enforce(info.isFile, "The source path is not a file and cannot be copied."); + } (); + } else { + info = DirEntry(from.toString); + enforce(info.isFile, "The source path is not a file and cannot be copied."); + } + { auto src = openFile(from, FileMode.read); scope(exit) src.close(); @@ -223,7 +234,17 @@ void copyFile(NativePath from, NativePath to, bool overwrite = false) dst.write(src); } - // TODO: retain attributes and time stamps + // TODO: also retain creation time on windows + + static if (__VERSION__ < 2078) { + () @trusted { + setAttributes(to.toString, info.attributes); + setTimes(to.toString, info.timeLastAccessed, info.timeLastModified); + } (); + } else { + setAttributes(to.toString, info.attributes); + setTimes(to.toString, info.timeLastAccessed, info.timeLastModified); + } } /// ditto void copyFile(string from, string to) From 2f552ac40835adf4b22c972dfa36a47f593e277b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 14 Jan 2019 00:35:00 +0100 Subject: [PATCH 3/3] Properly handle directory watcher creation failures. --- source/vibe/core/file.d | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/vibe/core/file.d b/source/vibe/core/file.d index 0ad3711..c3b6021 100644 --- a/source/vibe/core/file.d +++ b/source/vibe/core/file.d @@ -644,6 +644,8 @@ struct DirectoryWatcher { // TODO: avoid all those heap allocations! m_context = new Context; // FIME: avoid GC allocation (use FD user data slot) m_context.changeEvent = createManualEvent(); m_watcher = eventDriver.watchers.watchDirectory(path.toNativeString, recursive, &m_context.onChange); + if (m_watcher == WatcherID.invalid) + throw new Exception("Failed to watch directory."); m_context.path = path; m_context.recursive = recursive; m_context.changes = appender!(DirectoryChange[]);