[wp-trac] [WordPress Trac] #57375: Add move_dir() function
WordPress Trac
noreply at wordpress.org
Thu Dec 22 23:53:35 UTC 2022
#57375: Add move_dir() function
----------------------------+--------------------------
Reporter: afragen | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: 6.2
Component: Filesystem API | Version: trunk
Severity: normal | Resolution:
Keywords: has-patch | Focuses: performance
----------------------------+--------------------------
Comment (by costdev):
Some additional information:
== WP_Filesystem_* classes
`WP_Filesystem_Direct::move()` and the other filesystem `::move()` methods
are documented to move a file, not to move a directory. For non-direct
filesystems, the moving of directories is a happy extra due to the
behaviour of FTP/SSH functions.
== WP_Filesystem_Direct::move()
While `WP_Filesystem_Direct::move()` uses `rename()`, which works for
files ''and'' directories, if `rename()` should fail, it falls back to a
`copy()`, a single-file function.
== How should a `move_dir()` function differ?
It makes sense to introduce a `move_dir()` function that uses
`$wp_filesystem->move()` for non-direct filesystems, and `rename()` with a
fallback to `copy_dir()` for the direct filesystem.
== Performance
In terms of memory usage and speed, a recursive copy involves the
''contents'' of the files/directories, including calls to `mkdir()`, and a
subsequent `unlink()`.
`rename()`, on the other hand, simply updates the ''metadata''. This makes
`rename()` significantly more performant. It is purpose-built for moving
files and directories.
== PHP's `rename()` under the hood
As mentioned above, `rename()` updates the ''metadata''. However, when
PHP's `rename( $from, $to )` is called with `$from` and `$to` on different
drives, it uses a recursive copy. For anyone who has ever dragged and
dropped a file/directory from one drive to another, you'll likely have
noticed that this makes a copy on the second drive. This isn't a negative,
but just a bit of extra information on how `rename()` handles this
scenario for those interested.
-----
Overall, a +1 from me on implementing a function for the performant and
recursive moving of directories.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/57375#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list