Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/odb/source-inmemory.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / odb / source-inmemory.c` 393 lines ยท 10545 bytes Raw Blame History 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 enum odb_read_status 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 struct strbuf *errmsg UNUSED) 65 { 66 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 67 const struct inmemory_object *object; 68 69 object = find_cached_object(inmemory, oid); 70 if (!object) 71 return ODB_READ_NOT_FOUND; 72 73 populate_object_info(inmemory, oi, object); 74 return 0; 75 } 76 77 struct odb_read_stream_inmemory { 78 struct odb_stream base; 79 const unsigned char *buf; 80 size_t offset; 81 }; 82 83 static ssize_t odb_read_stream_inmemory_read(struct odb_stream *stream, 84 char *buf, size_t buf_len) 85 { 86 struct odb_read_stream_inmemory *inmemory = 87 container_of(stream, struct odb_read_stream_inmemory, base); 88 size_t bytes = buf_len; 89 90 if (buf_len > inmemory->base.size - inmemory->offset) 91 bytes = inmemory->base.size - inmemory->offset; 92 93 memcpy(buf, inmemory->buf + inmemory->offset, bytes); 94 inmemory->offset += bytes; 95 96 return bytes; 97 } 98 99 static int odb_read_stream_inmemory_close(struct odb_stream *stream UNUSED) 100 { 101 return 0; 102 } 103 104 static int odb_source_inmemory_read_object_stream(struct odb_stream **out, 105 struct odb_source *source, 106 const struct object_id *oid) 107 { 108 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 109 struct odb_read_stream_inmemory *stream; 110 const struct inmemory_object *object; 111 112 object = find_cached_object(inmemory, oid); 113 if (!object) 114 return -1; 115 116 CALLOC_ARRAY(stream, 1); 117 stream->base.read = odb_read_stream_inmemory_read; 118 stream->base.close = odb_read_stream_inmemory_close; 119 stream->base.size = object->size; 120 stream->base.type = object->type; 121 stream->buf = object->buf; 122 123 *out = &stream->base; 124 return 0; 125 } 126 127 struct odb_source_inmemory_for_each_object_data { 128 struct odb_source_inmemory *inmemory; 129 const struct object_info *request; 130 odb_for_each_object_cb cb; 131 void *cb_data; 132 }; 133 134 static int odb_source_inmemory_for_each_object_cb(const struct object_id *oid, 135 void *node_data, void *cb_data) 136 { 137 struct odb_source_inmemory_for_each_object_data *data = cb_data; 138 struct inmemory_object *object = node_data; 139 140 if (data->request) { 141 struct object_info oi = *data->request; 142 populate_object_info(data->inmemory, &oi, object); 143 return data->cb(oid, &oi, data->cb_data); 144 } else { 145 return data->cb(oid, NULL, data->cb_data); 146 } 147 } 148 149 static int odb_source_inmemory_for_each_object(struct odb_source *source, 150 const struct object_info *request, 151 odb_for_each_object_cb cb, 152 void *cb_data, 153 const struct odb_for_each_object_options *opts) 154 { 155 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 156 struct odb_source_inmemory_for_each_object_data payload = { 157 .inmemory = inmemory, 158 .request = request, 159 .cb = cb, 160 .cb_data = cb_data, 161 }; 162 struct object_id null_oid = { 0 }; 163 164 /* 165 * In-memory objects are neither promisor objects nor do they live in 166 * the local object directory, so there is nothing to iterate over. 167 */ 168 if (opts->flags & (ODB_FOR_EACH_OBJECT_PROMISOR_ONLY | 169 ODB_FOR_EACH_OBJECT_LOCAL_ONLY)) 170 return 0; 171 if (!inmemory->objects) 172 return 0; 173 174 return oidtree_each(inmemory->objects, 175 opts->prefix ? opts->prefix : &null_oid, opts->prefix_hex_len, 176 odb_source_inmemory_for_each_object_cb, &payload); 177 } 178 179 struct find_abbrev_len_data { 180 const struct object_id *oid; 181 unsigned len; 182 }; 183 184 static int find_abbrev_len_cb(const struct object_id *oid, 185 struct object_info *oi UNUSED, 186 void *cb_data) 187 { 188 struct find_abbrev_len_data *data = cb_data; 189 unsigned len = oid_common_prefix_hexlen(oid, data->oid); 190 if (len != hash_algos[oid->algo].hexsz && len >= data->len) 191 data->len = len + 1; 192 return 0; 193 } 194 195 static int odb_source_inmemory_find_abbrev_len(struct odb_source *source, 196 const struct object_id *oid, 197 unsigned min_len, 198 unsigned *out) 199 { 200 struct odb_for_each_object_options opts = { 201 .prefix = oid, 202 .prefix_hex_len = min_len, 203 }; 204 struct find_abbrev_len_data data = { 205 .oid = oid, 206 .len = min_len, 207 }; 208 int ret; 209 210 ret = odb_source_inmemory_for_each_object(source, NULL, find_abbrev_len_cb, 211 &data, &opts); 212 *out = data.len; 213 214 return ret; 215 } 216 217 static int count_objects_cb(const struct object_id *oid UNUSED, 218 struct object_info *oi UNUSED, 219 void *cb_data) 220 { 221 unsigned long *counter = cb_data; 222 (*counter)++; 223 return 0; 224 } 225 226 static int odb_source_inmemory_count_objects(struct odb_source *source, 227 enum odb_count_objects_flags flags UNUSED, 228 unsigned long *out) 229 { 230 struct odb_for_each_object_options opts = { 0 }; 231 *out = 0; 232 return odb_source_inmemory_for_each_object(source, NULL, count_objects_cb, 233 out, &opts); 234 } 235 236 static int odb_source_inmemory_write_object(struct odb_source *source, 237 const void *buf, size_t len, 238 enum object_type type, 239 const struct object_id *oid, 240 const struct object_id *compat_oid UNUSED, 241 const time_t *mtime UNUSED, 242 enum odb_write_object_flags flags UNUSED) 243 { 244 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 245 struct inmemory_object *object; 246 247 if (!inmemory->objects) { 248 CALLOC_ARRAY(inmemory->objects, 1); 249 oidtree_init(inmemory->objects); 250 } else if (oidtree_contains(inmemory->objects, oid)) { 251 return 0; 252 } 253 254 CALLOC_ARRAY(object, 1); 255 object->size = len; 256 object->type = type; 257 object->buf = xmemdupz(buf, len); 258 259 oidtree_insert(inmemory->objects, oid, object); 260 261 return 0; 262 } 263 264 static int odb_source_inmemory_write_object_stream(struct odb_source *source, 265 struct odb_stream *stream, 266 struct object_id *oid) 267 { 268 char buf[16384]; 269 size_t total_read = 0; 270 char *data; 271 int ret; 272 273 CALLOC_ARRAY(data, stream->size); 274 while (1) { 275 ssize_t bytes_read; 276 277 bytes_read = odb_stream_read(stream, buf, sizeof(buf)); 278 if (bytes_read < 0) { 279 ret = error("failed to read object stream"); 280 goto out; 281 } 282 if (!bytes_read) 283 break; 284 285 if (total_read + bytes_read > stream->size) { 286 ret = error("object stream yielded more bytes than expected"); 287 goto out; 288 } 289 290 memcpy(data + total_read, buf, bytes_read); 291 total_read += bytes_read; 292 } 293 294 if (total_read != stream->size) { 295 ret = error("object stream yielded less bytes than expected"); 296 goto out; 297 } 298 299 hash_object_file(source->odb->repo->hash_algo, data, total_read, 300 stream->type, oid); 301 302 ret = odb_source_inmemory_write_object(source, data, stream->size, 303 stream->type, oid, NULL, NULL, 0); 304 if (ret < 0) 305 goto out; 306 307 out: 308 free(data); 309 return ret; 310 } 311 312 static int odb_source_inmemory_freshen_object(struct odb_source *source, 313 const struct object_id *oid, 314 const time_t *mtime UNUSED) 315 { 316 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 317 if (find_cached_object(inmemory, oid)) 318 return 1; 319 return 0; 320 } 321 322 static int odb_source_inmemory_begin_transaction(struct odb_source *source UNUSED, 323 struct odb_transaction **out UNUSED, 324 enum odb_transaction_flags flags UNUSED) 325 { 326 return error("in-memory source does not support transactions"); 327 } 328 329 static void odb_source_inmemory_close(struct odb_source *source UNUSED) 330 { 331 } 332 333 static void odb_source_inmemory_prepare(struct odb_source *source UNUSED, 334 enum odb_prepare_flags flags UNUSED) 335 { 336 } 337 338 static int inmemory_object_free(const struct object_id *oid UNUSED, 339 void *node_data, 340 void *cb_data UNUSED) 341 { 342 struct inmemory_object *object = node_data; 343 free((void *) object->buf); 344 free(object); 345 return 0; 346 } 347 348 static void odb_source_inmemory_free(struct odb_source *source) 349 { 350 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source); 351 352 if (inmemory->objects) { 353 struct object_id null_oid = { 0 }; 354 355 oidtree_each(inmemory->objects, &null_oid, 0, 356 inmemory_object_free, NULL); 357 oidtree_clear(inmemory->objects); 358 free(inmemory->objects); 359 } 360 361 free(inmemory->base.path); 362 free(inmemory); 363 } 364 365 static int odb_source_inmemory_fsck(struct odb_source *source UNUSED, 366 struct odb_fsck_options *opts UNUSED) 367 { 368 return 0; 369 } 370 371 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb) 372 { 373 struct odb_source_inmemory *source; 374 375 CALLOC_ARRAY(source, 1); 376 odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source"); 377 378 source->base.free = odb_source_inmemory_free; 379 source->base.close = odb_source_inmemory_close; 380 source->base.prepare = odb_source_inmemory_prepare; 381 source->base.fsck = odb_source_inmemory_fsck; 382 source->base.read_object_info = odb_source_inmemory_read_object_info; 383 source->base.read_object_stream = odb_source_inmemory_read_object_stream; 384 source->base.for_each_object = odb_source_inmemory_for_each_object; 385 source->base.find_abbrev_len = odb_source_inmemory_find_abbrev_len; 386 source->base.count_objects = odb_source_inmemory_count_objects; 387 source->base.write_object = odb_source_inmemory_write_object; 388 source->base.write_object_stream = odb_source_inmemory_write_object_stream; 389 source->base.freshen_object = odb_source_inmemory_freshen_object; 390 source->base.begin_transaction = odb_source_inmemory_begin_transaction; 391 392 return source; 393 }