Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/Filesystem.php
| Show All 36 Lines | public static function readFile($path) { | ||||
| self::assertExists($path); | self::assertExists($path); | ||||
| self::assertIsFile($path); | self::assertIsFile($path); | ||||
| self::assertReadable($path); | self::assertReadable($path); | ||||
| $data = @file_get_contents($path); | $data = @file_get_contents($path); | ||||
| if ($data === false) { | if ($data === false) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to read file `%s'.", $path)); | pht("Failed to read file '%s'.", $path)); | ||||
| } | } | ||||
| return $data; | return $data; | ||||
| } | } | ||||
| /** | /** | ||||
| * Make assertions about the state of path in preparation for | * Make assertions about the state of path in preparation for | ||||
| * writeFile() and writeFileIfChanged(). | * writeFile() and writeFileIfChanged(). | ||||
| Show All 32 Lines | /* -( Files )-------------------------------------------------------------- */ | ||||
| * @task file | * @task file | ||||
| */ | */ | ||||
| public static function writeFile($path, $data) { | public static function writeFile($path, $data) { | ||||
| self::assertWritableFile($path); | self::assertWritableFile($path); | ||||
| if (@file_put_contents($path, $data) === false) { | if (@file_put_contents($path, $data) === false) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to write file `%s'.", $path)); | pht("Failed to write file '%s'.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Write a file in a manner similar to `file_put_contents()`, but only touch | * Write a file in a manner similar to `file_put_contents()`, but only touch | ||||
| * the file if the contents are different, and throw detailed exceptions on | * the file if the contents are different, and throw detailed exceptions on | ||||
| * failure. | * failure. | ||||
| * | * | ||||
| ▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | public static function appendFile($path, $data) { | ||||
| self::assertExists($dir); | self::assertExists($dir); | ||||
| self::assertIsDirectory($dir); | self::assertIsDirectory($dir); | ||||
| self::assertWritable($dir); | self::assertWritable($dir); | ||||
| assert_stringlike($data); | assert_stringlike($data); | ||||
| if (($fh = fopen($path, 'a')) === false) { | if (($fh = fopen($path, 'a')) === false) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to open file `%s'.", $path)); | pht("Failed to open file '%s'.", $path)); | ||||
| } | } | ||||
| $dlen = strlen($data); | $dlen = strlen($data); | ||||
| if (fwrite($fh, $data) !== $dlen) { | if (fwrite($fh, $data) !== $dlen) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to write %d bytes to `%s'.", $dlen, $path)); | pht("Failed to write %d bytes to '%s'.", $dlen, $path)); | ||||
| } | } | ||||
| if (!fflush($fh) || !fclose($fh)) { | if (!fflush($fh) || !fclose($fh)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed closing file `%s' after write.", $path)); | pht("Failed closing file '%s' after write.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Remove a file or directory. | * Remove a file or directory. | ||||
| * | * | ||||
| * @param string File to a path or directory to remove. | * @param string File to a path or directory to remove. | ||||
| ▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | public static function changePermissions($path, $umask) { | ||||
| $path = self::resolvePath($path); | $path = self::resolvePath($path); | ||||
| self::assertExists($path); | self::assertExists($path); | ||||
| if (!@chmod($path, $umask)) { | if (!@chmod($path, $umask)) { | ||||
| $readable_umask = sprintf('%04o', $umask); | $readable_umask = sprintf('%04o', $umask); | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to chmod `%s' to `%s'.", $path, $readable_umask)); | pht("Failed to chmod '%s' to '%s'.", $path, $readable_umask)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Get the last modified time of a file | * Get the last modified time of a file | ||||
| * | * | ||||
| * @param string Path to file | * @param string Path to file | ||||
| ▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | if (function_exists('openssl_random_pseudo_bytes')) { | ||||
| pht( | pht( | ||||
| '%s failed to generate entropy!', | '%s failed to generate entropy!', | ||||
| 'openssl_random_pseudo_bytes()')); | 'openssl_random_pseudo_bytes()')); | ||||
| } | } | ||||
| if (strlen($data) != $number_of_bytes) { | if (strlen($data) != $number_of_bytes) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| '%s returned an unexpected number of bytes (got %d, expected %d)!', | '%s returned an unexpected number of bytes (got %s, expected %s)!', | ||||
| 'openssl_random_pseudo_bytes()', | 'openssl_random_pseudo_bytes()', | ||||
| strlen($data), | new PhutilNumber(strlen($data)), | ||||
| $number_of_bytes)); | new PhutilNumber($number_of_bytes))); | ||||
| } | } | ||||
| return $data; | return $data; | ||||
| } | } | ||||
| // Try to use `/dev/urandom` if it's available. This is usually available | // Try to use `/dev/urandom` if it's available. This is usually available | ||||
| // on non-Windows systems, but some PHP config (open_basedir) and chrooting | // on non-Windows systems, but some PHP config (open_basedir) and chrooting | ||||
| ▲ Show 20 Lines • Show All 190 Lines • ▼ Show 20 Lines | public static function createDirectory( | ||||
| self::assertIsDirectory($dir); | self::assertIsDirectory($dir); | ||||
| self::assertExists($dir); | self::assertExists($dir); | ||||
| self::assertWritable($dir); | self::assertWritable($dir); | ||||
| self::assertNotExists($path); | self::assertNotExists($path); | ||||
| if (!mkdir($path, $umask)) { | if (!mkdir($path, $umask)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Failed to create directory `%s'.", $path)); | pht("Failed to create directory '%s'.", $path)); | ||||
| } | } | ||||
| // Need to change permissions explicitly because mkdir does something | // Need to change permissions explicitly because mkdir does something | ||||
| // slightly different. mkdir(2) man page: | // slightly different. mkdir(2) man page: | ||||
| // 'The parameter mode specifies the permissions to use. It is modified by | // 'The parameter mode specifies the permissions to use. It is modified by | ||||
| // the process's umask in the usual way: the permissions of the created | // the process's umask in the usual way: the permissions of the created | ||||
| // directory are (mode & ~umask & 0777)."' | // directory are (mode & ~umask & 0777)."' | ||||
| if ($umask) { | if ($umask) { | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | public static function listDirectory($path, $include_hidden = true) { | ||||
| self::assertExists($path); | self::assertExists($path); | ||||
| self::assertIsDirectory($path); | self::assertIsDirectory($path); | ||||
| self::assertReadable($path); | self::assertReadable($path); | ||||
| $list = @scandir($path); | $list = @scandir($path); | ||||
| if ($list === false) { | if ($list === false) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Unable to list contents of directory `%s'.", $path)); | pht("Unable to list contents of directory '%s'.", $path)); | ||||
| } | } | ||||
| foreach ($list as $k => $v) { | foreach ($list as $k => $v) { | ||||
| if ($v == '.' || $v == '..' || (!$include_hidden && $v[0] == '.')) { | if ($v == '.' || $v == '..' || (!$include_hidden && $v[0] == '.')) { | ||||
| unset($list[$k]); | unset($list[$k]); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 278 Lines • ▼ Show 20 Lines | /* -( Assert )------------------------------------------------------------- */ | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertExists($path) { | public static function assertExists($path) { | ||||
| if (!self::pathExists($path)) { | if (!self::pathExists($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("File system entity `%s' does not exist.", $path)); | pht("File system entity '%s' does not exist.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Assert that nothing exists at a specified location. | * Assert that nothing exists at a specified location. | ||||
| * | * | ||||
| * @param string Assert that this path does not exist. | * @param string Assert that this path does not exist. | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertNotExists($path) { | public static function assertNotExists($path) { | ||||
| if (file_exists($path) || is_link($path)) { | if (file_exists($path) || is_link($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Path `%s' already exists!", $path)); | pht("Path '%s' already exists!", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Assert that a path represents a file, strictly (i.e., not a directory). | * Assert that a path represents a file, strictly (i.e., not a directory). | ||||
| * | * | ||||
| * @param string Assert that this path is a file. | * @param string Assert that this path is a file. | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertIsFile($path) { | public static function assertIsFile($path) { | ||||
| if (!is_file($path)) { | if (!is_file($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Requested path `%s' is not a file.", $path)); | pht("Requested path '%s' is not a file.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Assert that a path represents a directory, strictly (i.e., not a file). | * Assert that a path represents a directory, strictly (i.e., not a file). | ||||
| * | * | ||||
| * @param string Assert that this path is a directory. | * @param string Assert that this path is a directory. | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertIsDirectory($path) { | public static function assertIsDirectory($path) { | ||||
| if (!is_dir($path)) { | if (!is_dir($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Requested path `%s' is not a directory.", $path)); | pht("Requested path '%s' is not a directory.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Assert that a file or directory exists and is writable. | * Assert that a file or directory exists and is writable. | ||||
| * | * | ||||
| * @param string Assert that this path is writable. | * @param string Assert that this path is writable. | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertWritable($path) { | public static function assertWritable($path) { | ||||
| if (!is_writable($path)) { | if (!is_writable($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Requested path `%s' is not writable.", $path)); | pht("Requested path '%s' is not writable.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Assert that a file or directory exists and is readable. | * Assert that a file or directory exists and is readable. | ||||
| * | * | ||||
| * @param string Assert that this path is readable. | * @param string Assert that this path is readable. | ||||
| * @return void | * @return void | ||||
| * | * | ||||
| * @task assert | * @task assert | ||||
| */ | */ | ||||
| public static function assertReadable($path) { | public static function assertReadable($path) { | ||||
| if (!is_readable($path)) { | if (!is_readable($path)) { | ||||
| throw new FilesystemException( | throw new FilesystemException( | ||||
| $path, | $path, | ||||
| pht("Path `%s' is not readable.", $path)); | pht("Path '%s' is not readable.", $path)); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||