at commit a51b77aa
| 1 | #include "git-compat-util.h" |
| 2 | #include "object-file.h" |
| 3 | #include "odb.h" |
| 4 | #include "fsck.h" |
| 5 | #include "odb/source-inmemory.h" |
| 6 | #include "odb/streaming.h" |
| 7 | #include "oidtree.h" |
| 8 | #include "repository.h" |
| 9 | |
| 10 | struct inmemory_object { |
| 11 | enum object_type type; |
| 12 | const void *buf; |
| 13 | unsigned long size; |
| 14 | }; |
| 15 | |
| 16 | static const struct inmemory_object *find_cached_object(struct odb_source_inmemory *source, |
| 17 | const struct object_id *oid) |
| 18 | { |
| 19 | static const struct inmemory_object empty_tree = { |
| 20 | .type = OBJ_TREE, |
| 21 | .buf = "", |
| 22 | }; |
| 23 | const struct inmemory_object *object; |
| 24 | |
| 25 | if (source->objects) { |
| 26 | object = oidtree_get(source->objects, oid); |
| 27 | if (object) |
| 28 | return object; |
| 29 | } |
| 30 | |
| 31 | if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree)) |
| 32 | return &empty_tree; |
| 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | static void populate_object_info(struct odb_source_inmemory *source, |
| 38 | struct object_info *oi, |
| 39 | const struct inmemory_object *object) |
| 40 | { |
| 41 | if (!oi) |
| 42 | return; |
| 43 | |
| 44 | if (oi->typep) |
| 45 | *(oi->typep) = object->type; |
| 46 | if (oi->sizep) |
| 47 | *(oi->sizep) = object->size; |
| 48 | if (oi->disk_sizep) |
| 49 | *(oi->disk_sizep) = 0; |
| 50 | if (oi->delta_base_oid) |
| 51 | oidclr(oi->delta_base_oid, source->base.odb->repo->hash_algo); |
| 52 | if (oi->contentp) |
| 53 | *oi->contentp = xmemdupz(object->buf, object->size); |
| 54 | if (oi->mtimep) |
| 55 | *oi->mtimep = 0; |
| 56 | if (oi->source_infop) |
| 57 | oi->source_infop->source = &source->base; |
| 58 | } |
| 59 | |
| 60 | static int odb_source_inmemory_read_object_info(struct odb_source *source, |
| 61 | const struct object_id *oid, |
| 62 | struct object_info *oi, |
| 63 | enum object_info_flags flags UNUSED) |
| 64 | { |
| 65 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 66 | const struct inmemory_object *object; |
| 67 | |
| 68 | object = find_cached_object(inmemory, oid); |
| 69 | if (!object) |
| 70 | return -1; |
| 71 | |
| 72 | populate_object_info(inmemory, oi, object); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | struct odb_read_stream_inmemory { |
| 77 | struct odb_stream base; |
| 78 | const unsigned char *buf; |
| 79 | size_t offset; |
| 80 | }; |
| 81 | |
| 82 | static ssize_t odb_read_stream_inmemory_read(struct odb_stream *stream, |
| 83 | char *buf, size_t buf_len) |
| 84 | { |
| 85 | struct odb_read_stream_inmemory *inmemory = |
| 86 | container_of(stream, struct odb_read_stream_inmemory, base); |
| 87 | size_t bytes = buf_len; |
| 88 | |
| 89 | if (buf_len > inmemory->base.size - inmemory->offset) |
| 90 | bytes = inmemory->base.size - inmemory->offset; |
| 91 | |
| 92 | memcpy(buf, inmemory->buf + inmemory->offset, bytes); |
| 93 | inmemory->offset += bytes; |
| 94 | |
| 95 | return bytes; |
| 96 | } |
| 97 | |
| 98 | static int odb_read_stream_inmemory_close(struct odb_stream *stream UNUSED) |
| 99 | { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int odb_source_inmemory_read_object_stream(struct odb_stream **out, |
| 104 | struct odb_source *source, |
| 105 | const struct object_id *oid) |
| 106 | { |
| 107 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 108 | struct odb_read_stream_inmemory *stream; |
| 109 | const struct inmemory_object *object; |
| 110 | |
| 111 | object = find_cached_object(inmemory, oid); |
| 112 | if (!object) |
| 113 | return -1; |
| 114 | |
| 115 | CALLOC_ARRAY(stream, 1); |
| 116 | stream->base.read = odb_read_stream_inmemory_read; |
| 117 | stream->base.close = odb_read_stream_inmemory_close; |
| 118 | stream->base.size = object->size; |
| 119 | stream->base.type = object->type; |
| 120 | stream->buf = object->buf; |
| 121 | |
| 122 | *out = &stream->base; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | struct odb_source_inmemory_for_each_object_data { |
| 127 | struct odb_source_inmemory *inmemory; |
| 128 | const struct object_info *request; |
| 129 | odb_for_each_object_cb cb; |
| 130 | void *cb_data; |
| 131 | }; |
| 132 | |
| 133 | static int odb_source_inmemory_for_each_object_cb(const struct object_id *oid, |
| 134 | void *node_data, void *cb_data) |
| 135 | { |
| 136 | struct odb_source_inmemory_for_each_object_data *data = cb_data; |
| 137 | struct inmemory_object *object = node_data; |
| 138 | |
| 139 | if (data->request) { |
| 140 | struct object_info oi = *data->request; |
| 141 | populate_object_info(data->inmemory, &oi, object); |
| 142 | return data->cb(oid, &oi, data->cb_data); |
| 143 | } else { |
| 144 | return data->cb(oid, NULL, data->cb_data); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int odb_source_inmemory_for_each_object(struct odb_source *source, |
| 149 | const struct object_info *request, |
| 150 | odb_for_each_object_cb cb, |
| 151 | void *cb_data, |
| 152 | const struct odb_for_each_object_options *opts) |
| 153 | { |
| 154 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 155 | struct odb_source_inmemory_for_each_object_data payload = { |
| 156 | .inmemory = inmemory, |
| 157 | .request = request, |
| 158 | .cb = cb, |
| 159 | .cb_data = cb_data, |
| 160 | }; |
| 161 | struct object_id null_oid = { 0 }; |
| 162 | |
| 163 | if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) || |
| 164 | (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)) |
| 165 | return 0; |
| 166 | if (!inmemory->objects) |
| 167 | return 0; |
| 168 | |
| 169 | return oidtree_each(inmemory->objects, |
| 170 | opts->prefix ? opts->prefix : &null_oid, opts->prefix_hex_len, |
| 171 | odb_source_inmemory_for_each_object_cb, &payload); |
| 172 | } |
| 173 | |
| 174 | struct find_abbrev_len_data { |
| 175 | const struct object_id *oid; |
| 176 | unsigned len; |
| 177 | }; |
| 178 | |
| 179 | static int find_abbrev_len_cb(const struct object_id *oid, |
| 180 | struct object_info *oi UNUSED, |
| 181 | void *cb_data) |
| 182 | { |
| 183 | struct find_abbrev_len_data *data = cb_data; |
| 184 | unsigned len = oid_common_prefix_hexlen(oid, data->oid); |
| 185 | if (len != hash_algos[oid->algo].hexsz && len >= data->len) |
| 186 | data->len = len + 1; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int odb_source_inmemory_find_abbrev_len(struct odb_source *source, |
| 191 | const struct object_id *oid, |
| 192 | unsigned min_len, |
| 193 | unsigned *out) |
| 194 | { |
| 195 | struct odb_for_each_object_options opts = { |
| 196 | .prefix = oid, |
| 197 | .prefix_hex_len = min_len, |
| 198 | }; |
| 199 | struct find_abbrev_len_data data = { |
| 200 | .oid = oid, |
| 201 | .len = min_len, |
| 202 | }; |
| 203 | int ret; |
| 204 | |
| 205 | ret = odb_source_inmemory_for_each_object(source, NULL, find_abbrev_len_cb, |
| 206 | &data, &opts); |
| 207 | *out = data.len; |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static int count_objects_cb(const struct object_id *oid UNUSED, |
| 213 | struct object_info *oi UNUSED, |
| 214 | void *cb_data) |
| 215 | { |
| 216 | unsigned long *counter = cb_data; |
| 217 | (*counter)++; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int odb_source_inmemory_count_objects(struct odb_source *source, |
| 222 | enum odb_count_objects_flags flags UNUSED, |
| 223 | unsigned long *out) |
| 224 | { |
| 225 | struct odb_for_each_object_options opts = { 0 }; |
| 226 | *out = 0; |
| 227 | return odb_source_inmemory_for_each_object(source, NULL, count_objects_cb, |
| 228 | out, &opts); |
| 229 | } |
| 230 | |
| 231 | static int odb_source_inmemory_write_object(struct odb_source *source, |
| 232 | const void *buf, size_t len, |
| 233 | enum object_type type, |
| 234 | const struct object_id *oid, |
| 235 | const struct object_id *compat_oid UNUSED, |
| 236 | const time_t *mtime UNUSED, |
| 237 | enum odb_write_object_flags flags UNUSED) |
| 238 | { |
| 239 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 240 | struct inmemory_object *object; |
| 241 | |
| 242 | if (!inmemory->objects) { |
| 243 | CALLOC_ARRAY(inmemory->objects, 1); |
| 244 | oidtree_init(inmemory->objects); |
| 245 | } else if (oidtree_contains(inmemory->objects, oid)) { |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | CALLOC_ARRAY(object, 1); |
| 250 | object->size = len; |
| 251 | object->type = type; |
| 252 | object->buf = xmemdupz(buf, len); |
| 253 | |
| 254 | oidtree_insert(inmemory->objects, oid, object); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int odb_source_inmemory_write_object_stream(struct odb_source *source, |
| 260 | struct odb_stream *stream, |
| 261 | struct object_id *oid) |
| 262 | { |
| 263 | char buf[16384]; |
| 264 | size_t total_read = 0; |
| 265 | char *data; |
| 266 | int ret; |
| 267 | |
| 268 | CALLOC_ARRAY(data, stream->size); |
| 269 | while (1) { |
| 270 | ssize_t bytes_read; |
| 271 | |
| 272 | bytes_read = odb_stream_read(stream, buf, sizeof(buf)); |
| 273 | if (bytes_read < 0) { |
| 274 | ret = error("failed to read object stream"); |
| 275 | goto out; |
| 276 | } |
| 277 | if (!bytes_read) |
| 278 | break; |
| 279 | |
| 280 | if (total_read + bytes_read > stream->size) { |
| 281 | ret = error("object stream yielded more bytes than expected"); |
| 282 | goto out; |
| 283 | } |
| 284 | |
| 285 | memcpy(data + total_read, buf, bytes_read); |
| 286 | total_read += bytes_read; |
| 287 | } |
| 288 | |
| 289 | if (total_read != stream->size) { |
| 290 | ret = error("object stream yielded less bytes than expected"); |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | hash_object_file(source->odb->repo->hash_algo, data, total_read, |
| 295 | stream->type, oid); |
| 296 | |
| 297 | ret = odb_source_inmemory_write_object(source, data, stream->size, |
| 298 | stream->type, oid, NULL, NULL, 0); |
| 299 | if (ret < 0) |
| 300 | goto out; |
| 301 | |
| 302 | out: |
| 303 | free(data); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int odb_source_inmemory_freshen_object(struct odb_source *source, |
| 308 | const struct object_id *oid, |
| 309 | const time_t *mtime UNUSED) |
| 310 | { |
| 311 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 312 | if (find_cached_object(inmemory, oid)) |
| 313 | return 1; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int odb_source_inmemory_begin_transaction(struct odb_source *source UNUSED, |
| 318 | struct odb_transaction **out UNUSED, |
| 319 | enum odb_transaction_flags flags UNUSED) |
| 320 | { |
| 321 | return error("in-memory source does not support transactions"); |
| 322 | } |
| 323 | |
| 324 | static int odb_source_inmemory_read_alternates(struct odb_source *source UNUSED, |
| 325 | struct strvec *out UNUSED) |
| 326 | { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int odb_source_inmemory_write_alternate(struct odb_source *source UNUSED, |
| 331 | const char *alternate UNUSED) |
| 332 | { |
| 333 | return error("in-memory source does not support alternates"); |
| 334 | } |
| 335 | |
| 336 | static void odb_source_inmemory_close(struct odb_source *source UNUSED) |
| 337 | { |
| 338 | } |
| 339 | |
| 340 | static void odb_source_inmemory_prepare(struct odb_source *source UNUSED, |
| 341 | enum odb_prepare_flags flags UNUSED) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | static int inmemory_object_free(const struct object_id *oid UNUSED, |
| 346 | void *node_data, |
| 347 | void *cb_data UNUSED) |
| 348 | { |
| 349 | struct inmemory_object *object = node_data; |
| 350 | free((void *) object->buf); |
| 351 | free(object); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void odb_source_inmemory_free(struct odb_source *source) |
| 356 | { |
| 357 | struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); |
| 358 | |
| 359 | if (inmemory->objects) { |
| 360 | struct object_id null_oid = { 0 }; |
| 361 | |
| 362 | oidtree_each(inmemory->objects, &null_oid, 0, |
| 363 | inmemory_object_free, NULL); |
| 364 | oidtree_clear(inmemory->objects); |
| 365 | free(inmemory->objects); |
| 366 | } |
| 367 | |
| 368 | free(inmemory->base.path); |
| 369 | free(inmemory); |
| 370 | } |
| 371 | |
| 372 | static int odb_source_inmemory_fsck(struct odb_source *source UNUSED, |
| 373 | struct odb_fsck_options *opts UNUSED) |
| 374 | { |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb) |
| 379 | { |
| 380 | struct odb_source_inmemory *source; |
| 381 | |
| 382 | CALLOC_ARRAY(source, 1); |
| 383 | odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false); |
| 384 | |
| 385 | source->base.free = odb_source_inmemory_free; |
| 386 | source->base.close = odb_source_inmemory_close; |
| 387 | source->base.prepare = odb_source_inmemory_prepare; |
| 388 | source->base.fsck = odb_source_inmemory_fsck; |
| 389 | source->base.read_object_info = odb_source_inmemory_read_object_info; |
| 390 | source->base.read_object_stream = odb_source_inmemory_read_object_stream; |
| 391 | source->base.for_each_object = odb_source_inmemory_for_each_object; |
| 392 | source->base.find_abbrev_len = odb_source_inmemory_find_abbrev_len; |
| 393 | source->base.count_objects = odb_source_inmemory_count_objects; |
| 394 | source->base.write_object = odb_source_inmemory_write_object; |
| 395 | source->base.write_object_stream = odb_source_inmemory_write_object_stream; |
| 396 | source->base.freshen_object = odb_source_inmemory_freshen_object; |
| 397 | source->base.begin_transaction = odb_source_inmemory_begin_transaction; |
| 398 | source->base.read_alternates = odb_source_inmemory_read_alternates; |
| 399 | source->base.write_alternate = odb_source_inmemory_write_alternate; |
| 400 | |
| 401 | return source; |
| 402 | } |