Skip to content

A module archive whose top folder is the module's own name is extracted beside the installed folder too (closed: not shipped in 3.7.2) - #443

Closed
robiso wants to merge 11 commits into
372from
fix/module-update-own-name-archive
Closed

robiso wants to merge 11 commits into
372from
fix/module-update-own-name-archive

Conversation

@robiso

@robiso robiso commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

A theme or plugin archive whose single top folder is the module's own name (foo/theme.php, foo/css/style.css) is now extracted beside the installed folder too, the way #441 does it for a GitHub archive (foo-master/, foo-main/). The installed folder is touched only after every entry of the new copy is on disk with the size the archive declares, and then, for both shapes, it is renamed aside and the new copy renamed in, instead of being deleted in place first.

Why

#441 kept the old order (delete, extract, rename) for an own-name archive, because extracting foo/ straight into themes/ would merge it into the live themes/foo/. A custom module repository (Settings, custom modules) can serve exactly that shape. For it the installed module was still deleted before an unchecked extract, so a failed extract (a collision inside the archive, a disk that fills) ended with "Successfully installed/updated" while the module was gone or partial.

The staging folder

An own-name archive is extracted into <type>/<name>.update.tmp/, inside the same themes/ or plugins/ folder, so the final rename() stays on one filesystem. The name is fixed, so a leftover of a request that was cut short is cleared by the next update of that module; no module can carry it because the installer's name charset has no dot. A link at that path is removed, never followed. If the staging folder cannot be created the action stops with an alert and nothing is deleted. On failure the staging folder is removed and the installed version is left in place; on success the new copy is swapped in (below) and the empty staging folder is removed. The staging folder exists only during the request (or after a killed one, until the next update); its top level holds <name>/, not an entry file, so it never shows in the Themes or Plugins tab and loadPlugins() never loads it.

A GitHub archive is extracted as before. Any other shape (files at the archive root, or mixed top folders) keeps the old order, as before.

The swap (GitHub and own-name archives)

Once the whole archive is on disk, the installed folder is renamed aside to <type>/<name>.old.<8 hex>.tmp, a name new for every request (made by tempPath(), which names the atomic saves' temp files), the new folder is renamed into place, and the aside copy is then removed where it can be. A rename needs write permission on themes/ or plugins/ only, so a subfolder the PHP user cannot empty (uploaded by FTP as another user, or made read-only) moves aside with the old copy. Before, the delete stopped at that subfolder, the rename met a non-empty folder and threw: an uncaught exception (HTTP 500), the module half-deleted and the new copy stranded beside it. A GitHub archive did the same on 372.

  • A remnant that cannot be emptied stays until a later update can clear it: every update first clears the remnants of earlier ones as far as it can (names of that shape only, the glob pattern writeFile() uses for its temp files, with a [, * or ? in the site's path escaped so it matches only itself). As the aside name is new each time, a remnant never blocks a later update; an update that cannot empty its aside copy leaves one more, and all of them go once they can be removed. loadPlugins() and the Plugins tab never pick one up (they look for <folder>/<folder>.php); the Themes tab lists one only while its theme.php is left.
  • Where the installed folder refuses to be renamed aside (locked or immutable: Finder's Locked, chflags uchg, chattr +i; a file in it held open on Windows; permissions on the parent), or no aside name can be made (tempPath() throws where random_bytes() has no source of randomness), the new copy is removed, the installed folder is left exactly as it was, and the action stops with "Could not move the installed version aside. Nothing was changed. Check that <type>/<name> is not locked or in use, then try again." Nothing is deleted first. On such a folder a delete gets through the files in its subfolders before the same refusal stops the rename into place, so deleting first ended in an uncaught exception with the module stripped to its top-level files and the new copy stranded beside it: what 372 does for a GitHub archive on that shape. 372's own-name order deleted what it could and extracted into the folder; it now refuses the same way, and the update lands once the folder can be moved. tempPath() is called only here, so the other ZIP shapes never reach it.
  • One refusal is the exception: a cross-device one (EXDEV; PHP's warning ends in "Invalid cross-device link" on glibc, "Cross-device link" on macOS and musl). A folder that comes from a lower layer of an overlay filesystem refuses every rename that way, yet it can be deleted, and a folder made in the upper layer can be renamed in. The bundled Sky theme in the project's Docker image (ghcr.io/robiso/wondercms:3.7.0, Docker's overlayfs with redirect_dir off) is such a folder, so every update of it was refused. For that refusal only, the installed folder is deleted and the new copy renamed in, as 372 does for a GitHub archive; there is no aside copy to go back to, so two guards stand in for it, for both archive shapes. The front door is a small private helper, canEmpty(): the folder and every folder under it can be listed and written to, links not followed (where is_writable() is disabled it cannot tell, so it says no). Where it says no (a subfolder the image ships owned by root, or one made read-only or unreadable) the action takes the refusal above, with "and that the web server can delete everything in it" added to the alert, and nothing is touched. Where it says yes, the delete runs without warnings (removeQuietly()), and if the installed folder still exists afterwards, because a part of it the check cannot foresee stayed (a volume or tmpfs mounted inside the module, another user's file in a sticky folder, a folder with write but no search permission, an immutable file), the new copy is moved into it entry by entry (mergeInto(): a file replaces the file of its name, a folder merges into the folder of its name, a link is moved and never followed, and a link at the destination is removed first, or, where it cannot be removed, left in place with the entry reported as not placed, so it is never merged into or written through; a folder rename() cannot move is made at the destination and filled entry by entry, which is the case under a mount point: PHP's rename() copies a file across one but never a folder, so with a volume or bind mount on css/ and an archive that carries css/fonts/, 8649435 left fonts/ out with the alert below on every retry), the empty new folder is removed, and the action ends with the success alert, as 372's extract over the folder did. An entry that cannot be placed (one under the part that stayed) is left out and reported: "Some of the update could not be moved into <type>/<name>; the rest is in place. Check that the web server can replace everything in it, then update again." Nothing on this path throws, and a folder is never left half-deleted: f28f4c6 and a763bf6 stopped at such a part, the rename in met a non-empty folder and threw, an uncaught exception with the module stripped to that part and the new copy stranded beside it (372 the same for a GitHub archive). The cross-device reason is read from the rename's own warning (error_clear_last() before it, error_get_last() after), matched at the end of the message, where PHP puts it, so a path holding the words cannot match. Where a plugin's error handler swallows the warning, the refusal stands as for any other reason.
  • A folder with a volume mounted on it or below it (a Docker -v on themes/sky or on themes/sky/css, a bind mount, a tmpfs) is not renamed aside. A rename takes a mount inside the folder along: on f25ea5a the mount went to <name>.old.<8 hex>.tmp/, the removal of the aside copy emptied the volume there and left it mounted in the remnant under the plain success alert, and after a remount (a reboot, a container restart) the theme had an empty css/. macOS renames a mount point itself the same way, so a volume on the module folder went aside whole. Before the rename aside a small private helper, holdsMount(), looks for a mount in three ways, links not followed: where Linux lists this process's mounts (/proc/self/mountinfo, read by a protected mountInfo(); field 5 is the mount point, its octal escapes for a space, tab, newline or backslash decoded in one pass), any mount point at the module folder or under it, which also finds a bind mount whose source is on the site's own filesystem and so keeps the device; then the module folder's device against its parent's (stat()'s dev, read by a protected deviceOf()), for a volume on the folder itself; then each subfolder's device against the folder's own. Where stat() is disabled and no mount is listed it finds none, as before. Where one is found, the update takes the cross-device path above (the canEmpty() front door, the quiet delete, the merge), so the mount stays where it is and ends up holding the new files, as 372's own-name extract over the folder kept it filled. On Linux a module folder that is itself a mount point refused the rename (EBUSY), so it could not be updated; it now takes the same path. A refusal there comes before any move was tried (a read-only volume, which canEmpty() rejects), so its alert reads "Could not update <type>/<name>: it cannot be written. Nothing was changed. Check that <type>/<name> is not locked or in use and that the web server can delete everything in it, then try again." What is left: where /proc/self/mountinfo cannot be read (not Linux, or open_basedir keeps PHP out of /proc) the device checks decide alone, so a mount that keeps the device, or one below a folder the web server cannot list, still goes aside with the rename, as on d7860ba.
  • If the rename into place fails after the rename aside, the aside copy is renamed back, the new copy is removed, and the action stops with "Could not move the update into place. The installed version was left in place." instead of throwing.
  • An installed folder that is a link is removed, as before, not renamed. A remnant that is a link is removed, never followed. The aside copy is removed without warnings by a small helper, removeQuietly(): what it cannot remove, or a folder it cannot open, stays.
  • A GitHub or own-name archive renames only the folder it extracted: the -main fallback probe now applies to the other ZIP shapes only, so a vanished extract folder takes the rollback (or the exception) instead of a success alert over a removed module.
  • The removal of the staging folder after the swap has the same link guard as the other removals.

The temp-file sweep in writeFile()

writeFile()'s sweep of stale temp files (on 372) builds its glob pattern on the file's path the same way, and it was unescaped: with the site at .../site[1]/, a save missed its own stale data/database.js.<8 hex>.tmp and removed the one under .../site1/. Both sweeps now escape the path with bracket expressions ([ as [[], * as [*], ? as [?]), the form the patterns already use for the hex digits, rather than with backslashes, which are path separators on Windows.

Measuring where filesize() is disabled

The size check after the extract ran only where filesize() exists, so on a host with filesize in disable_functions every entry was accepted unmeasured and a truncated archive (the central directory says 20 bytes, the data holds 10) was installed under a success alert. A small helper, extractedSize(), now reads each extracted file's size with the first function the host leaves enabled: filesize(), else stat(), else file_get_contents(), which the core needs everywhere; a file none of them can read counts as missing. Test: the module-install probe (tests/probe.php) run with disable_functions=filesize and with filesize,stat over an installed theme and a doctored-size GitHub archive expects the danger alert and the old folder intact (fails on 50df22f: success alert, truncated copy installed). The probe's redirect() now exits in the module modes, as the real one does, so a failure branch does not run on into the success one.

Tests

tests/LocalModulesTest.php, next to the #441 tests:

  • an own-name archive that cannot be extracted (a file where a folder is needed) leaves the installed version byte-identical, no foo.update.tmp left, danger alert, no success alert (fails on 372 head 52f2c91: success alert, module replaced by the partial copy)
  • an own-name archive whose file lands shorter than the archive declares leaves the installed version in place (fails on 52f2c91)
  • a clean own-name archive updates the module and leaves no staging folder (the existing ✅ A theme or plugin update keeps the installed version until the new one extracted completely #441 test, extended)
  • a stale foo.update.tmp/ from a killed request plus a clean archive: foo holds only the archive's files, no staging folder left (fails on 52f2c91)
  • a host that disables filesize() (and one that disables stat() too) still measures the extracted files and refuses a truncated archive (fails on 50df22f)
  • links at foo.update.tmp and at a remnant name, foo.old.0123abcd.tmp, pointing at a folder outside the module, next to a plain remnant folder foo.old.89abcdef.tmp/: after the update both links and the remnant folder are gone, the outside file is intact, nothing was extracted into it, and the module is updated (fails with a trailing slash on the staging path, without the remnant sweep, with GLOB_MARK on its glob, or with a sweep that follows a link: the outside file is deleted)
  • an installed foo whose locked/ subfolder cannot be emptied (0555), or cannot even be opened (0000), updated from a foo/ archive and from a foo-master/ archive: success alert, foo holds only the new copy, the remnant stays in a foo.old.<8 hex>.tmp/locked (each case fails on 3f2c974: Exception: Theme or plugin not installed, or UnexpectedValueException from DirectoryIterator for 0000; through the probe, exit 255 with the module half-deleted). Skipped when the user can write into a read-only folder (root).
  • two consecutive updates of foo from a foo/ archive, a locked 0555 folder uploaded into foo again before each: both land with a success alert, foo holds the new copy each time, and each upload stays in its own remnant (fails on 87d98f5, where the aside name was fixed: the second rename aside met the first remnant, the delete-first fallback stopped at the locked folder, and the second update threw Exception: Theme or plugin not installed). Skipped as root.
  • an installed foo that cannot be renamed aside (the stubbed tempPath() returns a path in a folder that does not exist, so the rename is refused on every platform, as root too) or gets no aside name (the stub throws, as random_bytes() does with no source of randomness), updated from a foo/ archive and from a foo-master/ archive: the danger alert "Could not move the installed version aside. Nothing was changed. Check that themes/foo is not locked or in use, then try again.", no success alert, foo byte-identical, no foo.update.tmp, foo-master or foo.old.* left. Each of the four cases fails on e4f75c4: a refused rename took the delete-first fallback and ended in a success alert over the deleted folder, and the throwing tempPath() escaped as an uncaught Exception; the throwing cases also fail with the try/catch removed.
  • an installed foo whose rename aside is refused across devices (the stubbed tempPath() returns a path in /dev, a filesystem of its own on Linux and macOS, so rename() gets a real EXDEV and PHP's own message), updated from a foo/ archive and from a foo-master/ archive: success alert, no danger alert, foo holds only the new copy, no staging, extracted or aside folder left (fails on 9a41318 with the danger alert "Could not move the installed version aside. Nothing was changed.", on macOS and in the Docker image; also fails with the delete left to a folder that was not installed). The test above fails if every refusal takes this path, and both fail if the match reads a path in the message instead of its reason. Skipped on Windows.
  • the same cross-device refusal over an installed foo whose locked/ subfolder cannot be emptied (0555, from a foo/ and from a foo-master/ archive) or opened (0000, from a foo/ archive): the danger alert naming themes/foo and ending "and that the web server can delete everything in it, then try again.", no success alert, foo holding theme.php and locked/keep.txt as before and nothing else, no staging, extracted or aside folder left. Each case fails on f28f4c6: the 0555 cases with Exception: Theme or plugin not installed, foo stripped to locked/keep.txt and the new copy stranded in foo.update.tmp/ or foo-master/; the 0000 case with UnexpectedValueException from DirectoryIterator and foo.update.tmp/ stranded. It also fails with the check removed, made shallow (no look into subfolders) or without its try/catch, and with the tail of the alert dropped; the cross-device test above fails if the check always refuses. Skipped as root and on Windows.
  • the same cross-device refusal over an installed foo (theme.php, css/style.css) whose locked/ subfolder has write but no search permission (0666), so canEmpty() says yes and the delete stops at it, from a foo/ and from a foo-master/ archive (theme.php, css/style.css, js/app.js), and from a foo/ archive that also carries locked/new.txt: the success alert, foo = css, js, locked, theme.php with the new content, locked/keep.txt as before and alone, no staging, extracted or aside folder left; no danger alert for the first two, and for the third the one naming themes/foo. Each case fails on a763bf6 with Exception: Theme or plugin not installed, foo stripped to locked/keep.txt and the new copy stranded. It also fails with the merge moving nothing, with a failed entry not reported, and with the fallback removed. Skipped as root and on Windows.
  • the same cross-device refusal over an installed foo whose css/ stays (a 0666 css/locked/ stops the delete there, as a mount point would), from a foo-master/ archive carrying theme.php and css/fonts/a.woff2, with the new copy's css/ made read-only once extracted (from the stubbed tempPath()), so rename() refuses its fonts/ folder while the file in it can still move, the split a mount point makes: the success alert, no danger alert, foo/css = fonts, locked with the new font in fonts/. Fails on 8649435 with the partial alert and fonts/ left out. Skipped as root and on Windows.
  • mergeInto() called directly (through reflection) over a foo whose css is a link to a folder outside the module, with a new css/style.css: it returns true, the link is replaced by a real folder holding the new file, and the outside folder holds only its own file. Fails with the destination-link check removed (the new file is written through the link into the outside folder). Skipped on Windows.
  • an installed foo whose css/ is on a device of its own (a stubbed deviceOf(); a 0666 css/locked/ keeps css/ in place through the delete, as a mount point does), from a foo/ and from a foo-master/ archive (theme.php, css/style.css, css/print.css): the success alert, no danger alert, css/ the same folder as before (its inode unchanged) holding locked, print.css and style.css with the new content, no staging, extracted or aside folder left. Fails on f25ea5a with only the deviceOf() seam added: the rename aside took css/ along, so the css/ in place is another folder and the remnant holds the old one. Skipped as root and on Windows.
  • on macOS, an HFS+ RAM disk (hdiutil, newfs_hfs, diskutil mount) mounted at themes/foo/css, through the real action from a foo/ and from a foo-master/ archive: the same, and the volume still mounted at css/; then mergeInto() called directly with an undeletable link (chflags -h uchg) on the volume pointing at a file outside the module: it returns false (in the action, the partial alert), the link and the outside file as they were. Fails on f25ea5a (the mount moved into the remnant, emptied) and with the new link skip removed (PHP's copy across the mount wrote through the link). Skipped where hdiutil is absent (CI).
  • an installed foo on a device of its own, everything in it on that device too (a stubbed deviceOf(); a 0666 locked/ keeps foo in place through the delete, as a mount point does), from a foo/ and from a foo-master/ archive (theme.php, css/style.css, css/print.css): the success alert, no danger alert, foo the same folder as before (its inode unchanged) holding css, locked and theme.php with the new content, no staging, extracted or aside folder left; then with a read-only css/ the danger alert beginning "Could not update themes/foo: it cannot be written. Nothing was changed." (no "aside" in it), no success alert, foo as it was. Fails on d7860ba (the rename aside took foo along, so the foo in place is another folder), with the parent's device left unread, and with the refusal's old wording. Skipped as root and on Windows.
  • holdsMount() called directly with a stubbed mountInfo(): a mount point under the module and one on the module folder itself count; /, /proc, the parent folder, a sibling sharing the name's prefix (foobar) and a folder under that sibling do not; a site path holding a space and a backslash before 040 (written \040 and \134040) is decoded once, never twice. Then the update with a bind mount listed at themes/foo/css (a 0666 css/locked/ keeps css/ in place through the delete), from a foo/ and from a foo-master/ archive: the success alert, css/ the same folder holding the new file. Fails with the mount list left unread, with the escapes left undecoded, and with a decode that undoes \134 before \040. On d7860ba the seam does not exist. Skipped as root and on Windows.
  • a site at .../site[1]/ and a sibling .../site1/, each with a remnant of foo and a stale database temp file: an update in site[1] clears only its own remnant, and a save there sweeps only its own temp file (fails with either escape removed: the own leftover stays and the sibling's is removed).

The immutable shape itself was run through the module-install probe with chflags uchg themes/foo on macOS (foo holding theme.php, css/, js/, images/), for both archive shapes. e4f75c4: exit 255, Theme or plugin not installed, foo stripped to theme.php, the new copy stranded in foo.update.tmp/ or foo-master/. This head: the danger alert, foo md5-identical, nothing left beside it. It is not in the suite: it needs chflags or chattr and a non-root user.

The Docker case was run through the same probe in the project image (ghcr.io/robiso/wondercms:3.7.0, PHP 8.4.27, overlayfs, redirect_dir=N), with index.php and tests/probe.php copied in, updating the bundled themes/sky from a sky/ and from a sky-master/ archive. A bare rename() of themes/sky there is refused with "Invalid cross-device link". 9a41318: the danger alert, Sky untouched, so Sky could never be updated. This head: "Successfully installed/updated sky." for both shapes, sky holding only the new copy, nothing left beside it. With a part planted in the image's Sky first (PHP runs as www-data), updating from a sky/ archive (the sky-master/ arm the same unless noted), each run from a fresh container:

part planted in themes/sky 3.7.1 (3ad007a) a763bf6 this head
locked/keep.txt, folder owned by root, 0755 success alert; theme.php new, locked/ kept (own-name arm); GitHub arm: exception, Sky stripped to locked/, sky-master/ stranded refusal alert, Sky md5-identical the same refusal, Sky md5-identical, nothing beside it
a tmpfs mounted at custom/ (--tmpfs) success, theme.php new, the mount point kept, its files replaced (own-name); GitHub arm: exception, Sky stripped exception, Sky stripped to the mount, sky.update.tmp/ stranded, the same on every retry success alert, theme.php and css new, the mount point kept, its files replaced, no warnings, nothing beside it; both arms
cache/ owned by root, 1777, holding root's file success, the file kept (own-name); GitHub arm: exception exception, Sky stripped to cache/rootfile success, cache/rootfile kept; both arms
noexec/ owned by www-data, 0666 (write, no search) success, noexec/f kept (own-name); GitHub arm: exception exception, Sky stripped to noexec/f success, noexec/f kept
the same, from an archive that also carries noexec/x success, x not written exception success alert plus the danger alert naming themes/sky; theme.php and css new, noexec/f kept, x not placed

The rollback has no input that reaches it (the rename aside has just shown the folder is writable); it was checked with a scratch change that makes the rename into place fail, for both archive shapes and with a locked subfolder: danger alert, the installed folder back whole, no .old.*.tmp, staging or -master folder left.

The other #441 tests (and, merged with 372, the #442 tests) and the install and symlink tests are unchanged and green, under PHPUnit 9.6 too. PHP 7.4 syntax; PHPUnit 9.6 assertions.

No VERSION bump.

…tracted beside the installed one

An archive whose single top folder is the module's own name (a custom
module repository can serve one) was still handled in the old order:
delete the installed folder, extract, rename. A failed extract ended
with a success alert and the module gone or partial.

It now unpacks into <type>/<name>.update.tmp/ beside the module, every
entry is measured as for a GitHub archive, and the installed folder goes
only once the new copy is complete. The staging name is fixed so a
leftover of a killed request is cleared by the next update; no module
can carry it since the installer's name charset has no dot. If the
staging folder cannot be created the action stops with an alert and
nothing is deleted. A GitHub archive and any other shape (files at the
root, mixed top folders) are handled exactly as before.

Tests: an own-name archive that cannot be extracted, one whose file
lands short, a stale staging folder from a killed request, and the
existing clean own-name update now checked through the staging path.
…contents() where filesize() is disabled

The size loop ran only where filesize() exists, so on a host with
filesize in disable_functions every entry was accepted unmeasured and a
truncated archive (central directory 20 bytes, data 10) was installed
under a success alert.

extractedSize() reads the size with the first function the host leaves
enabled: filesize(), else stat(), else file_get_contents(), which the
core needs everywhere; a file none of them can read counts as missing.

Test: the module-install probe run with disable_functions=filesize and
with filesize,stat over an installed theme and a doctored-size GitHub
archive expects the danger alert and the old folder intact. The probe's
redirect() now exits in the module modes, as the real one does, so a
failure branch does not run on into the success one; the probe helper
takes the list of disabled functions.
…renamed in; a link at the staging path is pinned

Once the whole archive was on disk (a GitHub archive, or one whose top
folder is the module's own name), the installed folder was deleted and
the new one renamed in. A subfolder the PHP user cannot empty (uploaded
by FTP as another user, or read-only) stopped the delete halfway, and
rename() then met a non-empty folder and threw: an uncaught exception,
the module half-deleted and the new copy stranded beside it.

The installed folder is now renamed aside to <name>.old.tmp (a rename
needs write on themes/ or plugins/ only, so such a subfolder moves with
it), the new folder is renamed into place, and the aside copy is
removed where it can be, without warnings (removeQuietly()). A remnant
stays as <name>.old.tmp: loadPlugins() and the Plugins tab never pick
it up, and the Themes tab lists it only while its theme.php is left.
If the rename aside fails, the installed folder is deleted first, as
before; if the rename into place fails after it, the aside copy is
renamed back and the action stops with a danger alert. A stale
<name>.old.tmp is cleared first; a link there, or as the installed
folder, is removed, never followed or renamed. A GitHub or own-name
archive renames only the folder it extracted (the -main probe is for
the other shapes), and the removal of the staging folder after the
swap has the link guard too.

Tests: links at foo.update.tmp and foo.old.tmp pointing at an outside
folder are removed and the outside file kept (fails with a trailing
slash on either path); an installed foo whose subfolder cannot be
emptied (0555) or opened (0000), updated from a foo/ and a foo-master/
archive, lands with the remnant in foo.old.tmp (fails on 3f2c974).
… never blocks the swap

The installed folder was renamed aside to one fixed name,
<name>.old.tmp. A subfolder the PHP user cannot empty stays in that
folder after the update, so at the next update of the module the
rename aside met a non-empty folder and failed; the fallback deleted
the installed folder first, stopped at a locked folder uploaded again
since, and the rename in threw: the uncaught exception and the
half-deleted module the swap was meant to end.

The aside name is now new for every request, made by tempPath()
(<name>.old.<8 hex>.tmp), and the remnants of earlier updates are
cleared first, best effort, with the glob pattern writeFile() uses for
the names tempPath() makes, through removeQuietly() (a link is
removed, never followed). A remnant that cannot be emptied stays and
never blocks a later update. The fallback to the old order is left for
a rename aside the parent folder refuses (permissions, a file held
open on Windows), where the same refusal stops that order too.

Tests: two consecutive updates from a foo/ archive, a locked 0555
folder uploaded into foo before each: both land, foo holds the new
copy each time, and each upload stays in its own remnant (fails on
87d98f5: the second update throws "Theme or plugin not installed").
The link test plants its link at a remnant name, foo.old.0123abcd.tmp,
next to a plain remnant folder: both go and the outside file is kept
(fails without the sweep, with GLOB_MARK, or with a sweep that follows
links). The locked-folder test finds its remnant by pattern.
…touched

Where the installed folder refused the rename aside (locked or
immutable, a mount point, a file held open in it on Windows), the
GitHub and own-name arms fell back to deleting it first. On such a
folder the delete gets through the files in its subfolders before the
same refusal stops the rename into place: the action threw "Theme or
plugin not installed" with the module stripped to its top-level files
and the new copy stranded beside it (chflags uchg on macOS, both
archive shapes).

There is no fallback now. When the rename aside is refused, or
tempPath() throws (random_bytes() with no source of randomness), the
new copy is removed, the installed folder is left as it was, and the
action stops with "Could not move the installed version aside. Nothing
was changed." The aside name is made only for an installed folder of
those two shapes, so the other ZIP shapes never call tempPath(). The
comment no longer says the installed folder is deleted first.

The remnant sweep and writeFile()'s temp sweep escape [, * and ? in the
path with bracket expressions: with the site at .../site[1]/ both
missed their own leftovers and matched those under .../site1/.

Tests: an installed foo that cannot be renamed aside (tempPath()
stubbed to a path in a folder that does not exist, refused on every
platform, as root too) or gets no aside name (the stub throws), updated
from a foo/ and a foo-master/ archive: danger alert, foo byte-identical,
no staging, foo-master or aside folder left (each case fails on
e4f75c4: a success alert over the deleted folder, or the uncaught
Exception). A site at site[1] beside site1: an update and a save there
sweep only their own leftovers (fails with either escape removed).
…mage content) takes the delete-then-rename order

On overlayfs with redirect_dir off (as in Docker Desktop's engine), a
folder that comes from a lower layer refuses rename() with EXDEV, while
it can be deleted and a folder made in the upper layer can be renamed
in. In the project's image, ghcr.io/robiso/wondercms:3.7.0, the bundled
Sky theme is such a folder: since 9a41318 every update of it stopped
with "Could not move the installed version aside. Nothing was
changed.", where 3ad007a and e4f75c4 deleted it and renamed the new
copy in.

The rename aside now reads its own warning, error_clear_last() before
it and error_get_last() after. A refusal whose message ends in
"cross-device link", in any case (glibc says "Invalid cross-device
link", macOS and musl "Cross-device link"), deletes the installed
folder, with the link guard, and renames the new copy in. Every other
refusal, and a tempPath() that throws, still removes the new copy and
leaves the installed folder as it was. The match is anchored at the end
of the message, where PHP puts the reason, so a path holding the words
cannot match.

Tests: an installed foo whose aside path is in /dev (a filesystem of
its own on Linux and macOS, so the rename aside gets a real EXDEV and
PHP's own message), updated from a foo/ and a foo-master/ archive:
success alert, foo holds only the new copy, no staging, extracted or
aside folder left. It fails on 9a41318 with the danger alert (on macOS,
and on Linux in the image), and with the delete left to a folder that
was not installed. The refusal test fails if every refusal deletes;
both fail if the match reads a path in the message instead of its
reason. In the image, with this index.php copied in, the bundled Sky
updates from a sky/ and a sky-master/ archive.
…y when every part of it can be

On the cross-device path (a folder from a lower overlay layer, such as
the bundled Sky in ghcr.io/robiso/wondercms:3.7.0) nothing is moved
aside before the delete. A subfolder the PHP user cannot empty (one the
image ships owned by root, or one made read-only) stopped the delete
halfway; the rename in then met a non-empty folder and threw "Theme or
plugin not installed", with theme.php gone and the new copy stranded in
<name>.update.tmp/ or <name>-master/. A subfolder it cannot open threw
UnexpectedValueException from DirectoryIterator.

That path now runs only when a small private check, canEmpty(), finds
that the folder and every folder under it can be listed and written
to, links not followed. Otherwise the update takes the refusal every
other refused rename takes: the new copy is removed and the installed
folder is left as it was. The check covers the GitHub arm too, where
372 already deleted first and threw on this shape. The refusal alert
now names the folder and what to check: "Check that <type>/<name> is
not locked or in use and that the web server can delete everything in
it, then try again." Two comments now also name a plugin error handler
that throws on the warning, and tempPath()'s second caller.

Tests: on the cross-device path, an installed foo with a locked/
subfolder that cannot be emptied (0555, from a foo/ and a foo-master/
archive) or opened (0000, from a foo/ archive): the danger alert naming
themes/foo, foo exactly as before, no staging, extracted or aside
folder left. Each case fails on f28f4c6 (the exception, or
UnexpectedValueException for 0000), and with the check removed, made
shallow or without its try/catch; the cross-device test fails if the
check always refuses. In the image, with a folder owned by root planted
in Sky, f28f4c6 threw with Sky stripped to that folder; this index.php
refuses with Sky md5-identical and nothing left beside it, and a plain
Sky still updates from a sky/ and a sky-master/ archive.
…rged over, as 3.7.1 did, never left half-deleted
…merged child by child; the destination-link check is pinned
…the update merges into it

A volume mounted on a module subfolder (a Docker -v on
themes/sky/css) does not stop the rename aside: the mount went with
the installed folder to <name>.old.<hex>.tmp/, the best-effort removal
emptied the volume there and left it mounted in the remnant, and the
owner saw success. After a remount the theme had an empty css/. Before
this change the own-name update extracted over the mount and kept it
filled.

Before the rename aside, a small private check, holdsMount(), walks the
installed folder's subfolders (links not followed) and compares their
device with the folder's own through a protected deviceOf() (stat()'s
dev; where stat() is disabled it finds no mount, as before). A folder
with a mount below it is not renamed aside: it takes the existing
cross-device path (canEmpty() first, the quiet delete, then the merge
of the new copy into what is left), so the mount stays where it is and
ends up holding the new files. A module folder that is itself a mount
point is unchanged (its rename is refused and nothing is touched).

mergeInto(): where a link at a file's destination cannot be removed,
the file is skipped and reported (the partial alert) instead of being
handed to rename(), which across a mount point copies through the link.
The docblock now says a link in the way is never written through.

Tests: deviceOf() stubbed so themes/foo/css is on a device of its own,
with a stuck subfolder keeping css/ in place through the delete as a
mount point does, from a foo/ and a foo-master/ archive: the success
alert alone, css/ the same folder (inode unchanged) holding the new
files, no aside, staging or extracted folder left. It fails on f25ea5a
with only the deviceOf() seam added (css/ moved aside, the new css/ a
different folder). On macOS, an HFS+ RAM disk mounted at themes/foo/css
through the real action, both archives: the same, the volume still
mounted at css/; then an undeletable link on the volume is reported by
the merge and its target left as it was. It is skipped where hdiutil is
absent (CI); it fails on f25ea5a (the mount moved into the remnant) and
with the new link skip removed (the copy wrote through the link).
…below it, keeps its place; the update merges into it

holdsMount() now also answers yes in two shapes the device walk could
not see, so the update skips the rename aside and takes the
cross-device path (canEmpty(), the quiet delete, mergeInto()), as it
already does for a volume on another device below the module:

- A volume mounted on the module folder itself (a Docker -v on
  themes/sky): every subfolder shares its device, and on macOS the
  rename aside took the mount along, emptied it in the remnant and left
  a plain folder with the new files. The folder's device is now
  compared with its parent's too.
- A bind mount below the module whose source is on the site's own
  filesystem: it keeps the device, and Linux renames a folder with a
  mount below it, so the mount travelled into the remnant. Where
  /proc/self/mountinfo can be read, any mount point at or under the
  module folder counts as a mount; field 5 is the mount point, its
  octal escapes (space, tab, newline, backslash) decoded in one pass.
  The file is read through a new protected mountInfo(), '' where there
  is no such file or it cannot be read (not Linux, open_basedir), so
  the device walk alone decides there, as before.

A refusal on that path comes before any move was tried (a read-only
volume), so its alert now begins "Could not update themes/<name>: it
cannot be written. Nothing was changed." instead of "Could not move
the installed version aside".

Tests: a stubbed deviceOf() puts themes/foo and everything in it on a
device of its own, a stuck subfolder keeping the folder in place
through the delete as a mount point does, from a foo/ and a foo-master/
archive: the success alert alone, themes/foo the same folder (inode
unchanged) holding the new files, nothing left beside it; then with a
read-only css/ the refusal with the new wording, nothing changed. It
fails on d7860ba (the folder moved aside). holdsMount() with a stubbed
mountInfo(): a mount point under the module and on the module folder
itself count, unrelated ones (/, /proc, the parent, a sibling sharing
the prefix) do not, an escaped space and a backslash before 040 decode
once; then the update with a bind mount listed at themes/foo/css from
both archives: css/ the same folder holding the new files.
@robiso robiso changed the title A module archive whose top folder is the module's own name is extracted beside the installed folder too ✅ A module archive whose top folder is the module's own name is extracted beside the installed folder too Nov 1, 2026
@robiso

robiso commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Greenlit after ten gate rounds (the maintainer allowed rounds beyond the usual three). Round 10: Reviewer 1 Fable 5.1 PASS LOW-only at 81e9e5f (a volume mounted at the module folder itself on a real HFS+ RAM disk: success, inode and device unchanged, new files on the volume; read-only volume: the new refusal, nothing changed; the mountinfo parse pinned through the seam, mutants M1 to M5 bite; head suite 318, merged 322; CI 16/16). Reviewer 2 Opus 5.5 (claude-opus-5-5) PASS LOW-only (the same volume shape end to end on both archive arms; 15 more mountinfo cases incl. a file bind mount, escapes, a 3000-line list; d7860ba moves the volume aside, the head does not). Earlier rounds, each closed by a fix and re-verified: r1 locked subfolder and staging-link test; r2 leftover aside blocking the next update; r3 destructive delete-first fallback on an immutable folder; r4 Docker overlay layers refusing every rename; r5 a partial delete on those layers; r6 a pre-check that cannot be complete (merge fallback added); r7 a folder that cannot cross a mount point and the destination-link check; r8 a volume mounted inside the module; r9 a volume on the module itself and a same-filesystem bind mount. Head: 81e9e5f. Open LOWs: the mountinfo comparison is not realpath()'d; an open_basedir that excludes /proc plus a plugin error handler that throws on silenced warnings crashes before any change (such a handler already breaks every save on 3.7.1); a deleted mount point below the module still counts; the known php -S port race in untouched backup tests. No money path, no UI change. Merge HELD for the maintainer's final panel on code size (panel 38), which may choose a smaller shape; this comment records the gate result for 81e9e5f.

@robiso robiso changed the title ✅ A module archive whose top folder is the module's own name is extracted beside the installed folder too A module archive whose top folder is the module's own name is extracted beside the installed folder too (closed: not shipped in 3.7.2) Nov 1, 2026
@robiso

robiso commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Closed without merging, by the maintainer's decision after the final twelve-seat review (option c, 8 of 12): #441 already protects every module on the official list and both documented custom-repository examples (GitHub archives); the two shapes this PR adds (a custom zip whose top folder is the module's own name; a host that disables filesize()) are stated limits in the 3.7.2 notes, admin-only and not security; and the 212 lines it grew to over ten review rounds, most of them for filesystem shapes a reviewer had to construct, are out of proportion for WonderCMS. The branch stays for reference; a 60-line version (staging folder plus measured entries, then the existing order) is the shape to take if the own-name case is ever reported by a site.

@robiso robiso closed this Nov 1, 2026
@robiso robiso mentioned this pull request Nov 1, 2026
@robiso
robiso deleted the fix/module-update-own-name-archive branch November 1, 2026 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant