Commit ff563a3de3ff9931c2702516c74c8aea8d5cf142

Stefan Sperling 2019-05-23T21:04:23

replace got_pack_get_packfile_size() with simple fstat()

diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 18d2e6e..34beeb2 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -170,5 +170,4 @@ const struct got_error *got_packfile_extract_object(struct got_pack *,
     struct got_object *, FILE *, FILE *, FILE *);
 const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
     struct got_object *, struct got_pack *);
-const struct got_error *got_pack_get_packfile_size(size_t *, const char *);
 struct got_pack *got_repo_get_cached_pack(struct got_repository *, const char *);
diff --git a/lib/pack.c b/lib/pack.c
index 6779323..9f38ab3 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -66,49 +66,6 @@ verify_fanout_table(uint32_t *fanout_table)
 }
 
 const struct got_error *
-got_pack_get_packfile_size(size_t *size, const char *path)
-{
-	struct stat sb;
-	char *dot;
-
-	*size = 0;
-
-	dot = strrchr(path, '.');
-	if (dot == NULL)
-		return got_error(GOT_ERR_BAD_PATH);
-
-	/* Path must point to a pack index or to a pack file. */
-	if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
-		const struct got_error *err = NULL;
-		char *path_pack;
-		char base_path[PATH_MAX];
-
-		/* Convert pack index path to pack file path. */
-		if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
-			return got_error(GOT_ERR_NO_SPACE);
-		dot = strrchr(base_path, '.');
-		if (dot == NULL)
-			return got_error(GOT_ERR_BAD_PATH);
-		*dot = '\0';
-		if (asprintf(&path_pack, "%s.pack", base_path) == -1)
-			return got_error_from_errno("asprintf");
-
-		if (stat(path_pack, &sb) != 0)
-			err = got_error_from_errno("asprintf");
-		free(path_pack);
-		if (err)
-			return err;
-	} else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
-		if (stat(path, &sb) != 0)
-			return got_error_from_errno2("stat", path);
-	} else
-		return got_error(GOT_ERR_BAD_PATH);
-
-	*size = sb.st_size;
-	return 0;
-}
-
-const struct got_error *
 got_packidx_init_hdr(struct got_packidx *p, int verify)
 {
 	const struct got_error *err = NULL;
diff --git a/lib/repository.c b/lib/repository.c
index 4a2efe8..bff89f5 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -722,6 +722,7 @@ got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
 {
 	const struct got_error *err = NULL;
 	struct got_pack *pack = NULL;
+	struct stat sb;
 	int i;
 
 	if (packp)
@@ -756,9 +757,11 @@ got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
 	if (err)
 		goto done;
 
-	err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
-	if (err)
+	if (fstat(pack->fd, &sb) != 0) {
+		err = got_error_from_errno("fstat");
 		goto done;
+	}
+	pack->filesize = sb.st_size;
 
 	pack->privsep_child = NULL;