File: locking.c

package info (click to toggle)
netatalk 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,716 kB
  • sloc: ansic: 85,115; sh: 10,385; perl: 1,703; makefile: 1,363
file content (96 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
   $Id: locking.c,v 1.4 2010-01-05 19:05:52 franklahm Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

/*!
 * @file
 * Netatalk utility functions
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <unistd.h>
#include <fcntl.h>
#include <atalk/util.h>

/*!
 * @def read_lock(fd, offset, whence, len)
 * @brief place read lock on file
 *
 * @param   fd         (r) File descriptor
 * @param   offset     (r) byte offset relative to l_whence
 * @param   whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
 * @param   len        (r) no. of bytes (0 means to EOF)
 *
 * @returns 0 on success, -1 on failure with
 *          fcntl return value and errno
 */

/*!
 * @def write_lock(fd, offset, whence, len)
 * @brief place write lock on file
 *
 * @param   fd         (r) File descriptor
 * @param   offset     (r) byte offset relative to l_whence
 * @param   whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
 * @param   len        (r) no. of bytes (0 means to EOF)
 *
 * @returns 0 on success, -1 on failure with
 *          fcntl return value and errno
 */

/*!
 * @def unlock(fd, offset, whence, len)
 * @brief unlock a file
 *
 * @param   fd         (r) File descriptor
 * @param   offset     (r) byte offset relative to l_whence
 * @param   whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
 * @param   len        (r) no. of bytes (0 means to EOF)
 *
 * @returns 0 on success, -1 on failure with
 *          fcntl return value and errno
 */

/*!
 * @brief lock a file with fctnl
 *
 * This function is called via the macros:
 * read_lock, write_lock, un_lock
 *
 * @param   fd         (r) File descriptor
 * @param   cmd        (r) cmd to fcntl, only F_SETLK is usable here
 * @param   type       (r) F_RDLCK, F_WRLCK, F_UNLCK
 * @param   offset     (r) byte offset relative to l_whence
 * @param   whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
 * @param   len        (r) no. of bytes (0 means to EOF)
 *
 * @returns 0 on success, -1 on failure with
 *          fcntl return value and errno
 *
 * @sa read_lock, write_lock, unlock
 */
int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;
    lock.l_start = offset;
    lock.l_whence = whence;
    lock.l_len = len;

    return (fcntl(fd, cmd, &lock));
}