summaryrefslogtreecommitdiffstats
path: root/main/apk-tools/0002-db-more-fix-for-read-only-cache-remounting.patch
blob: 7fee4d735351d1aa751cc986425386c1c3696a85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
From 95555ede4d732878d576415e5d338b0104b78ad6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Fri, 27 May 2011 16:49:25 +0300
Subject: [PATCH 2/2] db: more fix for read-only cache remounting

remount to read-write before trying to create the cache directory
subdirs. fix a fd leak that might prevent remounting back to rw.
---
 src/apk_database.h |    1 -
 src/database.c     |   31 ++++++++++++++-----------------
 src/url.c          |    1 +
 3 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/src/apk_database.h b/src/apk_database.h
index 9b032e7..b28a77b 100644
--- a/src/apk_database.h
+++ b/src/apk_database.h
@@ -110,7 +110,6 @@ struct apk_database {
 	apk_blob_t *arch;
 	unsigned int local_repos;
 	int permanent : 1;
-	int ro_cache : 1;
 	int compat_newfeatures : 1;
 	int compat_notinstallable : 1;
 
diff --git a/src/database.c b/src/database.c
index 0fc59df..9617b4e 100644
--- a/src/database.c
+++ b/src/database.c
@@ -1218,10 +1218,22 @@ int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts)
 
 		db->cache_dir = apk_linked_cache_dir;
 		db->cache_fd = fd;
+		if ((dbopts->open_flags & (APK_OPENF_WRITE | APK_OPENF_CACHE_WRITE)) &&
+		    fstatvfs(fd, &stvfs) == 0 && (stvfs.f_flag & ST_RDONLY) != 0) {
+			/* remount cache read-write */
+			db->cache_remount_dir = find_mountpoint(db->root_fd, db->cache_dir);
+			if (db->cache_remount_dir == NULL) {
+				apk_warning("Unable to find cache directory mount point");
+			} else if (do_remount(db->cache_remount_dir, "rw") != 0) {
+				free(db->cache_remount_dir);
+				db->cache_remount_dir = NULL;
+				apk_error("Unable to remount cache read-write");
+				r = EROFS;
+				goto ret_r;
+			}
+		}
 		mkdirat(db->cache_fd, "tmp", 0644);
 		db->cachetmp_fd = openat(db->cache_fd, "tmp", O_RDONLY | O_CLOEXEC);
-		if (fstatvfs(fd, &stvfs) == 0 && (stvfs.f_flag & ST_RDONLY) != 0)
-			db->ro_cache = 1;
 	} else {
 		if (fd >= 0)
 			close(fd);
@@ -1264,21 +1276,6 @@ int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts)
 		}
 	}
 
-	if ((dbopts->open_flags & (APK_OPENF_WRITE | APK_OPENF_CACHE_WRITE)) &&
-	    db->ro_cache) {
-		/* remount cache read-write */
-		db->cache_remount_dir = find_mountpoint(db->root_fd, db->cache_dir);
-		if (db->cache_remount_dir == NULL) {
-			apk_warning("Unable to find cache directory mount point");
-		} else if (do_remount(db->cache_remount_dir, "rw") != 0) {
-			free(db->cache_remount_dir);
-			db->cache_remount_dir = NULL;
-			apk_error("Unable to remount cache read-write");
-			r = EROFS;
-			goto ret_r;
-		}
-	}
-
 	if (!(dbopts->open_flags & APK_OPENF_NO_SYS_REPOS)) {
 		list_for_each_entry(repo, &dbopts->repository_list, list) {
 			r = apk_db_add_repository(db, APK_BLOB_STR(repo->url));
diff --git a/src/url.c b/src/url.c
index 0a17a7a..1fa9d66 100644
--- a/src/url.c
+++ b/src/url.c
@@ -136,6 +136,7 @@ int apk_url_download(const char *url, int atfd, const char *file)
 		exit(0);
 	}
 
+	close(fd);
 	waitpid(pid, &status, 0);
 	status = translate_wget(status);
 	if (status != 0) {
-- 
1.7.5.2