Skip to content

Address file permission issues in new directories created by WordPress on Windows - #66

Merged
fluiddot merged 2 commits into
trunkfrom
fix/windows-file-permissions
May 7, 2024
Merged

Address file permission issues in new directories created by WordPress on Windows#66
fluiddot merged 2 commits into
trunkfrom
fix/windows-file-permissions

Conversation

@fluiddot

@fluiddot fluiddot commented Apr 30, 2024

Copy link
Copy Markdown
Contributor

Related to #45.

Proposed Changes

Patch PHP runtimes to return correct file permissions on Windows, specifically focusing on the doStat function, which acts as the handler when invoking fs.stat, fs.lstat, and fs.statfs. The concept behind this patch is to replicate a similar workaround applied in Emscripten for NODEFS (commit reference) to deduce the missing executable bit (x) on Windows, by leveraging the read bit (r).

This adjustment will ensure that WordPress accurately retrieves the file permissions from the parent directory when creating different directories during media uploads (reference). Currently when getting the file permissions of a directory on Windows via PHP's stat function, it returns 666. The fact that the executable bit is missing (x), leads to WordPress not being able to access the directories created although it has permission.

Note

Note that the adjustments need to be made for every supported PHP version.

Testing Instructions

Windows

  • Open the Windows version of the app. It can be obtained from the CI jobs of this PR or built on a Windows machine.
  • Create a site.
  • Navigate to WP-Admin -> Media.
  • Upload an image.
  • Observe it uploads successfully.

macOS

  • Open the macOS version of the app. It can be built by running npm start on a macOS machine.
  • Create a site.
  • Navigate to WP-Admin -> Media.
  • Upload an image.
  • Observe it uploads successfully.

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
Instead of modifying the `chmod` and `mkdir` functions, the handler used when invoking `stat` is updated to ensure it returns correct permissions on Windows. This approach mirrors a workaround applied in `NODEFS.getMode`, where the execution bit is represented based on the read permission.
Reference: https://github.com/emscripten-core/emscripten/blob/63c648fa17be49fa9640d4a40c8ba2f7a9b2d444/src/library_nodefs.js#L71-L75
@fluiddot fluiddot self-assigned this Apr 30, 2024
Comment on lines +41 to +52
@@ -29535,6 +29550,11 @@ function init4(RuntimeName, PHPLoader) {
doStat: function(func, path, buf) {
try {
var stat = func(path);
+ if (NODEFS.isWindows) {
+ // Node.js on Windows never represents permission bit 'x', so
+ // propagate read bits to execute bits.
+ stat.mode = stat.mode | (stat.mode & 292) >> 2;
+ }
} catch (e) {
if (e && e.node && PATH.normalize(path) !== PATH.normalize(FS.getPath(e.node))) {
return -54;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This corresponds to PHP version 8.0, the one that is used by default in the Studio app.

@fluiddot
fluiddot requested a review from a team April 30, 2024 11:42
@fluiddot
fluiddot marked this pull request as ready for review April 30, 2024 11:42
@fluiddot fluiddot mentioned this pull request Apr 30, 2024
@fluiddot fluiddot changed the title Fix file permissions on Windows Apr 30, 2024
@wojtekn

wojtekn commented May 2, 2024

Copy link
Copy Markdown
Contributor

Thanks for the fix @fluiddot ! It works well for me on macOS. I didn't test on Windows, though.

Should we implement this fix in the wordpress-playground repository?

@sejas sejas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, great investigation in finding the permissions error! 👏
Yes, I think we should create a PR for php-wasm/node so the broader community can benefit from this fix.

@fluiddot

fluiddot commented May 6, 2024

Copy link
Copy Markdown
Contributor Author

Should we implement this fix in the wordpress-playground repository?

Yes, although ideally, we should apply the fix directly in the Emscripten repository. This way we'll ensure all PHP versions will have the fix. I'll work on a PR for this.

In the meantime, I can apply the same workaround in php-wasm/node package of wordpress-playground.

Yes, I think we should create a PR for php-wasm/node so the broader community can benefit from this fix.

Yes, I agree. This will address issues on Windows when using wp-now and VSCode extension, as reported in WordPress/playground-tools#121.

@fluiddot

fluiddot commented May 7, 2024

Copy link
Copy Markdown
Contributor Author

I created a PR in Emscripten to solve this problem as a long-term solution. It might take some time until it lands in a new version, so as shared here, I'll work on a fix in Playground. Once that's ready and merged, we could create another PR for the Studio app and remove this patch. That said, since this PR solves the issue, I'm merging it.

@fluiddot
fluiddot merged commit 9a4424b into trunk May 7, 2024
@fluiddot
fluiddot deleted the fix/windows-file-permissions branch May 7, 2024 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants