Skip to main content

Folders a library may use

Not released yet (comes after 1.7.0)

apps/backend/api/photos/allowed_folders.py answers which folders may be a scan directory, an upload folder or a further library folder. There are two modes, set by settings.LIBRARY_ON_ANY_DRIVE:

ModeWhereAllowed
offDocker, development, and the desktop program with --photos or PHOTOSDATA_ROOT and what is in it
onthe desktop program otherwise (settings/standalone.py)the home folder and every other connected drive, without the closed folders below

With the mode on, DATA_ROOT is the home folder. It stays the place for what has no folder of its own: Nextcloud downloads (nextcloud_media_root), the folders AUTO_CREATE_USER_DIRECTORY makes, and the storage figures of a user whose own folder is not reachable.

Functions​

  • top_folders(): the tops of the folder tree. One (DATA_ROOT) with the mode off; otherwise Pictures, the drives, Desktop, Documents, Downloads and the home folder. Each has a kind, which the picker turns into an icon and a translated name.
  • place_problem(path): whether the folder's place is allowed. It never reads the folder.
  • folder_problem(path): the same, and then whether the folder is there and can be read.
  • reading_problem(path): only whether the folder is there and can be read. The empty timeline asks this one, because an older library may be in a place that is not offered any more and still be fine.
  • problem_without_waiting(folder) (api/photos/folder_answer.py): the same for an endpoint that is polled, GET /api/library/preparation/. A dead network drive can block a look into it for minutes. So one thread per folder looks, the request waits for at most a second, and then answers with what is known from the last look. After 15 seconds without an answer that is drive_not_connected. verification/scanning/FolderAnswer.tla checks this.
  • problem_detail(problem, label): the API error, with the problem as its code: folder_missing, folder_not_allowed, folder_unreadable, folder_blocked_by_macos, drive_not_connected or not_a_folder. The web app shows its own text for each code.
  • UnpluggedDrives().holding(path): the drive a path is on when that drive is not connected, otherwise None. It looks up the drives once, when it is made, so make one for each batch of paths, after their files were looked for. It answers None for every path with the mode off. Ask it before a file that cannot be found is taken for a deleted one, or ask LibraryDrives, which also knows another drive under the same letter or name.

Which drive it is​

api/photos/library_drives.py answers whether a path that cannot be found is away with its drive. library_drives(user) is None with the mode off. LibraryDrives.away(path) answers None when the file was really deleted, otherwise the place and whether a drive is connected there. The missing-file check, Remove missing photos and removed_paths (MountWatch), the permanent delete (remove_photos) and the answers for an original that cannot be opened use it.

  1. A path on the system drive is never away.
  2. A drive that is not connected is away.
  3. A library folder with a remembered drive is away when another drive is there now, and not when it is the same one. Then a file that cannot be found was deleted.
  4. Without a remembered drive, or when its label changed, same_drive answers None and the drive is undecided (LibraryDrives.undecided). Then a file that is there is not away. A file that is not there is away unless at least one witness is found: a photo LibrePhotos knows from that place. This holds for the current library folder too.
  5. The place is the library folder that holds the path. Outside the library it is the top folder on that drive. Its witnesses are at most 200 files, photos that are not in the trash first. Photos added after LibraryDrive.seen_at are none, so pictures that a scan imported from a foreign stick do not vouch for it. When the same stick was formatted, or every photo of the place was deleted, this says "away", and the photos stay. Setting the limit to 100% removes them.

An accepted gap of rule 4: an undecided drive that holds a file with the name of a known photo at the same place counts as the photo drive. This only concerns folders that are not in the library any more. A library folder is looked at more closely before the check. The Missing photos limit is the guard then.

The witnesses are read from the database once for a LibraryDrives. refresh() looks at the connected drives, their identities and the witnesses' files again, and keeps the list. forget(paths) takes files out of it that were deleted on purpose. An "away" that came from the witnesses stays for the life of the object, so a job never changes its mind to the unsafe side.

LibraryDrive (migration 0274) holds the volume and the label of the drive of each library folder a user has or had. An empty volume means that the identity could not be read or was not confirmed yet. seen_at is the moment until which added photos are known to be from the right drive. drive_identity(drive) in api/runtime/drives.py reads them: the volume serial number and the label on Windows (GetVolumeInformationW), the volume UUID on macOS (getattrlist). macOS 15 makes up a random UUID for a FAT drive each time it is mounted again, so a random UUID (version 4) only counts on APFS, which stores it on the drive; other drives then count as not remembered. The serial number of an ExFAT drive that was formatted on a Mac is the second it was formatted in, so the Mac test makes its two disk images two seconds apart. st_dev is not stable on macOS. On Linux it answers None. No marker file is written to the drive, reading the identity makes macOS ask nothing, and the values are not in logs, job results, error reports or API answers.

The missing-file check learns the identity at the end of its tally (remember), when the drive was the same before and after:

  • The same volume with another label is confirmed when more than half of the known files of the folder were found.
  • Any other volume, and the first one, is confirmed only when no file of the folder is gone and one has the known content. A backup with most of the photos never becomes the photo drive, because the next check would mark the rest missing. When a photo was really deleted from a drive that is not remembered yet, this check marks it missing and the next one learns the drive. A file that exists but cannot be read counts as neither found nor gone.
  • With the limit at 100% the drive that is there is accepted.

An identity that is known is never replaced by an unreadable one. before_import() runs at the start of a scan, before files are looked for. It writes seen_at for a folder that has no row or no known photo yet, so the check at the end can tell the photos from before the scan from the ones it added.

Deleting on a drive that is not certain​

On an undecided drive remove_photos reads the files before it deletes them (_first_look, api/photos/known_files.py). recognise compares each media file with the content hash in File.hash. A file also counts when LibrePhotos itself wrote it last: its time is File.indexed_mtime_ns and later than Photo.added_on. A sidecar counts when it lies next to a recognised media file, because other programs change sidecars all the time. A file that is not recognised raises UnknownFileError, a DriveAwayError: the photo stays in the trash and counts in Removal.unrecognised (unrecognised_count in the job result and in the answer of DELETE /api/photosedit/delete/). A file another program changed stays until a scan has read it again.

Right before the rename, the device, the size and the time of the file have to be the ones that were read (_refuse_unrecognised), and a file that was read and is not there any more is not taken for a deleted one (_refuse_vanished). An accepted gap: the exact same file at the same path on another undecided drive is deleted.

After the commit _delete_staged removes the files that were renamed aside, with their journal rows. A row whose file is not there is only dropped when its drive is (_drive_is_here). With another stick under the letter it waits, so no *.librephotos-deleting file is left behind for good. The whole job, and the whole sweep, share one LibraryDrives per owner, because looking up the witnesses takes about 0.4 seconds with 100,000 photos. refresh() makes it look at the drives again without that.

verification/scanning/ScanMissing.tla (DriveRule, LearnDrive) and verification/trash/TrashDriveAway.tla are the models.

A scan reads only the drive with the photos​

A scan of a foreign stick under the letter of the library would import its pictures. For a file with the name of a known photo, rekey_replaced_file would even move the photo onto the stranger's content and drop its faces, thumbnails, things and text. So scan_photos asks OtherDrives (api/directory_watcher/other_drives.py) first. A library folder on a drive that is not confirmed is left out completely, with the notice "... looks like another drive, not the one with your photos; nothing was scanned there. Plug in the drive with your photos and scan again." This is so for the whole library, one folder and chosen files. The other library folders are scanned. Such a scan is not the baseline of the next one. The missing-file check and the check of the files' contents leave the folder out too, and the scan buttons and uploads answer with drive_not_connected (api/photos/missing_library.py).

LibraryDrives.confirmed(folder) decides:

Drive that is there nowConfirmed when
the remembered onealways
nothing remembered, or the label changedat least one known file is there and has the known content
another oneall known files are there, and at least one has the known content
any, and the folder has no known photoalways

photos_found (api/photos/known_photos.py) takes at most 200 known files and reads at most the 20 smallest that are there. It stops at the first one with the known content. A file that is older than the photo in the library and has another content is a stranger's, and then the answer is no. When every file that was read is newer than its photo, another program may have changed all of them. Then more than half of the names are enough.

So no library is stuck:

  • A first import: the folder has no known photo, so it is always confirmed. Its drive is remembered before the first file is read.
  • A library from before 1.8.0: nothing is remembered. One known file with the known content confirms it at each scan. A file that cannot be read is passed over. The first scan remembers the drive when all known files are there. When some were deleted, the missing check marks them missing and the next scan or check remembers it.
  • A drive without a lasting identity, such as a FAT drive on macOS 15: the known content confirms it at each scan.
  • Every other case: an admin sets the Missing photos limit to 100%, scans once, and sets it back.

With the Missing photos limit at 100% every drive is confirmed. remember_drives remembers the confirmed drive before anything is read, so each group of files can ask again (swapped_meanwhile in handle_file_group). When another drive was plugged in since, the group counts as failed and the next scan reads it again. An accepted gap: drives without a lasting identity that are swapped while a scan runs.

verification/scanning/ScanOtherDrive.tla is the model.

Nothing is written to another drive​

A file with the name of a known photo can also be on a stranger's stick, so "the file is there" is not enough before LibrePhotos writes to it, moves it or shows it as the photo. on_wrong_drive(owner_id, path) (api/photos/right_drive.py) is the one question for that. It answers True when the drive of the path is not connected, or is another one than the remembered drive. A full copy is another drive too, until a scan, the switch or the Missing photos limit at 100% accepts it. Without a remembered identity a known file with the known content decides.

Asked beforeWith the wrong drive
a write of metadata to the file or its sidecar (refuse_wrong_drive in api/metadata/waiting_writes.py)DriveNotConnectedError; the edit stays in LibrePhotos and is written when the drive is back
a move or a rename, and the folder to move to (files_to_move, move_photo_files, resolve_target, the sweep of interrupted moves)the photo stays where it is and counts as failed, the folder is refused, an interrupted move waits
the editor, the original, a ZIP file, a copy for others, all metadata, Why this date? and Restore from filethe answer names the drive, or the file counts as not found
a thumbnail made from the originalthe thumbnail fails as it does without the file
text recognition (ocr_image_source)it reads the big thumbnail
face regions in the file, faces in the frames of a video, burst tags, the shutter speed read again, a video converted ahead of timethey are read as when the file cannot be read

What the answer says. drive_problem(photo) and wrong_drive_code(owner_id, path) give the code: drive_not_connected when no drive is there, another_drive when one is there but not the one with the photos. Media files carry it in X-Media-Error. The text for another_drive asks for the drive with the photos, and says to scan the photos once after they were copied to a new drive: the scan confirms a copy with all photos and remembers that drive (remember_drives). The web app has its own texts for both (driveProblem in apps/frontend/src/api_client/media/mediaUrl.ts).

What it costs. With the mode off and for a file on the system drive it answers at once, without a query. For any other drive one look answers for all photos of a user for one second (LOOK_SECONDS): a few small queries (the user, the library folders, the remembered drives) and one question to the system for the kind of the drive. A network drive is left alone after that, so it costs those queries per second of use and its server is not asked. Any other drive is asked for its identity once per look. Where the content has to decide, that answer stays for a minute while the same volume is mounted (CONFIRMED_SECONDS). The answer is kept per process. Another drive that takes the place of the right one within that second is an accepted gap. A drive that is pulled out during a write is noticed, because the failed write asks again (noticing_a_drive_pulled_out).

Edits that wait. Ratings and keywords have their own pending state. A drive that is away does not count as one of their five attempts. Every other field (date, caption, location, rights, rotation, face tags) leaves a WaitingWrite row (migration 0275), one per photo and field. The row holds no value: the write takes what the photo has when it runs. write_waiting_edits queues the rows whose drive is there, hourly, and at once when the drive watch finds the drive or switches to it (write_what_waited_for_the_drive, which runs the sweeps of ratings and keywords too). A run takes 5000 rows. A full run queues the next one, which goes on behind the last row, so a run never takes a row again and ends when the drive is away. The row goes away when its write was done, when the owner turned Write edits to files off, or after five writes that ExifTool refused on the right drive. Its token (edit) changes with each new refusal, so a retry never deletes the row of an edit that came to wait meanwhile.

A job loads up to 500 photos at once, and the user can edit one of them before its turn comes. So write_holding_file reads the columns that are written (WRITTEN_COLUMNS in api/metadata/file_claim.py) again when it takes the file. Every other value is read by its writer after that.

A turn is written once. In a media file a turn goes on top of the orientation the file has, so writing it twice turns the photo twice. repeatable (api/metadata/photo_writer.py) says so, and the retry deletes such a row before the write. It puts the row back when ExifTool refused (wait_again). When something fails after ExifTool wrote, the error is an AfterTheWriteError, and nothing waits. When the program is killed in between, the turn stays in LibrePhotos and may be missing in the file.

verification/metadata/WaitingWrite.tla is the model.

When the drive comes back​

api/directory_watcher/drive_watch.py is one thread in the web server of the desktop program (standalone.py, _say_when_running). It never starts in Docker or with --photos.

run asks every 5 seconds which volumes are mounted where (drive_places): the letters with GetVolumeNameForVolumeMountPointW on Windows (only the letter for a network drive, because that call waits about 20 seconds for a server that is off), getfsstat without waiting on macOS. The system answers this from memory, so no disk wakes up and no server is asked. It only looks further when the answer changed and stayed the same for one round. DriveWatch.look then reads the identity of the connected drives and looks at the libraries (places_by_drive).

It does so again once a minute only while a switch has to wait for a job, a scan could not start, or the last look failed, for example because the drive was not ready. While a library drive is away, the minute question goes only to drives that have a letter but did not answer (silent_drives): one that BitLocker still locks, or an empty card reader. A card that is changed in a reader that keeps its letter is not noticed until the next change of drives, or a scan.

Network drives (Drive.remote) are never asked, neither for their identity nor for files, so a server that is off cannot block the thread and its lock. A library on one is not followed to another letter. The scheduled scan and the scan button read it as before.

For each drive with a library:

  • The library is at its place (places_here): the drive is the remembered one, or nothing is remembered and it is confirmed as below. Another drive with a copy of all photos is not enough here, so the real one is found under another letter. The identity is remembered when there was none (learn_drives). When the drive just came back, _start_scans starts the normal scan of each user whose folder is confirmed, unless a photo scan ended well in the last 5 minutes. A running scan is waited for.
  • It is not: find_drive looks at the other connected drives. Every library folder of every user on the old drive has to be there and readable, and one of its known files has to have the known content. How many known files have to be at their place depends on the drive (ENOUGH): one on the remembered drive, so it is found after the person tidied up elsewhere; more than half when the label changed or nothing is remembered; all of them on a drive with another identity. A folder without photos, such as an upload folder nobody used yet, may be missing and need not be remembered.
Found driveWhat happens
one with the remembered identityswitch_drive at once
the label changedoffered
nothing remembered, all photos thereoffered
nothing remembered, more than half of the photos thereoffered as a copy, with the number of photos that are not there; when it is taken, its identity is not remembered until no file is gone
another identity, with all photosoffered as another drive; it does not stand in the way of the remembered one
two with the remembered identity (a cloned disk)both offered
nonenothing; the photos count as away

switch_drive (api/photos/drive_switch.py) is one transaction. It takes the lock that every job start takes (lock_all_starts), so no job starts meanwhile, and refuses while a job is queued or running. It takes the library folder lock, reads the folders again and refuses when they changed, or when the new place is already in a library. Then it replaces the drive in File.path, FileCopy.path, StagedDeletion, PendingMove and PendingMoveFile, the user's scan, upload and excluded folders, LibraryFolder.path and LibraryDrive, for the files below the moved folders only. LibraryMount rows are dropped, the next scan writes them. A new column that holds a path below a library folder has to be added there. A former library folder moves along when LibraryDrive says that it was on the same drive; other paths on the old letter stay as they are. PhotoSearch.search_captions keeps the old letter until it is written again.

GET /api/photo-drive/ (admins) answers what to show, POST with the path of an offer takes it after looking again and refuses when the drive there is not the offered one any more, and DELETE closes the message. The offers and the message are only in memory. The answers hold the letter or name of a drive, never its identity. PhotoDriveNotice asks every POLL_PHOTO_DRIVE_MS, and only once where nothing is watched.

verification/scanning/DriveSwitch.tla is the model.

Where the places come from​

The drives (api/runtime/drives.py) and the user's folders (api/runtime/user_folders.py) are looked up for every request. A drive that was plugged in a second ago can be chosen, and one that was pulled out is refused.

SystemDrivesUser's folders
Windowsevery drive letter but the system drive, named by its label; a network drive by its share, without asking its serverthe registry (User Shell Folders), so a folder moved to OneDrive is found
macOSthe mounted folders in /Volumesthe English folder names, which macOS uses on disk in every language
Linux/proc/self/mounts in /media, /run/media and /mnt~/.config/user-dirs.dirs

The folder tree​

GET /api/dirtree/ without a path answers the tops. With the mode on it reads into none of them: on macOS each first look into Desktop, Documents, Downloads or a drive makes the system ask the user. path lists one folder, open answers the tops with the folders on the way down to one folder listed. Each folder that cannot be listed has its own code and is not answered as an empty one.

The picker (apps/frontend/src/components/setup/useFolderTree.ts) builds its rows from three answers: the tops, the tree opened to the chosen folder, and one listing for each opened folder. It asks for the tops every POLL_DRIVES_MS. When they changed, it asks for everything else again. A refused folder is asked for again after the same time, so the message goes away once the user has allowed or plugged in something. verification/web-ui/FolderPicker.tla checks this.

The Folders page​

GET /api/folders/subfolders/ always opens a user's own library, also for an admin whose library is a link out of the home folder. An admin may open every other allowed folder too. When the drive with the folder is not connected, the answer is a 403 with the code drive_not_connected, and the Folders page and the Folders card under Albums say so (code of useListFailure).

Security​

Choosing a library lets LibrePhotos read the folder, show its photos to its owner, and write to it (uploads, edits, deleting).

  • Only admins list folders, make folders and choose a library (IsAdminUser). Someone who runs the program with --host 0.0.0.0 and has not finished the setup lets everyone on the network become that admin. That was so before and already opened the home folder; now it also opens the connected drives. Finish the setup before you open the program to the network, or start it with --photos.
  • Closed, as written and by any other way to them: the data folder of LibrePhotos (database, secret key, thumbnails), its program folder, and on a drive with another system on it the folders of that system. _is_closed compares the file system's own identity of the folder and of every folder above it, so a short name, another case or a link does not get around it.
  • A path is made absolute first, so .. does not lead out. A symlink or junction has to end in an allowed place too.
  • On Windows a path has to start with a drive letter that is offered. \\server\share and \\?\ are refused, so nobody can make the program contact a server by typing one.
  • AppData and ~/Library are not listed but may be typed, because cloud drives keep their folders there.
  • A scan of the home folder leaves out the folders of LibrePhotos (_own_data_in in api/directory_watcher/scan_jobs.py).

Tests​

api/tests/photos/test_allowed_folders.py, test_drive_away.py, api/tests/scanning/test_missing_on_another_drive.py, api/tests/infra/test_dirtree_any_drive.py, test_drives.py, test_user_folders.py, test_standalone_library_places.py and api/tests/scanning/test_missing_on_unplugged_drive.py in the backend, with a made-up computer (api/tests/infra/any_drive.py); src/components/setup/DirectoryPicker.test.tsx in the web app.