# C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

© 2006-2013 Andrew Cooke (site) / post authors (content).

## Protocol for copying updated files

From: "andrew cooke" <andrew@...>

Date: Tue, 3 Mar 2009 12:14:13 -0300 (CLST)

Maybe this is blatantly obvious, but it's take me a day and a half to get
right (yes, I am feeling incredibly stupid...), and the final solution is
quite simple, so I thought I'd note it here for future reference.

The problem is that there is a file (on a separate file system, whose
clock is unreliable relative to the local system) that is occasionally
updated (perhaps recreated, perhaps simply modified), and which needs to
be "archived" whenever it changes, but which should not be archived while
in a "changing" state.

Obviously these things are poorly defined, but the solution below only
requires that there be two different timescales: the longer being the
interval between changes and the shorter being the approximate amount of
time required to modify the file.

(In addition, I used diff rather than mtime, but I think you could use
mtime to detect changes as long as you don't try to compare across
machines, so you'd need to keep a copy of the output from stat, or
similar)

The solution is to run a batch process that repeats every tick of the
shorter timescale, and to use an intermediate temp file.  The process
works as follows:

- If there is no temp file, or if the temp file differs from the source,
copy the source to temp and exit
- Otherwise, if there is no archive file, or if the temp file differs from
the latest archive file, copy the temp file to the archive

That's it.  The first step will trigger repeatedly during updates, then
the second step will fire once.

Andrew