forked from GithubBackups/vichan
Rollback file_write function
This commit is contained in:
parent
7fbb51bdd9
commit
96411813b4
@ -628,47 +628,41 @@ function purge($uri) {
|
|||||||
|
|
||||||
function file_write($path, $data, $simple = false, $skip_purge = false) {
|
function file_write($path, $data, $simple = false, $skip_purge = false) {
|
||||||
global $config, $debug;
|
global $config, $debug;
|
||||||
|
|
||||||
if (preg_match('/^remote:\/\/(.+)\:(.+)$/', $path, $m)) {
|
if (preg_match('/^remote:\/\/(.+)\:(.+)$/', $path, $m)) {
|
||||||
if (isset($config['remote'][$m[1]])) {
|
if (isset($config['remote'][$m[1]])) {
|
||||||
require_once 'inc/remote.php';
|
require_once 'inc/remote.php';
|
||||||
|
|
||||||
$remote = new Remote($config['remote'][$m[1]]);
|
$remote = new Remote($config['remote'][$m[1]]);
|
||||||
$remote->write($data, $m[2]);
|
$remote->write($data, $m[2]);
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
error('Invalid remote server: ' . $m[1]);
|
error('Invalid remote server: ' . $m[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// This will convert a local, relative path like "b/index.html" to a full path.
|
if (!$fp = dio_open($path, O_WRONLY | O_CREAT, 0644))
|
||||||
// dio_open does not work with relative paths on Windows machines.
|
|
||||||
$path = realpath(dirname($path)) . DIRECTORY_SEPARATOR . basename($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$fp = dio_open( $path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) {
|
|
||||||
error('Unable to open file for writing: ' . $path);
|
error('Unable to open file for writing: ' . $path);
|
||||||
}
|
|
||||||
|
|
||||||
// File locking
|
// File locking
|
||||||
if (function_exists("dio_fcntl") && dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) {
|
if (dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) {
|
||||||
error('Unable to lock file: ' . $path);
|
error('Unable to lock file: ' . $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Truncate file
|
||||||
|
if (!dio_truncate($fp, 0))
|
||||||
|
error('Unable to truncate file: ' . $path);
|
||||||
|
|
||||||
// Write data
|
// Write data
|
||||||
if (($bytes = dio_write($fp, $data)) === false) {
|
if (($bytes = dio_write($fp, $data)) === false)
|
||||||
error('Unable to write to file: ' . $path);
|
error('Unable to write to file: ' . $path);
|
||||||
}
|
|
||||||
|
|
||||||
// Unlock
|
// Unlock
|
||||||
if (function_exists("dio_fcntl")) {
|
dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK));
|
||||||
dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close
|
// Close
|
||||||
dio_close($fp);
|
dio_close($fp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create gzipped file.
|
* Create gzipped file.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user