Skip to content

Commit

Permalink
Use O_EXCL to create trash info file as required by spec
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Jul 27, 2016
1 parent 2f0dbbf commit 4412c00
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions source/trashcan.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum TrashOptions : int
checkStickyBit = 8,

/**
*
* All flags set.
*/
all = (TrashOptions.fallbackToUserDir | TrashOptions.fallbackToHomeDir | TrashOptions.checkStickyBit | TrashOptions.useTopDirs)
}
Expand Down Expand Up @@ -101,6 +101,7 @@ private:
import core.sys.posix.sys.types;
import core.sys.posix.sys.stat;
import core.sys.posix.unistd;
import core.sys.posix.fcntl;

@trusted string topDir(string path)
in {
Expand Down Expand Up @@ -247,8 +248,6 @@ private:
}
}
} else version(OSX) {
import std.exception;

void* handle = dlopen("CoreServices.framework/Versions/A/CoreServices", RTLD_NOW | RTLD_LOCAL);
if (handle !is null) {
scope(exit) dlclose(handle);
Expand Down Expand Up @@ -329,10 +328,18 @@ private:
}

import std.datetime;
import std.conv : octal;

auto currentTime = Clock.currTime;
currentTime.fracSecs = Duration.zero;
string contents = format("[Trash Info]\nPath=%s\nDeletionDate=%s\n", path.escapeValue(), currentTime.toISOExtString());
write(trashInfoPath, contents);
string timeString = currentTime.toISOExtString();
string contents = format("[Trash Info]\nPath=%s\nDeletionDate=%s\n", path.escapeValue(), timeString);

auto mode = O_CREAT | O_WRONLY | O_EXCL;
auto fd = open(toStringz(trashInfoPath), mode, octal!666);
errnoEnforce(fd != 0);
errnoEnforce(write(fd, contents.ptr, contents.length) == contents.length);

path.rename(trashFilePath);
} else {
static assert("Unsupported platform");
Expand Down

0 comments on commit 4412c00

Please sign in to comment.