소스 검색

dpkg: Only keep previous database stat info instead of open files

There's no point in keeping the files open across subsequent calls,
when the only thing we want to know is if the new file is the same as
the old one, just preserve the stat information for that. With this we
do no longer need to set the close-on-exec bit either.
Guillem Jover 12 년 전
부모
커밋
579b90b61b
2개의 변경된 파일15개의 추가작업 그리고 32개의 파일을 삭제
  1. 5 13
      src/divertdb.c
  2. 10 19
      src/statdb.c

+ 5 - 13
src/divertdb.c

@@ -41,13 +41,12 @@
 #include "main.h"
 #include "main.h"
 
 
 static struct diversion *diversions = NULL;
 static struct diversion *diversions = NULL;
-static FILE *diversionsfile = NULL;
 static char *diversionsname;
 static char *diversionsname;
 
 
 void
 void
 ensure_diversions(void)
 ensure_diversions(void)
 {
 {
-	struct stat sb_prev;
+	static struct stat sb_prev;
 	struct stat sb_next;
 	struct stat sb_next;
 	char linebuf[MAXDIVERTFILENAME];
 	char linebuf[MAXDIVERTFILENAME];
 	FILE *file;
 	FILE *file;
@@ -62,26 +61,18 @@ ensure_diversions(void)
 	if (!file) {
 	if (!file) {
 		if (errno != ENOENT)
 		if (errno != ENOENT)
 			ohshite(_("failed to open diversions file"));
 			ohshite(_("failed to open diversions file"));
-		if (!diversionsfile) {
-			onerr_abort--;
-			return;
-		}
-	} else if (diversionsfile) {
-		if (fstat(fileno(diversionsfile), &sb_prev))
-			ohshite(_("failed to fstat previous diversions file"));
+	} else {
 		if (fstat(fileno(file), &sb_next))
 		if (fstat(fileno(file), &sb_next))
 			ohshite(_("failed to fstat diversions file"));
 			ohshite(_("failed to fstat diversions file"));
+
 		if (sb_prev.st_dev == sb_next.st_dev &&
 		if (sb_prev.st_dev == sb_next.st_dev &&
 		    sb_prev.st_ino == sb_next.st_ino) {
 		    sb_prev.st_ino == sb_next.st_ino) {
 			fclose(file);
 			fclose(file);
 			onerr_abort--;
 			onerr_abort--;
 			return;
 			return;
 		}
 		}
+		sb_prev = sb_next;
 	}
 	}
-	if (diversionsfile)
-		fclose(diversionsfile);
-	diversionsfile = file;
-	setcloexec(fileno(diversionsfile), diversionsname);
 
 
 	for (ov = diversions; ov; ov = ov->next) {
 	for (ov = diversions; ov; ov = ov->next) {
 		ov->useinstead->divert->camefrom->divert = NULL;
 		ov->useinstead->divert->camefrom->divert = NULL;
@@ -121,5 +112,6 @@ ensure_diversions(void)
 		diversions = oicontest;
 		diversions = oicontest;
 	}
 	}
 
 
+	fclose(file);
 	onerr_abort--;
 	onerr_abort--;
 }
 }

+ 10 - 19
src/statdb.c

@@ -42,7 +42,6 @@
 #include "filesdb.h"
 #include "filesdb.h"
 #include "main.h"
 #include "main.h"
 
 
-static FILE *statoverridefile = NULL;
 static char *statoverridename;
 static char *statoverridename;
 
 
 uid_t
 uid_t
@@ -111,7 +110,7 @@ statdb_parse_mode(const char *str)
 void
 void
 ensure_statoverrides(void)
 ensure_statoverrides(void)
 {
 {
-	struct stat sb_prev;
+	static struct stat sb_prev;
 	struct stat sb_next;
 	struct stat sb_next;
 	FILE *file;
 	FILE *file;
 	char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
 	char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
@@ -127,28 +126,18 @@ ensure_statoverrides(void)
 	if (!file) {
 	if (!file) {
 		if (errno != ENOENT)
 		if (errno != ENOENT)
 			ohshite(_("failed to open statoverride file"));
 			ohshite(_("failed to open statoverride file"));
-		if (!statoverridefile) {
-			onerr_abort--;
-			return;
-		}
 	} else {
 	} else {
 		if (fstat(fileno(file), &sb_next))
 		if (fstat(fileno(file), &sb_next))
 			ohshite(_("failed to fstat statoverride file"));
 			ohshite(_("failed to fstat statoverride file"));
-		if (statoverridefile) {
-			if (fstat(fileno(statoverridefile), &sb_prev))
-				ohshite(_("failed to fstat previous statoverride file"));
-			if (sb_prev.st_dev == sb_next.st_dev &&
-			    sb_prev.st_ino == sb_next.st_ino) {
-				fclose(file);
-				onerr_abort--;
-				return;
-			}
+
+		if (sb_prev.st_dev == sb_next.st_dev &&
+		    sb_prev.st_ino == sb_next.st_ino) {
+			fclose(file);
+			onerr_abort--;
+			return;
 		}
 		}
+		sb_prev = sb_next;
 	}
 	}
-	if (statoverridefile)
-		fclose(statoverridefile);
-	statoverridefile = file;
-	setcloexec(fileno(statoverridefile), statoverridename);
 
 
 	if (!file) {
 	if (!file) {
 		onerr_abort--;
 		onerr_abort--;
@@ -158,6 +147,7 @@ ensure_statoverrides(void)
 	/* If the statoverride list is empty we don't need to bother
 	/* If the statoverride list is empty we don't need to bother
 	 * reading it. */
 	 * reading it. */
 	if (!sb_next.st_size) {
 	if (!sb_next.st_size) {
+		fclose(file);
 		onerr_abort--;
 		onerr_abort--;
 		return;
 		return;
 	}
 	}
@@ -230,5 +220,6 @@ ensure_statoverrides(void)
 		thisline = nextline;
 		thisline = nextline;
 	}
 	}
 
 
+	fclose(file);
 	onerr_abort--;
 	onerr_abort--;
 }
 }