lock.c - dedup - deduplicating backup program
 (HTM) git clone git://bitreich.org/dedup/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/dedup/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       lock.c (686B)
       ---
            1 #include <fcntl.h>
            2 #include <limits.h>
            3 #include <stdio.h>
            4 #include <unistd.h>
            5 
            6 int
            7 lockrepo(char *repo)
            8 {
            9         char path[PATH_MAX];
           10         struct flock fl;
           11         int fd;
           12 
           13         if (snprintf(path, sizeof(path), "%s/lock", repo) >=
           14             sizeof(path))
           15                 return -1;
           16 
           17         fd = open(path, O_RDWR | O_CREAT, 0600);
           18         if (fd < 0)
           19                 return -1;
           20         fl.l_type = F_WRLCK;
           21         fl.l_whence = SEEK_SET;
           22         fl.l_start = 0;
           23         fl.l_len = 0;
           24         if (fcntl(fd, F_SETLK, &fl) < 0) {
           25                 close(fd);
           26                 return -1;
           27         }
           28         return fd;
           29 }
           30 
           31 int
           32 unlockrepo(int fd)
           33 {
           34         struct flock fl;
           35 
           36         fl.l_type = F_UNLCK;
           37         fl.l_whence = SEEK_SET;
           38         fl.l_start = 0;
           39         fl.l_len = 0;
           40         if (fcntl(fd, F_SETLK, &fl) < 0)
           41                 return -1;
           42         if (close(fd) < 0)
           43                 return -1;
           44         return 0;
           45 }