add fd field to got_repository, modify got_packidx_open to use fds These changes are intended to make got more compatible with FreeBSD's Capsicum. ok stsp
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
diff --git a/include/got_repository.h b/include/got_repository.h
index 9e816e2..58d890d 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -32,6 +32,9 @@ const char *got_repo_get_path(struct got_repository *);
*/
const char *got_repo_get_path_git_dir(struct got_repository *);
+/* Obtain the file descriptor of the repository's .git directory. */
+int got_repo_get_fd(struct got_repository *);
+
/* Obtain the commit author name if parsed from gitconfig, else NULL. */
const char *got_repo_get_gitconfig_author_name(struct got_repository *);
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index c56e16c..39aa433 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -168,7 +168,7 @@ struct got_packfile_obj_data {
const struct got_error *got_packidx_init_hdr(struct got_packidx *, int);
const struct got_error *got_packidx_open(struct got_packidx **,
- const char *, int);
+ int, const char *, int);
const struct got_error *got_packidx_close(struct got_packidx *);
int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *);
const struct got_error *got_packidx_match_id_str_prefix(
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index d2a6b78..18d2953 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -34,6 +34,7 @@
struct got_repository {
char *path;
char *path_git_dir;
+ int gitdir_fd;
/* The pack index cache speeds up search for packed objects. */
struct got_packidx *packidx_cache[GOT_PACKIDX_CACHE_SIZE];
diff --git a/lib/pack.c b/lib/pack.c
index 71b5e07..b46583a 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -328,7 +328,8 @@ done:
}
const struct got_error *
-got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
+got_packidx_open(struct got_packidx **packidx,
+ int dir_fd, const char *relpath, int verify)
{
const struct got_error *err = NULL;
struct got_packidx *p;
@@ -340,15 +341,15 @@ got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
if (p == NULL)
return got_error_from_errno("calloc");
- p->fd = open(path, O_RDONLY | O_NOFOLLOW);
+ p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW);
if (p->fd == -1) {
- err = got_error_from_errno2("open", path);
+ err = got_error_from_errno2("openat", relpath);
free(p);
return err;
}
if (fstat(p->fd, &sb) != 0) {
- err = got_error_from_errno2("fstat", path);
+ err = got_error_from_errno2("fstat", relpath);
close(p->fd);
free(p);
return err;
@@ -361,7 +362,7 @@ got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
return err;
}
- p->path_packidx = strdup(path);
+ p->path_packidx = strdup(relpath);
if (p->path_packidx == NULL) {
err = got_error_from_errno("strdup");
goto done;
diff --git a/lib/repository.c b/lib/repository.c
index 4c8c8ac..7a2d299 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -77,6 +77,12 @@ got_repo_get_path_git_dir(struct got_repository *repo)
return repo->path_git_dir;
}
+int
+got_repo_get_fd(struct got_repository *repo)
+{
+ return repo->gitdir_fd;
+}
+
const char *
got_repo_get_gitconfig_author_name(struct got_repository *repo)
{
@@ -333,6 +339,8 @@ open_repo(struct got_repository *repo, const char *path)
{
const struct got_error *err = NULL;
+ repo->gitdir_fd = -1;
+
/* bare git repository? */
repo->path_git_dir = strdup(path);
if (repo->path_git_dir == NULL)
@@ -343,6 +351,12 @@ open_repo(struct got_repository *repo, const char *path)
err = got_error_from_errno("strdup");
goto done;
}
+ repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
+ if (repo->gitdir_fd == -1) {
+ err = got_error_from_errno2("open",
+ repo->path_git_dir);
+ goto done;
+ }
return NULL;
}
@@ -359,6 +373,12 @@ open_repo(struct got_repository *repo, const char *path)
err = got_error_from_errno("strdup");
goto done;
}
+ repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
+ if (repo->gitdir_fd == -1) {
+ err = got_error_from_errno2("open",
+ repo->path_git_dir);
+ goto done;
+ }
return NULL;
}
@@ -369,6 +389,10 @@ done:
repo->path = NULL;
free(repo->path_git_dir);
repo->path_git_dir = NULL;
+ if (repo->gitdir_fd != -1)
+ close(repo->gitdir_fd);
+ repo->gitdir_fd = -1;
+
}
return err;
}
@@ -916,11 +940,11 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
struct got_repository *repo, struct got_object_id *id)
{
const struct got_error *err;
- char *path_packdir;
- DIR *packdir;
+ DIR *packdir = NULL;
struct dirent *dent;
char *path_packidx;
size_t i;
+ int packdir_fd;
/* Search pack index cache. */
for (i = 0; i < nitems(repo->packidx_cache); i++) {
@@ -944,16 +968,21 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
}
/* No luck. Search the filesystem. */
- path_packdir = got_repo_get_path_objects_pack(repo);
- if (path_packdir == NULL)
- return got_error_from_errno("got_repo_get_path_objects_pack");
-
- packdir = opendir(path_packdir);
- if (packdir == NULL) {
+ packdir_fd = openat(got_repo_get_fd(repo),
+ GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
+ if (packdir_fd == -1) {
if (errno == ENOENT)
err = got_error_no_obj(id);
else
- err = got_error_from_errno2("opendir", path_packdir);
+ err = got_error_from_errno_fmt("openat: %s/%s",
+ got_repo_get_path_git_dir(repo),
+ GOT_OBJECTS_PACK_DIR);
+ goto done;
+ }
+
+ packdir = fdopendir(packdir_fd);
+ if (packdir == NULL) {
+ err = got_error_from_errno("fdopendir");
goto done;
}
@@ -963,7 +992,7 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
if (!is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
- if (asprintf(&path_packidx, "%s/%s", path_packdir,
+ if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
dent->d_name) == -1) {
err = got_error_from_errno("asprintf");
goto done;
@@ -983,7 +1012,8 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
continue; /* already searched */
}
- err = got_packidx_open(packidx, path_packidx, 0);
+ err = got_packidx_open(packidx, got_repo_get_fd(repo),
+ path_packidx, 0);
if (err) {
free(path_packidx);
goto done;
@@ -1003,7 +1033,6 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
err = got_error_no_obj(id);
done:
- free(path_packdir);
if (packdir && closedir(packdir) != 0 && err == NULL)
err = got_error_from_errno("closedir");
return err;
@@ -1032,13 +1061,15 @@ read_packfile_hdr(int fd, struct got_packidx *packidx)
}
static const struct got_error *
-open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
+open_packfile(int *fd, struct got_repository *repo,
+ const char *relpath, struct got_packidx *packidx)
{
const struct got_error *err = NULL;
- *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
+ *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
if (*fd == -1)
- return got_error_from_errno2("open", path_packfile);
+ return got_error_from_errno_fmt("openat: %s/%s",
+ got_repo_get_path_git_dir(repo), relpath);
if (packidx) {
err = read_packfile_hdr(*fd, packidx);
@@ -1088,7 +1119,7 @@ got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
goto done;
}
- err = open_packfile(&pack->fd, path_packfile, packidx);
+ err = open_packfile(&pack->fd, repo, path_packfile, packidx);
if (err)
goto done;
@@ -1200,22 +1231,25 @@ match_packed_object(struct got_object_id **unique_id,
struct got_repository *repo, const char *id_str_prefix, int obj_type)
{
const struct got_error *err = NULL;
- char *path_packdir;
- DIR *packdir;
+ DIR *packdir = NULL;
struct dirent *dent;
char *path_packidx;
struct got_object_id_queue matched_ids;
+ int packdir_fd;
SIMPLEQ_INIT(&matched_ids);
- path_packdir = got_repo_get_path_objects_pack(repo);
- if (path_packdir == NULL)
- return got_error_from_errno("got_repo_get_path_objects_pack");
+ packdir_fd = openat(got_repo_get_fd(repo),
+ GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
+ if (packdir_fd == -1) {
+ if (errno != ENOENT)
+ err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
+ goto done;
+ }
- packdir = opendir(path_packdir);
+ packdir = fdopendir(packdir_fd);
if (packdir == NULL) {
- if (errno != ENOENT)
- err = got_error_from_errno2("opendir", path_packdir);
+ err = got_error_from_errno("fdopendir");
goto done;
}
@@ -1223,17 +1257,17 @@ match_packed_object(struct got_object_id **unique_id,
struct got_packidx *packidx;
struct got_object_qid *qid;
-
if (!is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
- if (asprintf(&path_packidx, "%s/%s", path_packdir,
+ if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
dent->d_name) == -1) {
- err = got_error_from_errno("asprintf");
+ err = got_error_from_errno("strdup");
break;
}
- err = got_packidx_open(&packidx, path_packidx, 0);
+ err = got_packidx_open(&packidx, got_repo_get_fd(repo),
+ path_packidx, 0);
free(path_packidx);
if (err)
break;
@@ -1274,7 +1308,6 @@ match_packed_object(struct got_object_id **unique_id,
}
done:
got_object_id_queue_free(&matched_ids);
- free(path_packdir);
if (packdir && closedir(packdir) != 0 && err == NULL)
err = got_error_from_errno("closedir");
if (err) {