xbps_repo_close: only unlock the file lock if repo was opened as such.

If xbps_repo_open() was called with the lock arg set, xbps_repo_close()
will now unlock the repo file lock, without the need to set it.

This avoids the need to always unlock the file lock even if it wasn't
locked previously. This also introduceds an ABI/API break, but this
way it's cleaner.
This commit is contained in:
Juan RP 2015-01-11 09:11:38 +01:00
parent 9aaff4e2d0
commit b23855f692
7 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2012-2014 Juan Romero Pardines. * Copyright (c) 2012-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -216,7 +216,7 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
out: out:
if (repo) if (repo)
xbps_repo_close(repo, true); xbps_repo_close(repo);
if (tmprepodir) if (tmprepodir)
free(tmprepodir); free(tmprepodir);

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2012-2014 Juan Romero Pardines. * Copyright (c) 2012-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -155,7 +155,7 @@ index_clean(struct xbps_handle *xhp, const char *repodir)
xbps_dictionary_count(idx)); xbps_dictionary_count(idx));
out: out:
xbps_repo_close(repo, true); xbps_repo_close(repo);
if (idx) if (idx)
xbps_object_release(idx); xbps_object_release(idx);
if (idxmeta) if (idxmeta)

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2012-2014 Juan Romero Pardines. * Copyright (c) 2012-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -154,7 +154,7 @@ remove_obsoletes(struct xbps_handle *xhp, const char *repodir)
(void)closedir(dirp); (void)closedir(dirp);
rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, repo); rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, repo);
xbps_repo_close(repo, false); xbps_repo_close(repo);
xbps_object_release(array); xbps_object_release(array);
return rv; return rv;

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2013-2014 Juan Romero Pardines. * Copyright (c) 2013-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -302,7 +302,7 @@ out:
rsa = NULL; rsa = NULL;
} }
if (repo) { if (repo) {
xbps_repo_close(repo, true); xbps_repo_close(repo);
} }
return rv ? -1 : 0; return rv ? -1 : 0;
} }

View File

@ -48,7 +48,7 @@
* *
* This header documents the full API for the XBPS Library. * This header documents the full API for the XBPS Library.
*/ */
#define XBPS_API_VERSION "20150110-1" #define XBPS_API_VERSION "20150111"
#ifndef XBPS_VERSION #ifndef XBPS_VERSION
#define XBPS_VERSION "UNSET" #define XBPS_VERSION "UNSET"
@ -1257,6 +1257,7 @@ struct xbps_repo {
* @private * @private
*/ */
int fd; int fd;
bool is_locked;
/** /**
* var is_remote * var is_remote
* *
@ -1418,9 +1419,8 @@ struct xbps_repo *xbps_repo_open(struct xbps_handle *xhp, const char *url, bool
* Closes a repository object and releases resources. * Closes a repository object and releases resources.
* *
* @param[in] repo The repository object to close. * @param[in] repo The repository object to close.
* @param[in] lock Set it to true to release the POSIX file lock.
*/ */
void xbps_repo_close(struct xbps_repo *repo, bool lock); void xbps_repo_close(struct xbps_repo *repo);
/** /**
* *

View File

@ -225,10 +225,12 @@ xbps_repo_open(struct xbps_handle *xhp, const char *url, bool lock)
/* /*
* Open the repository archive. * Open the repository archive.
*/ */
if (lock) if (lock) {
repo->fd = open(repofile, O_RDWR); repo->fd = open(repofile, O_RDWR);
else repo->is_locked = true;
} else {
repo->fd = open(repofile, O_RDONLY); repo->fd = open(repofile, O_RDONLY);
}
if (repo->fd == -1) { if (repo->fd == -1) {
int rv = errno; int rv = errno;
@ -252,7 +254,7 @@ out:
} }
void void
xbps_repo_close(struct xbps_repo *repo, bool lock) xbps_repo_close(struct xbps_repo *repo)
{ {
assert(repo); assert(repo);
@ -267,7 +269,7 @@ xbps_repo_close(struct xbps_repo *repo, bool lock)
xbps_object_release(repo->idxmeta); xbps_object_release(repo->idxmeta);
repo->idxmeta = NULL; repo->idxmeta = NULL;
} }
if (lock && lockf(repo->fd, F_ULOCK, 0) == -1) if (repo->is_locked && lockf(repo->fd, F_ULOCK, 0) == -1)
xbps_dbg_printf(repo->xhp, "[repo] failed to unlock %s: %s\n", repo->uri, strerror(errno)); xbps_dbg_printf(repo->xhp, "[repo] failed to unlock %s: %s\n", repo->uri, strerror(errno));
close(repo->fd); close(repo->fd);

View File

@ -98,7 +98,7 @@ xbps_rpool_release(struct xbps_handle *xhp _unused)
while ((repo = SIMPLEQ_FIRST(&rpool_queue))) { while ((repo = SIMPLEQ_FIRST(&rpool_queue))) {
SIMPLEQ_REMOVE(&rpool_queue, repo, xbps_repo, entries); SIMPLEQ_REMOVE(&rpool_queue, repo, xbps_repo, entries);
xbps_repo_close(repo, true); xbps_repo_close(repo);
free(repo); free(repo);
} }
if (xhp->repositories) if (xhp->repositories)