|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the yannoff/console library |
| 5 | + * |
| 6 | + * (c) Yannoff (https://github.com/yannoff) |
| 7 | + * |
| 8 | + * @project yannoff/console |
| 9 | + * @link https://github.com/yannoff/console |
| 10 | + * @license https://github.com/yannoff/console/blob/master/LICENSE |
| 11 | + * |
| 12 | + * For the full copyright and license information, please view the LICENSE |
| 13 | + * file that was distributed with this source code. |
| 14 | + */ |
| 15 | + |
| 16 | +namespace Yannoff\Component\Console\IO; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class FStat |
| 20 | + * Handle the inode file statistics |
| 21 | + * |
| 22 | + * @source https://stackoverflow.com/a/11327451 |
| 23 | + * |
| 24 | + * @package Yannoff\Component\Console\IO |
| 25 | + */ |
| 26 | +class FStat |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Octal mode values from libc headers |
| 30 | + */ |
| 31 | + const S_IFMT = 0170000; |
| 32 | + const S_IFIFO = 0010000; |
| 33 | + const S_IFCHR = 0020000; |
| 34 | + const S_IFDIR = 0040000; |
| 35 | + const S_IFBLK = 0060000; |
| 36 | + const S_IFREG = 0100000; |
| 37 | + const S_IFLNK = 0120000; |
| 38 | + const S_IFSOCK = 0140000; |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the handle current protection mode |
| 42 | + * |
| 43 | + * @param resource $handle |
| 44 | + * |
| 45 | + * @return int |
| 46 | + */ |
| 47 | + protected static function imode($handle) |
| 48 | + { |
| 49 | + $stat = fstat($handle); |
| 50 | + return $stat['mode'] & self::S_IFMT; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check whether the handle is a FIFO |
| 55 | + * |
| 56 | + * @param resource $handle |
| 57 | + * |
| 58 | + * @return bool |
| 59 | + */ |
| 60 | + public static function isFifo($handle) |
| 61 | + { |
| 62 | + return self::imode($handle) == self::S_IFIFO; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check whether the handle is a character |
| 67 | + * |
| 68 | + * @param resource $handle |
| 69 | + * |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + public static function isChar($handle) |
| 73 | + { |
| 74 | + return self::imode($handle) == self::S_IFCHR; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Check whether the handle is a directory |
| 79 | + * |
| 80 | + * @param resource $handle |
| 81 | + * |
| 82 | + * @return bool |
| 83 | + */ |
| 84 | + public static function isDir($handle) |
| 85 | + { |
| 86 | + return self::imode($handle) == self::S_IFDIR; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Check whether the handle is a block device |
| 91 | + * |
| 92 | + * @param resource $handle |
| 93 | + * |
| 94 | + * @return bool |
| 95 | + */ |
| 96 | + public static function isBlock($handle) |
| 97 | + { |
| 98 | + return self::imode($handle) == self::S_IFBLK; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Check whether the handle is a regular file redirect |
| 103 | + * |
| 104 | + * @param resource $handle |
| 105 | + * |
| 106 | + * @return bool |
| 107 | + */ |
| 108 | + public static function isRegularFile($handle) |
| 109 | + { |
| 110 | + return self::imode($handle) == self::S_IFREG; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Check whether the handle is a symlink |
| 115 | + * |
| 116 | + * @param resource $handle |
| 117 | + * |
| 118 | + * @return bool |
| 119 | + */ |
| 120 | + public static function isLink($handle) |
| 121 | + { |
| 122 | + return self::imode($handle) == self::S_IFLNK; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Check whether the handle is a socket |
| 127 | + * |
| 128 | + * @param resource $handle |
| 129 | + * |
| 130 | + * @return bool |
| 131 | + */ |
| 132 | + public static function isSocket($handle) |
| 133 | + { |
| 134 | + return self::imode($handle) == self::S_IFSOCK; |
| 135 | + } |
| 136 | +} |
0 commit comments