allow lockfiles to be used in cases where we have a dir_fd and a relative path
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
diff --git a/lib/got_lib_lockfile.h b/lib/got_lib_lockfile.h
index 379232f..977f28b 100644
--- a/lib/got_lib_lockfile.h
+++ b/lib/got_lib_lockfile.h
@@ -27,5 +27,6 @@ struct got_lockfile {
int fd;
};
-const struct got_error *got_lockfile_lock(struct got_lockfile **, const char *);
-const struct got_error *got_lockfile_unlock(struct got_lockfile *);
+const struct got_error *got_lockfile_lock(struct got_lockfile **,
+ const char *, int);
+const struct got_error *got_lockfile_unlock(struct got_lockfile *, int);
diff --git a/lib/lockfile.c b/lib/lockfile.c
index 71a5d73..846038c 100644
--- a/lib/lockfile.c
+++ b/lib/lockfile.c
@@ -31,7 +31,7 @@
#include "got_lib_lockfile.h"
const struct got_error *
-got_lockfile_lock(struct got_lockfile **lf, const char *path)
+got_lockfile_lock(struct got_lockfile **lf, const char *path, int dir_fd)
{
const struct got_error *err = NULL;
int attempts = 5;
@@ -53,9 +53,15 @@ got_lockfile_lock(struct got_lockfile **lf, const char *path)
}
do {
- (*lf)->fd = open((*lf)->path,
- O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
- GOT_DEFAULT_FILE_MODE);
+ if (dir_fd != -1) {
+ (*lf)->fd = openat(dir_fd, (*lf)->path,
+ O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
+ GOT_DEFAULT_FILE_MODE);
+ } else {
+ (*lf)->fd = open((*lf)->path,
+ O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
+ GOT_DEFAULT_FILE_MODE);
+ }
if ((*lf)->fd != -1)
break;
if (errno != EEXIST) {
@@ -69,18 +75,22 @@ got_lockfile_lock(struct got_lockfile **lf, const char *path)
err = got_error(GOT_ERR_LOCKFILE_TIMEOUT);
done:
if (err) {
- got_lockfile_unlock(*lf);
+ got_lockfile_unlock(*lf, dir_fd);
*lf = NULL;
}
return err;
}
const struct got_error *
-got_lockfile_unlock(struct got_lockfile *lf)
+got_lockfile_unlock(struct got_lockfile *lf, int dir_fd)
{
const struct got_error *err = NULL;
- if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
+ if (dir_fd != -1) {
+ if (lf->path && lf->fd != -1 &&
+ unlinkat(dir_fd, lf->path, 0) != 0)
+ err = got_error_from_errno("unlinkat");
+ } else if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
err = got_error_from_errno("unlink");
if (lf->fd != -1 && close(lf->fd) == -1 && err == NULL)
err = got_error_from_errno("close");
diff --git a/lib/object_create.c b/lib/object_create.c
index 38484be..70dc0d9 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -87,7 +87,7 @@ create_object_file(struct got_object_id *id, FILE *content,
if (err)
goto done;
- err = got_lockfile_lock(&lf, objpath);
+ err = got_lockfile_lock(&lf, objpath, -1);
if (err)
goto done;
@@ -107,7 +107,7 @@ done:
if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
err = got_error_from_errno("fclose");
if (lf)
- unlock_err = got_lockfile_unlock(lf);
+ unlock_err = got_lockfile_unlock(lf, -1);
return err ? err : unlock_err;
}
diff --git a/lib/reference.c b/lib/reference.c
index b89875e..ee703d1 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -174,7 +174,7 @@ parse_ref_file(struct got_reference **ref, const char *name,
struct got_lockfile *lf = NULL;
if (lock) {
- err = got_lockfile_lock(&lf, abspath);
+ err = got_lockfile_lock(&lf, abspath, -1);
if (err) {
if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
err = got_error_not_ref(name);
@@ -189,7 +189,7 @@ parse_ref_file(struct got_reference **ref, const char *name,
else
err = got_error_not_ref(name);
if (lock)
- got_lockfile_unlock(lf);
+ got_lockfile_unlock(lf, -1);
return err;
}
@@ -206,7 +206,7 @@ parse_ref_file(struct got_reference **ref, const char *name,
err = got_error_from_errno2("getline", abspath);
}
if (lock)
- got_lockfile_unlock(lf);
+ got_lockfile_unlock(lf, -1);
goto done;
}
while (linelen > 0 && line[linelen - 1] == '\n') {
@@ -217,12 +217,12 @@ parse_ref_file(struct got_reference **ref, const char *name,
err = parse_ref_line(ref, absname, line);
if (lock) {
if (err)
- got_lockfile_unlock(lf);
+ got_lockfile_unlock(lf, -1);
else {
if (*ref)
(*ref)->lf = lf;
else
- got_lockfile_unlock(lf);
+ got_lockfile_unlock(lf, -1);
}
}
done:
@@ -479,7 +479,7 @@ got_ref_open(struct got_reference **ref, struct got_repository *repo,
}
if (lock) {
- err = got_lockfile_lock(&lf, packed_refs_path);
+ err = got_lockfile_lock(&lf, packed_refs_path, -1);
if (err)
goto done;
}
@@ -502,7 +502,7 @@ done:
if (!err && *ref == NULL)
err = got_error_not_ref(refname);
if (err && lf)
- got_lockfile_unlock(lf);
+ got_lockfile_unlock(lf, -1);
free(path_refs);
return err;
}
@@ -1175,7 +1175,7 @@ got_ref_write(struct got_reference *ref, struct got_repository *repo)
}
if (ref->lf == NULL) {
- err = got_lockfile_lock(&lf, path);
+ err = got_lockfile_lock(&lf, path, -1);
if (err)
goto done;
}
@@ -1203,7 +1203,7 @@ got_ref_write(struct got_reference *ref, struct got_repository *repo)
tmppath = NULL;
done:
if (ref->lf == NULL && lf)
- unlock_err = got_lockfile_unlock(lf);
+ unlock_err = got_lockfile_unlock(lf, -1);
if (f) {
if (fclose(f) == EOF && err == NULL)
err = got_error_from_errno("fclose");
@@ -1244,7 +1244,7 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
goto done;
if (delref->lf == NULL) {
- err = got_lockfile_lock(&lf, packed_refs_path);
+ err = got_lockfile_lock(&lf, packed_refs_path, -1);
if (err)
goto done;
}
@@ -1348,7 +1348,7 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
}
done:
if (delref->lf == NULL && lf)
- unlock_err = got_lockfile_unlock(lf);
+ unlock_err = got_lockfile_unlock(lf, -1);
if (f) {
if (fclose(f) == EOF && err == NULL)
err = got_error_from_errno("fclose");
@@ -1385,7 +1385,7 @@ delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
}
if (ref->lf == NULL) {
- err = got_lockfile_lock(&lf, path);
+ err = got_lockfile_lock(&lf, path, -1);
if (err)
goto done;
}
@@ -1396,7 +1396,7 @@ delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
err = got_error_from_errno2("unlink", path);
done:
if (ref->lf == NULL && lf)
- unlock_err = got_lockfile_unlock(lf);
+ unlock_err = got_lockfile_unlock(lf, -1);
free(path_refs);
free(path);
@@ -1446,7 +1446,7 @@ const struct got_error *
got_ref_unlock(struct got_reference *ref)
{
const struct got_error *err;
- err = got_lockfile_unlock(ref->lf);
+ err = got_lockfile_unlock(ref->lf, -1);
ref->lf = NULL;
return err;
}
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index 391b44a..c5017d5 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -1053,7 +1053,7 @@ purge_loose_object(struct got_object_id *id, void *data, void *arg)
}
if (!a->dry_run) {
- err = got_lockfile_lock(&lf, path);
+ err = got_lockfile_lock(&lf, path, -1);
if (err)
goto done;
if (unlink(path) == -1) {
@@ -1073,7 +1073,7 @@ done:
err = got_error_from_errno("close");
free(path);
if (lf)
- unlock_err = got_lockfile_unlock(lf);
+ unlock_err = got_lockfile_unlock(lf, -1);
return err ? err : unlock_err;
}