Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=b7e0906e624560b558d47b544974d9d73eec0acf

commit b7e0906e624560b558d47b544974d9d73eec0acf
Author: Michel Hermier <[email protected]>
Date:   Sat Dec 7 09:06:14 2013 +0100

libpacman: Add FFileLock (not used yet, automagic lock holder directory 
creation needs to be discussed).

diff --git a/lib/libpacman/CMakeLists.txt b/lib/libpacman/CMakeLists.txt
index 725da02..998f756 100644
--- a/lib/libpacman/CMakeLists.txt
+++ b/lib/libpacman/CMakeLists.txt
@@ -10,6 +10,7 @@ set(FLIB_SOURCES
hash/md5driver.c
hash/sha1.c
io/archive.c
+       io/ffilelock.c
io/ftp.c
util/list.c
util/log.c
diff --git a/lib/libpacman/io/ffilelock.c b/lib/libpacman/io/ffilelock.c
new file mode 100644
index 0000000..8ae9868
--- /dev/null
+++ b/lib/libpacman/io/ffilelock.c
@@ -0,0 +1,82 @@
+/*
+ *  ffilelock.c
+ *
+ *  Copyright (c) 2013 by Michel Hermier <[email protected]>
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+
+#include "config.h"
+
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include "io/ffilelock.h"
+
+#include "fstdlib.h"
+#include "util.h"
+
+struct FFileLock {
+       int fd, flags;
+       char pathname[PATH_MAX];
+};
+
+FFileLock *f_filelock_aquire(const char *pathname, int flags)
+{
+       FFileLock *filelock;
+
+       ASSERT(!f_strempty(pathname), RET_ERR(PM_ERR_WRONG_ARGS, NULL));
+       ASSERT((filelock = f_zalloc(sizeof(*filelock))) != NULL, return NULL);
+
+       filelock->flags = flags;
+       f_canonicalize_path(pathname, filelock->pathname, PATH_MAX, 
F_PATH_NOCHECK);
+       if((filelock->fd = open(pathname, O_CREAT | O_RDWR, 0)) == -1) {
+               goto error;
+       }
+       if(flock(filelock->fd, flags & F_FILELOCK_EXCLUSIVE ? LOCK_EX : 
LOCK_SH) == -1) {
+               goto error;
+       }
+       return filelock;
+
+error:
+       /* Do not unlink if we down't own it */
+       filelock->flags &= ~F_FILELOCK_UNLINK_ON_CLOSE;
+       f_filelock_release(filelock);
+       return NULL;
+}
+
+int f_filelock_release(FFileLock *filelock)
+{
+       ASSERT(filelock != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
+
+       if(filelock->flags & F_FILELOCK_UNLINK_ON_CLOSE) {
+               unlink(filelock->pathname);
+       }
+       if(filelock->fd != -1) {
+               close(filelock->fd);
+       }
+       free(filelock);
+       return 0;
+
+error:
+       _pacman_log(PM_LOG_WARNING, _("could not remove lock file %s"), 
filelock->pathname);
+       return -1;
+}
+
+/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libpacman/io/ffilelock.h b/lib/libpacman/io/ffilelock.h
new file mode 100644
index 0000000..8c1c835
--- /dev/null
+++ b/lib/libpacman/io/ffilelock.h
@@ -0,0 +1,34 @@
+/*
+ *  ffilelock.h
+ *
+ *  Copyright (c) 2013 by Michel Hermier <[email protected]>
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+#ifndef _F_FILELOCK_H
+#define _F_FILELOCK_H
+
+typedef struct FFileLock FFileLock;
+
+#define F_FILELOCK_EXCLUSIVE       (1<<0)
+#define F_FILELOCK_UNLINK_ON_CLOSE (1<<1)
+
+FFileLock *f_filelock_aquire(const char *pathname, int flags);
+int f_filelock_release(FFileLock *filelock);
+
+#endif /* _F_FILELOCK_H */
+
+/* vim: set ts=2 sw=2 noet: */
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to