Imported Upstream version 3.3.0
[debian/amanda] / common-src / amflock.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26
27 /* Moved from amanda.h and amflock.c by Dustin J. Mitchell <dustin@zmanda.com> */
28
29 /*
30  * New interface
31  */
32
33 typedef struct file_lock_ {
34     /* the entire contents of the locked file */
35     char *data;
36     size_t len;
37
38     /* do not touch! */
39     gboolean locked;
40     int fd;
41     char *filename;
42 } file_lock;
43
44 /* Create a new, unlocked file_lock object
45  *
46  * @param filename: filename of the file to lock
47  */
48 file_lock *file_lock_new(const char *filename);
49
50 /* Free a file_lock object, unlocking it in the process
51  * if necessary.
52  *
53  * @param lock: the file_lock object
54  */
55 void file_lock_free(file_lock *lock);
56
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
59  * will be 0.
60  *
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.
64  *
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
69  * mounts.
70  *
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.
74  *
75  * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on
76  * a busy lock (see above)
77  */
78 int file_lock_lock(file_lock *lock);
79
80 /* Lock the file in write or read mode, the file is not read
81  *
82  * @param lock: the file_lock object @returns: -1 on error, 0 on success, 1 on
83  * a busy lock (see above)
84  */
85 int file_lock_lock_wr(file_lock *lock);
86 int file_lock_lock_rd(file_lock *lock);
87
88 /* Return 1 if the object is already locked
89  */
90 int file_lock_locked(file_lock *lock);
91
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
94  * the file.
95  *
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
100  */
101 int file_lock_write(file_lock *lock, const char *data, size_t len);
102
103 /* Unlock a locked file, without first re-writing it to disk.
104  *
105  * @param lock: the file_lock object
106  * @returns: -1 on error (with errno set), 0 on succes
107  */
108 int file_lock_unlock(file_lock *lock);
109
110 /*
111  * Old interface
112  */
113
114 /*
115  * Get a file lock (for read/write files).
116  */
117 int amflock(int fd, char *resource);
118
119 /*
120  * Get a file lock (for read-only files).
121  */
122 int amroflock(int fd, char *resource);
123
124 /*
125  * Release a file lock.
126  */
127 int amfunlock(int fd, char *resource);
128
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;
135     char *impl_name;
136 } amflock_impl_t;