Просмотр исходного кода

libdpkg: Move generic file locking from lock.c to file.c

Guillem Jover лет назад: 16
Родитель
Сommit
972d84487a
8 измененных файлов с 54 добавлено и 90 удалено
  1. 0 1
      lib/dpkg/Makefile.am
  2. 1 0
      lib/dpkg/dbmodify.c
  3. 0 6
      lib/dpkg/dpkg.h
  4. 48 1
      lib/dpkg/file.c
  5. 4 0
      lib/dpkg/file.h
  6. 0 81
      lib/dpkg/lock.c
  7. 1 0
      lib/dpkg/trigdeferred.l
  8. 0 1
      po/POTFILES.in

+ 0 - 1
lib/dpkg/Makefile.am

@@ -34,7 +34,6 @@ libdpkg_a_SOURCES = \
 	file.c \
 	fields.c \
 	i18n.h \
-	lock.c \
 	log.c \
 	md5.c md5.h \
 	mlib.c \

+ 1 - 0
lib/dpkg/dbmodify.c

@@ -41,6 +41,7 @@
 #include <dpkg/i18n.h>
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/file.h>
 
 char *statusfile=NULL, *availablefile=NULL;
 char *triggersdir, *triggersfilefile, *triggersnewfilefile;

+ 0 - 6
lib/dpkg/dpkg.h

@@ -173,12 +173,6 @@ void cu_closepipe(int argc, void **argv);
 void cu_closedir(int argc, void **argv);
 void cu_closefd(int argc, void **argv);
 
-/*** lock.c ***/
-
-void file_lock(int *lockfd, const char *filename,
-               const char *emsg, const char *emsg_eagain);
-void file_unlock(void);
-
 /*** from mlib.c ***/
 
 void setcloexec(int fd, const char* fn);

+ 48 - 1
lib/dpkg/file.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * file.c - file handling functions
  *
- * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
+ * Copyright © 1994, 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  * Copyright © 2008 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
@@ -25,7 +25,9 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include <dpkg/dpkg.h>
@@ -51,3 +53,48 @@ file_copy_perms(const char *src, const char *dst)
 		ohshite(_("unable to set mode of target file '%.250s'"), dst);
 }
 
+static void
+file_unlock_cleanup(int argc, void **argv)
+{
+	int lockfd = *(int*)argv[0];
+	struct flock fl;
+
+	assert(lockfd >= 0);
+	fl.l_type = F_UNLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+	if (fcntl(lockfd, F_SETLK, &fl) == -1)
+		ohshite(_("unable to unlock dpkg status database"));
+}
+
+void
+file_unlock(void)
+{
+	pop_cleanup(ehflag_normaltidy); /* Calls file_unlock_cleanup. */
+}
+
+/* lockfd must be allocated statically as its addresses is passed to
+ * a cleanup handler. */
+void
+file_lock(int *lockfd, const char *filename,
+          const char *emsg, const char *emsg_eagain)
+{
+	struct flock fl;
+
+	setcloexec(*lockfd, filename);
+
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+
+	if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
+		if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
+			ohshit(emsg_eagain);
+		ohshite(emsg);
+	}
+
+	push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd);
+}
+

+ 4 - 0
lib/dpkg/file.h

@@ -30,6 +30,10 @@ DPKG_BEGIN_DECLS
  */
 void file_copy_perms(const char *src, const char *dst);
 
+void file_lock(int *lockfd, const char *filename,
+               const char *emsg, const char *emsg_eagain);
+void file_unlock(void);
+
 DPKG_END_DECLS
 
 #endif /* LIBDPKG_FILE_H */

+ 0 - 81
lib/dpkg/lock.c

@@ -1,81 +0,0 @@
-/*
- * libdpkg - Debian packaging suite library routines
- * lock.c - packages database locking
- *
- * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
- *
- * This 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 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, see <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <compat.h>
-
-#include <sys/file.h>
-
-#include <assert.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#include <dpkg/i18n.h>
-#include <dpkg/dpkg.h>
-#include <dpkg/dpkg-db.h>
-
-static void
-file_unlock_cleanup(int argc, void **argv)
-{
-  int lockfd = *(int*)argv[0];
-  struct flock fl;
-
-  assert(lockfd >= 0);
-  fl.l_type= F_UNLCK;
-  fl.l_whence= SEEK_SET;
-  fl.l_start= 0;
-  fl.l_len= 0;
-  if (fcntl(lockfd, F_SETLK, &fl) == -1)
-    ohshite(_("unable to unlock dpkg status database"));
-}
-
-void
-file_unlock(void)
-{
-  pop_cleanup(ehflag_normaltidy); /* Calls file_unlock_cleanup. */
-}
-
-/* lockfd must be allocated statically as its addresses is passed to
- * a cleanup handler. */
-void
-file_lock(int *lockfd, const char *filename,
-          const char *emsg, const char *emsg_eagain)
-{
-  struct flock fl;
-
-  setcloexec(*lockfd, filename);
-
-  fl.l_type = F_WRLCK;
-  fl.l_whence = SEEK_SET;
-  fl.l_start = 0;
-  fl.l_len = 0;
-
-  if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
-    if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
-      ohshit(emsg_eagain);
-    ohshite(emsg);
-  }
-
-  push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd);
-}
-

+ 1 - 0
lib/dpkg/trigdeferred.l

@@ -43,6 +43,7 @@
 #include <dpkg/i18n.h>
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/file.h>
 
 #define YY_NO_INPUT
 

+ 0 - 1
po/POTFILES.in

@@ -11,7 +11,6 @@ lib/dpkg/dump.c
 lib/dpkg/ehandle.c
 lib/dpkg/fields.c
 lib/dpkg/file.c
-lib/dpkg/lock.c
 lib/dpkg/log.c
 lib/dpkg/md5.c
 lib/dpkg/mlib.c