diff --git a/src/filesystem/Filesystem.php b/src/filesystem/Filesystem.php --- a/src/filesystem/Filesystem.php +++ b/src/filesystem/Filesystem.php @@ -677,18 +677,30 @@ * @param string Optional directory prefix. * @param int Permissions to create the directory with. By default, * these permissions are very restrictive (0700). + * @param string Optional root directory. If not provided, the system + * temporary directory (often "/tmp") will be used. * @return string Path to newly created temporary directory. * * @task directory */ - public static function createTemporaryDirectory($prefix = '', $umask = 0700) { + public static function createTemporaryDirectory( + $prefix = '', + $umask = 0700, + $root_directory = null) { $prefix = preg_replace('/[^A-Z0-9._-]+/i', '', $prefix); - $tmp = sys_get_temp_dir(); - if (!$tmp) { - throw new FilesystemException( - $tmp, - pht('Unable to determine system temporary directory.')); + if ($root_directory !== null) { + $tmp = $root_directory; + self::assertExists($tmp); + self::assertIsDirectory($tmp); + self::assertWritable($tmp); + } else { + $tmp = sys_get_temp_dir(); + if (!$tmp) { + throw new FilesystemException( + $tmp, + pht('Unable to determine system temporary directory.')); + } } $base = $tmp.DIRECTORY_SEPARATOR.$prefix; diff --git a/src/filesystem/TempFile.php b/src/filesystem/TempFile.php --- a/src/filesystem/TempFile.php +++ b/src/filesystem/TempFile.php @@ -30,10 +30,15 @@ * @param string? Filename hint. This is useful if you intend to edit the * file with an interactive editor, so the user's editor shows * "commit-message" instead of "p3810hf-1z9b89bas". + * @param string? Root directory to hold the file. If omitted, the system + * temporary directory (often "/tmp") will be used by default. * @task create */ - public function __construct($filename = null) { - $this->dir = Filesystem::createTemporaryDirectory(); + public function __construct($filename = null, $root_directory = null) { + $this->dir = Filesystem::createTemporaryDirectory( + '', + 0700, + $root_directory); if ($filename === null) { $this->file = tempnam($this->dir, getmypid().'-'); } else {