Snippets by language: TCL

Snippet ID Title

Snippets

Snippet ID Title
9
Torsten RohlfingThis (ba)sh script defines some useful functions for dependency checking and file locking.

Dependency checking is implemented in function "needs_update", which is called with two or more parameters (all paths to files or directories). The first parameter is the target file, which depends on the remaining files listed as parameters. If one of the prerequisite files is more recent than the target files, then the function returns "true", i.e., the target file needs to be updated. Otherwise, the function returns "false", i.e., the target file is up to date w.r.t. the given dependencies.

Example:

if needs_update target source1 source 2; then
cat source1 source2 > target
fi

The file locking (which tries hard to be NFS-safe, although it isn't completely) is implemented in two functions:

"lockfile_create" creates a lock for a given path in the file system, which is typically a target file that is being generated. If the function returns "true", locking was successful and processing should proceed. If the function returns false, locking failed (e.g., because another process has already locked the same file), and processing should skip this target file.

If a lock was successfully acquired, it must subsequently be freed (i.e., deleted) by calling "lockfile_delete".

Example:

if lockfile_create target; then
echo "Hello!" > target
lockfile_delete target
fi

Finally, there is a combination function, "needs_update_and_lock," which is called exactly like "needs_update", but attempts to acquire a lock on the target file in case it needs to be updated.

Example:

if needs_update_and_lock target src1 src2; then
cat src1 src2 > target
lockfile_delete target
fi