Change permissions recursive

@param string $filename
@param int $mode - ex. 0644

@return bool
974626
protected function chmodReal($filename, $mode)
    {
        try {
            $result = @chmod($filename, $mode);
        } catch (\Exception $e) {
            $result = false;
        }

        if (!$result) {
            $this->chown($filename, $this->getDefaultOwner(true));
            $this->chgrp($filename, $this->getDefaultGroup(true));

            try {
                $result = @chmod($filename, $mode);
            } catch (\Exception $e) {
                throw new Error($e->getMessage());
            }
        }

        return $result;
    }
370869
public function lock($mode = LOCK_SH)
    {

        if (!is_resource($this->fp)) return false;

        if (flock($this->fp, $mode) === false) return false;

        if ($mode != LOCK_UN) $this->file_lock = true;
        else $this->file_lock = false;

        return true;
    }
378718
public function chmodRecursive($path, $mode){

        $items = $this->recursiveIterate($path);

        foreach($items as $item){
            chmod($item,$mode);
        }//foreach

    }
348344
public function chmod($mode, $path)
    {
        $writeBinn = new BinnList;

        $writeBinn->addUint8(self::FSERV_CHMOD);
        $writeBinn->addStr($path);
        $writeBinn->addUint16($mode);

        $read = $this->writeAndReadSocket($writeBinn->serialize());

        $readBinn = new BinnList;
        $readBinn->binnOpen($read);
        $results = $readBinn->unserialize();

        if ($results[0] != self::FSERV_STATUS_OK) {
            throw new RuntimeException('Couldn\'t chmod: ' . isset($results[1]) ? $results[1] : 'Unknown');
        }

        return true;
    }
1257906
protected function changePermission(string $path, $mode = 0755, bool $recursively = false): void
    {
        $filesystem = $this->getContainer()['files'];

        $filesystem->chmod($path, $mode);

        if ($recursively === false) {
            return;
        }

        $lists = $filesystem->allFiles($path);

        $ignoredPath = function ($dir) {
            return (\substr($dir, -3) === '/..' || \substr($dir, -2) === '/.');
        };

        // this is to check if return value is just a single file,
        // avoiding infinite loop when we reach a file.
        if ($lists !== [$path]) {
            foreach ($lists as $dir) {
                // Not a file or folder, ignore it.
                if (! $ignoredPath($dir)) {
                    $this->changePermission($dir, $mode, true);
                }
            }
        }
    }
221576
static function applyStorageFilePermissions( $filename )
    {
        $siteConfig = eZINI::instance( 'site.ini' );
        $filePermissions = $siteConfig->variable( 'FileSettings', 'StorageFilePermissions');
        chmod( $filename, octdec( $filePermissions ) );
    }
743162
function chmod($mode, $filename, $recursive = false)
    {
        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
            return false;
        }

        $filename = $this->_realpath($filename);
        if ($filename === false) {
            return false;
        }

        if ($recursive) {
            $i = 0;
            $result = $this->_chmod_recursive($mode, $filename, $i);
            $this->_read_put_responses($i);
            return $result;
        }

        // SFTPv4+ has an additional byte field - type - that would need to be sent, as well. setting it to
        // SSH_FILEXFER_TYPE_UNKNOWN might work. if not, we'd have to do an SSH_FXP_STAT before doing an SSH_FXP_SETSTAT.
        $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
        if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($filename), $filename, $attr))) {
            return false;
        }

        /*
         "Because some systems must use separate system calls to set various attributes, it is possible that a failure 
          response will be returned, but yet some of the attributes may be have been successfully modified.  If possible,
          servers SHOULD avoid this situation; however, clients MUST be aware that this is possible."

          -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.6
        */
        $response = $this->_get_sftp_packet();
        if ($this->packet_type != NET_SFTP_STATUS) {
            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
            return false;
        }

        extract(unpack('Nstatus', $this->_string_shift($response, 4)));
        if ($status != NET_SFTP_STATUS_OK) {
            $this->_logError($response, $status);
        }

        // rather than return what the permissions *should* be, we'll return what they actually are.  this will also
        // tell us if the file actually exists.
        // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
        $packet = pack('Na*', strlen($filename), $filename);
        if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {
            return false;
        }

        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_ATTRS:
                $attrs = $this->_parseAttributes($response);
                return $attrs['permissions'];
            case NET_SFTP_STATUS:
                $this->_logError($response);
                return false;
        }

        user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);
        return false;
    }
250244
function chmod($mode, $filename, $recursive = false)
    {
        if (is_string($mode) && is_int($filename)) {
            $temp = $mode;
            $mode = $filename;
            $filename = $temp;
        }

        $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
        if (!$this->_setstat($filename, $attr, $recursive)) {
            return false;
        }
        if ($recursive) {
            return true;
        }

        $filename = $this->_realPath($filename);
        // rather than return what the permissions *should* be, we'll return what they actually are.  this will also
        // tell us if the file actually exists.
        // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
        $packet = pack('Na*', strlen($filename), $filename);
        if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {
            return false;
        }

        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_ATTRS:
                $attrs = $this->_parseAttributes($response);
                return $attrs['permissions'];
            case NET_SFTP_STATUS:
                $this->_logError($response);
                return false;
        }

        user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
        return false;
    }
241569
public function setTransferMode( $mode )
	{
		if( $mode != FTP_BINARY && $mode != FTP_ASCII )
			return FALSE;
		$this->mode	= $mode;
		return TRUE;
	}
57115
def changePermissionsRecursively(path, uid, gid):
    
    os.chown(path, uid, gid)
    for item in os.listdir(path):
        itempath = os.path.join(path, item)
        if os.path.isfile(itempath):
            # Setting owner
            try:
                os.chown(itempath, uid, gid)
            except Exception as e:
                # If this crashes it may be because we are running the
                # application in Windows systems, where os.chown does NOT work.
                pass
            # Setting permissions
            os.chmod(itempath, 600)
        elif os.path.isdir(itempath):
            # Setting owner
            try:
                os.chown(itempath, uid, gid)
            except Exception as e:
                # If this crashes it may be because we are running the
                # application in Windows systems, where os.chown does NOT work.
                pass
            # Setting permissions
            os.chmod(itempath, 6600)
            # Recursive function to iterate the files
            changePermissionsRecursively(itempath, uid, gid)
924968
public function filePermission($path, $mode = self::CHMOD_775)
    {
        $results = array();
        $success = 0;
        if(file_exists($path)) {
            if(!is_writable($path))
                chmod($path, $mode);

            if(!is_readable($path))
                chmod($path, $mode);

            if(is_readable($path) && is_writable($path))
                $status = 1;
        }

        $results = array(
            'path' => $path,
            'mode' => $mode,
            'success' => $success
        );

        return Json::encode($results);
    }
911758
private function checkPermissionsOrCreate($folder, $mode = 0755){
		if(!file_exists($folder)){
			mkdir($folder, $mode, true);
			return chmod($folder, $mode);
		}

		if(is_dir($folder)){
			if(!is_writable($folder)){
				return chmod($folder, $mode);
			}

			return true;
		}

		throw new Exception("You have no permission to write in folder: {$folder}");
	}
553603
public function chmod($filename = '', $mode = 0)
	{
		if (!$filename)
		{
			if (!isset($this->filename) || !$this->filename)
			{
				throw new \RuntimeException('Filename not set');
			}

			$filename = $this->filename;
		}

		// If no mode is set use the default
		if (!$mode)
		{
			$mode = $this->filemode;
		}

		// Capture PHP errors
		$php_errormsg = '';
		$track_errors = ini_get('track_errors');
		ini_set('track_errors', true);
		$sch = parse_url($filename, PHP_URL_SCHEME);

		// Scheme specific options; ftp's chmod support is fun.
		switch ($sch)
		{
			case 'ftp':
			case 'ftps':
				$res = Helper::ftpChmod($filename, $mode);
				break;

			default:
				$res = chmod($filename, $mode);
				break;
		}

		// Seek, interestingly, returns 0 on success or -1 on failure
		if (!$res)
		{
			throw new \RuntimeException($php_errormsg);
		}

		// Restore error tracking to what it was before.
		ini_set('track_errors', $track_errors);

		// Return the result
		return true;
	}
1034283
public function setMode($mode)
    {
        $this->mode = (null === $mode ? null : (bool) $mode);
        return $this;
    }
1265308
static function perms($filename) {

		if (!file_exists($filename)) {
			throw new FileException("Missing: $filename");
		}
		if (!is_writeable($filename)) {
			throw new FileException("Not writable: $filename");
		}

		if (is_dir($filename)) {
			$ok=chmod($filename,0777);
		} else {
			$ok=chmod($filename,0666);
		}

		if (!$ok) {
			throw new FileAccessException("Could not chmod $filename");
		}

		return $ok;
	}
1290290
function(callback) {
            // Optional permission modification
            if (disablePerms) {
              callback();
              return;
            }
            
            grunt.verbose.writeln('changing file perms...');

            // 511 = 0777 octal
            ftpout.raw.site('chmod', (file.mode & 511).toString(8), file.dest, function(err) {
              // Windows and some hosts don't implement the CHMOD site extension
              if (err && err.code !== 502 && err.code !== 504) {
                done(err);
                return;
              }

              grunt.verbose.writeln('done changing perms');
              callback();
            });
          }
858154
public static function chmod($files, $mode, $umask = 0000, $recursive = false)
    {
        self::ensureFileSystem()->chmod($files, $mode, $umask, $recursive);
    }