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

path.py - fix system cannot move file to different drive #5807

Merged
merged 2 commits into from
Jun 29, 2024
Merged

path.py - fix system cannot move file to different drive #5807

merged 2 commits into from
Jun 29, 2024

Conversation

AyluinReymaer
Copy link
Contributor

When the gallery-dl config file is set to download the temporary files into one disk drive and then the base-directory set in extractor is set to another drive, the way path.py copies the file to the target destination like this on line 340, it fails with the below error:

OSError(18, 'The system cannot move the file to a different disk drive')

Since this is an OSError, it will retry on line 347.

This will cause yet another error:

Unable to download data: FileNotFoundError: [Errno 2] No such file or directory

To resolve this, we attempt to create the folder first before doing any move operations with the exists_ok=True flag so that in case the folder already exists it doesn't throw an exception.

sample config:

{
    "downloader": {
        "rate": "5M",
        "retries": 3,
        "timeout": 10.0,
        "part-directory": "./data/.tmp_download/",
        "mtime": false,
        "http": {
            "adjust-extensions": true
        }
    },
    "extractor": {
        "base-directory": "W:/gallery-dl/files",
        "archive": "./data/archives/{category}.sqlite3",
        "archive-prefix": "",
        "sleep": 1,
        "skip": "abort:20",
        "deviantart": {
            "client-id": null,
            "client-secret": null,
            "auto-watch": false,
            "auto-unwatch": false,
            "comments": false,
            "extra": false,
            "flat": true,
            "folders": false,
            "group": true,
            "include": "gallery",
            "journals": "html",
            "mature": true,
            "metadata": false,
            "original": true,
            "wait-min": 0
        }
    }
]

This fixes the below error message:

OSError(18, 'The system cannot move the file to a different disk drive')
@mikf
Copy link
Owner

mikf commented Jun 28, 2024

On non-Windows systems, exception order is reversed (first FileNotFoundError -> create directory, then OSError: [Errno 18] Invalid cross-device link -> shutil.copyfile) and everything works as it should.

@mikf
Copy link
Owner

mikf commented Jun 28, 2024

diff --git a/gallery_dl/path.py b/gallery_dl/path.py
index 1616bbda..4b4d405b 100644
--- a/gallery_dl/path.py
+++ b/gallery_dl/path.py
@@ -344,7 +344,11 @@ class PathFormat():
                     continue
                 except OSError:
                     # move across different filesystems
-                    shutil.copyfile(self.temppath, self.realpath)
+                    try:
+                        shutil.copyfile(self.temppath, self.realpath)
+                    except FileNotFoundError:
+                        os.makedirs(self.realdirectory)
+                        shutil.copyfile(self.temppath, self.realpath)
                     os.unlink(self.temppath)
                 break
 

This calls os.makedirs only when necessary.

@AyluinReymaer
Copy link
Contributor Author

That also works, also tested on my side.

I've committed the new changes like you've advised, hopefully this should also work on non-windows hosts, though I don't have a way to test it unfortunately

@mikf mikf added the bug label Jun 29, 2024
@mikf mikf merged commit 1ca2cf5 into mikf:master Jun 29, 2024
9 checks passed
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.

2 participants