From 6a5506304ed6ea2417915440429a595d09bc5e94 Mon Sep 17 00:00:00 2001 From: audrasjb Date: Mon, 8 Aug 2022 12:41:13 +0000 Subject: [PATCH] Filesystem: Rewrite FTP/FTP Sockets `exists()` methods to implement a more stable check. WordPress FTP file checking was previously based upon `ftp_nlist()`. This function can be problematic at scale with a directory containing a large number of files. The same issue occurred using it with ftpsockets. This changeset rewrites the FTP `exists()` functions to utilize a more efficient and stable check. Props giox069, desrosj, mkox, afragen, costdev, pbiron, peterwilsoncc. Fixes #51170. See #53318, #39781. Built from https://develop.svn.wordpress.org/trunk@53860 git-svn-id: http://core.svn.wordpress.org/trunk@53419 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-filesystem-ftpext.php | 13 +++++++------ .../includes/class-wp-filesystem-ftpsockets.php | 10 ++++------ wp-includes/version.php | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/wp-admin/includes/class-wp-filesystem-ftpext.php b/wp-admin/includes/class-wp-filesystem-ftpext.php index 097cabdfd2..fc50aaf0ef 100644 --- a/wp-admin/includes/class-wp-filesystem-ftpext.php +++ b/wp-admin/includes/class-wp-filesystem-ftpext.php @@ -412,18 +412,18 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { * Checks if a file or directory exists. * * @since 2.5.0 + * @since 6.1.0 Rewrite using ftp_rawlist, uses 'LIST' on FTP server + * takes file path or directory path as parameter. * * @param string $file Path to file or directory. * @return bool Whether $file exists or not. */ public function exists( $file ) { - $list = ftp_nlist( $this->link, $file ); - - if ( empty( $list ) && $this->is_dir( $file ) ) { - return true; // File is an empty directory. + if ( $this->is_dir( $file ) ) { + return true; } - return ! empty( $list ); // Empty list = no file, so invert. + return ! empty( ftp_rawlist( $this->link, $file ) ); } /** @@ -510,9 +510,10 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { * Gets the file size (in bytes). * * @since 2.5.0 + * @since 6.1.0 Update for proper return values. * * @param string $file Path to file. - * @return int|false Size of the file in bytes on success, false on failure. + * @return int Size of the file in bytes on success, -1 on failure. */ public function size( $file ) { return ftp_size( $this->link, $file ); diff --git a/wp-admin/includes/class-wp-filesystem-ftpsockets.php b/wp-admin/includes/class-wp-filesystem-ftpsockets.php index 3c0c659456..d0fc128a1b 100644 --- a/wp-admin/includes/class-wp-filesystem-ftpsockets.php +++ b/wp-admin/includes/class-wp-filesystem-ftpsockets.php @@ -414,19 +414,17 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { * Checks if a file or directory exists. * * @since 2.5.0 + * @since 6.1.0 Rewrite using file size. * * @param string $file Path to file or directory. * @return bool Whether $file exists or not. */ public function exists( $file ) { - $list = $this->ftp->nlist( $file ); - - if ( empty( $list ) && $this->is_dir( $file ) ) { - return true; // File is an empty directory. + if ( $this->is_dir( $file ) ) { + return true; } - return ! empty( $list ); // Empty list = no file, so invert. - // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server. + return is_numeric( $this->size( $file ) ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 86d70b4ba0..e1684c482b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-53859'; +$wp_version = '6.1-alpha-53860'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.