commit 2090a03dadad63d6ea945cbe4abb19949b5aff44 from: Stefan Sperling date: Sun Sep 09 15:00:33 2018 UTC make got_packfile_open_object() accept a pack instead of a repo commit - 1510f46981bdb735f68ba6cf80ac2ff8e0971b85 commit + 2090a03dadad63d6ea945cbe4abb19949b5aff44 blob - ba5b7f508867f128f83a05c62f39a2159187276b blob + 1202094f2e3f254a1ea8df5f77cbacd47b8ceccf --- lib/got_lib_pack.h +++ lib/got_lib_pack.h @@ -161,7 +161,7 @@ const struct got_error* got_packidx_close(struct got_p int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *); const struct got_error *got_packfile_open_object(struct got_object **, - struct got_object_id *, struct got_repository *); + struct got_pack *, struct got_packidx *, int, struct got_object_id *); const struct got_error *got_packfile_extract_object(FILE **, struct got_object *, struct got_repository *); const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *, blob - 11b768f43c29b5eed657ffb5684b11607d28a63b blob + cf703fbcb757445a5e04d6a5673230301f5c4467 --- lib/object.c +++ lib/object.c @@ -161,6 +161,66 @@ open_loose_object(int *fd, struct got_object *obj, str } done: free(path); + return err; +} + +static const struct got_error * +get_packfile_path(char **path_packfile, struct got_packidx *packidx) +{ + size_t size; + + /* Packfile path contains ".pack" instead of ".idx", so add one byte. */ + size = strlen(packidx->path_packidx) + 2; + if (size < GOT_PACKFILE_NAMELEN + 1) + return got_error(GOT_ERR_BAD_PATH); + + *path_packfile = calloc(size, sizeof(**path_packfile)); + if (*path_packfile == NULL) + return got_error_from_errno(); + + /* Copy up to and excluding ".idx". */ + if (strlcpy(*path_packfile, packidx->path_packidx, + size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size) + return got_error(GOT_ERR_NO_SPACE); + + if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size) + return got_error(GOT_ERR_NO_SPACE); + + return NULL; +} + +static const struct got_error * +open_packed_object(struct got_object **obj, struct got_object_id *id, + struct got_repository *repo) +{ + const struct got_error *err = NULL; + struct got_pack *pack = NULL; + struct got_packidx *packidx = NULL; + int idx; + char *path_packfile; + + err = got_repo_search_packidx(&packidx, &idx, repo, id); + if (err) + return err; + + err = get_packfile_path(&path_packfile, packidx); + if (err) + return err; + + pack = got_repo_get_cached_pack(repo, path_packfile); + if (pack == NULL) { + err = got_repo_cache_pack(&pack, repo, path_packfile, packidx); + if (err) + goto done; + } + + err = got_packfile_open_object(obj, pack, packidx, idx, id); + if (err) + goto done; + + err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx); +done: + free(path_packfile); return err; } @@ -188,7 +248,7 @@ got_object_open(struct got_object **obj, struct got_re err = got_error_from_errno(); goto done; } - err = got_packfile_open_object(obj, id, repo); + err = open_packed_object(obj, id, repo); if (err) goto done; if (*obj == NULL) blob - 4ea89f7517554d13e857a8ae0abb3efc4b17e4a7 blob + 7e576516d56d0110c55d47b2b8a36975ec86335a --- lib/pack.c +++ lib/pack.c @@ -459,31 +459,6 @@ got_packidx_get_object_idx(struct got_packidx *packidx } return -1; -} - -static const struct got_error * -get_packfile_path(char **path_packfile, struct got_packidx *packidx) -{ - size_t size; - - /* Packfile path contains ".pack" instead of ".idx", so add one byte. */ - size = strlen(packidx->path_packidx) + 2; - if (size < GOT_PACKFILE_NAMELEN + 1) - return got_error(GOT_ERR_BAD_PATH); - - *path_packfile = calloc(size, sizeof(**path_packfile)); - if (*path_packfile == NULL) - return got_error_from_errno(); - - /* Copy up to and excluding ".idx". */ - if (strlcpy(*path_packfile, packidx->path_packidx, - size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size) - return got_error(GOT_ERR_NO_SPACE); - - if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size) - return got_error(GOT_ERR_NO_SPACE); - - return NULL; } const struct got_error * @@ -905,8 +880,8 @@ done: return err; } -static const struct got_error * -open_packed_object(struct got_object **obj, struct got_pack *pack, +const struct got_error * +got_packfile_open_object(struct got_object **obj, struct got_pack *pack, struct got_packidx *packidx, int idx, struct got_object_id *id) { const struct got_error *err = NULL; @@ -941,43 +916,8 @@ open_packed_object(struct got_object **obj, struct got default: err = got_error(GOT_ERR_OBJ_TYPE); break; - } - - return err; -} - -const struct got_error * -got_packfile_open_object(struct got_object **obj, struct got_object_id *id, - struct got_repository *repo) -{ - const struct got_error *err = NULL; - struct got_packidx *packidx = NULL; - struct got_pack *pack; - int idx; - char *path_packfile; - - err = got_repo_search_packidx(&packidx, &idx, repo, id); - if (err) - return err; - - err = get_packfile_path(&path_packfile, packidx); - if (err) - return err; - - pack = got_repo_get_cached_pack(repo, path_packfile); - if (pack == NULL) { - err = got_repo_cache_pack(&pack, repo, path_packfile, packidx); - if (err) - goto done; } - err = open_packed_object(obj, pack, packidx, idx, id); - if (err) - goto done; - - err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx); -done: - free(path_packfile); return err; }