|
23 | 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | 24 | */ |
25 | 25 |
|
| 26 | +#include <sys/file.h> |
26 | 27 | #include <sys/types.h> |
27 | 28 | #include <sys/stat.h> |
28 | 29 |
|
|
61 | 62 | * data type is specified on its edge, i.e array, bool, integer, string, |
62 | 63 | * dictionary. |
63 | 64 | */ |
64 | | -static int pkgdb_fd = -1; |
65 | | -static bool pkgdb_map_names_done = false; |
66 | 65 |
|
67 | 66 | int |
68 | 67 | xbps_pkgdb_lock(struct xbps_handle *xhp) |
69 | 68 | { |
| 69 | + char path[PATH_MAX]; |
70 | 70 | mode_t prev_umask; |
71 | | - int rv = 0; |
72 | | - /* |
73 | | - * Use a mandatory file lock to only allow one writer to pkgdb, |
74 | | - * other writers will block. |
75 | | - */ |
| 71 | + int r = 0; |
| 72 | + |
| 73 | + if (access(xhp->rootdir, W_OK) == -1 && errno != ENOENT) { |
| 74 | + return xbps_error_errno(errno, |
| 75 | + "failed to check whether the roodir is wriable: " |
| 76 | + "%s: %s\n", |
| 77 | + xhp->rootdir, strerror(errno)); |
| 78 | + } |
| 79 | + |
| 80 | + if (xbps_path_join(path, sizeof(path), xhp->metadir, "lock", (char *)NULL) == -1) { |
| 81 | + return xbps_error_errno(errno, |
| 82 | + "failed to create lockfile path: %s\n", strerror(errno)); |
| 83 | + } |
| 84 | + |
76 | 85 | prev_umask = umask(022); |
77 | | - xhp->pkgdb_plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB); |
78 | | - if (xbps_pkgdb_init(xhp) == ENOENT) { |
79 | | - /* if metadir does not exist, create it */ |
80 | | - if (access(xhp->metadir, R_OK|X_OK) == -1) { |
81 | | - if (errno != ENOENT) { |
82 | | - rv = errno; |
83 | | - goto ret; |
84 | | - } |
85 | | - if (xbps_mkpath(xhp->metadir, 0755) == -1) { |
86 | | - rv = errno; |
87 | | - xbps_dbg_printf("[pkgdb] failed to create metadir " |
88 | | - "%s: %s\n", xhp->metadir, strerror(rv)); |
89 | | - goto ret; |
90 | | - } |
| 86 | + |
| 87 | + /* if metadir does not exist, create it */ |
| 88 | + if (access(xhp->metadir, R_OK|X_OK) == -1) { |
| 89 | + if (errno != ENOENT) { |
| 90 | + umask(prev_umask); |
| 91 | + return xbps_error_errno(errno, |
| 92 | + "failed to check access to metadir: %s: %s\n", |
| 93 | + xhp->metadir, strerror(-r)); |
91 | 94 | } |
92 | | - /* if pkgdb is unexistent, create it with an empty dictionary */ |
93 | | - xhp->pkgdb = xbps_dictionary_create(); |
94 | | - if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist)) { |
95 | | - rv = errno; |
96 | | - xbps_dbg_printf("[pkgdb] failed to create pkgdb " |
97 | | - "%s: %s\n", xhp->pkgdb_plist, strerror(rv)); |
98 | | - goto ret; |
| 95 | + if (xbps_mkpath(xhp->metadir, 0755) == -1 && errno != EEXIST) { |
| 96 | + umask(prev_umask); |
| 97 | + return xbps_error_errno(errno, |
| 98 | + "failed to create metadir: %s: %s\n", |
| 99 | + xhp->metadir, strerror(errno)); |
99 | 100 | } |
100 | 101 | } |
101 | 102 |
|
102 | | - if ((pkgdb_fd = open(xhp->pkgdb_plist, O_CREAT|O_RDWR|O_CLOEXEC, 0664)) == -1) { |
103 | | - rv = errno; |
104 | | - xbps_dbg_printf("[pkgdb] cannot open pkgdb for locking " |
105 | | - "%s: %s\n", xhp->pkgdb_plist, strerror(rv)); |
106 | | - free(xhp->pkgdb_plist); |
107 | | - goto ret; |
| 103 | + xhp->lock_fd = open(path, O_CREAT|O_WRONLY|O_CLOEXEC, 0664); |
| 104 | + if (xhp->lock_fd == -1) { |
| 105 | + return xbps_error_errno(errno, |
| 106 | + "failed to create lock file: %s: %s\n", path, |
| 107 | + strerror(errno)); |
108 | 108 | } |
| 109 | + umask(prev_umask); |
109 | 110 |
|
110 | | - /* |
111 | | - * If we've acquired the file lock, then pkgdb is writable. |
112 | | - */ |
113 | | - if (lockf(pkgdb_fd, F_TLOCK, 0) == -1) { |
114 | | - rv = errno; |
115 | | - xbps_dbg_printf("[pkgdb] cannot lock pkgdb: %s\n", strerror(rv)); |
116 | | - } |
117 | | - /* |
118 | | - * Check if rootdir is writable. |
119 | | - */ |
120 | | - if (access(xhp->rootdir, W_OK) == -1) { |
121 | | - rv = errno; |
122 | | - xbps_dbg_printf("[pkgdb] rootdir %s: %s\n", xhp->rootdir, strerror(rv)); |
| 111 | + if (flock(xhp->lock_fd, LOCK_EX) == -1) { |
| 112 | + close(xhp->lock_fd); |
| 113 | + xhp->lock_fd = -1; |
| 114 | + return xbps_error_errno(errno, "failed to lock file: %s: %s\n", |
| 115 | + path, strerror(errno)); |
123 | 116 | } |
124 | 117 |
|
125 | | -ret: |
126 | | - umask(prev_umask); |
127 | | - return rv; |
| 118 | + return 0; |
128 | 119 | } |
129 | 120 |
|
130 | 121 | void |
131 | | -xbps_pkgdb_unlock(struct xbps_handle *xhp UNUSED) |
| 122 | +xbps_pkgdb_unlock(struct xbps_handle *xhp) |
132 | 123 | { |
133 | | - xbps_dbg_printf("%s: pkgdb_fd %d\n", __func__, pkgdb_fd); |
134 | | - |
135 | | - if (pkgdb_fd != -1) { |
136 | | - if (lockf(pkgdb_fd, F_ULOCK, 0) == -1) |
137 | | - xbps_dbg_printf("[pkgdb] failed to unlock pkgdb: %s\n", strerror(errno)); |
138 | | - |
139 | | - (void)close(pkgdb_fd); |
140 | | - pkgdb_fd = -1; |
141 | | - } |
| 124 | + if (xhp->lock_fd == -1) |
| 125 | + return; |
| 126 | + close(xhp->lock_fd); |
| 127 | + xhp->lock_fd = -1; |
142 | 128 | } |
143 | 129 |
|
144 | 130 | static int |
@@ -240,7 +226,7 @@ pkgdb_map_names(struct xbps_handle *xhp) |
240 | 226 | xbps_object_t obj; |
241 | 227 | int rv = 0; |
242 | 228 |
|
243 | | - if (pkgdb_map_names_done || !xbps_dictionary_count(xhp->pkgdb)) |
| 229 | + if (!xbps_dictionary_count(xhp->pkgdb)) |
244 | 230 | return 0; |
245 | 231 |
|
246 | 232 | /* |
@@ -269,9 +255,6 @@ pkgdb_map_names(struct xbps_handle *xhp) |
269 | 255 | } |
270 | 256 | } |
271 | 257 | xbps_object_iterator_release(iter); |
272 | | - if (!rv) { |
273 | | - pkgdb_map_names_done = true; |
274 | | - } |
275 | 258 | return rv; |
276 | 259 | } |
277 | 260 |
|
|
0 commit comments