Core\FileTransfer\Connection::setPermissions
$connection->setPermissions(string $path, uint $mode): void$connection->setPermissions( string $path, uint $mode, ): void
Sets the permissions of a file on the server. The mode is the Unix permission bits, written in octal (base 8). 0o644 means that the owner can read and write the file, and everybody else can only read it. The mode goes from 0o0 to 0o7777. A larger mode throws a RuntimeError.
SFTP sends SETSTAT. FTP and FTPS send SITE CHMOD. When an FTP server does not support that command, setPermissions throws an IOError that names it.
A directory block changes the permissions of a file. For a folder, it throws an IOError. Windows has no Unix permission bits, so there a directory block uses only the bit 0o200. Without this bit, the file is read-only.
setPermissions throws an IOError when the file does not exist or the server does not allow the change. A path throws a RuntimeError when it starts with / or has a NUL, CR or LF character. A part of the path between two / that is empty or .. also throws. Then nothing is sent.
metadata($path)->permissions() returns the permissions of a file, or null when the server does not send them. On Windows, a directory block always gives null.
Parameters
$pathstringThe path, relative to the block'sroot, such asin/orders.csv.$modeuintThe Unix permission bits, such as0o644for a file everybody can read and only the owner can write. The mode goes from0o0to0o7777, and a larger mode throws aRuntimeError. On Windows, adirectoryblock uses only the bit0o200, which lets the owner write the file. When this bit is not set, the file is read-only.
Return value
Returns void. Nothing.
Errors
RuntimeErrorThe path starts with/, has an empty part or a..part, or has a NUL, CR or LF character.IOErrorThe file does not exist, the server does not support setting permissions, or the server does not answer. Adirectoryblock also throws this error when the path is a folder.LogicErrorThe connection is closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once. |
Examples
The examples below run with this configuration file.
# The `partner` block reads and writes the folder `partner` beside this file. It needs no server.[file_transfer.partner]directory = "partner"
# The program may open the `partner` block.[capabilities.file_transfer]connect = ["partner"]
# A `directory` block needs read and write access to its folder.[capabilities.fs]read = ["partner"]write = ["partner"]Let everyone read an uploaded file
<?nvs// Shows how to let everybody read an uploaded file, and then how to let only the owner read it.
var $partner = Core\FileTransfer::open('partner');$partner->write('prices-01.csv', "id,price\n1,10\n" as bytes);
// `0o644` means that the owner can read and write the file, and everybody else can only read it.// This prints "everybody can read prices-01.csv".$partner->setPermissions('prices-01.csv', 0o644);echo "everybody can read prices-01.csv\n";
// `0o600` means that only the owner can read and write the file.// This prints "only the owner can read prices-01.csv".$partner->setPermissions('prices-01.csv', 0o600);echo "only the owner can read prices-01.csv\n";
$partner->remove('prices-01.csv');everybody can read prices-01.csvonly the owner can read prices-01.csvA mode above 0o7777 throws
<?nvs// Shows that a mode above `0o7777` throws a `RuntimeError`. Nothing is sent to the server.
var $partner = Core\FileTransfer::open('partner');
// The permission bits go from `0o0` to `0o7777`. `0o10644` is larger than `0o7777`, so `setPermissions` throws.try { $partner->setPermissions('readme.txt', 0o10644);} catch (RuntimeError $tooLarge) { echo $tooLarge->message, "\n";}Core\FileTransfer\Connection::setPermissions: `$mode` is 0o10644, and the permission bits are 0o0 to 0o7777A file that does not exist throws
<?nvs// Shows the errors `setPermissions` throws for a file that does not exist and for a path outside the folder.
var $partner = Core\FileTransfer::open('partner');
// `missing-03.csv` does not exist, so `setPermissions` throws an `IOError`.try { $partner->setPermissions('missing-03.csv', 0o644);} catch (IOError $missing) { echo "missing-03.csv does not exist\n";}
// A path with a `..` part throws a `RuntimeError`. Nothing is sent to the server.try { $partner->setPermissions('../nvs.toml', 0o644);} catch (RuntimeError $outside) { echo $outside->message, "\n";}missing-03.csv does not existCore\FileTransfer\Connection::setPermissions: the path "../nvs.toml" is not allowed: it has a `..` part, and a path cannot leave the block's `root`