Returns a writable stream for a given path and mode.

@param string $path The path to open.

@return resource|bool The file handle, or false.
552488
protected function getWritableStream($path)
    {
        try {
            $handle = $this->getFilesystem()->readStream($path);
            $this->needsCowCheck = true;
        } catch (FileNotFoundException $e) {
            $handle = fopen('php://temp', 'w+b');
            $this->needsFlush = true;
        }

        return $handle;
    }
200025
public function createFile(string $path): File {
    $localFile = $this->instantiateFile($path);
    $localFile->create();
    return $localFile;
  }
799913
function _stream_open($path, $mode, $options, &$opened_path)
    {
        $path = $this->_parse_path($path);

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

        $this->size = $this->sftp->size($path);
        $this->mode = preg_replace('#[bt]$#', '', $mode);
        $this->eof = false;

        if ($this->size === false) {
            if ($this->mode[0] == 'r') {
                return false;
            } else {
                $this->sftp->touch($path);
                $this->size = 0;
            }
        } else {
            switch ($this->mode[0]) {
                case 'x':
                    return false;
                case 'w':
                    $this->sftp->truncate($path, 0);
                    $this->size = 0;
            }
        }

        $this->pos = $this->mode[0] != 'a' ? 0 : $this->size;

        return true;
    }
508437
public function readStream(string $path)
    {
        $path = $this->normalizePath($path);
        $this->assertPresent($path);

        return $this->getAdapter()->readStream($path);
    }
864628
private static function detectStream($path)
    {
        $stream = 'file';

        if (false !== strpos($path, '://')) {
            $parts  = explode('://', $path);
            $stream = $parts[0];
        }

        return $stream;
    }
598257
public static function isOwner($path)
	{
		$tmp = md5(mt_rand());
		$ssp = ini_get('session.save_path');
		$jtp = JPATH_ROOT . '/tmp';

		// Try to find a writable directory
		$dir = is_writable('/tmp') ? '/tmp' : false;
		$dir = (!$dir && is_writable($ssp)) ? $ssp : false;
		$dir = (!$dir && is_writable($jtp)) ? $jtp : false;

		if ($dir)
		{
			$test = $dir . '/' . $tmp;

			// Create the test file
			$blank = '';
			File::write($test, $blank, false);

			// Test ownership
			$return = (fileowner($test) == fileowner($path));

			// Delete the test file
			File::delete($test);

			return $return;
		}

		return false;
	}
593110
public static function isWritable($path = false) {

        if($path && self::isFile($path) && is_writable($path)) {

            return true;
        }

        return false;
    }
1237465
protected function getContent(string $path): ?vfsStreamContent
    {
        if (null === self::$root) {
            return null;
        }

        if (self::$root->getName() === $path) {
            return self::$root;
        }

        if ($this->isInRoot($path) && self::$root->hasChild($path) === true) {
            return self::$root->getChild($path);
        }

        return null;
    }
22415
public function getUrl($path)
    {
        if ($this->publish($path)) {
            $obj = $this->getFileObject($path);
            if ($url = $obj->getWebContentLink()) {
                return str_replace('export=download', 'export=media', $url);
            }
            if ($url = $obj->getWebViewLink()) {
                return $url;
            }
        }
        return false;
    }
649667
public function getResourceByPath($path)
    {
        // get rid of everything after the ?
        $path = \strtok($path, '?');

        $resources = $this->getResourcesAsArray($this->resources);
        foreach ($resources as $resource) {
            if ($path === $resource->getUri()) {
                return $resource;
            }
        }
        // we never returned so throw exception
        throw new ResourceNotFoundException($path);
    }
265880
public function getFile(string $path): File {
    if($this->isDir($path)) {
      throw new NotAFileException($path);
    }

    return $this->instantiateFile($path);
  }
402245
public function readStream($path)
    {
        if ( ! $data = $this->read($path)) {
            return false;
        }

        $stream = \fopen('php://temp', 'w+b');
        \fwrite($stream, $data['contents']);
        \rewind($stream);
        $data['stream'] = $stream;
        unset($data['contents']);

        return $data;
    }
811625
public static function makeWritable(string $path)
    {
        // If it is already writable, there's nothing to do
        if (is_writable($path))
            return;

        $perms = self::getPermissions($path);

        $current_user = posix_getpwuid(posix_geteuid());
        $owner = posix_getpwuid(fileowner($path));
        $group = posix_getgrgid(filegroup($path));

        $is_owner = $current_user['uid'] === $owner['uid'];

        if (!$is_owner)
        {
            // @codeCoverageIgnoreStart
            // The file is really unwritable, and we cannot change the permissions. This
            // is not reliably testable without root permissions.
            throw new PermissionError($path, "Cannot change permissions - not the owner");
            // @codeCoverageIgnoreEnd
        }

        // We own the file, so we should be able to fix it
        $set_gid = false;
        if (self::$file_group !== null)
        {
            if (self::$file_group !== $group['name'] && !chgrp($path, self::$file_group))
            {
                // @codeCoverageIgnoreStart
                // This is not testable - a read only file system would be needed
                throw new PermissionError($path, "Cannot change group");
                // @codeCoverageIgnoreEnd
            }
            $set_gid = true;
        }

        // Owner and group are all right now, we should be able to modify the permissions
        $new_mode = $perms['mode'] | self::OWNER_WRITE | ($set_gid ? self::GROUP_WRITE : 0);

        if (is_dir($path))
            $new_mode |= self::OWNER_EXECUTE | ($set_gid ? self::GROUP_EXECUTE : 0);

        $what = is_dir($path) ? "directory" : "file";
        self::getLogger()->notice(
            "Changing permissions of {0} {1} to {2} (was: {3})", 
            [$what, $path, $new_mode, $perms['mode']]
        );

        try
        {
            @chmod($path, $new_mode);
        }
        // @codeCoverageIgnoreStart
        // Basically untestable - we need a read-only file system for this
        catch (Throwable $e)
        {
            throw new PermissionError($path, "Could not set permissions");
        }
        // @codeCoverageIgnoreEnd
    }
1369037
public function reads( $path )
	{
		try {
			$content = $this->fs->get( $path );
		} catch( \Exception $e ) {
			throw new Exception( $e->getMessage(), 0, $e );
		}

		if( ( $stream = tmpfile() ) === false ) {
			throw new Exception( 'Couldn\'t create temporary file' );
		}

		if( fwrite( $stream, $content ) === false ) {
			throw new Exception( 'Couldn\'t write to temporary file' );
		}

		if( rewind( $stream ) === false ) {
			throw new Exception( 'Couldn\'t rewind temporary file' );
		}

		return $stream;
	}
700068
public static function read($path)
    {
        $path = Normalize::diskFullPath($path);

        if (!self::exist($path)) {
            return false;
        }

        return @file_get_contents($path);
    }
146691
protected function writeFile($path)
    {
        $handle = fopen($path, Stream::MODE_READ_WRITE_RESET);

        if ($handle === false) {
            throw new \RuntimeException('Unable to write to path: ' . $path);
        }

        $this->stream->rewind();

        while (!$this->stream->eof()) {
            fwrite($handle, $this->stream->read(4096));
        }

        fclose($handle);
    }
792463
public static function from($path)
    {
        switch( self::getFileExtension($path) )
        {
            case 'png' : return imagecreatefrompng($path);
            case 'gif' : return imagecreatefromgif($path);
            case 'jpg' :
            case 'jpeg':
            default    : return imagecreatefromjpeg($path);
        }
    }