public function add_file_from_path($pathname, $contenthash = null) {

        list($contenthash, $filesize) = $this->validate_hash_and_file_size($contenthash, $pathname);

        $hashpath = $this->get_fulldir_from_hash($contenthash);
        $hashfile = $this->get_local_path_from_hash($contenthash, false);

        $newfile = true;

        if (file_exists($hashfile)) {
            if (filesize($hashfile) === $filesize) {
                return array($contenthash, $filesize, false);
            }
            if (file_storage::hash_from_path($hashfile) === $contenthash) {
                // Jackpot! We have a hash collision.
                mkdir("$this->filedir/jackpot/", $this->dirpermissions, true);
                copy($pathname, "$this->filedir/jackpot/{$contenthash}_1");
                copy($hashfile, "$this->filedir/jackpot/{$contenthash}_2");
                throw new file_pool_content_exception($contenthash);
            }
            debugging("Replacing invalid content file $contenthash");
            unlink($hashfile);
            $newfile = false;
        }

        if (!is_dir($hashpath)) {
            if (!mkdir($hashpath, $this->dirpermissions, true)) {
                // Permission trouble.
                throw new file_exception('storedfilecannotcreatefiledirs');
            }
        }

        // Let's try to prevent some race conditions.

        $prev = ignore_user_abort(true);
        @unlink($hashfile.'.tmp');
        if (!copy($pathname, $hashfile.'.tmp')) {
            // Borked permissions or out of disk space.
            @unlink($hashfile.'.tmp');
            ignore_user_abort($prev);
            throw new file_exception('storedfilecannotcreatefile');
        }
        if (file_storage::hash_from_path($hashfile.'.tmp') !== $contenthash) {
            // Highly unlikely edge case, but this can happen on an NFS volume with no space remaining.
            @unlink($hashfile.'.tmp');
            ignore_user_abort($prev);
            throw new file_exception('storedfilecannotcreatefile');
        }
        rename($hashfile.'.tmp', $hashfile);
        chmod($hashfile, $this->filepermissions); // Fix permissions if needed.
        @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way.
        ignore_user_abort($prev);

        return array($contenthash, $filesize, $newfile);
    }
public function add_file_from_path($pathname, $contenthash = null) {

        list($contenthash, $filesize) = $this->validate_hash_and_file_size($contenthash, $pathname);

        $hashpath = $this->get_fulldir_from_hash($contenthash);
        $hashfile = $this->get_local_path_from_hash($contenthash, false);

        $newfile = true;

        if (file_exists($hashfile)) {
            if (filesize($hashfile) === $filesize) {
                return array($contenthash, $filesize, false);
            }
            if (file_storage::hash_from_path($hashfile) === $contenthash) {
                // Jackpot! We have a hash collision.
                mkdir("$this->filedir/jackpot/", $this->dirpermissions, true);
                copy($pathname, "$this->filedir/jackpot/{$contenthash}_1");
                copy($hashfile, "$this->filedir/jackpot/{$contenthash}_2");
                throw new file_pool_content_exception($contenthash);
            }
            debugging("Replacing invalid content file $contenthash");
            unlink($hashfile);
            $newfile = false;
        }

        if (!is_dir($hashpath)) {
            if (!mkdir($hashpath, $this->dirpermissions, true)) {
                // Permission trouble.
                throw new file_exception('storedfilecannotcreatefiledirs');
            }
        }

        // Let's try to prevent some race conditions.

        $prev = ignore_user_abort(true);
        @unlink($hashfile.'.tmp');
        if (!copy($pathname, $hashfile.'.tmp')) {
            // Borked permissions or out of disk space.
            @unlink($hashfile.'.tmp');
            ignore_user_abort($prev);
            throw new file_exception('storedfilecannotcreatefile');
        }
        if (file_storage::hash_from_path($hashfile.'.tmp') !== $contenthash) {
            // Highly unlikely edge case, but this can happen on an NFS volume with no space remaining.
            @unlink($hashfile.'.tmp');
            ignore_user_abort($prev);
            throw new file_exception('storedfilecannotcreatefile');
        }
        rename($hashfile.'.tmp', $hashfile);
        chmod($hashfile, $this->filepermissions); // Fix permissions if needed.
        @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way.
        ignore_user_abort($prev);

        return array($contenthash, $filesize, $newfile);
    }
public function add_file_from_path ( $ pathname , $ contenthash = null ) { list ( $ contenthash , $ filesize ) = $ this -> validate_hash_and_file_size ( $ contenthash , $ pathname ) ; $ hashpath = $ this -> get_fulldir_from_hash ( $ contenthash ) ; $ hashfile = $ this -> get_local_path_from_hash ( $ contenthash , false ) ; $ newfile = true ; if ( file_exists ( $ hashfile ) ) { if ( filesize ( $ hashfile ) === $ filesize ) { return array ( $ contenthash , $ filesize , false ) ; } if ( file_storage :: hash_from_path ( $ hashfile ) === $ contenthash ) { // Jackpot! We have a hash collision. mkdir ( "$this->filedir/jackpot/" , $ this -> dirpermissions , true ) ; copy ( $ pathname , "$this->filedir/jackpot/{$contenthash}_1" ) ; copy ( $ hashfile , "$this->filedir/jackpot/{$contenthash}_2" ) ; throw new file_pool_content_exception ( $ contenthash ) ; } debugging ( "Replacing invalid content file $contenthash" ) ; unlink ( $ hashfile ) ; $ newfile = false ; } if ( ! is_dir ( $ hashpath ) ) { if ( ! mkdir ( $ hashpath , $ this -> dirpermissions , true ) ) { // Permission trouble. throw new file_exception ( 'storedfilecannotcreatefiledirs' ) ; } } // Let's try to prevent some race conditions. $ prev = ignore_user_abort ( true ) ; @ unlink ( $ hashfile . '.tmp' ) ; if ( ! copy ( $ pathname , $ hashfile . '.tmp' ) ) { // Borked permissions or out of disk space. @ unlink ( $ hashfile . '.tmp' ) ; ignore_user_abort ( $ prev ) ; throw new file_exception ( 'storedfilecannotcreatefile' ) ; } if ( file_storage :: hash_from_path ( $ hashfile . '.tmp' ) !== $ contenthash ) { // Highly unlikely edge case, but this can happen on an NFS volume with no space remaining. @ unlink ( $ hashfile . '.tmp' ) ; ignore_user_abort ( $ prev ) ; throw new file_exception ( 'storedfilecannotcreatefile' ) ; } rename ( $ hashfile . '.tmp' , $ hashfile ) ; chmod ( $ hashfile , $ this -> filepermissions ) ; // Fix permissions if needed. @ unlink ( $ hashfile . '.tmp' ) ; // Just in case anything fails in a weird way. ignore_user_abort ( $ prev ) ; return array ( $ contenthash , $ filesize , $ newfile ) ; }
Add the supplied file to the file system.

Note: If overriding this function, it is advisable to store the file
in the path returned by get_local_path_from_hash as there may be
subsequent uses of the file in the same request.

@param string $pathname Path to file currently on disk
@param string $contenthash SHA1 hash of content if known (performance only)
@return array (contenthash, filesize, newfile)
Add the supplied file to the file system .