Skip to content

Commit

Permalink
Merge pull request #2528 from tom-tan/safe-file-setAttributes
Browse files Browse the repository at this point in the history
Make std.file.setAttributes safe
  • Loading branch information
H. S. Teoh committed Sep 18, 2014
2 parents 20d06a3 + 1a91e85 commit f88c252
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions std/file.d
Expand Up @@ -1091,16 +1091,24 @@ uint getLinkAttributes(in char[] name) @safe
Throws:
$(D FileException) if the given file does not exist.
+/
void setAttributes(in char[] name, uint attributes)
void setAttributes(in char[] name, uint attributes) @safe
{
version (Windows)
{
cenforce(SetFileAttributesW(name.tempCStringW(), attributes), name);
static auto trustedSetFileAttributesW(in char[] fileName, uint dwFileAttributes) @trusted
{
return SetFileAttributesW(fileName.tempCStringW(), dwFileAttributes);
}
cenforce(trustedSetFileAttributesW(name, attributes), name);
}
else version (Posix)
{
static auto trustedChmod(in char[] path, mode_t mode) @trusted
{
return chmod(path.tempCString(), mode);
}
assert(attributes <= mode_t.max);
cenforce(!chmod(name.tempCString(), cast(mode_t)attributes), name);
cenforce(!trustedChmod(name, cast(mode_t)attributes), name);
}
}

Expand Down

0 comments on commit f88c252

Please sign in to comment.