chiark / gitweb /
Actually WAITING is correct because duct will never delete both F and D
[innduct.git] / lib / lockfile.c
1 /*  $Id: lockfile.c 6020 2002-12-20 00:19:58Z rra $
2 **
3 **  Lock a file or a range in a file.
4 **
5 **  Provides inn_lock_file and inn_lock_range functions to lock or unlock a
6 **  file or ranges within a file with a more convenient syntax than fcntl.
7 **  Assume that fcntl is available.
8 */
9
10 #include "config.h"
11 #include "clibrary.h"
12 #include <errno.h>
13 #include <fcntl.h>
14
15 #include "libinn.h"
16
17 bool
18 inn_lock_file(int fd, enum inn_locktype type, bool block)
19 {
20     return inn_lock_range(fd, type, block, 0, 0);
21 }
22
23 bool
24 inn_lock_range(int fd, enum inn_locktype type, bool block, off_t offset,
25                off_t size)
26 {
27     struct flock fl;
28     int status;
29
30     switch (type) {
31         case INN_LOCK_READ:     fl.l_type = F_RDLCK;    break;
32         case INN_LOCK_WRITE:    fl.l_type = F_WRLCK;    break;
33         default:
34         case INN_LOCK_UNLOCK:   fl.l_type = F_UNLCK;    break;
35     }
36
37     do {
38         fl.l_whence = SEEK_SET;
39         fl.l_start = offset;
40         fl.l_len = size;
41
42         status = fcntl(fd, block ? F_SETLKW : F_SETLK, &fl);
43     } while (status == -1 && errno == EINTR);
44     return (status != -1);
45 }