Skip to content

Commit 4412c00

Browse files
committed
Use O_EXCL to create trash info file as required by spec
1 parent 2f0dbbf commit 4412c00

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

source/trashcan.d

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ enum TrashOptions : int
6464
checkStickyBit = 8,
6565

6666
/**
67-
*
67+
* All flags set.
6868
*/
6969
all = (TrashOptions.fallbackToUserDir | TrashOptions.fallbackToHomeDir | TrashOptions.checkStickyBit | TrashOptions.useTopDirs)
7070
}
@@ -101,6 +101,7 @@ private:
101101
import core.sys.posix.sys.types;
102102
import core.sys.posix.sys.stat;
103103
import core.sys.posix.unistd;
104+
import core.sys.posix.fcntl;
104105

105106
@trusted string topDir(string path)
106107
in {
@@ -247,8 +248,6 @@ private:
247248
}
248249
}
249250
} else version(OSX) {
250-
import std.exception;
251-
252251
void* handle = dlopen("CoreServices.framework/Versions/A/CoreServices", RTLD_NOW | RTLD_LOCAL);
253252
if (handle !is null) {
254253
scope(exit) dlclose(handle);
@@ -329,10 +328,18 @@ private:
329328
}
330329

331330
import std.datetime;
331+
import std.conv : octal;
332+
332333
auto currentTime = Clock.currTime;
333334
currentTime.fracSecs = Duration.zero;
334-
string contents = format("[Trash Info]\nPath=%s\nDeletionDate=%s\n", path.escapeValue(), currentTime.toISOExtString());
335-
write(trashInfoPath, contents);
335+
string timeString = currentTime.toISOExtString();
336+
string contents = format("[Trash Info]\nPath=%s\nDeletionDate=%s\n", path.escapeValue(), timeString);
337+
338+
auto mode = O_CREAT | O_WRONLY | O_EXCL;
339+
auto fd = open(toStringz(trashInfoPath), mode, octal!666);
340+
errnoEnforce(fd != 0);
341+
errnoEnforce(write(fd, contents.ptr, contents.length) == contents.length);
342+
336343
path.rename(trashFilePath);
337344
} else {
338345
static assert("Unsupported platform");

0 commit comments

Comments
 (0)