refactor dump_packed_object() a bit; no functional change
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
diff --git a/include/got_error.h b/include/got_error.h
index 65bbcde..ebe18b5 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -33,6 +33,7 @@
#define GOT_ERR_PACKIDX_CSUM 15
#define GOT_ERR_BAD_PACKFILE 16
#define GOT_ERR_NO_OBJ 17
+#define GOT_ERR_NOT_IMPL 18
static const struct got_error {
int code;
@@ -56,6 +57,7 @@ static const struct got_error {
{ GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
{ GOT_ERR_BAD_PACKFILE, "bad pack file" },
{ GOT_ERR_NO_OBJ, "object not found" },
+ { GOT_ERR_NOT_IMPL, "feature not implemented" },
};
const struct got_error * got_error(int code);
diff --git a/lib/pack.c b/lib/pack.c
index 1d72c45..dc94a55 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -328,89 +328,107 @@ read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
}
static const struct got_error *
-dump_packed_object(FILE **f, FILE *packfile, off_t offset)
+dump_plain_object(FILE *infile, uint8_t type, uint64_t size, FILE *outfile)
{
- const struct got_error *err = NULL;
- const char *template = "/tmp/got.XXXXXXXXXX";
- uint64_t size = 0;
- uint8_t type = 0;
- uint8_t sizeN;
- int i;
+ const char *type_tag = got_object_get_type_tag(type);
size_t n;
- const char *type_tag;
- *f = got_opentemp();
- if (*f == NULL) {
- err = got_error(GOT_ERR_FILE_OPEN);
- goto done;
- }
+ if (type_tag == NULL)
+ return got_error(GOT_ERR_OBJ_TYPE);
- if (fseeko(packfile, offset, SEEK_SET) != 0) {
- err = got_error_from_errno();
- goto done;
+ 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;
}
- i = 0;
+ return NULL;
+}
+
+static const struct got_error *
+decode_type_and_size(uint8_t *type, uint64_t *size, FILE *packfile)
+{
+ uint8_t t = 0;
+ uint64_t s = 0;
+ uint8_t sizeN;
+ size_t n;
+ int i = 0;
+
do {
/* We do not support size values which don't fit in 64 bit. */
- if (i > 9) {
- err = got_error(GOT_ERR_NO_SPACE);
- goto done;
- }
+ if (i > 9)
+ return got_error(GOT_ERR_NO_SPACE);
n = fread(&sizeN, sizeof(sizeN), 1, packfile);
- if (n != 1) {
- err = got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
- goto done;
- }
+ if (n != 1)
+ return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
if (i == 0) {
- type = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
+ t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
- size = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
+ s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
} else {
size_t shift = 4 + 7 * (i - 1);
- size |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
+ s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
}
i++;
} while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
- if (type == GOT_OBJ_TYPE_OFFSET_DELTA)
- printf("object type OFFSET_DELTA not yet implemented\n");
- else if (type == GOT_OBJ_TYPE_REF_DELTA)
- printf("object type REF_DELTA not yet implemented\n");
- else if (type == GOT_OBJ_TYPE_TAG)
- printf("object type TAG not yet implemented\n");
+ *type = t;
+ *size = s;
+ return NULL;
+}
+
+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;
- type_tag = got_object_get_type_tag(type);
- if (type_tag == NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_HDR);
+ *f = got_opentemp();
+ if (*f == NULL) {
+ err = got_error(GOT_ERR_FILE_OPEN);
goto done;
}
- fprintf(*f, "%s %llu", type_tag, size);
- fputc('\0', *f);
-
- while (size > 0) {
- uint8_t data[2048];
- size_t len = MIN(size, sizeof(data));
-
- n = fread(data, len, 1, packfile);
- if (n != 1) {
- err = got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
- goto done;
- }
+ if (fseeko(packfile, offset, SEEK_SET) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
- n = fwrite(data, len, 1, *f);
- if (n != 1) {
- err = got_ferror(*f, GOT_ERR_BAD_PACKIDX);
- goto done;
- }
+ err = decode_type_and_size(&type, &size, packfile);
+ if (err)
+ goto done;
- size -= len;
+ 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;
}
- printf("object type is %d\n", type);
rewind(*f);
done:
if (err && *f)