/** * This function will make directory writable. * * @param string path * @return void * @throw Kohana_Exception */ public static function make_writable($path, $chmod = NULL) { try { $dir = new SplFileInfo($path); if ($dir->isFile()) { throw new Kohana_Exception('Could not make :path writable directory because it is regular file', array(':path' => Debug::path($path))); } elseif ($dir->isLink()) { throw new Kohana_Exception('Could not make :path writable directory because it is link', array(':path' => Debug::path($path))); } elseif (!$dir->isDir()) { // Try create directory Ku_Dir::make($path, $chmod); clearstatcache(TRUE, $path); } if (!$dir->isWritable()) { // Try make directory writable chmod($dir->getRealPath(), $chmod === NULL ? Ku_Dir::$default_dir_chmod : $chmod); clearstatcache(TRUE, $path); // Check result if (!$dir->isWritable()) { throw new Exception('Make dir writable failed', 0); } } } catch (Kohana_Exception $e) { // Rethrow exception throw $e; } catch (Exception $e) { throw new Kohana_Exception('Could not make :path directory writable', array(':path' => Debug::path($path))); } }
public function __construct(SplFileInfo $fileInfo) { $this->perms = $fileInfo->getPerms(); $this->size = $fileInfo->getSize(); $this->is_dir = $fileInfo->isDir(); $this->is_file = $fileInfo->isFile(); $this->is_link = $fileInfo->isLink(); if (($this->perms & 0xc000) === 0xc000) { $this->typename = 'File socket'; $this->typeflag = 's'; } elseif ($this->is_file) { if ($this->is_link) { $this->typename = 'File symlink'; $this->typeflag = 'l'; } else { $this->typename = 'File'; $this->typeflag = '-'; } } elseif (($this->perms & 0x6000) === 0x6000) { $this->typename = 'Block special file'; $this->typeflag = 'b'; } elseif ($this->is_dir) { if ($this->is_link) { $this->typename = 'Directory symlink'; $this->typeflag = 'l'; } else { $this->typename = 'Directory'; $this->typeflag = 'd'; } } elseif (($this->perms & 0x2000) === 0x2000) { $this->typename = 'Character special file'; $this->typeflag = 'c'; } elseif (($this->perms & 0x1000) === 0x1000) { $this->typename = 'FIFO pipe file'; $this->typeflag = 'p'; } parent::__construct('FsPath'); $this->path = $fileInfo->getPathname(); $this->realpath = realpath($this->path); if ($this->is_link && method_exists($fileInfo, 'getLinktarget')) { $this->linktarget = $fileInfo->getLinktarget(); } $flags = array($this->typeflag); // User $flags[] = $this->perms & 0x100 ? 'r' : '-'; $flags[] = $this->perms & 0x80 ? 'w' : '-'; $flags[] = $this->perms & 0x40 ? $this->perms & 0x800 ? 's' : 'x' : ($this->perms & 0x800 ? 'S' : '-'); // Group $flags[] = $this->perms & 0x20 ? 'r' : '-'; $flags[] = $this->perms & 0x10 ? 'w' : '-'; $flags[] = $this->perms & 0x8 ? $this->perms & 0x400 ? 's' : 'x' : ($this->perms & 0x400 ? 'S' : '-'); // Other $flags[] = $this->perms & 0x4 ? 'r' : '-'; $flags[] = $this->perms & 0x2 ? 'w' : '-'; $flags[] = $this->perms & 0x1 ? $this->perms & 0x200 ? 't' : 'x' : ($this->perms & 0x200 ? 'T' : '-'); $this->contents = implode($flags); }
public static function factory($path) { $fileInfo = new SplFileInfo($path); if ($fileInfo->isDir()) { $element = new Stagehand_DirectorySnap_Element_Directory($path); } elseif ($fileInfo->isLink()) { $element = new Stagehand_DirectorySnap_Element_Link($path); } elseif ($fileInfo->isFile()) { $element = new Stagehand_DirectorySnap_Element_File($path); } return $element; }
private function cleanFile(\SplFileInfo $fileInfo) { if ($fileInfo->isLink()) { $result = @unlink($fileInfo->getPathname()); if (!$result) { $this->error("Cannot delete symlink '{$fileInfo->getPathname()}'."); } } elseif ($fileInfo->isDir()) { $result = @rmdir($fileInfo->getPathname()); if (!$result) { $this->error("Cannot delete file '{$fileInfo->getPathname()}'."); } } elseif ($fileInfo->isFile()) { $result = @unlink($fileInfo->getPathname()); if (!$result) { $this->error("Cannot delete file '{$fileInfo->getPathname()}'."); } } }
/** * testSymlinkingSpecifiedPlugin * * @return void */ public function testSymlinkingSpecifiedPlugin() { Plugin::load('TestPlugin'); Plugin::load('Company/TestPluginThree'); $this->Task->symlink('TestPlugin'); $path = WWW_ROOT . 'test_plugin'; $link = new \SplFileInfo($path); $this->assertTrue(file_exists($path . DS . 'root.js')); unlink($path); $path = WWW_ROOT . 'company' . DS . 'test_plugin_three'; $link = new \SplFileInfo($path); $this->assertFalse($link->isDir()); $this->assertFalse($link->isLink()); }
<?php $pd = new PharData(__DIR__ . '/tgz/with_symlink.tar.gz'); $tempdir = tempnam(sys_get_temp_dir(), ''); unlink($tempdir); mkdir($tempdir); $pd->extractTo($tempdir); $fi = new SplFileInfo($tempdir . '/foo/herp'); var_dump($fi->isLink()); var_dump($fi->getLinkTarget()); $rdi = new RecursiveDirectoryIterator($tempdir, FileSystemIterator::SKIP_DOTS); $rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST); foreach ($rii as $path => $info) { if ($info->isDir()) { rmdir($path); } else { unlink($path); } } rmdir($tempdir);
<?php include __DIR__ . '/../../../test/sample_dir/fix_mtimes.inc'; $info = new SplFileInfo(__DIR__ . '/../../sample_dir'); if (!$info->isFile()) { echo $info->getRealPath(); } $info = new SplFileInfo(__DIR__ . '/../../sample_dir/file'); var_dump($info->getbaseName()); var_dump($info->getbaseName('.cpp')); $info->getCTime(); $info->getGroup(); $info->getInode(); $info->getMTime(); $info->getOwner(); $info->getPerms(); $info->getSize(); $info->getType(); $info->isDir(); $info->isFile(); $info->isLink(); $info->isReadable(); $info->isWritable();
/** * @param SplFileInfo $file * * @return array */ private function createFileResult(SplFileInfo $file) { $fileResult = array('path' => $this->replaceWindowsPath($this->getRelativePath($file->getRealPath(), ABSPATH)), 'pathEncoded' => false, 'isLink' => false, 'exists' => false, 'isDirectory' => false, 'owner' => 0, 'group' => 0, 'permissions' => 0); if (!seems_utf8($fileResult['path'])) { $fileResult['path'] = $this->pathEncode($fileResult['path']); $fileResult['pathEncoded'] = true; } try { $fileResult['link'] = $file->isLink(); // need to be first $fileResult['size'] = $file->getSize(); $fileResult['isDirectory'] = $file->isDir(); $fileResult['owner'] = $file->getOwner(); $fileResult['group'] = $file->getGroup(); $fileResult['permissions'] = $file->getPerms(); $fileResult['exists'] = true; if ($file->isLink()) { $fileResult['linkTarget'] = $file->getLinkTarget(); } } catch (RuntimeException $e) { } return $fileResult; }
<?php $info = new SplFileInfo('SplFileInfo.php'); printf("Extension: %s\n", $info->getExtension()); printf("Filename: %s\n", $info->getFilename()); printf("Path: %s\n", $info->getPathname()); printf("RealPath: %s\n", $info->getRealPath()); printf("Type: %s\n", $info->getType()); printf("Size: %s\n", $info->getSize()); printf("Dir: %b, Exec: %b, File: %b, Link: %b, R: %b, W: %b\n", $info->isDir(), $info->isExecutable(), $info->isFile(), $info->isLink(), $info->isReadable(), $info->isWritable()); $file = $info->openFile('r'); foreach ($file as $line) { printf("%s", $line); }
<?php $link = __DIR__ . '/test_link'; symlink(__FILE__, $link); $fileInfo = new SplFileInfo($link); if ($fileInfo->isLink()) { echo $fileInfo->getLinkTarget() == __FILE__ ? 'same' : 'different', PHP_EOL; } var_dump(unlink($link));
/** * @depends testExist * @return void */ public function testIsLink() { $path = __DIR__; $filePath = $path . '/example.txt'; $fileObject = new File($filePath); $this->assertFalse($fileObject->isLink()); $fileObject = new File($path); $this->assertFalse($fileObject->isLink()); $fileObject = new \SplFileInfo($filePath); $this->assertFalse($fileObject->isLink()); $fileObject = new \SplFileInfo($path); $this->assertFalse($fileObject->isLink()); }
protected function getFileDetailsRawStatistic(\SplFileInfo $info, $fileGiven) { return ['File is Dir' => $info->isDir(), 'File is Executable' => $info->isExecutable(), 'File is File' => $info->isFile(), 'File is Link' => $info->isLink(), 'File is Readable' => $info->isReadable(), 'File is Writable' => $info->isWritable(), 'File Permissions' => $this->explainPerms($info->getPerms()), 'Size' => $info->getSize(), 'Sha1' => sha1_file($fileGiven), 'Timestamp Accessed' => $this->getFileTimes($info->getATime()), 'Timestamp Changed' => $this->getFileTimes($info->getCTime()), 'Timestamp Modified' => $this->getFileTimes($info->getMTime())]; }
/** * Tell whether to ignore the element. * * @param SplFileInfo $element The element to check. * * @return bool True if the element should be ignored, false otherwise. */ public function isIgnored(SplFileInfo $element) { return $element->isLink(); }
public function getLogMessageFileName($session = true) { if ($session && !$this->_logMessageFileName) { try { $this->_logMessageFileName = $this->getSession()->getLogMessageFileName(); } catch (Exception $e) { Mage::logException($e); $this->_logMessageFileName = null; } } if (!$this->_logMessageFileName) { $this->initBackupData(); if ($this->_profile && $this->_profileId) { $logDir = $this->_profile->getData('profile_log_path'); $this->_logMessageFileName = $logDir . DS . $this->getBackupLogFile(); try { $splFileInfo = new SplFileInfo($this->_logMessageFileName); if (!$splFileInfo->isFile() && !$splFileInfo->isLink()) { $splFileInfo->openFile('a'); if (!$splFileInfo->isReadable()) { Mage::logException(new Exception('Log file is not readable')); } if (!$splFileInfo->isWritable()) { Mage::logException(new Exception('Log file is not writable')); } } } catch (Exception $e) { Mage::logException($e); } $this->getSession()->setLogMessageFileName($this->_logMessageFileName); } } return $this->_logMessageFileName; }
public function testDecoratedMethods() { $decorated = $this->getMockBuilder('hanneskod\\classtools\\Tests\\MockSplFileInfo')->setConstructorArgs([''])->getMock(); $decorated->expects($this->once())->method('getRelativePath'); $decorated->expects($this->once())->method('getRelativePathname'); $decorated->expects($this->once())->method('getContents'); $decorated->expects($this->once())->method('getATime'); $decorated->expects($this->once())->method('getBasename'); $decorated->expects($this->once())->method('getCTime'); $decorated->expects($this->once())->method('getExtension'); $decorated->expects($this->once())->method('getFileInfo'); $decorated->expects($this->once())->method('getFilename'); $decorated->expects($this->once())->method('getGroup'); $decorated->expects($this->once())->method('getInode'); $decorated->expects($this->once())->method('getLinkTarget'); $decorated->expects($this->once())->method('getMTime'); $decorated->expects($this->once())->method('getOwner'); $decorated->expects($this->once())->method('getPath'); $decorated->expects($this->once())->method('getPathInfo'); $decorated->expects($this->once())->method('getPathname'); $decorated->expects($this->once())->method('getPerms'); $decorated->expects($this->once())->method('getRealPath'); $decorated->expects($this->once())->method('getSize'); $decorated->expects($this->once())->method('getType'); $decorated->expects($this->once())->method('isDir'); $decorated->expects($this->once())->method('isExecutable'); $decorated->expects($this->once())->method('isFile'); $decorated->expects($this->once())->method('isLink'); $decorated->expects($this->once())->method('isReadable'); $decorated->expects($this->once())->method('isWritable'); $decorated->expects($this->once())->method('openFile'); $decorated->expects($this->once())->method('setFileClass'); $decorated->expects($this->once())->method('setInfoClass'); $decorated->expects($this->once())->method('__toString')->will($this->returnValue('')); $fileInfo = new SplFileInfo($decorated); $fileInfo->getRelativePath(); $fileInfo->getRelativePathname(); $fileInfo->getContents(); $fileInfo->getATime(); $fileInfo->getBasename(); $fileInfo->getCTime(); $fileInfo->getExtension(); $fileInfo->getFileInfo(); $fileInfo->getFilename(); $fileInfo->getGroup(); $fileInfo->getInode(); $fileInfo->getLinkTarget(); $fileInfo->getMTime(); $fileInfo->getOwner(); $fileInfo->getPath(); $fileInfo->getPathInfo(); $fileInfo->getPathname(); $fileInfo->getPerms(); $fileInfo->getRealPath(); $fileInfo->getSize(); $fileInfo->getType(); $fileInfo->isDir(); $fileInfo->isExecutable(); $fileInfo->isFile(); $fileInfo->isLink(); $fileInfo->isReadable(); $fileInfo->isWritable(); $fileInfo->openFile(); $fileInfo->setFileClass(); $fileInfo->setInfoClass(); (string) $fileInfo; }
/** */ public function isLink() { if (null === $this->l) { $this->l = parent::isLink(); } return $this->l; }
<?php /** * Read file info * @author Bramus Van Damme <*****@*****.**> */ $filename = __DIR__ . '/testfile.txt'; $fi = new SplFileInfo($filename); echo '<p>The file ' . $fi->getFileName() . ' was last modified on ' . date('Y-m-d H:i:s', $fi->getMTime()) . '</p>' . PHP_EOL; echo '<p>The file ' . $fi->getFileName() . ' is ' . ($fi->isDir() ? '' : 'not') . ' a directory</p>' . PHP_EOL; echo '<p>The file ' . $fi->getFileName() . ' is ' . ($fi->isFile() ? '' : 'not') . ' a file</p>' . PHP_EOL; echo '<p>The file ' . $fi->getFileName() . ' is ' . ($fi->isLink() ? '' : 'not') . ' a shortcut</p>' . PHP_EOL; // EOF