open packed objects correctly; don't worry about their contents yet
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
diff --git a/include/got_object.h b/include/got_object.h
index 9e3f32b..204947b 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -73,9 +73,15 @@ struct got_object {
#define GOT_OBJ_TYPE_OFFSET_DELTA 6
#define GOT_OBJ_TYPE_REF_DELTA 7
+ int flags;
+#define GOT_OBJ_FLAG_PACKED 0x01
+
size_t hdrlen;
size_t size;
struct got_object_id id;
+
+ char *path_packfile;
+ off_t pack_offset;
};
struct got_repository;
diff --git a/lib/object.c b/lib/object.c
index a0ff6db..f3581b6 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -134,7 +134,7 @@ inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
n = fread(zb->inbuf, 1, zb->inlen, f);
if (n == 0) {
if (ferror(f))
- return got_error(GOT_ERR_IO);
+ return got_ferror(f, GOT_ERR_IO);
*outlenp = 0;
return NULL;
}
@@ -268,34 +268,26 @@ object_path(char **path, struct got_object_id *id, struct got_repository *repo)
return err;
}
-static const struct got_error *
-open_object(FILE **f, struct got_object_id *id, struct got_repository *repo)
+const struct got_error *
+open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
{
const struct got_error *err = NULL;
char *path;
- err = object_path(&path, id, repo);
+ if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+ return got_error(GOT_ERR_NOT_IMPL);
+ }
+
+ err = object_path(&path, &obj->id, repo);
if (err)
return err;
-
*f = fopen(path, "rb");
if (*f == NULL) {
- if (errno != ENOENT) {
- err = got_error_from_errno();
- goto done;
- }
- err = got_packfile_extract_object(f, id, repo);
- if (err)
- goto done;
- if (*f == NULL) {
- err = got_error(GOT_ERR_NO_OBJ);
- goto done;
- }
+ err = got_error_from_errno();
+ goto done;
}
done:
free(path);
- if (err && *f != NULL)
- fclose(*f);
return err;
}
@@ -303,24 +295,43 @@ const struct got_error *
got_object_open(struct got_object **obj, struct got_repository *repo,
struct got_object_id *id)
{
- const struct got_error *err;
+ const struct got_error *err = NULL;
+ char *path;
FILE *f;
- err = open_object(&f, id, repo);
+ err = object_path(&path, id, repo);
if (err)
return err;
- err = read_object_header(obj, repo, f);
- if (err == NULL)
+ f = fopen(path, "rb");
+ if (f == NULL) {
+ if (errno != ENOENT) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_packfile_open_object(obj, id, repo);
+ if (err)
+ goto done;
+ if (*obj == NULL)
+ err = got_error(GOT_ERR_NO_OBJ);
+ } else {
+ err = read_object_header(obj, repo, f);
+ if (err)
+ goto done;
memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
- else
+ }
+done:
+ free(path);
+ if (err && f)
fclose(f);
return err;
+
}
void
got_object_close(struct got_object *obj)
{
+ free(obj->path_packfile);
free(obj);
}
@@ -587,7 +598,7 @@ got_object_commit_open(struct got_commit_object **commit,
if (obj->type != GOT_OBJ_TYPE_COMMIT)
return got_error(GOT_ERR_OBJ_TYPE);
- err = open_object(&f, &obj->id, repo);
+ err = open_object(&f, obj, repo);
if (err)
return err;
@@ -656,7 +667,7 @@ got_object_tree_open(struct got_tree_object **tree,
if (obj->type != GOT_OBJ_TYPE_TREE)
return got_error(GOT_ERR_OBJ_TYPE);
- err = open_object(&f, &obj->id, repo);
+ err = open_object(&f, obj, repo);
if (err)
return err;
@@ -695,7 +706,7 @@ got_object_blob_open(struct got_blob_object **blob,
if (*blob == NULL)
return got_error(GOT_ERR_NO_MEM);
- err = open_object(&((*blob)->f), &obj->id, repo);
+ err = open_object(&((*blob)->f), obj, repo);
if (err) {
free(*blob);
return err;
diff --git a/lib/pack.c b/lib/pack.c
index dc94a55..7e7325a 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -292,16 +292,15 @@ get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
i = betoh32(packidx->fanout_table[id0 - 1]);
while (i < totobj) {
- struct got_object_id *oid = &packidx->sorted_ids[i++];
+ struct got_object_id *oid = &packidx->sorted_ids[i];
uint32_t offset;
int cmp = got_object_id_cmp(id, oid);
- if (cmp < 0)
- continue;
- if (cmp > 0)
+ if (cmp == 0)
+ return i;
+ else if (cmp > 0)
break;
-
- return i;
+ i++;
}
return -1;
@@ -328,36 +327,6 @@ read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
}
static const struct got_error *
-dump_plain_object(FILE *infile, uint8_t type, uint64_t size, FILE *outfile)
-{
- const char *type_tag = got_object_get_type_tag(type);
- size_t n;
-
- if (type_tag == NULL)
- return got_error(GOT_ERR_OBJ_TYPE);
-
- fprintf(outfile, "%s %llu", type_tag, size);
- fputc('\0', outfile);
-
- while (size > 0) {
- uint8_t data[2048];
- size_t len = MIN(size, sizeof(data));
-
- n = fread(data, len, 1, infile);
- if (n != 1)
- return got_ferror(infile, GOT_ERR_BAD_PACKIDX);
-
- n = fwrite(data, len, 1, outfile);
- if (n != 1)
- return got_ferror(outfile, GOT_ERR_BAD_PACKIDX);
-
- size -= len;
- }
-
- return NULL;
-}
-
-static const struct got_error *
decode_type_and_size(uint8_t *type, uint64_t *size, FILE *packfile)
{
uint8_t t = 0;
@@ -392,63 +361,21 @@ decode_type_and_size(uint8_t *type, uint64_t *size, FILE *packfile)
}
static const struct got_error *
-dump_packed_object(FILE **f, FILE *packfile, off_t offset)
-{
- const struct got_error *err = NULL;
- const char *template = "/tmp/got.XXXXXXXXXX";
- uint8_t type;
- uint64_t size;
- FILE *outfile = NULL;
-
- *f = got_opentemp();
- if (*f == NULL) {
- err = got_error(GOT_ERR_FILE_OPEN);
- goto done;
- }
-
- if (fseeko(packfile, offset, SEEK_SET) != 0) {
- err = got_error_from_errno();
- goto done;
- }
-
- err = decode_type_and_size(&type, &size, packfile);
- if (err)
- goto done;
-
- switch (type) {
- case GOT_OBJ_TYPE_COMMIT:
- case GOT_OBJ_TYPE_TREE:
- case GOT_OBJ_TYPE_BLOB:
- err = dump_plain_object(packfile, type, size, *f);
- break;
- case GOT_OBJ_TYPE_REF_DELTA:
- case GOT_OBJ_TYPE_TAG:
- case GOT_OBJ_TYPE_OFFSET_DELTA:
- default:
- err = got_error(GOT_ERR_NOT_IMPL);
- goto done;
- }
-
- rewind(*f);
-done:
- if (err && *f)
- fclose(*f);
- return err;
-}
-
-static const struct got_error *
-extract_object(FILE **f, const char *path_packdir,
- struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
+open_packed_object(struct got_object **obj, struct got_repository *repo,
+ const char *path_packdir, struct got_packidx_v2_hdr *packidx,
+ struct got_object_id *id)
{
const struct got_error *err = NULL;
int idx = get_object_idx(packidx, id);
off_t offset;
- char *path_packfile;
- FILE *packfile;
char hex[SHA1_DIGEST_STRING_LENGTH];
char *sha1str;
+ char *path_packfile;
+ FILE *packfile;
+ uint8_t type;
+ uint64_t size;
- *f = NULL;
+ *obj = NULL;
if (idx == -1) /* object not found in pack index */
return NULL;
@@ -475,20 +402,55 @@ extract_object(FILE **f, const char *path_packdir,
if (err)
goto done;
- printf("Dumping object at offset %llu\n", offset);
- err = dump_packed_object(f, packfile, offset);
+ if (fseeko(packfile, offset, SEEK_SET) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ err = decode_type_and_size(&type, &size, packfile);
if (err)
goto done;
+ *obj = calloc(1, sizeof(**obj));
+ if (*obj == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+
+ switch (type) {
+ case GOT_OBJ_TYPE_COMMIT:
+ case GOT_OBJ_TYPE_TREE:
+ case GOT_OBJ_TYPE_BLOB:
+ (*obj)->path_packfile = strdup(path_packfile);
+ if ((*obj)->path_packfile == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ (*obj)->type = type;
+ (*obj)->flags = GOT_OBJ_FLAG_PACKED;
+ (*obj)->hdrlen = 0;
+ (*obj)->size = size;
+ memcpy(&(*obj)->id, id, sizeof((*obj)->id));
+ (*obj)->pack_offset = offset;
+ break;
+ case GOT_OBJ_TYPE_REF_DELTA:
+ case GOT_OBJ_TYPE_TAG:
+ case GOT_OBJ_TYPE_OFFSET_DELTA:
+ default:
+ err = got_error(GOT_ERR_NOT_IMPL);
+ goto done;
+ }
done:
free(path_packfile);
+ if (err)
+ free(*obj);
if (packfile && fclose(packfile) == -1 && err == 0)
err = got_error_from_errno();
return err;
}
const struct got_error *
-got_packfile_extract_object(FILE **f, struct got_object_id *id,
+got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
struct got_repository *repo)
{
const struct got_error *err = NULL;
@@ -525,10 +487,10 @@ got_packfile_extract_object(FILE **f, struct got_object_id *id,
if (err)
goto done;
- err = extract_object(f, path_packdir, packidx, id);
+ err = open_packed_object(obj, repo, path_packdir, packidx, id);
if (err)
goto done;
- if (*f != NULL)
+ if (*obj != NULL)
break;
}
diff --git a/lib/pack.h b/lib/pack.h
index 9e034eb..e26be48 100644
--- a/lib/pack.h
+++ b/lib/pack.h
@@ -121,5 +121,5 @@ const struct got_error *got_packidx_open(struct got_packidx_v2_hdr **,
const char *);
void got_packidx_close(struct got_packidx_v2_hdr *);
-const struct got_error *got_packfile_extract_object(FILE **,
+const struct got_error *got_packfile_open_object(struct got_object **,
struct got_object_id *, struct got_repository *);