<commit_before><?php

namespace League\Flysystem\Adapter;

use Finfo;
use SplFileInfo;
use FilesystemIterator;
use DirectoryIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use League\Flysystem\Util;
use League\Flysystem\AdapterInterface;

class Local extends AbstractAdapter
{
    protected static $permissions = array(
        'public' => 0644,
        'private' => 0000,
    );

    /**
     * Constructor
     *
     * @param  string  $root
     */
    public function __construct($root)
    {
        $root = $this->ensureRootDirectory($root);

        $this->root = Util::normalizePrefix($root, DIRECTORY_SEPARATOR);
    }

    /**
     * Ensure the root directory exists.
     *
     * @param   string  $root  root directory path
     * @return  string  real path to root
     */
    protected function ensureRootDirectory($root)
    {
        if ( ! is_dir($root)) {
            mkdir($root, 0777, true);
        }

        return realpath($root);
    }

    /**
     * Prefix a path with the root
     *
     * @param   string  $path
     * @return  string  prefixed path
     */
    protected function prefix($path)
    {
        if (empty($path)) {
            return $this->root;
        }

        $path = str_replace('/', DIRECTORY_SEPARATOR, $path);

        return $this->root.Util::normalizePath($path, DIRECTORY_SEPARATOR);
    }

    /**
     * Check whether a file is present
     *
     * @param   string   $path
     * @return  boolean
     */
    public function has($path)
    {
        return file_exists($this->prefix($path));
    }

    /**
     * Write a file
     *
     * @param $path
     * @param $contents
     * @param null $config
     * @return array|bool
     */
    public function write($path, $contents, $config = null)
    {
        $location = $this->prefix($path);
        $config = Util::ensureConfig($config);

        if ( ! is_dir($dirname = dirname($location))) {
            mkdir($dirname, 0777, true);
        }

        if (($size = file_put_contents($location, $contents, LOCK_EX)) === false) {
            return false;
        }

        $result = array(
            'contents' => $contents,
            'type' => 'file',
            'size' => $size,
            'path' => $path,
        );

        if ($visibility = $config->get('visibility')) {
            $result['visibility'] = $visibility;
            $this->setVisibility($path, $visibility);
        }

        return $result;
    }

    /**
     * Write using a stream
     *
     * @param $path
     * @param $resource
     * @param null $config
     * @return array|bool
     */
    public function writeStream($path, $resource, $config = null)
    {
        rewind($resource);
        $config = Util::ensureConfig($config);
        $location = $this->prefix($path);

        if ( ! is_dir($dirname = dirname($location))) {
            mkdir($dirname, 0777, true);
        }

        if ( ! $stream = fopen($location, 'w+')) {
            return false;
        }

        while ( ! feof($resource)) {
            fwrite($stream, fread($resource, 1024), 1024);
        }

        if ( ! fclose($stream)) {
            return false;
        }

        if ($visibility = $config->get('visibility')) {
            $this->setVisibility($path, $visibility);
        }

        return compact('path', 'visibility');
    }

    /**
     * Get a read-stream for a file
     *
     * @param $path
     * @return array|bool
     */
    public function readStream($path)
    {
        $stream = fopen($this->prefix($path), 'r+');

        return compact('stream', 'path');
    }

    /**
     * Update a file using a stream<commit_msg>Fixes #72: Use r instead of r+, read streams should only be readable in the Local adapter<commit_after><?php

namespace League\Flysystem\Adapter;

use Finfo;
use SplFileInfo;
use FilesystemIterator;
use DirectoryIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use League\Flysystem\Util;
use League\Flysystem\AdapterInterface;

class Local extends AbstractAdapter
{
    protected static $permissions = array(
        'public' => 0644,
        'private' => 0000,
    );

    /**
     * Constructor
     *
     * @param  string  $root
     */
    public function __construct($root)
    {
        $root = $this->ensureRootDirectory($root);

        $this->root = Util::normalizePrefix($root, DIRECTORY_SEPARATOR);
    }

    /**
     * Ensure the root directory exists.
     *
     * @param   string  $root  root directory path
     * @return  string  real path to root
     */
    protected function ensureRootDirectory($root)
    {
        if ( ! is_dir($root)) {
            mkdir($root, 0777, true);
        }

        return realpath($root);
    }

    /**
     * Prefix a path with the root
     *
     * @param   string  $path
     * @return  string  prefixed path
     */
    protected function prefix($path)
    {
        if (empty($path)) {
            return $this->root;
        }

        $path = str_replace('/', DIRECTORY_SEPARATOR, $path);

        return $this->root.Util::normalizePath($path, DIRECTORY_SEPARATOR);
    }

    /**
     * Check whether a file is present
     *
     * @param   string   $path
     * @return  boolean
     */
    public function has($path)
    {
        return file_exists($this->prefix($path));
    }

    /**
     * Write a file
     *
     * @param $path
     * @param $contents
     * @param null $config
     * @return array|bool
     */
    public function write($path, $contents, $config = null)
    {
        $location = $this->prefix($path);
        $config = Util::ensureConfig($config);

        if ( ! is_dir($dirname = dirname($location))) {
            mkdir($dirname, 0777, true);
        }

        if (($size = file_put_contents($location, $contents, LOCK_EX)) === false) {
            return false;
        }

        $result = array(
            'contents' => $contents,
            'type' => 'file',
            'size' => $size,
            'path' => $path,
        );

        if ($visibility = $config->get('visibility')) {
            $result['visibility'] = $visibility;
            $this->setVisibility($path, $visibility);
        }

        return $result;
    }

    /**
     * Write using a stream
     *
     * @param $path
     * @param $resource
     * @param null $config
     * @return array|bool
     */
    public function writeStream($path, $resource, $config = null)
    {
        rewind($resource);
        $config = Util::ensureConfig($config);
        $location = $this->prefix($path);

        if ( ! is_dir($dirname = dirname($location))) {
            mkdir($dirname, 0777, true);
        }

        if ( ! $stream = fopen($location, 'w+')) {
            return false;
        }

        while ( ! feof($resource)) {
            fwrite($stream, fread($resource, 1024), 1024);
        }

        if ( ! fclose($stream)) {
            return false;
        }

        if ($visibility = $config->get('visibility')) {
            $this->setVisibility($path, $visibility);
        }

        return compact('path', 'visibility');
    }

    /**
     * Get a read-stream for a file
     *
     * @param $path
     * @return array|bool
     */
    public function readStream($path)
    {
        $stream = fopen($this->prefix($path), 'r');

        return compact('stream', 'path');
    }

    /**
     * Update a file using a stream
{'fraction_non_alphanumeric': 0.13899770363366204, 'fraction_numerical': 0.007834661623666082, 'mean_word_length': 1.9161087042142575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1511167', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2123, 'n_words': 509}