Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get success when try to create a folder that already exists #44507

Conversation

vitormattos
Copy link
Contributor

@vitormattos vitormattos commented Mar 27, 2024

The PHP function mkdir return false when the directory already exists:

https://www.php.net/manual/en/function.mkdir.php#refsect1-function.mkdir-returnvalues

If the directory that we is trying to create already exists, would be good to return success and consider that the folder exists to prevent fatal error because the scenario about existing folder is not covered now.

How to reproduce this:

Try to create a folder that already exists.

Checklist

@solracsf solracsf added this to the Nextcloud 29 milestone Mar 27, 2024
@solracsf solracsf added the 3. to review Waiting for reviews label Mar 27, 2024
@vitormattos vitormattos force-pushed the fix/try-to-create-folder-that-already-exists branch from 3a21135 to 3817962 Compare March 28, 2024 02:35
The PHP function mkdir return false when the directory already exists:

https://www.php.net/manual/en/function.mkdir.php#refsect1-function.mkdir-returnvalues

If the directory that we is trying to create already exists, would be
good to return success and consider that the folder exists to prevent
fatal error because the scenario about existing folder is not covered
now.

Signed-off-by: Vitor Mattos <vitor@php.rio>
@vitormattos vitormattos force-pushed the fix/try-to-create-folder-that-already-exists branch from 3817962 to 177dde6 Compare March 28, 2024 02:36
@skjnldsv skjnldsv mentioned this pull request Mar 28, 2024
81 tasks
@skjnldsv skjnldsv added the bug label Mar 28, 2024
@@ -159,7 +159,9 @@ public function newFolder($path) {
$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
$this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
if (!$this->view->mkdir($fullPath)) {
throw new NotPermittedException('Could not create folder "' . $fullPath . '"');
Copy link
Member

Choose a reason for hiding this comment

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

Why not the opposite?
Check with is_dir first, try to mkdir after it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the most simple way to don't generate big impacts at the code. The original code generates an error, I only wrapped the error with a condition to prevent generate the error when the directory exists.

Thinking more, If we check if is a directory before make the directory, will be in the most cases, a more step. I think that is most fast only check if the path is a directory after the command mkdir return false. Create the directory is the main objective, if works fine, we don't do nothing, we only need to check if is a dir to prevent to generate a wrong error (reading, wrong error sounds strange 😂).

@solracsf
Copy link
Member

Should avoid things like these, where the folder indeed exists ond disk (race condtion?):

{
  "reqId": "7F6XqDortpC9wM1xLnRO",
  "level": 3,
  "time": "2024-03-30T09:19:11+01:00",
  "remoteAddr": "91.15.6.11",
  "user": "deol@gmail.com",
  "app": "index",
  "method": "GET",
  "url": "/apps/photos/api/v1/preview/3144334?etag=8b817782ce1c479d032685390f6bdac3&x=64&y=64",
  "message": "Exception thrown: OCP\\Files\\NotPermittedException",
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
  "version": "27.1.8.1",
  "exception": {
    "Exception": "OCP\\Files\\NotPermittedException",
    "Message": "Could not create folder \"/appdata_ocwfs52oscxg/preview/b/5/d/a/c/f/f/3144334\"",
    "Code": 0,
    "Trace": [
      {
        "file": "/var/www/nextcloud/lib/private/Files/AppData/AppData.php",
        "line": 147,
        "function": "newFolder",
        "class": "OC\\Files\\Node\\Folder",
        "type": "->",
        "args": [
          "b/5/d/a/c/f/f/3144334"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Preview/Storage/Root.php",
        "line": 74,
        "function": "newFolder",
        "class": "OC\\Files\\AppData\\AppData",
        "type": "->",
        "args": [
          "b/5/d/a/c/f/f/3144334"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Preview/Generator.php",
        "line": 613,
        "function": "newFolder",
        "class": "OC\\Preview\\Storage\\Root",
        "type": "->",
        "args": [
          "3144334"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Preview/Generator.php",
        "line": 139,
        "function": "getPreviewFolder",
        "class": "OC\\Preview\\Generator",
        "type": "->",
        "args": [
          [
            "OC\\Files\\Node\\File"
          ]
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Preview/Generator.php",
        "line": 116,
        "function": "generatePreviews",
        "class": "OC\\Preview\\Generator",
        "type": "->",
        "args": [
          [
            "OC\\Files\\Node\\File"
          ],
          [
            [
              64,
              64,
              false,
              "fill"
            ]
          ],
          "video/mp4"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/PreviewManager.php",
        "line": 192,
        "function": "getPreview",
        "class": "OC\\Preview\\Generator",
        "type": "->",
        "args": [
          [
            "OC\\Files\\Node\\File"
          ],
          64,
          64,
          false,
          "fill",
          null
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/photos/lib/Controller/PreviewController.php",
        "line": 162,
        "function": "getPreview",
        "class": "OC\\PreviewManager",
        "type": "->",
        "args": [
          [
            "OC\\Files\\Node\\File"
          ],
          64,
          64
        ]
      },
      {
        "file": "/var/www/nextcloud/apps/photos/lib/Controller/PreviewController.php",
        "line": 128,
        "function": "fetchPreview",
        "class": "OCA\\Photos\\Controller\\PreviewController",
        "type": "->",
        "args": [
          [
            "OC\\Files\\Node\\File"
          ],
          64,
          64
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 230,
        "function": "index",
        "class": "OCA\\Photos\\Controller\\PreviewController",
        "type": "->",
        "args": [
          3144334,
          64,
          64
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 137,
        "function": "executeController",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          [
            "OCA\\Photos\\Controller\\PreviewController"
          ],
          "index"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/AppFramework/App.php",
        "line": 183,
        "function": "dispatch",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->",
        "args": [
          [
            "OCA\\Photos\\Controller\\PreviewController"
          ],
          "index"
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/private/Route/Router.php",
        "line": 315,
        "function": "main",
        "class": "OC\\AppFramework\\App",
        "type": "::",
        "args": [
          "OCA\\Photos\\Controller\\PreviewController",
          "index",
          [
            "OC\\AppFramework\\DependencyInjection\\DIContainer"
          ],
          [
            "3144334",
            "photos.preview.index"
          ]
        ]
      },
      {
        "file": "/var/www/nextcloud/lib/base.php",
        "line": 1068,
        "function": "match",
        "class": "OC\\Route\\Router",
        "type": "->",
        "args": [
          "/apps/photos/api/v1/preview/3144334"
        ]
      },
      {
        "file": "/var/www/nextcloud/index.php",
        "line": 38,
        "function": "handleRequest",
        "class": "OC",
        "type": "::",
        "args": []
      }
    ],
    "File": "/var/www/nextcloud/lib/private/Files/Node/Folder.php",
    "Line": 162,
    "CustomMessage": "Exception thrown: OCP\\Files\\NotPermittedException"
  },
  "id": "660998a6ae1ee"
}

@vitormattos
Copy link
Contributor Author

Could not create folder "/appdata_ocwfs52oscxg/preview/b/5/d/a/c/f/f/3144334"

Was exactly this the error that I fixed.

This was referenced Apr 4, 2024
@blizzz blizzz modified the milestones: Nextcloud 29, Nextcloud 30 Apr 8, 2024
Copy link
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

@vitormattos
Copy link
Contributor Author

Closing my PR after then my solution was implemented at PR: #45413

@skjnldsv skjnldsv removed this from the Nextcloud 30 milestone Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants