aboutsummaryrefslogtreecommitdiffstats
path: root/main/apk-tools/0003-db-command-line-repositories-take-preference.patch
blob: 1f1786c35ae87a4c35729cd735e5deba05f8e9a3 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
From 6f1de8cd530f598f3f79414390d98fd8e683f2af Mon Sep 17 00:00:00 2001
From: Timo Teras <timo.teras@iki.fi>
Date: Thu, 6 Aug 2009 10:17:28 +0300
Subject: [PATCH 3/7] db: command line repositories take preference

over the ones in config file. also remove the apk_root == NULL
check as this cannot happen anymore. a valid root is always
required.
---
 src/database.c |  145 +++++++++++++++++++++++++++-----------------------------
 1 files changed, 70 insertions(+), 75 deletions(-)

diff --git a/src/database.c b/src/database.c
index 30e0431..834383b 100644
--- a/src/database.c
+++ b/src/database.c
@@ -895,59 +895,56 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
 	db->cache_dir = apk_static_cache_dir;
 	db->permanent = 1;
 
-	if (root != NULL) {
-		db->root = strdup(root);
-		db->root_fd = openat(AT_FDCWD, db->root, O_RDONLY);
-		if (db->root_fd < 0 && (flags & APK_OPENF_CREATE)) {
-			mkdirat(AT_FDCWD, db->root, 0755);
-			db->root_fd = openat(AT_FDCWD, root, O_RDONLY);
-		}
-		if (db->root_fd < 0) {
-			msg = "Unable to open root";
-			goto ret_errno;
-		}
-		if (fstat64(db->root_fd, &st) != 0 || major(st.st_dev) == 0)
-			db->permanent = 0;
-
-		if (fstatat64(db->root_fd, apk_linked_cache_dir, &st, 0) == 0 &&
-		    S_ISDIR(st.st_mode))
-			db->cache_dir = apk_linked_cache_dir;
-
-		if (flags & APK_OPENF_WRITE) {
+	db->root = strdup(root);
+	db->root_fd = openat(AT_FDCWD, db->root, O_RDONLY);
+	if (db->root_fd < 0 && (flags & APK_OPENF_CREATE)) {
+		mkdirat(AT_FDCWD, db->root, 0755);
+		db->root_fd = openat(AT_FDCWD, root, O_RDONLY);
+	}
+	if (db->root_fd < 0) {
+		msg = "Unable to open root";
+		goto ret_errno;
+	}
+	if (fstat64(db->root_fd, &st) != 0 || major(st.st_dev) == 0)
+		db->permanent = 0;
+
+	if (fstatat64(db->root_fd, apk_linked_cache_dir, &st, 0) == 0 &&
+	    S_ISDIR(st.st_mode))
+		db->cache_dir = apk_linked_cache_dir;
+
+	if (flags & APK_OPENF_WRITE) {
+		db->lock_fd = openat(db->root_fd, "var/lib/apk/lock",
+				     O_CREAT | O_RDWR, 0400);
+		if (db->lock_fd < 0 && errno == ENOENT &&
+		    (flags & APK_OPENF_CREATE)) {
+			r = apk_db_create(db);
+			if (r != 0) {
+				msg = "Unable to create database";
+				goto ret_r;
+			}
 			db->lock_fd = openat(db->root_fd, "var/lib/apk/lock",
 					     O_CREAT | O_RDWR, 0400);
-			if (db->lock_fd < 0 && errno == ENOENT &&
-			    (flags & APK_OPENF_CREATE)) {
-				r = apk_db_create(db);
-				if (r != 0) {
-					msg = "Unable to create database";
-					goto ret_r;
-				}
-				db->lock_fd = openat(db->root_fd,
-						     "var/lib/apk/lock",
-						     O_CREAT | O_RDWR, 0400);
-			}
-			if (db->lock_fd < 0 ||
-			    flock(db->lock_fd, LOCK_EX | LOCK_NB) < 0) {
-				msg = "Unable to lock database";
-				if (apk_wait) {
-					struct sigaction sa, old_sa;
-
-					apk_message("Waiting for repository lock");
-					memset(&sa, 0, sizeof sa);
-					sa.sa_handler = handle_alarm;
-					sa.sa_flags   = SA_ONESHOT;
-					sigaction(SIGALRM, &sa, &old_sa);
-
-					alarm(apk_wait);
-					if (flock(db->lock_fd, LOCK_EX) < 0)
-						goto ret_errno;
-
-					alarm(0);
-					sigaction(SIGALRM, &old_sa, NULL);
-				} else
+		}
+		if (db->lock_fd < 0 ||
+		    flock(db->lock_fd, LOCK_EX | LOCK_NB) < 0) {
+			msg = "Unable to lock database";
+			if (apk_wait) {
+				struct sigaction sa, old_sa;
+
+				apk_message("Waiting for repository lock");
+				memset(&sa, 0, sizeof sa);
+				sa.sa_handler = handle_alarm;
+				sa.sa_flags   = SA_ONESHOT;
+				sigaction(SIGALRM, &sa, &old_sa);
+
+				alarm(apk_wait);
+				if (flock(db->lock_fd, LOCK_EX) < 0)
 					goto ret_errno;
-			}
+
+				alarm(0);
+				sigaction(SIGALRM, &old_sa, NULL);
+			} else
+				goto ret_errno;
 		}
 	}
 
@@ -959,33 +956,18 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
 	db->cachetmp_fd = openat(db->cache_fd, "tmp", O_RDONLY);
 	db->keys_fd = openat(db->root_fd, "etc/apk/keys", O_RDONLY);
 
-	if (root != NULL) {
-		r = apk_db_read_state(db, flags);
-		if (r == -ENOENT && (flags & APK_OPENF_CREATE)) {
-			r = apk_db_create(db);
-			if (r != 0) {
-				msg = "Unable to create database";
-				goto ret_r;
-			}
-			r = apk_db_read_state(db, flags);
-		}
+	r = apk_db_read_state(db, flags);
+	if (r == -ENOENT && (flags & APK_OPENF_CREATE)) {
+		r = apk_db_create(db);
 		if (r != 0) {
-			msg = "Unable to read database state";
+			msg = "Unable to create database";
 			goto ret_r;
 		}
-
-		if (!(flags & APK_OPENF_NO_REPOS)) {
-			if (apk_repos == NULL)
-				apk_repos = "etc/apk/repositories";
-			blob = apk_blob_from_file(db->root_fd, apk_repos);
-			if (!APK_BLOB_IS_NULL(blob)) {
-				r = apk_blob_for_each_segment(
-					blob, "\n",
-					apk_db_add_repository, db);
-				rr = r ?: rr;
-				free(blob.ptr);
-			}
-		}
+		r = apk_db_read_state(db, flags);
+	}
+	if (r != 0) {
+		msg = "Unable to read database state";
+		goto ret_r;
 	}
 
 	if (!(flags & APK_OPENF_NO_REPOS)) {
@@ -993,10 +975,23 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
 			r = apk_db_add_repository(db, APK_BLOB_STR(repo->url));
 			rr = r ?: rr;
 		}
-
+		if (apk_repos == NULL)
+			apk_repos = "etc/apk/repositories";
+		blob = apk_blob_from_file(db->root_fd, apk_repos);
+		if (!APK_BLOB_IS_NULL(blob)) {
+			r = apk_blob_for_each_segment(
+				blob, "\n",
+				apk_db_add_repository, db);
+			rr = r ?: rr;
+			free(blob.ptr);
+		}
 		if (apk_flags & APK_UPDATE_CACHE)
 			apk_db_index_write_nr_cache(db);
 	}
+	if (rr != 0) {
+		r = rr;
+		goto ret_r;
+	}
 
 	return rr;
 
-- 
1.6.4