Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / files / engine / PhabricatorLocalDiskFileStorageEngine.php
blobd9f7caf123006269c37b4d30103ef9f98ac76e02
1 <?php
3 /**
4 * Local disk storage engine. Keeps files on local disk. This engine is easy
5 * to set up, but it doesn't work if you have multiple web frontends!
7 * @task internal Internals
8 */
9 final class PhabricatorLocalDiskFileStorageEngine
10 extends PhabricatorFileStorageEngine {
13 /* -( Engine Metadata )---------------------------------------------------- */
16 /**
17 * This engine identifies as "local-disk".
19 public function getEngineIdentifier() {
20 return 'local-disk';
23 public function getEnginePriority() {
24 return 5;
27 public function canWriteFiles() {
28 $path = PhabricatorEnv::getEnvConfig('storage.local-disk.path');
29 return (bool)strlen($path);
33 /* -( Managing File Data )------------------------------------------------- */
36 /**
37 * Write the file data to local disk. Returns the relative path as the
38 * file data handle.
39 * @task impl
41 public function writeFile($data, array $params) {
42 $root = $this->getLocalDiskFileStorageRoot();
44 // Generate a random, unique file path like "ab/29/1f918a9ac39201ff". We
45 // put a couple of subdirectories up front to avoid a situation where we
46 // have one directory with a zillion files in it, since this is generally
47 // bad news.
48 do {
49 $name = md5(mt_rand());
50 $name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name);
51 if (!Filesystem::pathExists($root.'/'.$name)) {
52 break;
54 } while (true);
56 $parent = $root.'/'.dirname($name);
57 if (!Filesystem::pathExists($parent)) {
58 execx('mkdir -p %s', $parent);
61 AphrontWriteGuard::willWrite();
62 Filesystem::writeFile($root.'/'.$name, $data);
64 return $name;
68 /**
69 * Read the file data off local disk.
70 * @task impl
72 public function readFile($handle) {
73 $path = $this->getLocalDiskFileStorageFullPath($handle);
74 return Filesystem::readFile($path);
78 /**
79 * Deletes the file from local disk, if it exists.
80 * @task impl
82 public function deleteFile($handle) {
83 $path = $this->getLocalDiskFileStorageFullPath($handle);
84 if (Filesystem::pathExists($path)) {
85 AphrontWriteGuard::willWrite();
86 Filesystem::remove($path);
91 /* -( Internals )---------------------------------------------------------- */
94 /**
95 * Get the configured local disk path for file storage.
97 * @return string Absolute path to somewhere that files can be stored.
98 * @task internal
100 private function getLocalDiskFileStorageRoot() {
101 $root = PhabricatorEnv::getEnvConfig('storage.local-disk.path');
103 if (!$root || $root == '/' || $root[0] != '/') {
104 throw new PhabricatorFileStorageConfigurationException(
105 pht(
106 "Malformed local disk storage root. You must provide an absolute ".
107 "path, and can not use '%s' as the root.",
108 '/'));
111 return rtrim($root, '/');
116 * Convert a handle into an absolute local disk path.
118 * @param string File data handle.
119 * @return string Absolute path to the corresponding file.
120 * @task internal
122 private function getLocalDiskFileStorageFullPath($handle) {
123 // Make sure there's no funny business going on here. Users normally have
124 // no ability to affect the content of handles, but double-check that
125 // we're only accessing local storage just in case.
126 if (!preg_match('@^[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{28}\z@', $handle)) {
127 throw new Exception(
128 pht(
129 "Local disk filesystem handle '%s' is malformed!",
130 $handle));
132 $root = $this->getLocalDiskFileStorageRoot();
133 return $root.'/'.$handle;