~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-security

« back to all changes in this revision

Viewing changes to builtin-fsck.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-10-04 08:27:01 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20071004082701-rsd058ontoqz4i30
Tags: 1:1.5.3.4-1
new upstream point release (closes: #445188).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "builtin.h"
1
2
#include "cache.h"
2
3
#include "commit.h"
3
4
#include "tree.h"
20
21
static int keep_cache_objects;
21
22
static unsigned char head_sha1[20];
22
23
static int errors_found;
 
24
static int write_lost_and_found;
 
25
static int verbose;
23
26
#define ERROR_OBJECT 01
24
27
#define ERROR_REACHABLE 02
25
28
 
137
140
        if (!obj->used) {
138
141
                printf("dangling %s %s\n", typename(obj->type),
139
142
                       sha1_to_hex(obj->sha1));
 
143
                if (write_lost_and_found) {
 
144
                        char *filename = git_path("lost-found/%s/%s",
 
145
                                obj->type == OBJ_COMMIT ? "commit" : "other",
 
146
                                sha1_to_hex(obj->sha1));
 
147
                        FILE *f;
 
148
 
 
149
                        if (safe_create_leading_directories(filename)) {
 
150
                                error("Could not create lost-found");
 
151
                                return;
 
152
                        }
 
153
                        if (!(f = fopen(filename, "w")))
 
154
                                die("Could not open %s", filename);
 
155
                        if (obj->type == OBJ_BLOB) {
 
156
                                enum object_type type;
 
157
                                unsigned long size;
 
158
                                char *buf = read_sha1_file(obj->sha1,
 
159
                                                &type, &size);
 
160
                                if (buf) {
 
161
                                        fwrite(buf, size, 1, f);
 
162
                                        free(buf);
 
163
                                }
 
164
                        } else
 
165
                                fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
 
166
                        fclose(f);
 
167
                }
140
168
                return;
141
169
        }
142
170
 
149
177
 
150
178
static void check_object(struct object *obj)
151
179
{
 
180
        if (verbose)
 
181
                fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
 
182
 
152
183
        if (obj->flags & REACHABLE)
153
184
                check_reachable_object(obj);
154
185
        else
161
192
 
162
193
        /* Look up all the requirements, warn about missing objects.. */
163
194
        max = get_max_object_index();
 
195
        if (verbose)
 
196
                fprintf(stderr, "Checking connectivity (%d objects)\n", max);
 
197
 
164
198
        for (i = 0; i < max; i++) {
165
199
                struct object *obj = get_indexed_object(i);
166
200
 
229
263
        const char *o_name;
230
264
        const unsigned char *o_sha1;
231
265
 
 
266
        if (verbose)
 
267
                fprintf(stderr, "Checking tree %s\n",
 
268
                                sha1_to_hex(item->object.sha1));
 
269
 
232
270
        init_tree_desc(&desc, item->buffer, item->size);
233
271
 
234
272
        o_mode = 0;
256
294
                case S_IFREG | 0644:
257
295
                case S_IFLNK:
258
296
                case S_IFDIR:
259
 
                case S_IFDIRLNK:
 
297
                case S_IFGITLINK:
260
298
                        break;
261
299
                /*
262
300
                 * This is nonstandard, but we had a few of these
317
355
        char *buffer = commit->buffer;
318
356
        unsigned char tree_sha1[20], sha1[20];
319
357
 
 
358
        if (verbose)
 
359
                fprintf(stderr, "Checking commit %s\n",
 
360
                        sha1_to_hex(commit->object.sha1));
 
361
 
320
362
        if (memcmp(buffer, "tree ", 5))
321
363
                return objerror(&commit->object, "invalid format - expected 'tree' line");
322
364
        if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
336
378
        if (!commit->parents && show_root)
337
379
                printf("root %s\n", sha1_to_hex(commit->object.sha1));
338
380
        if (!commit->date)
339
 
                printf("bad commit date in %s\n", 
 
381
                printf("bad commit date in %s\n",
340
382
                       sha1_to_hex(commit->object.sha1));
341
383
        return 0;
342
384
}
345
387
{
346
388
        struct object *tagged = tag->tagged;
347
389
 
 
390
        if (verbose)
 
391
                fprintf(stderr, "Checking tag %s\n",
 
392
                        sha1_to_hex(tag->object.sha1));
 
393
 
348
394
        if (!tagged) {
349
395
                return objerror(&tag->object, "could not load tagged object");
350
396
        }
446
492
        if (!dir)
447
493
                return;
448
494
 
 
495
        if (verbose)
 
496
                fprintf(stderr, "Checking directory %s\n", path);
 
497
 
449
498
        while ((de = readdir(dir)) != NULL) {
450
499
                char name[100];
451
500
                unsigned char sha1[20];
480
529
{
481
530
        struct object *obj;
482
531
 
 
532
        if (verbose)
 
533
                fprintf(stderr, "Checking reflog %s->%s\n",
 
534
                        sha1_to_hex(osha1), sha1_to_hex(nsha1));
 
535
 
483
536
        if (!is_null_sha1(osha1)) {
484
537
                obj = lookup_object(osha1);
485
538
                if (obj) {
549
602
static void fsck_object_dir(const char *path)
550
603
{
551
604
        int i;
 
605
 
 
606
        if (verbose)
 
607
                fprintf(stderr, "Checking object directory\n");
 
608
 
552
609
        for (i = 0; i < 256; i++) {
553
610
                static char dir[4096];
554
611
                sprintf(dir, "%s/%02x", path, i);
564
621
        int null_is_error = 0;
565
622
        const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
566
623
 
 
624
        if (verbose)
 
625
                fprintf(stderr, "Checking HEAD link\n");
 
626
 
567
627
        if (!head_points_at)
568
628
                return error("Invalid HEAD");
569
629
        if (!strcmp(head_points_at, "HEAD"))
586
646
        int i;
587
647
        int err = 0;
588
648
 
 
649
        if (verbose)
 
650
                fprintf(stderr, "Checking cache tree\n");
 
651
 
589
652
        if (0 <= it->entry_count) {
590
653
                struct object *obj = parse_object(it->sha1);
591
654
                if (!obj) {
605
668
 
606
669
static const char fsck_usage[] =
607
670
"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
608
 
"[--strict] <head-sha1>*]";
 
671
"[--strict] [--verbose] <head-sha1>*]";
609
672
 
610
 
int cmd_fsck(int argc, char **argv, const char *prefix)
 
673
int cmd_fsck(int argc, const char **argv, const char *prefix)
611
674
{
612
675
        int i, heads;
613
676
 
645
708
                        check_strict = 1;
646
709
                        continue;
647
710
                }
 
711
                if (!strcmp(arg, "--verbose")) {
 
712
                        verbose = 1;
 
713
                        continue;
 
714
                }
 
715
                if (!strcmp(arg, "--lost-found")) {
 
716
                        check_full = 1;
 
717
                        include_reflogs = 0;
 
718
                        write_lost_and_found = 1;
 
719
                        continue;
 
720
                }
648
721
                if (*arg == '-')
649
722
                        usage(fsck_usage);
650
723
        }
668
741
                        verify_pack(p, 0);
669
742
 
670
743
                for (p = packed_git; p; p = p->next) {
671
 
                        uint32_t i, num = p->num_objects;
 
744
                        uint32_t i, num;
 
745
                        if (open_pack_index(p))
 
746
                                continue;
 
747
                        num = p->num_objects;
672
748
                        for (i = 0; i < num; i++)
673
749
                                fsck_sha1(nth_packed_object_sha1(p, i));
674
750
                }
676
752
 
677
753
        heads = 0;
678
754
        for (i = 1; i < argc; i++) {
679
 
                const char *arg = argv[i]; 
 
755
                const char *arg = argv[i];
680
756
 
681
757
                if (*arg == '-')
682
758
                        continue;
715
791
                        struct object *obj;
716
792
 
717
793
                        mode = ntohl(active_cache[i]->ce_mode);
718
 
                        if (S_ISDIRLNK(mode))
 
794
                        if (S_ISGITLINK(mode))
719
795
                                continue;
720
796
                        blob = lookup_blob(active_cache[i]->sha1);
721
797
                        if (!blob)