Skip to content

Commit 22a58f5

Browse files
anatolallanmcrae
authored andcommitted
Swap alpm_db_update() implementation to multiplexed version
Now when all callers of the old alpm_db_update() function are gone we can remove this implementation. And then rename alpm_dbs_update() function to alpm_db_update(). Signed-off-by: Anatol Pomozov <[email protected]> Signed-off-by: Allan McRae <[email protected]>
1 parent 557845b commit 22a58f5

File tree

4 files changed

+7
-205
lines changed

4 files changed

+7
-205
lines changed

README

+3
Original file line numberDiff line numberDiff line change
@@ -664,5 +664,8 @@ API CHANGES BETWEEN 5.2 AND 6.0
664664
- ALPM_EVENT_PKGDOWNLOAD_START, ALPM_EVENT_PKGDOWNLOAD_DONE, ALPM_EVENT_PKGDOWNLOAD_FAILED
665665

666666
[CHANGED]
667+
- alpm_db_update() changed its signature and now accepts a list of databases
668+
rather than a single database. This is need to handle database downloading
669+
in a multiplexed way.
667670

668671
[ADDED]

lib/libalpm/alpm.h

+2-38
Original file line numberDiff line numberDiff line change
@@ -1039,42 +1039,6 @@ int alpm_db_add_server(alpm_db_t *db, const char *url);
10391039
int alpm_db_remove_server(alpm_db_t *db, const char *url);
10401040
/** @} */
10411041

1042-
/** Update a package database
1043-
*
1044-
* An update of the package database \a db will be attempted. Unless
1045-
* \a force is true, the update will only be performed if the remote
1046-
* database was modified since the last update.
1047-
*
1048-
* This operation requires a database lock, and will return an applicable error
1049-
* if the lock could not be obtained.
1050-
*
1051-
* Example:
1052-
* @code
1053-
* alpm_list_t *syncs = alpm_get_syncdbs();
1054-
* for(i = syncs; i; i = alpm_list_next(i)) {
1055-
* alpm_db_t *db = alpm_list_getdata(i);
1056-
* result = alpm_db_update(0, db);
1057-
*
1058-
* if(result < 0) {
1059-
* printf("Unable to update database: %s\n", alpm_strerrorlast());
1060-
* } else if(result == 1) {
1061-
* printf("Database already up to date\n");
1062-
* } else {
1063-
* printf("Database updated\n");
1064-
* }
1065-
* }
1066-
* @endcode
1067-
*
1068-
* @note After a successful update, the \link alpm_db_get_pkgcache()
1069-
* package cache \endlink will be invalidated
1070-
* @param force if true, then forces the update, otherwise update only in case
1071-
* the database isn't up to date
1072-
* @param db pointer to the package database to update
1073-
* @return 0 on success, -1 on error (pm_errno is set accordingly), 1 if up to
1074-
* to date
1075-
*/
1076-
int alpm_db_update(int force, alpm_db_t *db);
1077-
10781042
/** Update package databases
10791043
*
10801044
* An update of the package databases in the list \a dbs will be attempted.
@@ -1087,7 +1051,7 @@ int alpm_db_update(int force, alpm_db_t *db);
10871051
* Example:
10881052
* @code
10891053
* alpm_list_t *dbs = alpm_get_syncdbs();
1090-
* ret = alpm_dbs_update(config->handle, dbs, force);
1054+
* ret = alpm_db_update(config->handle, dbs, force);
10911055
* if(ret < 0) {
10921056
* pm_printf(ALPM_LOG_ERROR, _("failed to synchronize all databases (%s)\n"),
10931057
* alpm_strerror(alpm_errno(config->handle)));
@@ -1102,7 +1066,7 @@ int alpm_db_update(int force, alpm_db_t *db);
11021066
* the databases aren't up to date
11031067
* @return 0 on success, -1 on error (pm_errno is set accordingly)
11041068
*/
1105-
int alpm_dbs_update(alpm_handle_t *handle, alpm_list_t *dbs, int force);
1069+
int alpm_db_update(alpm_handle_t *handle, alpm_list_t *dbs, int force);
11061070

11071071
/** Get a package entry from a package database.
11081072
* @param db pointer to the package database to get the package from

lib/libalpm/be_sync.c

+1-166
Original file line numberDiff line numberDiff line change
@@ -136,172 +136,7 @@ static int sync_db_validate(alpm_db_t *db)
136136
return 0;
137137
}
138138

139-
int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
140-
{
141-
char *syncpath;
142-
const char *dbext;
143-
alpm_list_t *i;
144-
int updated = 0;
145-
int ret = -1;
146-
mode_t oldmask;
147-
alpm_handle_t *handle;
148-
int siglevel;
149-
150-
/* Sanity checks */
151-
ASSERT(db != NULL, return -1);
152-
handle = db->handle;
153-
handle->pm_errno = ALPM_ERR_OK;
154-
ASSERT(db != handle->db_local, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
155-
ASSERT(db->servers != NULL, RET_ERR(handle, ALPM_ERR_SERVER_NONE, -1));
156-
157-
if(!(db->usage & ALPM_DB_USAGE_SYNC)) {
158-
return 0;
159-
}
160-
161-
syncpath = get_sync_dir(handle);
162-
if(!syncpath) {
163-
return -1;
164-
}
165-
166-
/* force update of invalid databases to fix potential mismatched database/signature */
167-
if(db->status & DB_STATUS_INVALID) {
168-
force = 1;
169-
}
170-
171-
/* make sure we have a sane umask */
172-
oldmask = umask(0022);
173-
174-
siglevel = alpm_db_get_siglevel(db);
175-
176-
/* attempt to grab a lock */
177-
if(_alpm_handle_lock(handle)) {
178-
free(syncpath);
179-
umask(oldmask);
180-
RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1);
181-
}
182-
183-
dbext = db->handle->dbext;
184-
185-
for(i = db->servers; i; i = i->next) {
186-
const char *server = i->data, *final_db_url = NULL;
187-
struct dload_payload payload = {0};
188-
size_t len;
189-
int sig_ret = 0;
190-
191-
/* set hard upper limit of 128MiB */
192-
payload.max_size = 128 * 1024 * 1024;
193-
194-
/* print server + filename into a buffer */
195-
len = strlen(server) + strlen(db->treename) + strlen(dbext) + 2;
196-
MALLOC(payload.fileurl, len,
197-
{
198-
free(syncpath);
199-
umask(oldmask);
200-
RET_ERR(handle, ALPM_ERR_MEMORY, -1);
201-
}
202-
);
203-
snprintf(payload.fileurl, len, "%s/%s%s", server, db->treename, dbext);
204-
payload.handle = handle;
205-
payload.force = force;
206-
payload.unlink_on_fail = 1;
207-
208-
ret = _alpm_download(&payload, syncpath, NULL, &final_db_url);
209-
_alpm_dload_payload_reset(&payload);
210-
updated = (updated || ret == 0);
211-
212-
if(ret != -1 && updated && (siglevel & ALPM_SIG_DATABASE)) {
213-
/* an existing sig file is no good at this point */
214-
char *sigpath = _alpm_sigpath(handle, _alpm_db_path(db));
215-
if(!sigpath) {
216-
ret = -1;
217-
break;
218-
}
219-
unlink(sigpath);
220-
free(sigpath);
221-
222-
223-
/* check if the final URL from internal downloader looks reasonable */
224-
if(final_db_url != NULL) {
225-
if(strlen(final_db_url) < 3
226-
|| strcmp(final_db_url + strlen(final_db_url) - strlen(dbext),
227-
dbext) != 0) {
228-
final_db_url = NULL;
229-
}
230-
}
231-
232-
/* if we downloaded a DB, we want the .sig from the same server */
233-
if(final_db_url != NULL) {
234-
/* print final_db_url into a buffer (leave space for .sig) */
235-
len = strlen(final_db_url) + 5;
236-
} else {
237-
/* print server + filename into a buffer (leave space for separator and .sig) */
238-
len = strlen(server) + strlen(db->treename) + strlen(dbext) + 6;
239-
}
240-
241-
MALLOC(payload.fileurl, len,
242-
{
243-
free(syncpath);
244-
umask(oldmask);
245-
RET_ERR(handle, ALPM_ERR_MEMORY, -1);
246-
}
247-
);
248-
249-
if(final_db_url != NULL) {
250-
snprintf(payload.fileurl, len, "%s.sig", final_db_url);
251-
} else {
252-
snprintf(payload.fileurl, len, "%s/%s%s.sig", server, db->treename, dbext);
253-
}
254-
255-
payload.handle = handle;
256-
payload.force = 1;
257-
payload.errors_ok = (siglevel & ALPM_SIG_DATABASE_OPTIONAL);
258-
259-
/* set hard upper limit of 16KiB */
260-
payload.max_size = 16 * 1024;
261-
262-
sig_ret = _alpm_download(&payload, syncpath, NULL, NULL);
263-
/* errors_ok suppresses error messages, but not the return code */
264-
sig_ret = payload.errors_ok ? 0 : sig_ret;
265-
_alpm_dload_payload_reset(&payload);
266-
}
267-
268-
if(ret != -1 && sig_ret != -1) {
269-
break;
270-
}
271-
}
272-
273-
if(updated) {
274-
/* Cache needs to be rebuilt */
275-
_alpm_db_free_pkgcache(db);
276-
277-
/* clear all status flags regarding validity/existence */
278-
db->status &= ~DB_STATUS_VALID;
279-
db->status &= ~DB_STATUS_INVALID;
280-
db->status &= ~DB_STATUS_EXISTS;
281-
db->status &= ~DB_STATUS_MISSING;
282-
283-
/* if the download failed skip validation to preserve the download error */
284-
if(ret != -1 && sync_db_validate(db) != 0) {
285-
/* pm_errno should be set */
286-
ret = -1;
287-
}
288-
}
289-
290-
if(ret == -1) {
291-
/* pm_errno was set by the download code */
292-
_alpm_log(handle, ALPM_LOG_DEBUG, "failed to sync db: %s\n",
293-
alpm_strerror(handle->pm_errno));
294-
} else {
295-
handle->pm_errno = ALPM_ERR_OK;
296-
}
297-
298-
_alpm_handle_unlock(handle);
299-
free(syncpath);
300-
umask(oldmask);
301-
return ret;
302-
}
303-
304-
int SYMEXPORT alpm_dbs_update(alpm_handle_t *handle, alpm_list_t *dbs, int force) {
139+
int SYMEXPORT alpm_db_update(alpm_handle_t *handle, alpm_list_t *dbs, int force) {
305140
char *syncpath;
306141
const char *dbext = handle->dbext;
307142
alpm_list_t *i;

src/pacman/util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ int sync_syncdbs(int level, alpm_list_t *syncs)
154154
int force = (level < 2 ? 0 : 1);
155155

156156
multibar_move_completed_up(false);
157-
ret = alpm_dbs_update(config->handle, syncs, force);
157+
ret = alpm_db_update(config->handle, syncs, force);
158158
if(ret < 0) {
159159
pm_printf(ALPM_LOG_ERROR, _("failed to synchronize all databases (%s)\n"),
160160
alpm_strerror(alpm_errno(config->handle)));

0 commit comments

Comments
 (0)