Optimisations to WP_Filesystem; Pass known information to called functions. Props aldenta (John Ford) for investigation and patch. See #10913

git-svn-id: http://svn.automattic.com/wordpress/trunk@17525 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2011-03-22 00:04:15 +00:00
parent 540eaaff22
commit a83a2842ea
5 changed files with 27 additions and 25 deletions

View File

@ -193,11 +193,14 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
return $grouparray['name'];
}
function copy($source, $destination, $overwrite = false) {
function copy($source, $destination, $overwrite = false, $mode = false) {
if ( ! $overwrite && $this->exists($destination) )
return false;
return copy($source, $destination);
$rtval = copy($source, $destination);
if ( $mode )
$this->chmod($destination, $mode);
return $rtval;
}
function move($source, $destination, $overwrite = false) {
@ -216,12 +219,12 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
}
}
function delete($file, $recursive = false) {
function delete($file, $recursive = false, $type = false) {
if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
return false;
$file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
if ( $this->is_file($file) )
if ( 'f' == $type || $this->is_file($file) )
return @unlink($file);
if ( ! $recursive && $this->is_dir($file) )
return @rmdir($file);
@ -233,7 +236,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
$retval = true;
if ( is_array($filelist) ) //false if no files, So check first.
foreach ($filelist as $filename => $fileinfo)
if ( ! $this->delete($file . $filename, $recursive) )
if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
$retval = false;
if ( file_exists($file) && ! @rmdir($file) )

View File

@ -183,22 +183,22 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
$dir = $this->dirlist($file);
return $dir[$file]['group'];
}
function copy($source, $destination, $overwrite = false ) {
function copy($source, $destination, $overwrite = false, $mode = false) {
if ( ! $overwrite && $this->exists($destination) )
return false;
$content = $this->get_contents($source);
if ( false === $content)
return false;
return $this->put_contents($destination, $content);
return $this->put_contents($destination, $content, $mode);
}
function move($source, $destination, $overwrite = false) {
return ftp_rename($this->link, $source, $destination);
}
function delete($file, $recursive = false ) {
function delete($file, $recursive = false, $type = false) {
if ( empty($file) )
return false;
if ( $this->is_file($file) )
if ( 'f' == $type || $this->is_file($file) )
return @ftp_delete($this->link, $file);
if ( !$recursive )
return @ftp_rmdir($this->link, $file);
@ -206,7 +206,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
$filelist = $this->dirlist( trailingslashit($file) );
if ( !empty($filelist) )
foreach ( $filelist as $delete_file )
$this->delete( trailingslashit($file) . $delete_file['name'], $recursive);
$this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
return @ftp_rmdir($this->link, $file);
}

View File

@ -193,7 +193,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
return $dir[$file]['group'];
}
function copy($source, $destination, $overwrite = false ) {
function copy($source, $destination, $overwrite = false, $mode = false) {
if ( ! $overwrite && $this->exists($destination) )
return false;
@ -201,17 +201,17 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
if ( false === $content )
return false;
return $this->put_contents($destination, $content);
return $this->put_contents($destination, $content, $mode);
}
function move($source, $destination, $overwrite = false ) {
return $this->ftp->rename($source, $destination);
}
function delete($file, $recursive = false ) {
function delete($file, $recursive = false, $type = false) {
if ( empty($file) )
return false;
if ( $this->is_file($file) )
if ( 'f' == $type || $this->is_file($file) )
return $this->ftp->delete($file);
if ( !$recursive )
return $this->ftp->rmdir($file);

View File

@ -238,28 +238,28 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
return $grouparray['name'];
}
function copy($source, $destination, $overwrite = false ) {
function copy($source, $destination, $overwrite = false, $mode = false) {
if ( ! $overwrite && $this->exists($destination) )
return false;
$content = $this->get_contents($source);
if ( false === $content)
return false;
return $this->put_contents($destination, $content);
return $this->put_contents($destination, $content, $mode);
}
function move($source, $destination, $overwrite = false) {
return @ssh2_sftp_rename($this->link, $source, $destination);
}
function delete($file, $recursive = false) {
if ( $this->is_file($file) )
function delete($file, $recursive = false, $type = false) {
if ( 'f' == $type || $this->is_file($file) )
return ssh2_sftp_unlink($this->sftp_link, $file);
if ( ! $recursive )
return ssh2_sftp_rmdir($this->sftp_link, $file);
$filelist = $this->dirlist($file);
if ( is_array($filelist) ) {
foreach ( $filelist as $filename => $fileinfo) {
$this->delete($file . '/' . $filename, $recursive);
$this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
}
}
return ssh2_sftp_rmdir($this->sftp_link, $file);

View File

@ -80,9 +80,9 @@ function get_home_path() {
$home = get_option( 'home' );
$siteurl = get_option( 'siteurl' );
if ( $home != '' && $home != $siteurl ) {
$wp_path_rel_to_home = str_replace($home, '', $siteurl); /* $siteurl - $home */
$pos = strpos($_SERVER["SCRIPT_FILENAME"], $wp_path_rel_to_home);
$home_path = substr($_SERVER["SCRIPT_FILENAME"], 0, $pos);
$wp_path_rel_to_home = str_replace($home, '', $siteurl); /* $siteurl - $home */
$pos = strpos($_SERVER["SCRIPT_FILENAME"], $wp_path_rel_to_home);
$home_path = substr($_SERVER["SCRIPT_FILENAME"], 0, $pos);
$home_path = trailingslashit( $home_path );
} else {
$home_path = ABSPATH;
@ -773,13 +773,12 @@ function copy_dir($from, $to) {
foreach ( (array) $dirlist as $filename => $fileinfo ) {
if ( 'f' == $fileinfo['type'] ) {
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) ) {
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
// If copy failed, chmod file to 0644 and try again.
$wp_filesystem->chmod($to . $filename, 0644);
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
return new WP_Error('copy_failed', __('Could not copy file.'), $to . $filename);
}
$wp_filesystem->chmod($to . $filename, FS_CHMOD_FILE);
} elseif ( 'd' == $fileinfo['type'] ) {
if ( !$wp_filesystem->is_dir($to . $filename) ) {
if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )