Path.setAttributes

Set attributes of the path * * Params: * attributes = value representing attributes to set on path.

struct Path
const
void
setAttributes
(
in uint attributes
)

Examples

Example of changing attributes of file.

import dshould;
import std.conv: octal;
Path root = createTempPath();
scope(exit) root.remove();

// Here we have to import bitmasks from system;
import core.sys.posix.sys.stat;

root.join("test-file.txt").writeFile("Hello World!");

// Check that file has numeric permissions 644
root.join("test-file.txt").hasAttributes(octal!644).should.be(true);


auto attributes = root.join("test-file.txt").getAttributes();

// Test that file is readable by user
(attributes & S_IRUSR).should.equal(S_IRUSR);

// Test that file is not writeable by others
(attributes & S_IWOTH).should.not.equal(S_IWOTH);

// Add right to write file by others
root.join("test-file.txt").setAttributes(attributes | S_IWOTH);

// Test that file is now writable by others
root.join("test-file.txt").hasAttributes(S_IWOTH).should.be(true);

// Test that numeric permissions changed
root.join("test-file.txt").hasAttributes(octal!646).should.be(true);

// Set attributes as numeric value
root.join("test-file.txt").setAttributes(octal!660);

// Test that no group users can write the file
root.join("test-file.txt").hasAttributes(octal!660).should.be(true);

// Test that others do not have any access to the file
root.join("test-file.txt").hasAttributes(octal!104).should.be(false);
root.join("test-file.txt").hasAttributes(octal!106).should.be(false);
root.join("test-file.txt").hasAttributes(octal!107).should.be(false);
root.join("test-file.txt").hasAttributes(S_IWOTH).should.be(false);
root.join("test-file.txt").hasAttributes(S_IROTH).should.be(false);
root.join("test-file.txt").hasAttributes(S_IXOTH).should.be(false);

Meta