aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-01-24 20:03:35 +0000
committerMario <mario@mariovavti.com>2021-01-24 20:03:35 +0000
commit77793e17c04b1f67c10a83e7877c36a8b11ddbaa (patch)
tree293729c08f5f0193a6445f641b5ac09d43168d3b /Zotlabs/Lib
parente486442eb1fdda0c4efa7eafc6d876b3c2c4b0ea (diff)
parent552796286e4a2a61cc6020b7aa785dc88553e2d0 (diff)
downloadvolse-hubzilla-77793e17c04b1f67c10a83e7877c36a8b11ddbaa.tar.gz
volse-hubzilla-77793e17c04b1f67c10a83e7877c36a8b11ddbaa.tar.bz2
volse-hubzilla-77793e17c04b1f67c10a83e7877c36a8b11ddbaa.zip
Merge branch 'dev' into 'dev'
Add support filesystem storage for xchan profile photos See merge request hubzilla/core!1898
Diffstat (limited to 'Zotlabs/Lib')
-rw-r--r--Zotlabs/Lib/Hashpath.php55
1 files changed, 55 insertions, 0 deletions
diff --git a/Zotlabs/Lib/Hashpath.php b/Zotlabs/Lib/Hashpath.php
new file mode 100644
index 000000000..f3b25d2b6
--- /dev/null
+++ b/Zotlabs/Lib/Hashpath.php
@@ -0,0 +1,55 @@
+<?php
+namespace Zotlabs\Lib;
+
+/*
+ * Zotlabs\Lib\Hashpath
+ *
+ * Creates hashed directory structures for fast access and resistance to overloading any single directory with files.
+ *
+ * Takes a $hash which could be any string
+ * a $prefix which is where to place the hash directory in the filesystem, default is current directory
+ * use an empty string for $prefix to place hash directories directly off the root directory
+ * an optional $depth and $slice (default is 2) to indicate the hash level
+ * $depth = 1, 256 directories, suitable for < 384K records/files
+ * $depth = 2, 65536 directories, suitable for < 98M records/files
+ * $depth = 3, 16777216 directories, suitable for < 2.5B records/files
+ * ...
+ * The total number of records anticipated divided by the number of hash directories should generally be kept to
+ * less than 1500 entries for optimum performance though this varies by operating system and filesystem type.
+ * ext4 uses 32 bit inode numbers (~4B record limit) so use caution or alternative filesystem types with $depth above 3.
+ * an optional $mkdir (boolean) to recursively create the directory (ignoring errors) before returning
+ *
+ * examples: for a $hash of 'abcdefg' and prefix of 'path' the following paths are returned for $depth = 1 and $depth = 3
+ * path/7d/7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a
+ * path/7d/1a/54/7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a
+ *
+ * see also: boot.php:os_mkdir() - here we provide the equivalent of mkdir -p with permissions of 770.
+ *
+ */
+
+class Hashpath {
+
+ static function path($hash, $prefix = '.', $depth = 1, $slice = 2, $mkdir = true, $alg = false) {
+
+ if ($alg)
+ $hash = hash($alg, $hash);
+
+ $start = 0;
+ if ($depth < 1)
+ $depth = 1;
+ $sluglen = $depth * $slice;
+
+ do {
+ $slug = substr($hash, $start, $slice);
+ $prefix .= '/' . $slug;
+ $start += $slice;
+ $sluglen -= $slice;
+ }
+ while ($sluglen);
+
+ if ($mkdir)
+ os_mkdir($prefix, STORAGE_DEFAULT_PERMISSIONS, true);
+
+ return $prefix . '/' . $hash;
+ }
+}