1/*
2   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13*/
14
15/*!
16 * @file
17 * Netatalk utility functions
18 */
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif /* HAVE_CONFIG_H */
23
24#include <unistd.h>
25#include <fcntl.h>
26#include <atalk/util.h>
27
28/*!
29 * @def read_lock(fd, offset, whence, len)
30 * @brief place read lock on file
31 *
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)
36 *
37 * @returns 0 on success, -1 on failure with
38 *          fcntl return value and errno
39 */
40
41/*!
42 * @def write_lock(fd, offset, whence, len)
43 * @brief place write lock on file
44 *
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)
49 *
50 * @returns 0 on success, -1 on failure with
51 *          fcntl return value and errno
52 */
53
54/*!
55 * @def unlock(fd, offset, whence, len)
56 * @brief unlock a file
57 *
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)
62 *
63 * @returns 0 on success, -1 on failure with
64 *          fcntl return value and errno
65 */
66
67/*!
68 * @brief lock a file with fctnl
69 *
70 * This function is called via the macros:
71 * read_lock, write_lock, un_lock
72 *
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)
79 *
80 * @returns 0 on success, -1 on failure with
81 *          fcntl return value and errno
82 *
83 * @sa read_lock, write_lock, unlock
84 */
85int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
86{
87    struct flock lock;
88
89    lock.l_type = type;
90    lock.l_start = offset;
91    lock.l_whence = whence;
92    lock.l_len = len;
93
94    return (fcntl(fd, cmd, &lock));
95}
96