![]() |
Synchronization of files and databases | ![]() |
Besides editing remote files directly, a client can keep a local directory in sync with a remote directory, and keep some local databases (currently the bibliographic database) in sync with the server database. Both mechanisms follow the same scheme: the client computes a status list describing what has changed on either side since the last synchronization, lets the user resolve conflicts, and then applies the resulting operations with a few bulk requests. Both are experimental and are mainly driven by the widgets in
client/client-widgets.scm
(open-sync-widget, remote-interactive-sync, client-auto-sync).
The client remembers, for every pair of a local file and a remote file that were synchronized, an entry of type "sync" in the client database (user-database "sync"), with fields name (local file), remote-name, date (local modification time at the last synchronization), remote-id (resource identifier of the remote file at the last synchronization) and sync-date. Since every save on the server creates a new resource (see), a change of the remote identifier means that the remote file has been modified. Directories which should be synchronized automatically are recorded as entries of type "auto-sync" (client-auto-sync-add, client-auto-sync-list).
(client-sync-status local-base remote-base cont) in
client/client-sync.scm
proceeds as follows.
client-sync-list enumerates the local tree, skipping files for which dont-sync? holds (hidden files, backups, LaTeX auxiliary files, …). The service remote-sync-list returns the readable remote tree as a list of (dir? name rid).
Both lists are merged on relative names (compute-sync-list). For each name, get-url-sync-info fetches or creates the bookkeeping entry, which yields the current local date date, the recorded date date*, the current remote identifier remote-id and the recorded one remote-id*.
get-sync-status classifies the entry:
if both sides are unchanged (for directories: if both sides exist and existed);
if the local file is unchanged and the remote file disappeared;
if the local file disappeared and the remote file is unchanged;
if only the remote side changed, or the file only exists remotely and was never synchronized;
if only the local side changed, or the file only exists locally and was never synchronized;
otherwise, where x and y are * or - according to whether the file currently exists locally resp. remotely.
requalify-deleted turns a deletion into a conflict when some descendant of the deleted directory was modified on the other side.
Each status line has the form (cmd dir? local-name local-id remote-name remote-id). The synchronization widget displays the conflicts and lets the user choose Keep, Local or Remote for each of them; requalify-conflicting then converts a conflict into an upload, download or deletion.
(client-sync-proceed l msg cont) processes the status list in four asynchronous steps: uploads (one remote-upload request with the contents of all files and the commit message msg), downloads (one remote-download request), remote deletions (remote-remove-several) and local deletions. After each successful transfer the bookkeeping entry is updated with the new modification time and remote identifier (post-upload, post-download). On the server (
server/server-sync.scm
), the upload service creates missing directories and files and saves existing ones through the same functions as the remote file system services, so that every upload creates a new version. Remaining conflicts which the user decided to keep are not touched.
The functions remote-upload and remote-download of
client-sync.scm
(not to be confused with the services of the same names) implement one-way transfers: they treat all conflicts as uploads, resp. downloads. sync-repair removes bookkeeping entries whose local file no longer exists.
client/client-db-sync.scm
and
server/server-db-sync.scm
synchronize database entries of given kinds. A kind stands for a set of entry types (table db-kind-table); db-sync-kinds currently only returns "bib" (the user's bibliographic database), unless disabled with db-sync-kind. Entries are matched by their name field.
The client stores the local and remote times of the last synchronization in an entry of type "db-sync" of (user-database "sync") (db-last-sync).
db-client-sync-status computes the local changes since the local time with db-change-list (
database/db-convert.scm
, which uses the :modified query of the database), and asks the server for its changes since the remote time with remote-db-changes. For each name, db-change-status compares both sides and produces upload, download, local-delete, remote-delete or conflict lines (entries which only differ by access rights or meta data are considered equal, see db-equivalent?). Conflicts are resolved in favour of the server by default (db-requalify-conflicting).
db-client-sync-proceed sends the list to the service remote-db-sync, which applies uploads and remote deletions to the entries owned by the user (uploaded entries are given the user as owner and are made readable by "all"), but only if no other change happened on the server since the remote time; otherwise it returns #f and the client must start again. Likewise, the client applies downloads and local deletions only if no local change happened in the meantime. New versions of entries are created with db-update-entry, which keeps a newer link from the old version.
Finally db-dub-in-sync records the new times.
This optimistic scheme avoids locks: a synchronization is simply retried when the other side changed concurrently.