pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools.git
Log | Files | Refs | README | LICENSE

commit 945efae19305d4416f44b666b37340901d5055ce
parent ab213e6eb726133a978bb3d15bdcc42f42ca9129
Author: sin <sin@2f30.org>
Date:   Sun Jun 15 22:38:57 +0100

Lock /var/pkg directly

No need for a separate lockfile.

Diffstat:
infopkg.c | 5+----
installpkg.c | 5+----
lock.c | 61++++++++++++++-----------------------------------------------
removepkg.c | 5+----
util.h | 3+--
5 files changed, 18 insertions(+), 61 deletions(-)
diff --git a/infopkg.c b/infopkg.c @@ -32,7 +32,6 @@ main(int argc, char *argv[]) char *prefix = "/"; char path[PATH_MAX]; int Oflag = 0; - int lockfd; int i; int r; @@ -57,7 +56,7 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } - lockfd = lockdb(); + lockdb(); dir = opendir("var/pkg"); if (!dir) { @@ -82,8 +81,6 @@ main(int argc, char *argv[]) closedir(dir); - unlockdb(lockfd); - return EXIT_SUCCESS; } diff --git a/installpkg.c b/installpkg.c @@ -39,7 +39,6 @@ main(int argc, char *argv[]) char cwd[PATH_MAX]; char *prefix = "/"; int fflag = 0; - int lockfd; ARGBEGIN { case 'v': @@ -67,7 +66,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - lockfd = lockdb(); + lockdb(); r = chdir(cwd); if (r < 0) { @@ -100,8 +99,6 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - unlockdb(lockfd); - return EXIT_SUCCESS; } diff --git a/lock.c b/lock.c @@ -1,65 +1,32 @@ /* See LICENSE file for copyright and license details. */ +#include <dirent.h> #include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <fcntl.h> +#include <sys/types.h> #include <unistd.h> -#define LOCKPATH "var/pkg/.lock" - /* lock the package dabatase - assumes cwd is the root prefix */ -int +void lockdb(void) { - struct flock fl; - int r; - int fd; - - fl.l_type = F_WRLCK; - fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 0; - fl.l_pid = getpid(); - - fd = open(LOCKPATH, O_WRONLY | O_CREAT, 0600); - if (fd < 0) { - fprintf(stderr, "failed to create lockfile: %s\n", - strerror(errno)); - exit(EXIT_FAILURE); - } + DIR *dir; - r = fcntl(fd, F_SETLKW, &fl); - if (r < 0) { - fprintf(stderr, "failed to obtain lock: %s\n", + dir = opendir("var/pkg"); + if (!dir) { + fprintf(stderr, "opendir %s: %s\n", "var/pkg", strerror(errno)); exit(EXIT_FAILURE); } - return fd; -} - -/* unlock the package dabatase - assumes cwd is the root prefix */ -void -unlockdb(int fd) -{ - struct flock fl; - int r; - - fl.l_type = F_UNLCK; - fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 0; - fl.l_pid = getpid(); - - r = fcntl(fd, F_SETLKW, &fl); - if (r < 0) { - fprintf(stderr, "failed to clear lock: %s\n", - strerror(errno)); + if (flock(dirfd(dir), LOCK_EX | LOCK_NB) < 0) { + if (errno == EWOULDBLOCK) + fprintf(stderr, "package db already locked\n"); + else + fprintf(stderr, "flock %s: %s\n", "var/pkg", + strerror(errno)); exit(EXIT_FAILURE); } - - close(fd); - - unlink(LOCKPATH); } diff --git a/removepkg.c b/removepkg.c @@ -38,7 +38,6 @@ main(int argc, char *argv[]) struct dirent *dp; char filename[PATH_MAX]; char *prefix = "/"; - int lockfd; int found = 0; int r; int i; @@ -67,7 +66,7 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } - lockfd = lockdb(); + lockdb(); dir = opendir("var/pkg"); if (!dir) { @@ -107,8 +106,6 @@ main(int argc, char *argv[]) closedir(dir); - unlockdb(lockfd); - return EXIT_SUCCESS; } diff --git a/util.h b/util.h @@ -6,8 +6,7 @@ extern char *argv0; -int lockdb(void); -void unlockdb(int); +void lockdb(void); #undef strlcat size_t strlcat(char *, const char *, size_t);