.htaccess
1 year ago
AtomicWrite.php
5 months ago
File.php
1 year ago
index.html
1 year ago
web.config
1 year ago
File.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Filesystem; |
| 4 | |
| 5 | use JetBackup\Exception\IOException; |
| 6 | |
| 7 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 8 | |
| 9 | class File { |
| 10 | |
| 11 | const IFMT = 0170000; |
| 12 | const IFDIR = 0040000; |
| 13 | const IFCHR = 0020000; |
| 14 | const IFBLK = 0060000; |
| 15 | const IFREG = 0100000; |
| 16 | const IFIFO = 0010000; |
| 17 | const IFLNK = 0120000; |
| 18 | const IFSOCK = 0140000; |
| 19 | |
| 20 | private $_file; |
| 21 | private $_stat; |
| 22 | private $_readable; |
| 23 | private $_fixed_size; |
| 24 | |
| 25 | public function __construct($file) { |
| 26 | $this->_file = $file; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @throws IOException |
| 31 | */ |
| 32 | public function getStat($key = null, $default = null) { |
| 33 | if ($this->_stat === null) { |
| 34 | $this->_stat = @lstat($this->_file); |
| 35 | if (!$this->_stat) throw new IOException('[getStat] Unable to retrieve file status: ' . $this->_file); |
| 36 | } |
| 37 | return $key ? ($this->_stat[$key] ?? $default) : $this->_stat; |
| 38 | } |
| 39 | |
| 40 | public function path():string { return $this->_file; } |
| 41 | public function dir() { return $this->isDir() ? dir($this->path()) : null; } |
| 42 | public function exists():bool { return $this->_stat !== null || file_exists($this->_file); } |
| 43 | public function size():int { |
| 44 | if($this->_fixed_size === null) { |
| 45 | if($this->isDir() || $this->isLink() || strpos($this->_file, "\0") !== false) $this->_fixed_size = true; |
| 46 | else $this->_fixed_size = false; |
| 47 | } |
| 48 | if($this->_fixed_size) return 0; |
| 49 | return (int) $this->getStat('size', 0); |
| 50 | } |
| 51 | public function mtime():int { return (int) $this->getStat('mtime', 0); } |
| 52 | public function uid():int { return (int) $this->getStat('uid', 0); } |
| 53 | public function gid():int { return (int) $this->getStat('gid', 0); } |
| 54 | public function mode():int { return (int) $this->getStat('mode', 0); } |
| 55 | |
| 56 | public function isDir():bool { return ($this->mode() & self::IFMT) == self::IFDIR; } |
| 57 | public function isFile():bool { return ($this->mode() & self::IFMT) == self::IFREG; } |
| 58 | public function isLink():bool { return ($this->mode() & self::IFMT) == self::IFLNK; } |
| 59 | public function isBlockDevice():bool { return ($this->mode() & self::IFMT) == self::IFBLK; } |
| 60 | public function isCharacterDevice():bool { return ($this->mode() & self::IFMT) == self::IFCHR; } |
| 61 | public function isFifo():bool { return ($this->mode() & self::IFMT) == self::IFIFO; } |
| 62 | public function isSocket():bool { return ($this->mode() & self::IFMT) == self::IFSOCK; } |
| 63 | public function isReadable():bool { |
| 64 | if($this->_readable === null) $this->_readable = is_readable($this->_file); |
| 65 | return $this->_readable; |
| 66 | } |
| 67 | } |