2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 /* Moved from amanda.h and amflock.c by Dustin J. Mitchell <dustin@zmanda.com> */
33 typedef struct file_lock_ {
34 /* the entire contents of the locked file */
44 /* Create a new, unlocked file_lock object
46 * @param filename: filename of the file to lock
48 file_lock *file_lock_new(const char *filename);
50 /* Free a file_lock object, unlocking it in the process
53 * @param lock: the file_lock object
55 void file_lock_free(file_lock *lock);
57 /* Lock a file and read its contents. This function will create the file if it
58 * doesn't already exist, in which case lock->data will be NULL and lock->len
61 * If the file cannot be locked, this function will not block, but will return
62 * 1. Callers must poll for lock release, if it is desired. This is for
63 * compatibility with the GMainLoop, which does not allow arbitrary blocking.
65 * The lock represented by this function applies both within the current
66 * process (excluding other threads) and across all Amanda processes on the
67 * system, assuming that the filename is specified identically. On
68 * sufficiently modern systems, it will also function propertly across NFS
71 * Becuse this function may use fcntl to implement the locking, it is critical
72 * that the locked filename not be opened for any other reason while the lock
73 * is held, as this may unintentionally release the lock.
75 * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on
76 * a busy lock (see above)
78 int file_lock_lock(file_lock *lock);
80 /* Lock the file in write or read mode, the file is not read
82 * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on
83 * a busy lock (see above)
85 int file_lock_lock_wr(file_lock *lock);
86 int file_lock_lock_rd(file_lock *lock);
88 /* Return 1 if the object is already locked
90 int file_lock_locked(file_lock *lock);
92 /* Write the given data to the locked file, and reset the file_lock
93 * data member to point to a copy of the new data. This does not unlock
96 * @param lock: the file_lock object
97 * @param data: data to write
98 * @param len: size of data
99 * @returns: -1 on error (with errno set), 0 on succes
101 int file_lock_write(file_lock *lock, const char *data, size_t len);
103 /* Unlock a locked file, without first re-writing it to disk.
105 * @param lock: the file_lock object
106 * @returns: -1 on error (with errno set), 0 on succes
108 int file_lock_unlock(file_lock *lock);
115 * Get a file lock (for read/write files).
117 int amflock(int fd, char *resource);
120 * Get a file lock (for read-only files).
122 int amroflock(int fd, char *resource);
125 * Release a file lock.
127 int amfunlock(int fd, char *resource);
129 /* Implementation interface */
130 typedef int (*amflock_fn)(int, char *);
131 typedef struct amflock_impl_s {
132 amflock_fn amflock_impl;
133 amflock_fn amroflock_impl;
134 amflock_fn amfunlock_impl;