Checks if the target file should be added based on the given mode.

@param string $target The target path.

@param int    $mode   The overwrite mode.

@return bool

@throws \RuntimeException If target exists and can not or must not be removed.
198920
private function canAddTarget($target, $mode)
    {
        // Mode is set to ignore existing targets
        if ($mode === self::DUPLICATE_IGNORE) {
            return false;
        }

        // Error if we're not allowed to overwrite or can't remove the existing target
        if ($mode !== self::DUPLICATE_OVERWRITE || !$this->filesystem->remove($target)) {
            throw new \RuntimeException(sprintf('Installation target "%s" already exists', $target));
        }

        return true;
    }
1096538
public function setMode(string $mode): WebAclRule {
        self::checkMode($mode);
        $this->mode = (int) $mode;
        return $this;
    }
744449
public function createDir($mode = 0777)
    {
        return is_dir($this->absoluteFileName) || mkdir($this->absoluteFileName, $mode, true);
    }
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;
    }
1177484
public static function createFile($file, $mode = 0775)
    {
        if (empty($file)) return false;
        if (is_file($file)) {
            return true;
        }
        $dir = dirname($file);
        self::createDir($dir);
        $fh = @fopen($file, 'a');
        if ($fh) {
            fclose($fh);
            return true;
        }
        return false;
    }
601609
public static function open($uri, $mode)
    {
        $fp = fopen($uri, $mode);
        
        if (!$fp) {
            throw new \RuntimeException("Failed to open the '$uri' stream in '$mode' mode");
        }
        
        return new static($fp);
    }
302134
public function watch($resource)
    {
        if (!$this->files->exists($resource)) {
            throw new RuntimeException('Resource must exist before you can watch it.');
        } elseif ($this->files->isDirectory($resource)) {
            $resource = new DirectoryResource(new SplFileInfo($resource), $this->files);

            $resource->setupDirectory();
        } else {
            $resource = new FileResource(new SplFileInfo($resource), $this->files);
        }

        // The listener gives users the ability to bind listeners on the events
        // created when watching a file or directory. We'll give the listener
        // to the tracker so the tracker can fire any bound listeners.
        $listener = new Listener();

        $this->tracker->register($resource, $listener);

        return $listener;
    }
443560
public function isFresh(AssetTarget $target)
    {
        $buildName = $this->buildFileName($target);
        $buildFile = $this->outputDir($target) . DIRECTORY_SEPARATOR . $buildName;

        if (!file_exists($buildFile)) {
            return false;
        }
        $buildTime = filemtime($buildFile);

        if ($this->configTime && $this->configTime >= $buildTime) {
            return false;
        }

        foreach ($target->files() as $file) {
            $time = $file->modifiedTime();
            if ($time === false || $time >= $buildTime) {
                return false;
            }
        }

        $filters = $this->filterRegistry->collection($target);
        foreach ($filters->filters() as $filter) {
            foreach ($filter->getDependencies($target) as $child) {
                $time = $child->modifiedTime();
                if ($time >= $buildTime) {
                    return false;
                }
            }
        }
        return true;
    }
454609
protected static function recursiveMakeDirectory(string $dir, int $mode = 0777): bool {
		return (is_dir(dirname($dir)) || static::recursiveMakeDirectory(dirname($dir))) ? mkdir($dir, $mode) : true;
	}
464239
protected function createLink($source, $target)
    {
        $success = $this->filesystem->relativeSymlink($source, $target);
        if (!$success) {
            throw new RuntimeException("Could not create symlink at $target");
        }
    }
1078085
public function hasTarget(Target $target): bool
    {
        foreach ($this->_allOfType($target->type()) as $t) {
            if ($target->equals($t)) {
                return true;
            }
        }
        return false;
    }
424292
private function mkdir($target)
    {
        if (\is_dir($target)) {
            return;
        }

        if (! $this->file->mkdir($target)) {
            $this->output->writeln("<error>[$target] could not be created. Please check directory permissions.</error>");
        }
    }
902917
public function isEditing($mode = null)
    {
        $page_mode = $this->virtual_page->isActive();

        // Requested edit, but not allowed
        if ($page_mode == 'edit' && !$this->allow_edit) {
            throw $this->exception('Editing is not allowed');
        }

        // Requested add but not allowed
        if ($page_mode == 'add' && !$this->allow_add) {
            throw $this->exception('Adding is not allowed');
        }

        // Request matched argument exactly
        if (!is_null($mode)) {
            return $mode === $page_mode;
        }

        // Argument was blank, then edit/add is OK
        return (boolean) $page_mode;
    }
780642
protected function canICopyTo($target)
    {
        $fileExists = file_exists($target);
        $emptyFile  = $fileExists ? filesize($target) === 0 : false;

        return (!$fileExists || $emptyFile);
    }
205352
public function ircMode($target, $mode = null, $param = null)
    {
        $this->queueIrcRequest('MODE', [ $target, $mode, $param ]);
    }
402631
private function downloadTemporaryFile($url, $target)
    {
        try {
            if (($fh = @fopen($target, 'w')) === false) {
                throw new RuntimeException(
                    sprintf(
                        'Could not create file "%s": %s',
                        $target,
                        @error_get_last()['message']
                    )
                );
            }

            if ((@fwrite($fh, $this->httpClient->get($url, 'application/octet-stream'))) === false) {
                throw new RuntimeException(
                    sprintf(
                        'The "%s" file could not be downloaded: %s',
                        $url,
                        @error_get_last()['message']
                    )
                );
            }
        } finally {
            if (is_resource($fh)) {
                fclose($fh);
            }
        }
    }
763611
public function symlink($target, $link)
    {
        if (!file_exists($target)) {
            throw new \InvalidArgumentException('Target not found: ' . $target);
        }
        if ($this->relative) {
            $target = $this->makePathRelative($target, dirname($link));
        }
        $this->fs->symlink($target, $link, $this->copyOnWindows);
    }