/** * Change the owner of an array of files or directories. * * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner * @param string $user The new owner user name * @param bool $recursive Whether change the owner recursively or not * * @throws IOException When the change fail */ public function chown($files, $user, $recursive = false) { foreach ($this->toIterator($files) as $file) { if ($recursive && is_dir($file) && !is_link($file)) { $this->chown(new \FilesystemIterator($file), $user, true); } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } else { if (true !== @chown($file, $user)) { throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } } }
/** * {@inheritdoc} */ public function chown($file, $user) { $file = $this->normalizePath($file); return is_link($file) && function_exists('lchown') ? lchown($file, $user) : chown($file, $user); }
/** * Change file owner. * * @access public * @param mixed $user User. * @return bool */ public function changeOwner($user) { return lchown($this->getStreamName(), $user); }
/** * Sets the owner for the fs resource * * @param mixed $owner * @return boolean True if successful */ public function chown($owner) { if ($this->isLink() && function_exists('lchown')) { return @lchown($this->path, $owner); } else { return @chown($this->path, $owner); } }
/** * Change file owner * * Change a file owner to filepath * * @param string $path Path to the file * @param string $user A file owner name or number UID * @param bool $recursive Recursive set a group * @return bool State of changes * @throws \RuntimeException */ public function setChown($path, $user, $recursive = false) { $state = false; if ($recursive) { $iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD); foreach ($iter as $val) { if (is_link($val->getPathName())) { if (!@lchown($path, $user)) { throw new \Exception(sprintf("Can not change file owner for link %s with user %s", $path, $user)); } $state = true; } else { if ($val->isFile()) { if (!@chown($path, $user)) { throw new \Exception(sprintf("Can not change file owner for file %s with user %s", $path, $user)); } $state = true; } } } } else { if (is_link($path)) { if (!lchown($path, $user)) { throw new Exception(sprintf("Can not change file owner for link %s with user %s", $path, $user)); } $state = true; } else { if (is_file($path)) { if (!chown($path, $user)) { throw new Exception(sprintf("Can not change file owner for file %s with user %s", $path, $user)); } $state = true; } } } return $state; }
var_dump(is_writable($path1)); var_dump(is_writeable($path1)); var_dump(is_readable($path1)); var_dump(is_executable($path1)); var_dump(is_file($path1)); var_dump(is_dir($path1)); var_dump(is_link($path1)); var_dump(file_exists($path1)); var_dump(stat($path1)); var_dump(lstat($path1)); var_dump(realpath($path1)); var_dump(disk_free_space($path1)); var_dump(diskfreespace($path1)); var_dump(disk_total_space($path1)); var_dump(chmod($path1, '644')); var_dump(chown($path1, 'nobody')); var_dump(lchown($path1, 'nobody')); var_dump(chgrp($path1, 'nogrp')); var_dump(lchgrp($path1, 'nogrp')); var_dump(touch($path1)); var_dump(copy($path1, $path2)); var_dump(rename($path1, $path2)); var_dump(unlink($path1, $path2)); var_dump(link($path1, $path2)); var_dump(symlink($path1, $path2)); var_dump(fnmatch($path1, $path2)); var_dump(tempnam($path1, 'tmp')); var_dump(mkdir($path1)); var_dump(chdir($path1)); var_dump(chroot($path1)); var_dump(scandir($path1));
/** * Extract the current entry to which the iterator points. * * Extract the current entry to which the iterator points, and return true if the current entry is extracted. * If the iterator doesn't point to a valid entry, this method returns false. * * True if the file is extracted correctly, otherwise false. * * @param string $target * The full path to which the target should be extracted. * @param bool $keepExisting * True if the file shouldn't be overwritten if they already exist. * For the opposite behaviour, false should be given. * * @throws ezcArchiveValueException if the archive contains invalid values. * @throws ezcBaseFileNotFoundException if the link cannot be found. * * @return bool */ public function extractCurrent($target, $keepExisting = false) { if ($this->file === null) { throw new ezcArchiveException("The archive is closed"); } if (!$this->valid()) { return false; } $isWindows = substr(php_uname('s'), 0, 7) == 'Windows' ? true : false; $entry = $this->current(); $type = $entry->getType(); $fileName = $target . DIRECTORY_SEPARATOR . $entry->getPath(); if ($type == ezcArchiveEntry::IS_LINK) { $linkName = $target . DIRECTORY_SEPARATOR . $entry->getLink(); if (!file_exists($linkName)) { throw new ezcBaseFileNotFoundException($linkName, "link", "Hard link could not be created."); } } $this->createDefaultDirectory($fileName); if (!$keepExisting || !is_link($fileName) && !file_exists($fileName)) { if ((file_exists($fileName) || is_link($fileName)) && !is_dir($fileName)) { unlink($fileName); } if (!file_exists($fileName)) { switch ($type) { case ezcArchiveEntry::IS_CHARACTER_DEVICE: if (ezcBaseFeatures::hasFunction('posix_mknod')) { posix_mknod($fileName, POSIX_S_IFCHR, $entry->getMajor(), $entry->getMinor()); } else { throw new ezcArchiveValueException($type); } break; case ezcArchiveEntry::IS_BLOCK_DEVICE: if (ezcBaseFeatures::hasFunction('posix_mknod')) { posix_mknod($fileName, POSIX_S_IFBLK, $entry->getMajor(), $entry->getMinor()); } else { throw new ezcArchiveValueException($type); } break; case ezcArchiveEntry::IS_FIFO: if (ezcBaseFeatures::hasFunction('posix_mknod')) { posix_mknod($fileName, POSIX_S_IFIFO); } else { throw new ezcArchiveValueException($type); } break; case ezcArchiveEntry::IS_SYMBOLIC_LINK: if ($isWindows) { // FIXME.. need to be sure that target file // already extracted before copying it to link destination. $sourcePath = dirname($fileName) . '/' . $entry->getLink(); $fileName = str_replace('/', '\\', $fileName); copy($sourcePath, $fileName); } else { symlink($entry->getLink(), $fileName); } break; case ezcArchiveEntry::IS_LINK: if ($isWindows) { copy($target . DIRECTORY_SEPARATOR . $entry->getLink(), $fileName); } else { link($target . DIRECTORY_SEPARATOR . $entry->getLink(), $fileName); } break; case ezcArchiveEntry::IS_DIRECTORY: $permissions = $entry->getPermissions(); if ($permissions === null || $permissions === false) { $permissions = '0777'; } mkdir($fileName, octdec($permissions), true); break; case ezcArchiveEntry::IS_FILE: $this->writeCurrentDataToFile($fileName); break; default: throw new ezcArchiveValueException($type); } if ($type == ezcArchiveEntry::IS_SYMBOLIC_LINK && ezcBaseFeatures::hasFunction('posix_geteuid') && posix_geteuid() == 0) { $user = $entry->getUserId(); $group = $entry->getGroupId(); @lchown($fileName, $user); @lchgrp($fileName, $group); } // Change the username and group if the filename exists and if // the intention is to keep it as a file. A zip archive // stores the symlinks in a file; thus don't change these. if (file_exists($fileName) && ($type == ezcArchiveEntry::IS_FILE || $type == ezcArchiveEntry::IS_DIRECTORY)) { $group = $entry->getGroupId(); $user = $entry->getUserId(); $time = $entry->getModificationTime(); $perms = octdec($entry->getPermissions()); if ($this->options && $this->options->extractCallback) { $this->options->extractCallback->{$type == ezcArchiveEntry::IS_DIRECTORY ? 'createDirectoryCallback' : 'createFileCallback'}($fileName, $perms, $user, $group); } if (ezcBaseFeatures::hasFunction('posix_geteuid') && posix_geteuid() === 0) { @chgrp($fileName, $group); @chown($fileName, $user); } if ($perms != false) { chmod($fileName, $perms); } touch($fileName, $time); } } return true; } return false; }
<?php /* Prototype : bool lchown (string filename, mixed user) * Description: Change file owner of a symlink * Source code: ext/standard/filestat.c * Alias to functions: */ echo "*** Testing lchown() : basic functionality ***\n"; $filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lchown_basic.txt'; $symlink = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lchown_basic_symlink.txt'; $uid = posix_getuid(); var_dump(touch($filename)); var_dump(symlink($filename, $symlink)); var_dump(lchown($filename, $uid)); var_dump(fileowner($symlink) === $uid); ?> ===DONE=== <?php $filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lchown_basic.txt'; $symlink = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lchown_basic_symlink.txt'; unlink($filename); unlink($symlink);
/** * Change the owner of an array of files or directories. * * @internal services: * (+) FilesystemInterface chown($files, string $user, bool $recursive = false); * * @param string $file The filename to change owner * @param string $user The new owner user name * * @throws IOException When the change fails * * @return FilesystemInterface The current interface */ protected function chownSingleFile($file, string $user) : FilesystemInterface { if (is_link($file)) { if (true !== @lchown($file, $user)) { throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } else { if (true !== @chown($file, $user)) { throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } return $this; }
/** * Changes user ownership of symlink * * @param string $filename Path to the file. * @param mixed $user User name or number. * * @return bool */ public function lchown(string $filename, $user) : bool { return lchown($filename, $user); }
/** * Change the owner of an array of files or directories. * * @param string|array|Traversable $files A filename, an array of files, or a Traversable instance to change owner * @param string $user The new owner user name * @param bool $recursive Whether change the owner recursively or not * * @throws ehough_filesystem_exception_IOException When the change fail */ public function chown($files, $user, $recursive = false) { foreach ($this->toIterator($files) as $file) { if ($recursive && is_dir($file) && !is_link($file)) { if (version_compare(PHP_VERSION, '5.3.0') < 0) { $this->chown(new ehough_filesystem_iterator_SkipDotsRecursiveDirectoryIterator($file), $user, true); } else { $this->chown(new FilesystemIterator($file), $user, true); } } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { throw new ehough_filesystem_exception_IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } else { if (true !== @chown($file, $user)) { throw new ehough_filesystem_exception_IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } } }
<?php /* Prototype : bool lchown (string filename, mixed user) * Description: Change file owner of a symlink * Source code: ext/standard/filestat.c * Alias to functions: */ echo "*** Testing lchown() : error functionality ***\n"; // Set up $filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lchown.txt'; touch($filename); $uid = posix_getuid(); // Less than expected arguments var_dump(lchown($filename)); // More than expected arguments var_dump(lchown($filename, $uid, 'foobar')); // Non-existent filename var_dump(lchown('foobar_lchown.txt', $uid)); // Wrong argument types var_dump(lchown(new StdClass(), $uid)); var_dump(lchown(array(), $uid)); // Bad user var_dump(lchown($filename, -5)); ?> ===DONE===
{ return true; } public function url_stat($path, $flags) { return true; } } stream_wrapper_register("abc", "A") or die("Failed to register protocol"); touch('abc://a'); touch('abc://b', 15); touch('abc://c', 15, 25); chmod('abc://d', 0123); chmod('abc://e', 0345); chown('abc://f', 0); chown('abc://g', 10); chown('abc://h', 'root'); lchown('abc://i', 0); lchown('abc://j', 10); lchown('abc://k', 'root'); chgrp('abc://l', 0); chgrp('abc://m', 10); chgrp('abc://n', 'root'); lchgrp('abc://o', 0); lchgrp('abc://p', 10); lchgrp('abc://q', 'root'); // explicit errors chown('abc://r', []); lchown('abc://s', []); chgrp('abc://t', []); lchgrp('abc://u', []);
<?php $file = tempnam('/tmp', 'lchown'); $link = tempnam('/tmp', 'lchown'); touch($file); @symlink($file, $link); var_dump(lchown($link, 'ihopenomachinehasthisuserthatwouldbebad')); var_dump(chown($file, 'ihopenomachinehasthisuserthatwouldbebad'));
function lchgrp($fn, $group) { return lchown($fn, ":{$group}"); }
/** * Create chown function to allow mocking in unit tests * Attempts to change the owner of the file filename to user user . * Only the superuser may change the owner of a file. * * @param string $path Path to the file. * @param mixed $uid A user name or number. * * @return boolean true on success or false on failure */ public function chown($path, $uid) { if (is_link($path)) { return lchown($path, $uid); } return chown($path, $uid); }
/** * Changes file owner * * Attempts to change the owner. Only the superuser may change the owner of a file. * * @param mixed $user A user name or number. * @return boolean Returns TRUE on success or FALSE on failure. */ public function setOwner($user) { if ($this->isLink()) { return lchown($this->pathname, $user); } else { return chown($this->pathname, $user); } }
/** * @param mixed $newOwner The user name or user number or an object representing the user. * @param bool $recursive * @return bool TRUE if the owner has been successfully modified, FALSE otherwise. * @throws EyeIOException */ public function chown($newOwner, $recursive = false) { if ($recursive) { //TODO recursive chown throw new EyeNotImplementedException(__METHOD__ . ': $recursive = true'); } $path = AdvancedPathLib::getPhpLocalHackPath($this->path); try { if ($this->isLink()) { return lchown($path, $newOwner); } else { return chown($path, $newOwner); } } catch (EyeErrorException $e) { throw new EyeIOException('Unable to change file\'s owner at ' . $this->path . '.', 0, $e); } }