Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/odb/source-files.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "abspath.h"
3
#include "blob.h"
4
#include "chdir-notify.h"
5
#include "config.h"
6
#include "gettext.h"
7
#include "hex.h"
8
#include "lockfile.h"
9
#include "object-file.h"
10
#include "odb.h"
11
#include "odb/source.h"
12
#include "odb/source-files.h"
13
#include "odb/source-loose.h"
14
#include "pack-objects.h"
15
#include "packfile.h"
16
#include "path.h"
17
#include "promisor-remote.h"
18
#include "quote.h"
19
#include "repack.h"
20
#include "run-command.h"
21
#include "strbuf.h"
22
#include "string-list.h"
23
#include "strmap.h"
24
#include "strvec.h"
25
#include "tree.h"
26
#include "write-or-die.h"
27
28
struct odb_files_dir *odb_files_dir_new(struct object_database *odb,
29
          const char *path, bool local)
30
0
{
31
0
  struct odb_files_dir *dir;
32
33
0
  CALLOC_ARRAY(dir, 1);
34
0
  dir->abspath = absolute_pathdup(path);
35
0
  dir->local = local;
36
0
  dir->loose = odb_source_loose_new(odb, path);
37
0
  dir->packed = odb_source_packed_new(odb, path, local);
38
39
0
  return dir;
40
0
}
41
42
void odb_files_dir_free(struct odb_files_dir *dir)
43
0
{
44
0
  if (!dir)
45
0
    return;
46
0
  odb_source_free(&dir->loose->base);
47
0
  odb_source_free(&dir->packed->base);
48
0
  free(dir->abspath);
49
0
  free(dir);
50
0
}
51
52
static void odb_source_files_reparent(const char *old_cwd,
53
              const char *new_cwd,
54
              void *cb_data)
55
0
{
56
0
  struct odb_source_files *files = cb_data;
57
0
  char *path = reparent_relative_path(old_cwd, new_cwd,
58
0
              files->base.path);
59
60
0
  free(files->base.path);
61
0
  files->base.path = path;
62
0
}
63
64
static void odb_source_files_free(struct odb_source *source)
65
0
{
66
0
  struct odb_source_files *files = odb_source_files_downcast(source);
67
68
0
  chdir_notify_unregister(odb_source_files_reparent, files);
69
70
0
  while (files->dirs) {
71
0
    struct odb_files_dir *next = files->dirs->next;
72
0
    odb_files_dir_free(files->dirs);
73
0
    files->dirs = next;
74
0
  }
75
0
  strset_clear(&files->dir_paths);
76
0
  stat_validity_clear(&files->alternates_validity);
77
78
0
  odb_source_release(&files->base);
79
0
  free(files);
80
0
}
81
82
static void odb_source_files_close(struct odb_source *source)
83
0
{
84
0
  struct odb_source_files *files = odb_source_files_downcast(source);
85
86
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
87
0
    odb_source_close(&dir->loose->base);
88
0
    odb_source_close(&dir->packed->base);
89
0
  }
90
0
}
91
92
static int odb_source_files_create_on_disk(struct odb_source *source,
93
             const struct odb_create_on_disk_options *opts)
94
0
{
95
0
  struct lock_file alternates_lock = LOCK_INIT;
96
0
  struct strbuf path = STRBUF_INIT;
97
0
  struct strset seen = STRSET_INIT;
98
0
  struct strbuf line = STRBUF_INIT;
99
0
  int ret;
100
101
0
  safe_create_leading_directories_no_share(source->path);
102
0
  safe_create_dir(source->odb->repo, source->path, 1);
103
104
0
  strbuf_addf(&path, "%s/pack", source->path);
105
0
  safe_create_dir(source->odb->repo, path.buf, 1);
106
107
0
  strbuf_reset(&path);
108
0
  strbuf_addf(&path, "%s/info", source->path);
109
0
  safe_create_dir(source->odb->repo, path.buf, 1);
110
111
0
  if (opts->alternates && opts->alternates->nr) {
112
0
    FILE *alternates, *orig;
113
114
0
    strbuf_reset(&path);
115
0
    strbuf_addf(&path, "%s/info/alternates", source->path);
116
117
0
    repo_hold_lock_file_for_update(source->odb->repo, &alternates_lock,
118
0
                 path.buf, LOCK_DIE_ON_ERROR);
119
120
0
    alternates = fdopen_lock_file(&alternates_lock, "w");
121
0
    if (!alternates) {
122
0
      ret = error_errno(_("unable to fdopen alternates lockfile"));
123
0
      goto out;
124
0
    }
125
126
    /*
127
     * The alternates file may already exist, e.g. when it has been
128
     * seeded from a template directory. Read any preexisting
129
     * entries so that we don't end up writing duplicates.
130
     */
131
0
    orig = fopen(path.buf, "r");
132
0
    if (orig) {
133
0
      while (strbuf_getline(&line, orig) != EOF) {
134
0
        strset_add(&seen, line.buf);
135
0
        fprintf(alternates, "%s\n", line.buf);
136
0
      }
137
138
0
      if (ferror(orig)) {
139
0
        ret = error_errno(_("unable to read alternates file"));
140
0
        fclose(orig);
141
0
        goto out;
142
0
      }
143
144
0
      fclose(orig);
145
0
    } else if (errno != ENOENT) {
146
0
      ret = error_errno(_("unable to read alternates file"));
147
0
      goto out;
148
0
    }
149
150
0
    for (size_t i = 0; i < opts->alternates->nr; i++) {
151
0
      const char *alternate = opts->alternates->v[i];
152
0
      if (!strset_add(&seen, alternate))
153
0
        continue;
154
0
      fprintf(alternates, "%s\n", alternate);
155
0
    }
156
157
0
    if (ferror(alternates)) {
158
0
      ret = error_errno(_("unable to write alternates file"));
159
0
      goto out;
160
0
    }
161
162
0
    if (commit_lock_file(&alternates_lock)) {
163
0
      ret = error_errno(_("unable to commit alternates file"));
164
0
      goto out;
165
0
    }
166
0
  }
167
168
  /* Reprepare the object database to activate alternates. */
169
0
  odb_reprepare(source->odb);
170
171
0
  ret = 0;
172
173
0
out:
174
0
  rollback_lock_file(&alternates_lock);
175
0
  strbuf_release(&line);
176
0
  strbuf_release(&path);
177
0
  strset_clear(&seen);
178
0
  return ret;
179
0
}
180
181
/*
182
 * Return non-zero iff the path is usable as an alternate object directory.
183
 */
184
static bool odb_files_dir_is_usable(struct odb_source_files *files,
185
            const char *path)
186
0
{
187
0
  struct strbuf buf = STRBUF_INIT;
188
0
  bool usable = false;
189
190
  /*
191
   * Prevent the common mistake of listing the same thing twice,
192
   * or object directory itself.
193
   *
194
   * NEEDSWORK: we're using "core.ignoreCase" to deduplicate
195
   * alternates that _may_ be the same. This requires quite a bit
196
   * of boilerplate for dubious benefit:
197
   *
198
   *   - Duplicating alternates should really only lead to
199
   *     regressed performance.
200
   *
201
   *   - We don't properly resolve symlinks or mointpoints, so we
202
   *     may still end up duplicating alternates.
203
   *
204
   *   - The value may be lying, in which case we might
205
   *     deduplicate alternates that are in fact not mapping to the
206
   *     same directory.
207
   *
208
   * We should investigate whether we can remove this whole
209
   * mechanism outright.
210
   */
211
0
  if (strset_empty(&files->dir_paths)) {
212
0
    files->dir_paths_icase = 0;
213
0
    repo_config_get_bool(files->base.odb->repo, "core.ignorecase",
214
0
             &files->dir_paths_icase);
215
216
0
    strbuf_realpath(&buf, files->base.path, 1);
217
0
    if (files->dir_paths_icase)
218
0
      strbuf_tolower(&buf);
219
0
    strset_add(&files->dir_paths, buf.buf);
220
221
0
    for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
222
0
      strbuf_reset(&buf);
223
0
      strbuf_addstr(&buf, dir->abspath);
224
0
      if (files->dir_paths_icase)
225
0
        strbuf_tolower(&buf);
226
0
      strset_add(&files->dir_paths, buf.buf);
227
0
    }
228
0
  }
229
230
0
  strbuf_reset(&buf);
231
0
  strbuf_addstr(&buf, path);
232
0
  if (files->dir_paths_icase)
233
0
    strbuf_tolower(&buf);
234
235
0
  if (strset_contains(&files->dir_paths, buf.buf))
236
0
    goto out;
237
238
  /* Detect cases where alternate disappeared */
239
0
  if (!is_directory(path)) {
240
0
    error(_("object directory %s does not exist; "
241
0
      "check .git/objects/info/alternates"),
242
0
          path);
243
0
    goto out;
244
0
  }
245
246
0
  usable = true;
247
248
0
out:
249
0
  strbuf_release(&buf);
250
0
  return usable;
251
0
}
252
253
static void parse_alternates(const char *string,
254
           int sep,
255
           const char *relative_base,
256
           struct strvec *out)
257
0
{
258
0
  struct strbuf pathbuf = STRBUF_INIT;
259
0
  struct strbuf buf = STRBUF_INIT;
260
261
0
  if (!string || !*string)
262
0
    return;
263
264
0
  while (*string) {
265
0
    const char *end;
266
267
0
    strbuf_reset(&buf);
268
0
    strbuf_reset(&pathbuf);
269
270
0
    if (*string == '#') {
271
      /* comment; consume up to next separator */
272
0
      end = strchrnul(string, sep);
273
0
    } else if (*string == '"' && !unquote_c_style(&buf, string, &end)) {
274
      /*
275
       * quoted path; unquote_c_style has copied the
276
       * data for us and set "end". Broken quoting (e.g.,
277
       * an entry that doesn't end with a quote) falls
278
       * back to the unquoted case below.
279
       */
280
0
    } else {
281
      /* normal, unquoted path */
282
0
      end = strchrnul(string, sep);
283
0
      strbuf_add(&buf, string, end - string);
284
0
    }
285
286
0
    if (*end)
287
0
      end++;
288
0
    string = end;
289
290
0
    if (!buf.len)
291
0
      continue;
292
293
0
    if (!is_absolute_path(buf.buf) && relative_base) {
294
0
      strbuf_realpath(&pathbuf, relative_base, 1);
295
0
      strbuf_addch(&pathbuf, '/');
296
0
    }
297
0
    strbuf_addbuf(&pathbuf, &buf);
298
299
0
    strbuf_reset(&buf);
300
0
    if (!strbuf_realpath(&buf, pathbuf.buf, 0)) {
301
0
      error(_("unable to normalize alternate object path: %s"),
302
0
            pathbuf.buf);
303
0
      continue;
304
0
    }
305
306
    /*
307
     * The trailing slash after the directory name is given by
308
     * this function at the end. Remove duplicates.
309
     */
310
0
    while (buf.len && buf.buf[buf.len - 1] == '/')
311
0
      strbuf_setlen(&buf, buf.len - 1);
312
313
0
    strvec_push(out, buf.buf);
314
0
  }
315
316
0
  strbuf_release(&pathbuf);
317
0
  strbuf_release(&buf);
318
0
}
319
320
static void read_alternates(const char *object_dir, struct strvec *out,
321
          struct stat_validity *validity)
322
0
{
323
0
  struct strbuf buf = STRBUF_INIT;
324
0
  char *path;
325
326
0
  path = xstrfmt("%s/info/alternates", object_dir);
327
0
  if (!validity || !stat_validity_check(validity, path)) {
328
0
    bool successful = false;
329
0
    int fd;
330
331
0
    fd = open(path, O_RDONLY);
332
0
    if (fd < 0) {
333
0
      warn_on_fopen_errors(path);
334
0
    } else {
335
0
      if (validity)
336
0
        stat_validity_update(validity, fd);
337
0
      if (strbuf_read(&buf, fd, 1024) < 0) {
338
0
        warning_errno(_("unable to read alternates file '%s'"),
339
0
                path);
340
0
      } else {
341
0
        parse_alternates(buf.buf, '\n', object_dir, out);
342
0
        successful = true;
343
0
      }
344
0
    }
345
346
0
    if (validity && !successful)
347
0
      stat_validity_clear(validity);
348
0
    if (fd >= 0)
349
0
      close(fd);
350
0
  }
351
352
0
  strbuf_release(&buf);
353
0
  free(path);
354
0
}
355
356
static void add_alternate_recursively(struct odb_source_files *files,
357
              const char *path,
358
              int depth)
359
0
{
360
0
  struct odb_files_dir *alternate;
361
0
  struct strvec alternates = STRVEC_INIT;
362
363
0
  if (!odb_files_dir_is_usable(files, path))
364
0
    goto out;
365
366
0
  alternate = odb_files_dir_new(files->base.odb, path, false);
367
368
  /* add the alternate entry */
369
0
  *files->dirs_tail = alternate;
370
0
  files->dirs_tail = &(alternate->next);
371
372
0
  if (files->dir_paths_icase) {
373
0
    struct strbuf path = STRBUF_INIT;
374
375
0
    strbuf_addstr(&path, alternate->abspath);
376
0
    strbuf_tolower(&path);
377
0
    strset_add(&files->dir_paths, path.buf);
378
379
0
    strbuf_release(&path);
380
0
  } else {
381
0
    strset_add(&files->dir_paths, alternate->abspath);
382
0
  }
383
384
  /* recursively add alternates */
385
0
  read_alternates(alternate->abspath, &alternates, NULL);
386
0
  if (alternates.nr && depth + 1 > 5) {
387
0
    error(_("%s: ignoring alternate object stores, nesting too deep"),
388
0
          path);
389
0
  } else {
390
0
    for (size_t i = 0; i < alternates.nr; i++)
391
0
      add_alternate_recursively(files, alternates.v[i], depth + 1);
392
0
  }
393
394
0
 out:
395
0
  strvec_clear(&alternates);
396
0
}
397
398
static void prepare_alternates(struct odb_source_files *files,
399
             const char *alternate_db)
400
0
{
401
0
  struct strvec alternates = STRVEC_INIT;
402
403
0
  parse_alternates(alternate_db, PATH_SEP, NULL, &alternates);
404
0
  read_alternates(files->base.path, &alternates,
405
0
      &files->alternates_validity);
406
407
0
  for (size_t i = 0; i < alternates.nr; i++)
408
0
    add_alternate_recursively(files, alternates.v[i], 0);
409
410
0
  strvec_clear(&alternates);
411
0
}
412
413
static void odb_source_files_prepare(struct odb_source *source,
414
             enum odb_prepare_flags flags)
415
0
{
416
0
  struct odb_source_files *files = odb_source_files_downcast(source);
417
418
  /*
419
   * Reprepare alternates, in case the alternates file was modified
420
   * during the course of this process. This only _adds_ directories to
421
   * the linked list, so existing directories will continue to exist
422
   * for the lifetime of the process.
423
   */
424
0
  if (flags & ODB_PREPARE_FLUSH_CACHES)
425
0
    prepare_alternates(files, NULL);
426
427
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
428
0
    odb_source_prepare(&dir->loose->base, flags);
429
0
    odb_source_prepare(&dir->packed->base, flags);
430
0
  }
431
0
}
432
433
static enum odb_read_status odb_source_files_read_object_info(struct odb_source *source,
434
                    const struct object_id *oid,
435
                    struct object_info *oi,
436
                    enum object_info_flags flags,
437
                    struct strbuf *errmsg)
438
0
{
439
0
  struct odb_source_files *files = odb_source_files_downcast(source);
440
0
  struct odb_files_dir **tail = NULL;
441
0
  enum odb_read_status status = ODB_READ_NOT_FOUND;
442
0
  enum odb_read_status ret;
443
0
  bool retry = false;
444
445
0
retry:
446
  /*
447
   * Reading an object may fail even though the object exists, for
448
   * example because it is corrupt. Report this failure to the caller in
449
   * case none of the directories was able to read the object, and
450
   * prefer the first such error in case multiple reads have failed.
451
   */
452
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
453
0
    if (retry)
454
0
      odb_source_prepare(&dir->packed->base, ODB_PREPARE_FLUSH_CACHES);
455
456
0
    ret = odb_source_read_object_info(&dir->packed->base, oid, oi, flags,
457
0
              status == ODB_READ_NOT_FOUND ? errmsg : NULL);
458
0
    if (!ret)
459
0
      return 0;
460
0
    if (ret != ODB_READ_NOT_FOUND && status == ODB_READ_NOT_FOUND)
461
0
      status = ret;
462
0
  }
463
464
  /*
465
   * When we have reprepared alternates then we only re-scan directories
466
   * that were newly added.
467
   */
468
0
  for (struct odb_files_dir *dir = tail ? *tail : files->dirs; dir; dir = dir->next) {
469
0
    ret = odb_source_read_object_info(&dir->loose->base, oid, oi, flags,
470
0
              status == ODB_READ_NOT_FOUND ? errmsg : NULL);
471
0
    if (!ret)
472
0
      return 0;
473
0
    if (ret != ODB_READ_NOT_FOUND && status == ODB_READ_NOT_FOUND)
474
0
      status = ret;
475
0
  }
476
477
0
  if (!retry && !(flags & OBJECT_INFO_QUICK)) {
478
0
    tail = files->dirs_tail;
479
0
    prepare_alternates(files, NULL);
480
0
    retry = true;
481
0
    goto retry;
482
0
  }
483
484
0
  return status;
485
0
}
486
487
static int odb_source_files_read_object_stream(struct odb_stream **out,
488
                 struct odb_source *source,
489
                 const struct object_id *oid)
490
0
{
491
0
  struct odb_source_files *files = odb_source_files_downcast(source);
492
493
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next)
494
0
    if (!odb_source_read_object_stream(out, &dir->packed->base, oid))
495
0
      return 0;
496
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next)
497
0
    if (!odb_source_read_object_stream(out, &dir->loose->base, oid))
498
0
      return 0;
499
500
0
  return -1;
501
0
}
502
503
static int odb_source_files_for_each_object(struct odb_source *source,
504
              const struct object_info *request,
505
              odb_for_each_object_cb cb,
506
              void *cb_data,
507
              const struct odb_for_each_object_options *opts)
508
0
{
509
0
  struct odb_source_files *files = odb_source_files_downcast(source);
510
0
  int ret;
511
512
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
513
0
    if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !dir->local)
514
0
      continue;
515
516
0
    if (!(opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
517
0
      ret = odb_source_for_each_object(&dir->loose->base, request, cb, cb_data, opts);
518
0
      if (ret)
519
0
        return ret;
520
0
    }
521
522
0
    ret = odb_source_for_each_object(&dir->packed->base, request, cb, cb_data, opts);
523
0
    if (ret)
524
0
      return ret;
525
0
  }
526
527
0
  return 0;
528
0
}
529
530
static int odb_source_files_count_objects(struct odb_source *source,
531
            enum odb_count_objects_flags flags,
532
            unsigned long *out)
533
0
{
534
0
  struct odb_source_files *files = odb_source_files_downcast(source);
535
0
  unsigned long count = 0;
536
0
  int ret;
537
538
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
539
0
    unsigned long dir_count;
540
541
0
    ret = odb_source_count_objects(&dir->packed->base, flags, &dir_count);
542
0
    if (ret < 0)
543
0
      goto out;
544
0
    count += dir_count;
545
546
0
    if (!(flags & ODB_COUNT_OBJECTS_APPROXIMATE)) {
547
0
      ret = odb_source_count_objects(&dir->loose->base, flags, &dir_count);
548
0
      if (ret < 0)
549
0
        goto out;
550
0
      count += dir_count;
551
0
    }
552
0
  }
553
554
0
  *out = count;
555
0
  ret = 0;
556
557
0
out:
558
0
  return ret;
559
0
}
560
561
static int odb_source_files_find_abbrev_len(struct odb_source *source,
562
              const struct object_id *oid,
563
              unsigned min_len,
564
              unsigned *out)
565
0
{
566
0
  struct odb_source_files *files = odb_source_files_downcast(source);
567
0
  unsigned len = min_len;
568
0
  int ret = 0;
569
570
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
571
0
    ret = odb_source_find_abbrev_len(&dir->packed->base, oid, len, &len);
572
0
    if (ret < 0)
573
0
      goto out;
574
575
0
    ret = odb_source_find_abbrev_len(&dir->loose->base, oid, len, &len);
576
0
    if (ret < 0)
577
0
      goto out;
578
0
  }
579
580
0
  *out = len;
581
0
  ret = 0;
582
583
0
out:
584
0
  return ret;
585
0
}
586
587
static int odb_source_files_freshen_object(struct odb_source *source,
588
             const struct object_id *oid,
589
             const time_t *mtime)
590
0
{
591
0
  struct odb_source_files *files = odb_source_files_downcast(source);
592
593
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next)
594
0
    if (odb_source_freshen_object(&dir->packed->base, oid, mtime) ||
595
0
        odb_source_freshen_object(&dir->loose->base, oid, mtime))
596
0
      return 1;
597
598
0
  return 0;
599
0
}
600
601
static int odb_source_files_write_object(struct odb_source *source,
602
           const void *buf, size_t len,
603
           enum object_type type,
604
           const struct object_id *oid,
605
           const struct object_id *compat_oid,
606
           const time_t *mtime,
607
           enum odb_write_object_flags flags)
608
0
{
609
0
  struct odb_source_files *files = odb_source_files_downcast(source);
610
0
  return odb_source_write_object(&files->dirs->loose->base, buf, len, type,
611
0
               oid, compat_oid, mtime, flags);
612
0
}
613
614
static int odb_source_files_write_object_stream(struct odb_source *source,
615
            struct odb_stream *stream,
616
            struct object_id *oid)
617
0
{
618
0
  struct odb_source_files *files = odb_source_files_downcast(source);
619
0
  return odb_source_write_object_stream(&files->dirs->loose->base, stream, oid);
620
0
}
621
622
static int odb_source_files_begin_transaction(struct odb_source *source,
623
                struct odb_transaction **out,
624
                enum odb_transaction_flags flags)
625
0
{
626
0
  return odb_transaction_files_begin(source, out, flags);
627
0
}
628
629
static int too_many_loose_objects(struct odb_source_files *files, int limit)
630
0
{
631
0
  unsigned long loose_count;
632
633
0
  if (limit <= 0)
634
0
    return 0;
635
636
0
  if (odb_source_count_objects(&files->dirs->loose->base, ODB_COUNT_OBJECTS_APPROXIMATE,
637
0
             &loose_count) < 0)
638
0
    return 0;
639
640
  /*
641
   * This is weird, but stems from legacy behaviour: the GC auto
642
   * threshold was always essentially interpreted as if it was rounded up
643
   * to the next multiple 256 of, so we retain this behaviour for now.
644
   */
645
0
  return loose_count > (DIV_ROUND_UP(((unsigned long) limit), 256) * 256);
646
0
}
647
648
static struct packed_git *find_base_packs(struct odb_source_files *files,
649
            struct string_list *packs,
650
            unsigned long limit)
651
0
{
652
0
  struct packfile_list_entry *e;
653
0
  struct packed_git *base = NULL;
654
655
0
  for (e = packfile_store_get_packs(files->dirs->packed); e; e = e->next) {
656
0
    if (e->pack->is_cruft)
657
0
      continue;
658
0
    if (limit) {
659
0
      if ((uintmax_t) e->pack->pack_size >= limit)
660
0
        string_list_append(packs, e->pack->pack_name);
661
0
    } else if (!base || base->pack_size < e->pack->pack_size) {
662
0
      base = e->pack;
663
0
    }
664
0
  }
665
666
0
  if (base)
667
0
    string_list_append(packs, base->pack_name);
668
669
0
  return base;
670
0
}
671
672
static int too_many_packs(struct odb_source_files *files, int gc_auto_pack_limit)
673
0
{
674
0
  struct packfile_list_entry *e;
675
0
  int cnt = 0;
676
677
0
  if (gc_auto_pack_limit <= 0)
678
0
    return 0;
679
680
0
  for (e = packfile_store_get_packs(files->dirs->packed); e; e = e->next) {
681
0
    if (e->pack->pack_keep)
682
0
      continue;
683
    /*
684
     * Perhaps check the size of the pack and count only
685
     * very small ones here?
686
     */
687
0
    cnt++;
688
0
  }
689
0
  return gc_auto_pack_limit < cnt;
690
0
}
691
692
static uint64_t total_ram(void)
693
0
{
694
0
#if defined(HAVE_SYSINFO)
695
0
  struct sysinfo si;
696
697
0
  if (!sysinfo(&si)) {
698
0
    uint64_t total = si.totalram;
699
700
0
    if (si.mem_unit > 1)
701
0
      total *= (uint64_t)si.mem_unit;
702
0
    return total;
703
0
  }
704
#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
705
  uint64_t physical_memory;
706
  int mib[2];
707
  size_t length;
708
709
  mib[0] = CTL_HW;
710
# if defined(HW_MEMSIZE)
711
  mib[1] = HW_MEMSIZE;
712
# elif defined(HW_PHYSMEM64)
713
  mib[1] = HW_PHYSMEM64;
714
# else
715
  mib[1] = HW_PHYSMEM;
716
# endif
717
  length = sizeof(physical_memory);
718
  if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) {
719
    if (length == 4) {
720
      uint32_t mem;
721
722
      if (!sysctl(mib, 2, &mem, &length, NULL, 0))
723
        physical_memory = mem;
724
    }
725
    return physical_memory;
726
  }
727
#elif defined(GIT_WINDOWS_NATIVE)
728
  MEMORYSTATUSEX memInfo;
729
730
  memInfo.dwLength = sizeof(MEMORYSTATUSEX);
731
  if (GlobalMemoryStatusEx(&memInfo))
732
    return memInfo.ullTotalPhys;
733
#endif
734
0
  return 0;
735
0
}
736
737
static uint64_t estimate_repack_memory(struct odb_source_files *files,
738
               struct packed_git *pack)
739
0
{
740
0
  unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
741
0
  unsigned long delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT;
742
0
  unsigned long nr_objects;
743
0
  size_t os_cache, heap;
744
745
0
  if (odb_source_count_objects(&files->base, ODB_COUNT_OBJECTS_APPROXIMATE,
746
0
             &nr_objects) < 0)
747
0
    return 0;
748
749
0
  if (!pack || !nr_objects)
750
0
    return 0;
751
752
0
  repo_config_get_ulong(files->base.odb->repo, "pack.deltacachesize",
753
0
            &max_delta_cache_size);
754
0
  repo_config_get_ulong(files->base.odb->repo, "core.deltabasecachelimit",
755
0
            &delta_base_cache_limit);
756
757
  /*
758
   * First we have to scan through at least one pack.
759
   * Assume enough room in OS file cache to keep the entire pack
760
   * or we may accidentally evict data of other processes from
761
   * the cache.
762
   */
763
0
  os_cache = pack->pack_size + pack->index_size;
764
  /* then pack-objects needs lots more for book keeping */
765
0
  heap = sizeof(struct object_entry) * nr_objects;
766
  /*
767
   * internal rev-list --all --objects takes up some memory too,
768
   * let's say half of it is for blobs
769
   */
770
0
  heap += sizeof(struct blob) * nr_objects / 2;
771
  /*
772
   * and the other half is for trees (commits and tags are
773
   * usually insignificant)
774
   */
775
0
  heap += sizeof(struct tree) * nr_objects / 2;
776
  /* and then obj_hash[], underestimated in fact */
777
0
  heap += sizeof(struct object *) * nr_objects;
778
  /* revindex is used also */
779
0
  heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
780
  /*
781
   * read_sha1_file() (either at delta calculation phase, or
782
   * writing phase) also fills up the delta base cache
783
   */
784
0
  heap += delta_base_cache_limit;
785
  /* and of course pack-objects has its own delta cache */
786
0
  heap += max_delta_cache_size;
787
788
0
  return os_cache + heap;
789
0
}
790
791
static int keep_one_pack(struct string_list_item *item, void *data)
792
0
{
793
0
  struct strvec *args = data;
794
0
  strvec_pushf(args, "--keep-pack=%s", basename(item->string));
795
0
  return 0;
796
0
}
797
798
static void add_repack_all_option(struct repository *repo,
799
          const struct odb_optimize_options *opts,
800
          struct string_list *keep_pack,
801
          struct strvec *args)
802
0
{
803
0
  char *repack_filter = NULL;
804
0
  char *repack_filter_to = NULL;
805
806
0
  repo_config_get_string(repo, "gc.repackfilter", &repack_filter);
807
0
  repo_config_get_string(repo, "gc.repackfilterto", &repack_filter_to);
808
809
0
  if (opts->prune_expire && !strcmp(opts->prune_expire, "now") &&
810
0
      !(opts->cruft_packs && opts->expire_to))
811
0
    strvec_push(args, "-a");
812
0
  else if (opts->cruft_packs) {
813
0
    strvec_push(args, "--cruft");
814
0
    if (opts->prune_expire)
815
0
      strvec_pushf(args, "--cruft-expiration=%s", opts->prune_expire);
816
0
    if (opts->max_cruft_size)
817
0
      strvec_pushf(args, "--max-cruft-size=%lu",
818
0
             opts->max_cruft_size);
819
0
    if (opts->expire_to)
820
0
      strvec_pushf(args, "--expire-to=%s", opts->expire_to);
821
0
  } else {
822
0
    strvec_push(args, "-A");
823
0
    if (opts->prune_expire)
824
0
      strvec_pushf(args, "--unpack-unreachable=%s", opts->prune_expire);
825
0
  }
826
827
0
  if (keep_pack)
828
0
    for_each_string_list(keep_pack, keep_one_pack, args);
829
830
0
  if (repack_filter && *repack_filter)
831
0
    strvec_pushf(args, "--filter=%s", repack_filter);
832
0
  if (repack_filter_to && *repack_filter_to)
833
0
    strvec_pushf(args, "--filter-to=%s", repack_filter_to);
834
835
0
  free(repack_filter);
836
0
  free(repack_filter_to);
837
0
}
838
839
static void add_repack_incremental_option(struct strvec *args)
840
0
{
841
0
  strvec_push(args, "--no-write-bitmap-index");
842
0
}
843
844
bool odb_source_files_optimize_required(struct odb_source *source,
845
          const struct odb_optimize_options *opts)
846
0
{
847
0
  struct odb_source_files *files = odb_source_files_downcast(source);
848
0
  struct repository *repo = source->odb->repo;
849
850
0
  switch (opts->strategy) {
851
0
  case ODB_OPTIMIZE_INCREMENTAL: {
852
0
    int gc_auto_threshold = 6700;
853
0
    int gc_auto_pack_limit = 50;
854
855
0
    repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
856
0
    repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
857
858
    /*
859
     * Setting gc.auto to 0 or negative can disable the
860
     * automatic gc.
861
     */
862
0
    if (gc_auto_threshold <= 0)
863
0
      return false;
864
0
    if (!too_many_packs(files, gc_auto_pack_limit) &&
865
0
        !too_many_loose_objects(files, gc_auto_threshold))
866
0
      return false;
867
868
0
    return true;
869
0
  }
870
0
  case ODB_OPTIMIZE_GEOMETRIC: {
871
0
    struct pack_geometry geometry = {
872
0
      .split_factor = 2,
873
0
    };
874
0
    struct pack_objects_args po_args = {
875
0
      .local = 1,
876
0
    };
877
0
    struct existing_packs existing_packs = EXISTING_PACKS_INIT;
878
0
    struct string_list kept_packs = STRING_LIST_INIT_DUP;
879
0
    int auto_value = 6700;
880
0
    bool ret;
881
882
0
    repo_config_get_int(repo, "maintenance.geometric-repack.auto",
883
0
            &auto_value);
884
0
    if (!auto_value)
885
0
      return false;
886
0
    if (auto_value < 0)
887
0
      return true;
888
889
0
    repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
890
0
            &geometry.split_factor);
891
892
0
    existing_packs.repo = repo;
893
0
    existing_packs_collect(&existing_packs, &kept_packs);
894
0
    pack_geometry_init(&geometry, &existing_packs, &po_args);
895
0
    pack_geometry_split(&geometry);
896
897
    /*
898
     * When we'd merge at least two packs with one another we always
899
     * perform the repack.
900
     */
901
0
    if (geometry.split) {
902
0
      ret = true;
903
0
      goto out;
904
0
    }
905
906
    /*
907
     * Otherwise, we estimate the number of loose objects to determine
908
     * whether we want to create a new packfile or not.
909
     */
910
0
    if (too_many_loose_objects(files, auto_value)) {
911
0
      ret = true;
912
0
      goto out;
913
0
    }
914
915
0
    ret = false;
916
917
0
  out:
918
0
    existing_packs_release(&existing_packs);
919
0
    pack_geometry_release(&geometry);
920
0
    return ret;
921
0
  }
922
0
  default:
923
0
    BUG("unknown maintenance strategy '%d'", opts->strategy);
924
0
  }
925
0
}
926
927
int odb_source_files_optimize(struct odb_source *source,
928
            const struct odb_optimize_options *opts)
929
0
{
930
0
  struct odb_source_files *files = odb_source_files_downcast(source);
931
0
  struct repository *repo = source->odb->repo;
932
0
  struct child_process repack_cmd = CHILD_PROCESS_INIT;
933
0
  unsigned long big_pack_threshold = 0;
934
0
  int gc_auto_threshold = 6700;
935
0
  int gc_auto_pack_limit = 50;
936
0
  int ret;
937
938
0
  repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
939
0
  repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
940
0
  repo_config_get_ulong(repo, "gc.bigpackthreshold", &big_pack_threshold);
941
942
0
  if (repo->repository_format_precious_objects)
943
0
    return 0;
944
945
0
  repack_cmd.git_cmd = 1;
946
0
  repack_cmd.odb_to_close = repo->objects;
947
948
0
  strvec_pushl(&repack_cmd.args, "repack", "-d", "-l", NULL);
949
0
  if (opts->flags & ODB_OPTIMIZE_NO_REUSE_DELTAS)
950
0
    strvec_push(&repack_cmd.args, "-f");
951
0
  if (opts->depth > 0)
952
0
    strvec_pushf(&repack_cmd.args, "--depth=%d", opts->depth);
953
0
  if (opts->window > 0)
954
0
    strvec_pushf(&repack_cmd.args, "--window=%d", opts->window);
955
0
  if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
956
0
    strvec_push(&repack_cmd.args, "-q");
957
958
  /*
959
   * There's three cases we need to consider:
960
   *
961
   *   - If we're invoked without `--auto` we'll need to perform a full
962
   *     repack.
963
   *
964
   *   - If we're invoked with `--auto` and there's too many packs, then
965
   *     we perform a full repack, as well.
966
   *
967
   *   - Otherwise we perform an incremental repack.
968
   */
969
0
  switch (opts->strategy) {
970
0
  case ODB_OPTIMIZE_INCREMENTAL:
971
0
    if (!(opts->flags & ODB_OPTIMIZE_AUTO)) {
972
0
      struct string_list keep_pack = STRING_LIST_INIT_NODUP;
973
974
0
      if (opts->keep_largest_pack != -1) {
975
0
        if (opts->keep_largest_pack)
976
0
          find_base_packs(files, &keep_pack, 0);
977
0
      } else if (big_pack_threshold) {
978
0
        find_base_packs(files, &keep_pack, big_pack_threshold);
979
0
      }
980
981
0
      add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
982
0
      string_list_clear(&keep_pack, 0);
983
0
    } else {
984
0
      if (too_many_packs(files, gc_auto_pack_limit)) {
985
0
        struct string_list keep_pack = STRING_LIST_INIT_NODUP;
986
987
0
        if (big_pack_threshold) {
988
0
          find_base_packs(files, &keep_pack, big_pack_threshold);
989
0
          if (keep_pack.nr >= (unsigned long) gc_auto_pack_limit) {
990
0
            string_list_clear(&keep_pack, 0);
991
0
            find_base_packs(files, &keep_pack, 0);
992
0
          }
993
0
        } else {
994
0
          struct packed_git *p = find_base_packs(files, &keep_pack, 0);
995
0
          uint64_t mem_have, mem_want;
996
997
0
          mem_have = total_ram();
998
0
          mem_want = estimate_repack_memory(files, p);
999
1000
          /*
1001
           * Only allow 1/2 of memory for pack-objects, leave
1002
           * the rest for the OS and other processes in the
1003
           * system.
1004
           */
1005
0
          if (!mem_have || mem_want < mem_have / 2)
1006
0
            string_list_clear(&keep_pack, 0);
1007
0
        }
1008
1009
0
        add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args);
1010
0
        string_list_clear(&keep_pack, 0);
1011
0
      } else {
1012
0
        add_repack_incremental_option(&repack_cmd.args);
1013
0
      }
1014
0
    }
1015
1016
0
    break;
1017
0
  case ODB_OPTIMIZE_GEOMETRIC: {
1018
0
    struct pack_geometry geometry = {
1019
0
      .split_factor = 2,
1020
0
    };
1021
0
    struct pack_objects_args po_args = {
1022
0
      .local = 1,
1023
0
    };
1024
0
    struct existing_packs existing_packs = EXISTING_PACKS_INIT;
1025
0
    struct string_list kept_packs = STRING_LIST_INIT_DUP;
1026
1027
0
    repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor",
1028
0
            &geometry.split_factor);
1029
1030
0
    existing_packs.repo = repo;
1031
0
    existing_packs_collect(&existing_packs, &kept_packs);
1032
0
    pack_geometry_init(&geometry, &existing_packs, &po_args);
1033
0
    pack_geometry_split(&geometry);
1034
1035
0
    if (geometry.split < geometry.pack_nr) {
1036
0
      strvec_pushf(&repack_cmd.args, "--geometric=%d",
1037
0
             geometry.split_factor);
1038
0
    } else {
1039
0
      add_repack_all_option(repo, opts, NULL, &repack_cmd.args);
1040
0
    }
1041
0
    if (repo->settings.core_multi_pack_index)
1042
0
      strvec_push(&repack_cmd.args, "--write-midx");
1043
1044
0
    existing_packs_release(&existing_packs);
1045
0
    pack_geometry_release(&geometry);
1046
0
    break;
1047
0
  }
1048
0
  default:
1049
0
    die("unknown maintenance strategy '%d'", opts->strategy);
1050
0
  }
1051
1052
0
  if (run_command(&repack_cmd)) {
1053
0
    ret = error("failed to run %s", repack_cmd.args.v[0]);
1054
0
    goto out;
1055
0
  }
1056
1057
  /* Geometric repacking uses cruft packs, so we don't have to prune separately. */
1058
0
  if (opts->strategy != ODB_OPTIMIZE_GEOMETRIC && opts->prune_expire) {
1059
0
    struct child_process prune_cmd = CHILD_PROCESS_INIT;
1060
1061
0
    strvec_pushl(&prune_cmd.args, "prune", "--expire", NULL);
1062
    /* run `git prune` even if using cruft packs */
1063
0
    strvec_push(&prune_cmd.args, opts->prune_expire);
1064
0
    if (!(opts->flags & ODB_OPTIMIZE_VERBOSE))
1065
0
      strvec_push(&prune_cmd.args, "--no-progress");
1066
0
    if (repo_has_promisor_remote(repo))
1067
0
      strvec_push(&prune_cmd.args,
1068
0
            "--exclude-promisor-objects");
1069
0
    prune_cmd.git_cmd = 1;
1070
1071
0
    if (run_command(&prune_cmd)) {
1072
0
      ret = error("failed to run %s", prune_cmd.args.v[0]);
1073
0
      goto out;
1074
0
    }
1075
0
  }
1076
1077
0
  if (opts->flags & ODB_OPTIMIZE_AUTO && too_many_loose_objects(files, gc_auto_threshold))
1078
0
    warning(_("There are too many unreachable loose objects; "
1079
0
      "run 'git prune' to remove them."));
1080
1081
0
  ret = 0;
1082
1083
0
out:
1084
0
  return ret;
1085
0
}
1086
1087
struct odb_pack_generator_files {
1088
  struct odb_pack_generator base;
1089
  struct child_process cp;
1090
};
1091
1092
static int odb_pack_generator_files_finish(struct odb_pack_generator *_generator)
1093
0
{
1094
0
  struct odb_pack_generator_files *generator =
1095
0
    (struct odb_pack_generator_files *)_generator;
1096
0
  int ret;
1097
1098
0
  ret = finish_command(&generator->cp);
1099
0
  free(generator);
1100
1101
0
  if (ret) {
1102
    /*
1103
     * On failure, pack-objects is expected to have written a
1104
     * useful error message to its standard error stream already.
1105
     * Death by signal is worth mentioning, though, with the
1106
     * exception of SIGPIPE: that is a normal occurrence when the
1107
     * consumer of the pack hangs up.
1108
     */
1109
0
    if (ret > 128 && ret - 128 == SIGPIPE)
1110
0
      return -1;
1111
0
    if (ret > 128)
1112
0
      error(_("pack-objects died of signal %d"), ret - 128);
1113
0
    return -1;
1114
0
  }
1115
1116
0
  return 0;
1117
0
}
1118
1119
static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
1120
            struct odb_pack_generator **out,
1121
            const struct odb_generate_pack_options *opts)
1122
0
{
1123
0
  struct odb_pack_generator_files *generator;
1124
0
  struct child_process *cp;
1125
0
  FILE *in;
1126
1127
0
  CALLOC_ARRAY(generator, 1);
1128
0
  child_process_init(&generator->cp);
1129
0
  cp = &generator->cp;
1130
1131
  /*
1132
   * The hook is expected to spawn "$hook git pack-objects <args...>"
1133
   * and to behave like git-pack-objects(1) would have. This can for
1134
   * example be used to serve precomputed packfiles.
1135
   */
1136
0
  if (opts->pack_objects_hook) {
1137
0
    strvec_push(&cp->args, opts->pack_objects_hook);
1138
0
    strvec_push(&cp->args, "git");
1139
0
    cp->use_shell = 1;
1140
0
  } else {
1141
0
    cp->git_cmd = 1;
1142
0
  }
1143
1144
  /*
1145
   * The caller-provided shallow boundary overrides any shallow state
1146
   * that the repository itself may have, so the shallow file needs to
1147
   * be neutralized.
1148
   */
1149
0
  if (opts->shallows.nr) {
1150
0
    strvec_push(&cp->args, "--shallow-file");
1151
0
    strvec_push(&cp->args, "");
1152
0
  }
1153
0
  strvec_push(&cp->args, "pack-objects");
1154
0
  strvec_push(&cp->args, "--revs");
1155
0
  strvec_push(&cp->args, "--stdout");
1156
0
  if (opts->thin)
1157
0
    strvec_push(&cp->args, "--thin");
1158
0
  if (opts->shallow)
1159
0
    strvec_push(&cp->args, "--shallow");
1160
0
  if (opts->ofs_delta)
1161
0
    strvec_push(&cp->args, "--delta-base-offset");
1162
0
  if (opts->include_tag)
1163
0
    strvec_push(&cp->args, "--include-tag");
1164
0
  if (opts->missing_allow_promisor)
1165
0
    strvec_push(&cp->args, "--missing=allow-promisor");
1166
0
  if (opts->disable_bitmaps)
1167
0
    strvec_push(&cp->args, "--no-use-bitmap-index");
1168
0
  switch (opts->progress) {
1169
0
  case ODB_GENERATE_PACK_PROGRESS_NONE:
1170
0
    strvec_push(&cp->args, "--quiet");
1171
0
    break;
1172
0
  case ODB_GENERATE_PACK_PROGRESS_STANDARD:
1173
0
    strvec_push(&cp->args, "--progress");
1174
0
    break;
1175
0
  case ODB_GENERATE_PACK_PROGRESS_VERBOSE:
1176
0
    strvec_push(&cp->args, "--all-progress");
1177
0
    break;
1178
0
  default:
1179
0
    BUG("unknown progress option %d", opts->progress);
1180
0
  }
1181
0
  if (opts->filter_spec)
1182
0
    strvec_pushf(&cp->args, "--filter=%s", opts->filter_spec);
1183
0
  if (opts->uri_protocols)
1184
0
    for (size_t i = 0; i < opts->uri_protocols->nr; i++)
1185
0
      strvec_pushf(&cp->args, "--uri-protocol=%s",
1186
0
             opts->uri_protocols->items[i].string);
1187
1188
0
  cp->in = -1;
1189
0
  cp->out = opts->pack_fd;
1190
0
  cp->err = opts->progress_fd;
1191
0
  cp->clean_on_exit = 1;
1192
1193
0
  if (start_command(cp)) {
1194
0
    free(generator);
1195
0
    return error(_("could not spawn pack-objects"));
1196
0
  }
1197
1198
  /*
1199
   * Feed the objects to pack-objects. This is safe to do synchronously
1200
   * because pack-objects consumes all of its standard input before it
1201
   * starts to generate the pack.
1202
   */
1203
0
  in = xfdopen(cp->in, "w");
1204
0
  for (size_t i = 0; i < opts->shallows.nr; i++)
1205
0
    fprintf(in, "--shallow %s\n", oid_to_hex(&opts->shallows.oid[i]));
1206
0
  for (size_t i = 0; i < opts->wants.nr; i++)
1207
0
    fprintf(in, "%s\n", oid_to_hex(&opts->wants.oid[i]));
1208
0
  fprintf(in, "--not\n");
1209
0
  for (size_t i = 0; i < opts->haves.nr; i++)
1210
0
    fprintf(in, "%s\n", oid_to_hex(&opts->haves.oid[i]));
1211
0
  fprintf(in, "\n");
1212
0
  fflush(in);
1213
0
  if (ferror(in)) {
1214
0
    error(_("error writing to pack-objects"));
1215
0
    fclose(in);
1216
0
    if (opts->pack_fd < 0)
1217
0
      close(cp->out);
1218
0
    if (opts->progress_fd < 0)
1219
0
      close(cp->err);
1220
0
    finish_command(cp);
1221
0
    free(generator);
1222
0
    return -1;
1223
0
  }
1224
0
  fclose(in);
1225
1226
0
  generator->base.out = opts->pack_fd < 0 ? cp->out : -1;
1227
0
  generator->base.err = opts->progress_fd < 0 ? cp->err : -1;
1228
0
  generator->base.finish = odb_pack_generator_files_finish;
1229
1230
0
  *out = &generator->base;
1231
0
  return 0;
1232
0
}
1233
1234
static int fsck_commit_graph(struct odb_source_files *files,
1235
           struct odb_files_dir *dir,
1236
           struct odb_fsck_options *opts)
1237
0
{
1238
0
  struct repository *repo = files->base.odb->repo;
1239
0
  struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1240
1241
0
  prepare_repo_settings(repo);
1242
0
  if (!repo->settings.core_commit_graph)
1243
0
    return 0;
1244
1245
0
  commit_graph_verify.git_cmd = 1;
1246
0
  strvec_pushl(&commit_graph_verify.args, "commit-graph", "verify",
1247
0
         "--object-dir", dir->abspath, NULL);
1248
0
  if (opts->flags & ODB_FSCK_PROGRESS)
1249
0
    strvec_push(&commit_graph_verify.args, "--progress");
1250
0
  else
1251
0
    strvec_push(&commit_graph_verify.args, "--no-progress");
1252
0
  if (run_command(&commit_graph_verify))
1253
0
    return -1;
1254
1255
0
  return 0;
1256
0
}
1257
1258
static int odb_source_files_fsck(struct odb_source *source,
1259
         struct odb_fsck_options *opts)
1260
0
{
1261
0
  struct odb_source_files *files = odb_source_files_downcast(source);
1262
0
  int ret = 0;
1263
1264
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
1265
0
    if (!(opts->flags & ODB_FSCK_FULL) && !dir->local)
1266
0
      continue;
1267
1268
0
    ret |= odb_source_fsck(&dir->loose->base, opts);
1269
0
    ret |= odb_source_fsck(&dir->packed->base, opts);
1270
0
    ret |= fsck_commit_graph(files, dir, opts);
1271
0
  }
1272
1273
0
  return ret;
1274
0
}
1275
1276
struct odb_files_dir *odb_source_files_find_dir(struct object_database *odb, const char *obj_dir)
1277
0
{
1278
0
  struct odb_source_files *files = odb_source_files_downcast(odb->source);
1279
0
  char *obj_dir_real = real_pathdup(obj_dir, 1);
1280
0
  struct strbuf odb_path_real = STRBUF_INIT;
1281
0
  struct odb_files_dir *dir;
1282
1283
0
  for (dir = files->dirs; dir; dir = dir->next) {
1284
0
    strbuf_realpath(&odb_path_real, dir->abspath, 1);
1285
0
    if (!strcmp(obj_dir_real, odb_path_real.buf))
1286
0
      break;
1287
0
  }
1288
1289
0
  free(obj_dir_real);
1290
0
  strbuf_release(&odb_path_real);
1291
0
  return dir;
1292
0
}
1293
1294
struct odb_source_files *odb_source_files_new(struct object_database *odb,
1295
                const char *payload,
1296
                enum odb_new_flags flags)
1297
0
{
1298
0
  struct odb_source_files *files;
1299
0
  char *object_dir = NULL;
1300
0
  char *alternates = NULL;
1301
1302
0
  if (flags & ODB_NEW_HONOR_ENV) {
1303
0
    object_dir = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1304
0
    alternates = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1305
0
  }
1306
0
  if (!object_dir && payload) {
1307
0
    if (is_absolute_path(payload))
1308
0
      object_dir = xstrdup(payload);
1309
0
    else
1310
0
      object_dir = xstrfmt("%s/%s", odb->repo->commondir, payload);
1311
0
  }
1312
0
  if (!object_dir)
1313
0
    object_dir = xstrfmt("%s/objects", odb->repo->commondir);
1314
1315
0
  CALLOC_ARRAY(files, 1);
1316
0
  odb_source_init(&files->base, odb, ODB_SOURCE_FILES, object_dir);
1317
1318
0
  files->dirs = odb_files_dir_new(odb, object_dir, true);
1319
0
  files->dirs_tail = &files->dirs->next;
1320
0
  strset_init(&files->dir_paths);
1321
1322
0
  files->base.free = odb_source_files_free;
1323
0
  files->base.close = odb_source_files_close;
1324
0
  files->base.create_on_disk = odb_source_files_create_on_disk;
1325
0
  files->base.prepare = odb_source_files_prepare;
1326
0
  files->base.fsck = odb_source_files_fsck;
1327
0
  files->base.read_object_info = odb_source_files_read_object_info;
1328
0
  files->base.read_object_stream = odb_source_files_read_object_stream;
1329
0
  files->base.for_each_object = odb_source_files_for_each_object;
1330
0
  files->base.count_objects = odb_source_files_count_objects;
1331
0
  files->base.find_abbrev_len = odb_source_files_find_abbrev_len;
1332
0
  files->base.freshen_object = odb_source_files_freshen_object;
1333
0
  files->base.write_object = odb_source_files_write_object;
1334
0
  files->base.write_object_stream = odb_source_files_write_object_stream;
1335
0
  files->base.begin_transaction = odb_source_files_begin_transaction;
1336
0
  files->base.optimize = odb_source_files_optimize;
1337
0
  files->base.optimize_required = odb_source_files_optimize_required;
1338
0
  files->base.generate_pack = odb_source_files_generate_pack;
1339
1340
  /*
1341
   * Ideally, we would only ever store absolute paths in the source. This
1342
   * is not (yet) possible though because we access and assume relative
1343
   * paths in the primary ODB source in some user-facing functionality.
1344
   */
1345
0
  if (!is_absolute_path(object_dir))
1346
0
    chdir_notify_register(odb_source_files_reparent, files);
1347
1348
0
  prepare_alternates(files, alternates);
1349
1350
0
  free(object_dir);
1351
0
  free(alternates);
1352
0
  return files;
1353
0
}