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
This commit is contained in:
audrasjb 2022-08-08 12:41:13 +00:00
parent af08ed758d
commit 6a5506304e
3 changed files with 12 additions and 13 deletions

View File

@ -412,18 +412,18 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
* Checks if a file or directory exists. * Checks if a file or directory exists.
* *
* @since 2.5.0 * @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. * @param string $file Path to file or directory.
* @return bool Whether $file exists or not. * @return bool Whether $file exists or not.
*/ */
public function exists( $file ) { public function exists( $file ) {
$list = ftp_nlist( $this->link, $file ); if ( $this->is_dir( $file ) ) {
return true;
if ( empty( $list ) && $this->is_dir( $file ) ) {
return true; // File is an empty directory.
} }
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). * Gets the file size (in bytes).
* *
* @since 2.5.0 * @since 2.5.0
* @since 6.1.0 Update for proper return values.
* *
* @param string $file Path to file. * @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 ) { public function size( $file ) {
return ftp_size( $this->link, $file ); return ftp_size( $this->link, $file );

View File

@ -414,19 +414,17 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
* Checks if a file or directory exists. * Checks if a file or directory exists.
* *
* @since 2.5.0 * @since 2.5.0
* @since 6.1.0 Rewrite using file size.
* *
* @param string $file Path to file or directory. * @param string $file Path to file or directory.
* @return bool Whether $file exists or not. * @return bool Whether $file exists or not.
*/ */
public function exists( $file ) { public function exists( $file ) {
$list = $this->ftp->nlist( $file ); if ( $this->is_dir( $file ) ) {
return true;
if ( empty( $list ) && $this->is_dir( $file ) ) {
return true; // File is an empty directory.
} }
return ! empty( $list ); // Empty list = no file, so invert. return is_numeric( $this->size( $file ) );
// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.