X-Git-Url: https://git.gag.com/?a=blobdiff_plain;ds=sidebyside;f=common-src%2Famflock.h;h=5f917e2b0d7c03f67c209100d60baaa4c726ecc3;hb=d28952249e392eb31bc8eecc53f6c477f30c617b;hp=f9d33e136011e299072b99a98e4d89fb3dcccbc9;hpb=94a044f90357edefa6f4ae9f0b1d5885b0e34aee;p=debian%2Famanda diff --git a/common-src/amflock.h b/common-src/amflock.h index f9d33e1..5f917e2 100644 --- a/common-src/amflock.h +++ b/common-src/amflock.h @@ -1,6 +1,7 @@ /* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1991-1999 University of Maryland at College Park + * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved. * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its @@ -26,7 +27,90 @@ /* Moved from amanda.h and amflock.c by Dustin J. Mitchell */ -/* Public interface */ +/* + * New interface + */ + +typedef struct file_lock { + /* the entire contents of the locked file */ + char *data; + size_t len; + + /* do not touch! */ + gboolean locked; + int fd; + char *filename; +} file_lock; + +/* Create a new, unlocked file_lock object + * + * @param filename: filename of the file to lock + */ +file_lock *file_lock_new(const char *filename); + +/* Free a file_lock object, unlocking it in the process + * if necessary. + * + * @param lock: the file_lock object + */ +void file_lock_free(file_lock *lock); + +/* Lock a file and read its contents. This function will create the file if it + * doesn't already exist, in which case lock->data will be NULL and lock->len + * will be 0. + * + * If the file cannot be locked, this function will not block, but will return + * 1. Callers must poll for lock release, if it is desired. This is for + * compatibility with the GMainLoop, which does not allow arbitrary blocking. + * + * The lock represented by this function applies both within the current + * process (excluding other threads) and across all Amanda processes on the + * system, assuming that the filename is specified identically. On + * sufficiently modern systems, it will also function propertly across NFS + * mounts. + * + * Becuse this function may use fcntl to implement the locking, it is critical + * that the locked filename not be opened for any other reason while the lock + * is held, as this may unintentionally release the lock. + * + * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on + * a busy lock (see above) + */ +int file_lock_lock(file_lock *lock); + +/* Lock the file in write or read mode, the file is not read + * + * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on + * a busy lock (see above) + */ +int file_lock_lock_wr(file_lock *lock); +int file_lock_lock_rd(file_lock *lock); + +/* Return 1 if the object is already locked + */ +int file_lock_locked(file_lock *lock); + +/* Write the given data to the locked file, and reset the file_lock + * data member to point to a copy of the new data. This does not unlock + * the file. + * + * @param lock: the file_lock object + * @param data: data to write + * @param len: size of data + * @returns: -1 on error (with errno set), 0 on succes + */ +int file_lock_write(file_lock *lock, const char *data, size_t len); + +/* Unlock a locked file, without first re-writing it to disk. + * + * @param lock: the file_lock object + * @returns: -1 on error (with errno set), 0 on succes + */ +int file_lock_unlock(file_lock *lock); + +/* + * Old interface + */ /* * Get a file lock (for read/write files).