Skip to main content

Moving and renaming photo files

Not released yet (comes after 1.7.0)

A move (Move to folder…) and a rename (Rename files…) both go through api/photos/files.py _carry_out. Each photo moves all its files (files_to_move: the main file, RAW or JPEG variant, Live Photo video, XMP sidecars) or none of them.

Never overwriting a file​

The free-name check and the move are two steps, so another writer can put a file at the name in between. Every file step therefore goes through api/photos/no_replace.py, which uploads use too (api/photos/upload_copies.py):

  • place(source, dest) gives the file its new name without replacing one: os.link, which fails when the name exists; where hard links are not available (FAT, some network shares, a symlink) it creates the name with open(dest, "xb") and renames the file over that empty placeholder with os.replace. Only an error that says the file system cannot link (cannot_link: EPERM, EACCES, EOPNOTSUPP, ENOTSUP, EMLINK, ENOSYS, or on Windows the errors 1, 5, 50, 87 and 1142) takes that path; a missing source (ENOENT) or any other error is raised with nothing left at the target. When the rename fails (a Windows sharing violation from an antivirus or indexer), the original error is raised and the placeholder is removed when it can be: always while the source still exists, and otherwise while it is still empty; an empty name that stays behind is skipped as taken next time.
  • move_without_replacing(source, dest, staging, held) is place plus removing the source name. A target on another file system (EXDEV: a second disk, per-folder Docker bind mounts, mergerfs or Unraid pools) is copied to the staging name in the target folder (.move-<uuid>.part) in 16 MiB chunks, renewing the lease (held) before each chunk, fsynced, given the source's modification time and permissions, placed without replacing, and only then is the source removed. Before the source is removed, held asks whether the move still holds its pending-move row and its file claim; when it does not, the new name is taken away again and the move stops. On Linux and macOS the target folder is fsynced before the source name goes. On Windows a read-only source is made writable for its unlink, and the target gets the read-only attribute back; when the unlink still fails (another program holds the file open), the source gets its attribute back and the target name is made writable and removed, so only the source name stays.
  • The cleanups only delete a name while the other name still exists, so no failure deletes the only copy of a file. A staging copy that is the only copy left (the source vanished meanwhile) is kept and logged. Staging names start with a dot, so the scan skips them.

Planning a move or rename (is_taken) counts a file on disk, a File or FileCopy row and the destinations of every pending move as taken. When another pending move takes a destination between the plan and the journal (the destination is unique), the move plans again and picks the next free name (or skips the photo with "skip"), up to three times.

The file claim​

Not released yet (comes after 1.7.0)

A move or rename takes the photo's file claim (api/metadata/file_claim.py, Photo.file_writer), which every ExifTool write to the photo's file or sidecar and the rescan read take too, before it lists the files to move (_holding_file in api/photos/files.py). Its token is the pending move's holder token and its lease the move's 10 minutes, renewed with the journal (Journal.renew); it is released in the transaction that records the paths (Journal.end). A move that leaves its pending move behind keeps the claim, so no write reaches the files until the sweep settles them. The sweep moves the claim to its own token when it takes the row over (take_over_file), and leaves the row for the next run when another writer holds the claim.

When the claim is busy the photo is not moved (FileBusyError). The "Move Photos" and "Rename Photos" jobs, and a move or rename within the request, queue the busy photos for the end of the claim (moves.requeue_busy_moves, renames.requeue_busy_renames, through enqueue_task_at with a dedupe key made of the photo, the claim's end and a hash of the destination or pattern, so two requests for one photo both wait); that task starts a new job for them, or waits a minute while a move, rename or scan of the owner runs. A request of up to 100 photos first checks the claims and answers 409 file_busy when one is held, so nothing moves. The counts of a move or rename (the answer and the job's result) say how many photos wait this way (busy).

A photo in the trash, or already deleted for good, is refused (in_trash), and checked again once the claim is held: an Empty trash between the check and the claim, which leaves the photo without a main file, counts it as refused instead of failing the whole move.

claim_file reloads the photo's main file once it holds the claim, so a writer or reader that loaded the photo before a move uses the path the move recorded. A rescan read that finds the claim busy reads without it (and imports no keyword); a scan and a move refuse each other as jobs, which keeps that read off a move.

Up to 1.7.0 a move did not take the claim: a rating or keyword write between the link and the unlink, or after the move at the old path, was lost.

The "Move Photos" and "Rename Photos" jobs, and the moves and renames of up to 100 photos that run within the request, refuse each other (_refused_types in api/jobs/queue.py): starting one while the other runs is a 409 job_already_running, as two moves were already.

Up to 1.7.0, the move was shutil.move, which replaces an existing file, and a rename could run beside a move: a rename into the move's target between its check and its move lost the renamed photo's file.

Pending moves​

Before the first file of a photo moves, api/photos/pending_moves.py begin commits a PendingMove row (a holder token, the host, held_until 10 minutes ahead, which of the files the scan had indexed) and one PendingMoveFile per file: source, destination (unique across pending moves) and staging name. The mover renews the lease by token before each file and before removing each source, and before each file it moves back after a failure; when the token is gone, it stops without moving anything back. The transaction that records the new paths deletes the row by its token. A move that fails cleanly moves its files back and deletes the row; when a file cannot be moved back, the row stays.

api/photos/move_sweep.py finish_pending_moves settles rows left behind by a kill. manage.py start_upgrade_tasks runs it at every start for the rows of this host, whatever their lease (no mover of this host runs before the workers start), and start_job_cleanup_service schedules it hourly for rows whose lease passed. Per owner it waits while a scan runs and registers as a "Move Photos" job, so no scan starts meanwhile; when another move or rename of the owner is still listed as running, that job already keeps scans away and the sweep runs without its own, unless a scan started between the two checks, when it waits. Per row, taken over with a conditional UPDATE to a new token:

  1. Settle every file: remove stray staging files while the file is at one of its names; an empty file at either name is removed only when it is the placeholder of a move without hard links that a kill cut short: the mover records the placeholder's path, device, inode and modification time on the PendingMoveFile row (placeholder, Journal.noting) right after it creates it and before it renames the file over it, and the sweep compares them (is_our_placeholder). Any other empty file, a user's or one another app is still writing, blocks the name like any foreign file; a file under both names (a kill between the link and the unlink, or a copy that was placed) that is the same file, or has the same bytes, loses its source name and counts as moved.
  2. Roll forward when no target is blocked by another file: move the files still at their source, without replacing.
  3. Otherwise, or when a forward move fails, roll back: move the moved files back to their sources, without replacing.
  4. Record the paths (forward to the destinations, or back to the sources for rows a rescan had already moved) in one transaction that deletes the row by its token.

A row none of whose files is on disk (the library is not mounted) waits for the next run. So does one with a file or a target on an external drive that is not connected, or that another drive took the place of (on_wrong_drive). When a move can be neither finished nor undone, or the record fails, the row stays, the error is logged, the sweep's job fails ("interrupted photo moves could not be finished or undone"), and the next hourly run tries again. One failing row never stops the others or the start. A photo with a pending move cannot be moved or renamed again until the row is gone.

The models are verification/organize/MovePathRace.tla, MoveKill.tla and MoveClaim.tla. They assume that a renew and the file step after it are not separated by a pause longer than the 10-minute lease.

Up to 1.7.0, a kill between two file moves left a photo's files in two folders for good: the next scan followed each file to where it was, and a later move only takes the files in the main file's folder.