浏览代码

libdpkg: Refactor file lock setup into file_lock_setup()

Guillem Jover 16 年之前
父节点
当前提交
0238821c66
共有 1 个文件被更改,包括 14 次插入8 次删除
  1. 14 8
      lib/dpkg/file.c

+ 14 - 8
lib/dpkg/file.c

@@ -53,6 +53,16 @@ file_copy_perms(const char *src, const char *dst)
 		ohshite(_("unable to set mode of target file '%.250s'"), dst);
 }
 
+static void
+file_lock_setup(struct flock *fl, short type)
+{
+	fl->l_type = type;
+	fl->l_whence = SEEK_SET;
+	fl->l_start = 0;
+	fl->l_len = 0;
+	fl->l_pid = 0;
+}
+
 static void
 file_unlock_cleanup(int argc, void **argv)
 {
@@ -60,10 +70,9 @@ file_unlock_cleanup(int argc, void **argv)
 	struct flock fl;
 
 	assert(lockfd >= 0);
-	fl.l_type = F_UNLCK;
-	fl.l_whence = SEEK_SET;
-	fl.l_start = 0;
-	fl.l_len = 0;
+
+	file_lock_setup(&fl, F_UNLCK);
+
 	if (fcntl(lockfd, F_SETLK, &fl) == -1)
 		ohshite(_("unable to unlock dpkg status database"));
 }
@@ -84,10 +93,7 @@ file_lock(int *lockfd, const char *filename,
 
 	setcloexec(*lockfd, filename);
 
-	fl.l_type = F_WRLCK;
-	fl.l_whence = SEEK_SET;
-	fl.l_start = 0;
-	fl.l_len = 0;
+	file_lock_setup(&fl, F_WRLCK);
 
 	if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
 		if (emsg_eagain && (errno == EACCES || errno == EAGAIN))