Example #1
0
 /**
  * Determines whether a file is text or binary by checking the first few bytes in the file.
  * The exact number of bytes is system dependent, but it is typically several thousand.
  * If every byte in that part of the file is non-null, considers the file to be text;
  * otherwise it considers the file to be binary.
  * 
  * @param	string		$file
  * @return 	boolean
  */
 public static function isBinary($file)
 {
     // open file
     $file = new File($file, 'rb');
     // get block size
     $stat = $file->stat();
     $blockSize = $stat['blksize'];
     if ($blockSize < 0) {
         $blockSize = 1024;
     }
     if ($blockSize > $file->filesize()) {
         $blockSize = $file->filesize();
     }
     if ($blockSize <= 0) {
         return false;
     }
     // get bytes
     $block = $file->read($blockSize);
     return strlen($block) == 0 || preg_match_all('/\\x00/', $block, $match) > 0;
 }
Example #2
0
 public static function open($path, $flags, $cb, $mode = null, $pri = EIO_PRI_DEFAULT)
 {
     if (self::$supported) {
         $fdCacheKey = $path . "" . $flags;
         $noncache = strpos($flags, '!') !== false;
         $flags = File::convertFlags($flags);
         if (!$noncache && ($item = FS::$fdCache->get($fdCacheKey))) {
             // cache hit
             $file = $item->getValue();
             if ($file === null) {
                 // operation in progress
                 $item->addListener($cb);
             } else {
                 // hit
                 call_user_func($cb, $file);
             }
             return;
         } elseif (!$noncache) {
             $item = FS::$fdCache->put($fdCacheKey, null);
             $item->addListener($cb);
         }
         return eio_open($path, $flags, $mode, $pri, function ($arg, $fd) use($cb, $path, $flags, $fdCacheKey, $noncache) {
             if (!$fd) {
                 if ($noncache) {
                     call_user_func($cb, false);
                 } else {
                     FS::$fdCache->put($fdCacheKey, false, self::$badFDttl);
                 }
                 return;
             }
             $file = new File($fd);
             $file->append = ($flags | EIO_O_APPEND) === $flags;
             $file->path = $path;
             if ($file->append) {
                 $file->stat(function ($file, $stat) use($cb, $noncache, $fdCacheKey) {
                     $file->pos = $stat['size'];
                     if (!$noncache) {
                         $file->fdCacheKey = $fdCacheKey;
                         FS::$fdCache->put($fdCacheKey, $file);
                     } else {
                         call_user_func($cb, $file);
                     }
                 });
             } else {
                 if (!$noncache) {
                     $file->fdCacheKey = $fdCacheKey;
                     FS::$fdCache->put($fdCacheKey, $file);
                 } else {
                     call_user_func($cb, $file);
                 }
             }
         }, $path);
     }
     $mode = File::convertFlags($flags, true);
     $fd = fopen($path, $mode);
     if (!$fd) {
         call_user_func($cb, false);
         return;
     }
     $file = new File($fd);
     $file->path = $path;
     call_user_func($cb, $file);
 }