Skip to content

Ensure that media files have the correct permissions (read-write) #238

@aplaice

Description

@aplaice

I believe that the issue which I encountered is that, with flatpak-installed Anki, if one CrowdAnki-imports a deck with media, then (depending on the location of the file source), these files might be read-only (from the PoV of flatpak), so when copying them into Anki's collection.media/, they're kept as read-only.

This leads to an issue when re-importing, since then Anki/CrowdAnki can't overwrite the files.

To reproduce

(On Linux)

  1. Using flatpak-installed Anki (25.09.2) import a deck (containing media) using CrowdAnki: import from disk, from a folder within ~/Downloads/.
  2. (Optionally) Check the file permissions of the newly imported media (the media should be at ~/.var/app/net.ankiweb.Anki/data/Anki2/User 1/collection.media/)
  3. Re-import the same deck using CrowdAnki.

Expected behaviour

  1. The deck is imported.
  2. The media are rw.
  3. The deck is re-imported.

Actual behaviour

  1. The deck is imported.
  2. The media are read-only (r).
  3. Re-import causes a crash:
Details
Anki 25.09.2 (3890e12c) (ao)
Python 3.12.11 Qt 6.9.3 PyQt 6.9.1
Platform: Linux-6.8.0-84-generic-x86_64-with-glibc2.40

Traceback (most recent call last):
  File "/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/addons21/1788670778/anki/ui/action_vendor.py", line 32, in <lambda>
    lambda: AnkiJsonImporter.import_deck(self.window.col, self.directory_vendor))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/addons21/1788670778/importer/anki_importer.py", line 112, in import_deck
    AnkiJsonImporter.import_deck_from_path(collection, Path(directory_path))
  File "/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/addons21/1788670778/importer/anki_importer.py", line 101, in import_deck_from_path
    if importer.load_deck(directory_path):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/addons21/1788670778/importer/anki_importer.py", line 42, in load_deck
    self.import_media(directory_path)
  File "/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/addons21/1788670778/importer/anki_importer.py", line 56, in import_media
    shutil.copy(full_filename, self.collection.media.dir())
  File "/usr/lib/python3.12/shutil.py", line 435, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.12/shutil.py", line 262, in copyfile
    with open(dst, 'wb') as fdst:
         ^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/home/adam/.var/app/net.ankiweb.Anki/data/Anki2/User 1/collection.media/ug-flag-tajikistan.svg'

===Add-ons (active)===
(add-on provided name [Add-on folder, installed at, version, is config changed])
CrowdAnki JSON exportimport Edit history Collaborate on deck creation ['1788670778', 2023-10-30T19:57, 'None', '']

===IDs of active AnkiWeb add-ons===
1788670778

===Add-ons (inactive)===
(add-on provided name [Add-on folder, installed at, version, is config changed])

(NB the same error occurs for CrowdAnki from AnkiWeb and from git.)

Further info

I have not tested with latest Anki not from flatpak because I can't easily get it to run (according to Anki, Ubuntu LTS 22.04 is apparently not "a modern Linux distro" :/), so my belief that this is linked to flatpak is only a strong suspicion.

Some solutions:

  1. run os.chmod afterwards
  2. Just use shutil.copyfile, since shutil.copy is basically copyfile(...) + copymode(...) and we simply don't want the second part. (What are the "default" permissions?) (OTOH I'm not sure how this would mesh with non-Linux platforms (default permissions there).)

Later aside: export with flatpak also doesn't work, at all, due to create directory permission errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions