2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Netatalk utility functions
22 #endif /* HAVE_CONFIG_H */
26 #include <atalk/util.h>
29 * @def read_lock(fd, offset, whence, len)
30 * @brief place read lock on file
32 * @param fd (r) File descriptor
33 * @param offset (r) byte offset relative to l_whence
34 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END
35 * @param len (r) no. of bytes (0 means to EOF)
37 * @returns 0 on success, -1 on failure with
38 * fcntl return value and errno
42 * @def write_lock(fd, offset, whence, len)
43 * @brief place write lock on file
45 * @param fd (r) File descriptor
46 * @param offset (r) byte offset relative to l_whence
47 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END
48 * @param len (r) no. of bytes (0 means to EOF)
50 * @returns 0 on success, -1 on failure with
51 * fcntl return value and errno
55 * @def unlock(fd, offset, whence, len)
56 * @brief unlock a file
58 * @param fd (r) File descriptor
59 * @param offset (r) byte offset relative to l_whence
60 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END
61 * @param len (r) no. of bytes (0 means to EOF)
63 * @returns 0 on success, -1 on failure with
64 * fcntl return value and errno
68 * @brief lock a file with fctnl
70 * This function is called via the macros:
71 * read_lock, write_lock, un_lock
73 * @param fd (r) File descriptor
74 * @param cmd (r) cmd to fcntl, only F_SETLK is usable here
75 * @param type (r) F_RDLCK, F_WRLCK, F_UNLCK
76 * @param offset (r) byte offset relative to l_whence
77 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END
78 * @param len (r) no. of bytes (0 means to EOF)
80 * @returns 0 on success, -1 on failure with
81 * fcntl return value and errno
83 * @sa read_lock, write_lock, unlock
85 int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
90 lock.l_start = offset;
91 lock.l_whence = whence;
94 return (fcntl(fd, cmd, &lock));