Move Files an Application Tracks Through Its API, Because Its Database Records Their Paths

In progress

Applications that manage files, such as download clients, media servers or photo libraries, keep their own record of where each file is. Moving the files with mv changes the disk but not that record: the application then reports the files missing, stops serving them, or fetches them again. Asking the application to move them through its API changes both in one operation.

Why mv Breaks the Application

A download client stores each torrent's save path in its state. Running mv /downloads/* /data/media/downloads/ relocates the data, but the client still looks in /downloads, where nothing is left. The same holds for any application with an index: the database and the filesystem now disagree, and the database is what the application believes.

Moving Through the API

qBittorrent's Web API has a setLocation call that moves the data and updates each torrent's path. Python's requests keeps the login cookie in a session, which is clumsy to do with curl:

session = requests.Session()                      # keeps the auth cookie
login = session.post(f"{URL}/api/v2/auth/login",
                     data={"username": "admin", "password": password})
if login.text != "Ok.":
    sys.exit("Authentication failed!")

torrents = session.get(f"{URL}/api/v2/torrents/info").json()
hashes = "|".join(t["hash"] for t in torrents)
session.post(f"{URL}/api/v2/torrents/setLocation",
             data={"hashes": hashes, "location": "/data/media/downloads/"})

The location is a path inside the application's container, so it must be one the container mounts, and the same path other applications use for those files (see Files Written to an Unmounted Container Path Live Only in That Container's Layer).

When a Direct Move Is Fine

Files nobody indexes, such as a static web root or a plain backup folder, can be moved directly. Applications that rescan on a schedule recover by themselves, after a period where their library shows missing items.

References