b433c0c2d09e1ba363bd9c10f7144f089dd66b70
   1#include "refs.h"
   2#include "cache.h"
   3
   4#include <errno.h>
   5
   6struct ref_list {
   7        struct ref_list *next;
   8        unsigned char flag; /* ISSYMREF? ISPACKED? */
   9        unsigned char sha1[20];
  10        char name[FLEX_ARRAY];
  11};
  12
  13static const char *parse_ref_line(char *line, unsigned char *sha1)
  14{
  15        /*
  16         * 42: the answer to everything.
  17         *
  18         * In this case, it happens to be the answer to
  19         *  40 (length of sha1 hex representation)
  20         *  +1 (space in between hex and name)
  21         *  +1 (newline at the end of the line)
  22         */
  23        int len = strlen(line) - 42;
  24
  25        if (len <= 0)
  26                return NULL;
  27        if (get_sha1_hex(line, sha1) < 0)
  28                return NULL;
  29        if (!isspace(line[40]))
  30                return NULL;
  31        line += 41;
  32        if (isspace(*line))
  33                return NULL;
  34        if (line[len] != '\n')
  35                return NULL;
  36        line[len] = 0;
  37        return line;
  38}
  39
  40static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
  41                                int flag, struct ref_list *list)
  42{
  43        int len;
  44        struct ref_list **p = &list, *entry;
  45
  46        /* Find the place to insert the ref into.. */
  47        while ((entry = *p) != NULL) {
  48                int cmp = strcmp(entry->name, name);
  49                if (cmp > 0)
  50                        break;
  51
  52                /* Same as existing entry? */
  53                if (!cmp)
  54                        return list;
  55                p = &entry->next;
  56        }
  57
  58        /* Allocate it and add it in.. */
  59        len = strlen(name) + 1;
  60        entry = xmalloc(sizeof(struct ref_list) + len);
  61        hashcpy(entry->sha1, sha1);
  62        memcpy(entry->name, name, len);
  63        entry->flag = flag;
  64        entry->next = *p;
  65        *p = entry;
  66        return list;
  67}
  68
  69static struct ref_list *get_packed_refs(void)
  70{
  71        static int did_refs = 0;
  72        static struct ref_list *refs = NULL;
  73
  74        if (!did_refs) {
  75                FILE *f = fopen(git_path("packed-refs"), "r");
  76                if (f) {
  77                        struct ref_list *list = NULL;
  78                        char refline[PATH_MAX];
  79                        while (fgets(refline, sizeof(refline), f)) {
  80                                unsigned char sha1[20];
  81                                const char *name = parse_ref_line(refline, sha1);
  82                                if (!name)
  83                                        continue;
  84                                list = add_ref(name, sha1, REF_ISPACKED, list);
  85                        }
  86                        fclose(f);
  87                        refs = list;
  88                }
  89                did_refs = 1;
  90        }
  91        return refs;
  92}
  93
  94static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
  95{
  96        DIR *dir = opendir(git_path("%s", base));
  97
  98        if (dir) {
  99                struct dirent *de;
 100                int baselen = strlen(base);
 101                char *ref = xmalloc(baselen + 257);
 102
 103                memcpy(ref, base, baselen);
 104                if (baselen && base[baselen-1] != '/')
 105                        ref[baselen++] = '/';
 106
 107                while ((de = readdir(dir)) != NULL) {
 108                        unsigned char sha1[20];
 109                        struct stat st;
 110                        int flag;
 111                        int namelen;
 112
 113                        if (de->d_name[0] == '.')
 114                                continue;
 115                        namelen = strlen(de->d_name);
 116                        if (namelen > 255)
 117                                continue;
 118                        if (has_extension(de->d_name, ".lock"))
 119                                continue;
 120                        memcpy(ref + baselen, de->d_name, namelen+1);
 121                        if (stat(git_path("%s", ref), &st) < 0)
 122                                continue;
 123                        if (S_ISDIR(st.st_mode)) {
 124                                list = get_ref_dir(ref, list);
 125                                continue;
 126                        }
 127                        if (!resolve_ref(ref, sha1, 1, &flag)) {
 128                                error("%s points nowhere!", ref);
 129                                continue;
 130                        }
 131                        list = add_ref(ref, sha1, flag, list);
 132                }
 133                free(ref);
 134                closedir(dir);
 135        }
 136        return list;
 137}
 138
 139static struct ref_list *get_loose_refs(void)
 140{
 141        static int did_refs = 0;
 142        static struct ref_list *refs = NULL;
 143
 144        if (!did_refs) {
 145                refs = get_ref_dir("refs", NULL);
 146                did_refs = 1;
 147        }
 148        return refs;
 149}
 150
 151/* We allow "recursive" symbolic refs. Only within reason, though */
 152#define MAXDEPTH 5
 153
 154const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
 155{
 156        int depth = MAXDEPTH, len;
 157        char buffer[256];
 158        static char ref_buffer[256];
 159
 160        if (flag)
 161                *flag = 0;
 162
 163        for (;;) {
 164                const char *path = git_path("%s", ref);
 165                struct stat st;
 166                char *buf;
 167                int fd;
 168
 169                if (--depth < 0)
 170                        return NULL;
 171
 172                /* Special case: non-existing file.
 173                 * Not having the refs/heads/new-branch is OK
 174                 * if we are writing into it, so is .git/HEAD
 175                 * that points at refs/heads/master still to be
 176                 * born.  It is NOT OK if we are resolving for
 177                 * reading.
 178                 */
 179                if (lstat(path, &st) < 0) {
 180                        struct ref_list *list = get_packed_refs();
 181                        while (list) {
 182                                if (!strcmp(ref, list->name)) {
 183                                        hashcpy(sha1, list->sha1);
 184                                        if (flag)
 185                                                *flag |= REF_ISPACKED;
 186                                        return ref;
 187                                }
 188                                list = list->next;
 189                        }
 190                        if (reading || errno != ENOENT)
 191                                return NULL;
 192                        hashclr(sha1);
 193                        return ref;
 194                }
 195
 196                /* Follow "normalized" - ie "refs/.." symlinks by hand */
 197                if (S_ISLNK(st.st_mode)) {
 198                        len = readlink(path, buffer, sizeof(buffer)-1);
 199                        if (len >= 5 && !memcmp("refs/", buffer, 5)) {
 200                                buffer[len] = 0;
 201                                strcpy(ref_buffer, buffer);
 202                                ref = ref_buffer;
 203                                if (flag)
 204                                        *flag |= REF_ISSYMREF;
 205                                continue;
 206                        }
 207                }
 208
 209                /*
 210                 * Anything else, just open it and try to use it as
 211                 * a ref
 212                 */
 213                fd = open(path, O_RDONLY);
 214                if (fd < 0)
 215                        return NULL;
 216                len = read(fd, buffer, sizeof(buffer)-1);
 217                close(fd);
 218
 219                /*
 220                 * Is it a symbolic ref?
 221                 */
 222                if (len < 4 || memcmp("ref:", buffer, 4))
 223                        break;
 224                buf = buffer + 4;
 225                len -= 4;
 226                while (len && isspace(*buf))
 227                        buf++, len--;
 228                while (len && isspace(buf[len-1]))
 229                        len--;
 230                buf[len] = 0;
 231                memcpy(ref_buffer, buf, len + 1);
 232                ref = ref_buffer;
 233                if (flag)
 234                        *flag |= REF_ISSYMREF;
 235        }
 236        if (len < 40 || get_sha1_hex(buffer, sha1))
 237                return NULL;
 238        return ref;
 239}
 240
 241int create_symref(const char *ref_target, const char *refs_heads_master)
 242{
 243        const char *lockpath;
 244        char ref[1000];
 245        int fd, len, written;
 246        const char *git_HEAD = git_path("%s", ref_target);
 247
 248#ifndef NO_SYMLINK_HEAD
 249        if (prefer_symlink_refs) {
 250                unlink(git_HEAD);
 251                if (!symlink(refs_heads_master, git_HEAD))
 252                        return 0;
 253                fprintf(stderr, "no symlink - falling back to symbolic ref\n");
 254        }
 255#endif
 256
 257        len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
 258        if (sizeof(ref) <= len) {
 259                error("refname too long: %s", refs_heads_master);
 260                return -1;
 261        }
 262        lockpath = mkpath("%s.lock", git_HEAD);
 263        fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); 
 264        written = write(fd, ref, len);
 265        close(fd);
 266        if (written != len) {
 267                unlink(lockpath);
 268                error("Unable to write to %s", lockpath);
 269                return -2;
 270        }
 271        if (rename(lockpath, git_HEAD) < 0) {
 272                unlink(lockpath);
 273                error("Unable to create %s", git_HEAD);
 274                return -3;
 275        }
 276        if (adjust_shared_perm(git_HEAD)) {
 277                unlink(lockpath);
 278                error("Unable to fix permissions on %s", lockpath);
 279                return -4;
 280        }
 281        return 0;
 282}
 283
 284int read_ref(const char *ref, unsigned char *sha1)
 285{
 286        if (resolve_ref(ref, sha1, 1, NULL))
 287                return 0;
 288        return -1;
 289}
 290
 291static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
 292                           void *cb_data)
 293{
 294        int retval;
 295        struct ref_list *packed = get_packed_refs();
 296        struct ref_list *loose = get_loose_refs();
 297
 298        while (packed && loose) {
 299                struct ref_list *entry;
 300                int cmp = strcmp(packed->name, loose->name);
 301                if (!cmp) {
 302                        packed = packed->next;
 303                        continue;
 304                }
 305                if (cmp > 0) {
 306                        entry = loose;
 307                        loose = loose->next;
 308                } else {
 309                        entry = packed;
 310                        packed = packed->next;
 311                }
 312                if (strncmp(base, entry->name, trim))
 313                        continue;
 314                if (is_null_sha1(entry->sha1))
 315                        continue;
 316                if (!has_sha1_file(entry->sha1)) {
 317                        error("%s does not point to a valid object!", entry->name);
 318                        continue;
 319                }
 320                retval = fn(entry->name + trim, entry->sha1,
 321                            entry->flag, cb_data);
 322                if (retval)
 323                        return retval;
 324        }
 325
 326        packed = packed ? packed : loose;
 327        while (packed) {
 328                if (!strncmp(base, packed->name, trim)) {
 329                        retval = fn(packed->name + trim, packed->sha1,
 330                                    packed->flag, cb_data);
 331                        if (retval)
 332                                return retval;
 333                }
 334                packed = packed->next;
 335        }
 336        return 0;
 337}
 338
 339int head_ref(each_ref_fn fn, void *cb_data)
 340{
 341        unsigned char sha1[20];
 342        int flag;
 343
 344        if (resolve_ref("HEAD", sha1, 1, &flag))
 345                return fn("HEAD", sha1, flag, cb_data);
 346        return 0;
 347}
 348
 349int for_each_ref(each_ref_fn fn, void *cb_data)
 350{
 351        return do_for_each_ref("refs/", fn, 0, cb_data);
 352}
 353
 354int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 355{
 356        return do_for_each_ref("refs/tags/", fn, 10, cb_data);
 357}
 358
 359int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 360{
 361        return do_for_each_ref("refs/heads/", fn, 11, cb_data);
 362}
 363
 364int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 365{
 366        return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
 367}
 368
 369/* NEEDSWORK: This is only used by ssh-upload and it should go; the
 370 * caller should do resolve_ref or read_ref like everybody else.  Or
 371 * maybe everybody else should use get_ref_sha1() instead of doing
 372 * read_ref().
 373 */
 374int get_ref_sha1(const char *ref, unsigned char *sha1)
 375{
 376        if (check_ref_format(ref))
 377                return -1;
 378        return read_ref(mkpath("refs/%s", ref), sha1);
 379}
 380
 381int delete_ref(const char *refname, unsigned char *sha1)
 382{
 383        struct ref_lock *lock;
 384        int err, i, ret = 0;
 385
 386        lock = lock_any_ref_for_update(refname, sha1);
 387        if (!lock)
 388                return 1;
 389        i = strlen(lock->lk->filename) - 5; /* .lock */
 390        lock->lk->filename[i] = 0;
 391        err = unlink(lock->lk->filename);
 392        if (err) {
 393                ret = 1;
 394                error("unlink(%s) failed: %s",
 395                      lock->lk->filename, strerror(errno));
 396        }
 397        lock->lk->filename[i] = '.';
 398
 399        err = unlink(lock->log_file);
 400        if (err && errno != ENOENT)
 401                fprintf(stderr, "warning: unlink(%s) failed: %s",
 402                        lock->log_file, strerror(errno));
 403
 404        return ret;
 405}
 406
 407/*
 408 * Make sure "ref" is something reasonable to have under ".git/refs/";
 409 * We do not like it if:
 410 *
 411 * - any path component of it begins with ".", or
 412 * - it has double dots "..", or
 413 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
 414 * - it ends with a "/".
 415 */
 416
 417static inline int bad_ref_char(int ch)
 418{
 419        return (((unsigned) ch) <= ' ' ||
 420                ch == '~' || ch == '^' || ch == ':' ||
 421                /* 2.13 Pattern Matching Notation */
 422                ch == '?' || ch == '*' || ch == '[');
 423}
 424
 425int check_ref_format(const char *ref)
 426{
 427        int ch, level;
 428        const char *cp = ref;
 429
 430        level = 0;
 431        while (1) {
 432                while ((ch = *cp++) == '/')
 433                        ; /* tolerate duplicated slashes */
 434                if (!ch)
 435                        return -1; /* should not end with slashes */
 436
 437                /* we are at the beginning of the path component */
 438                if (ch == '.' || bad_ref_char(ch))
 439                        return -1;
 440
 441                /* scan the rest of the path component */
 442                while ((ch = *cp++) != 0) {
 443                        if (bad_ref_char(ch))
 444                                return -1;
 445                        if (ch == '/')
 446                                break;
 447                        if (ch == '.' && *cp == '.')
 448                                return -1;
 449                }
 450                level++;
 451                if (!ch) {
 452                        if (level < 2)
 453                                return -1; /* at least of form "heads/blah" */
 454                        return 0;
 455                }
 456        }
 457}
 458
 459static struct ref_lock *verify_lock(struct ref_lock *lock,
 460        const unsigned char *old_sha1, int mustexist)
 461{
 462        if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
 463                error("Can't verify ref %s", lock->ref_name);
 464                unlock_ref(lock);
 465                return NULL;
 466        }
 467        if (hashcmp(lock->old_sha1, old_sha1)) {
 468                error("Ref %s is at %s but expected %s", lock->ref_name,
 469                        sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
 470                unlock_ref(lock);
 471                return NULL;
 472        }
 473        return lock;
 474}
 475
 476static int remove_empty_dir_recursive(char *path, int len)
 477{
 478        DIR *dir = opendir(path);
 479        struct dirent *e;
 480        int ret = 0;
 481
 482        if (!dir)
 483                return -1;
 484        if (path[len-1] != '/')
 485                path[len++] = '/';
 486        while ((e = readdir(dir)) != NULL) {
 487                struct stat st;
 488                int namlen;
 489                if ((e->d_name[0] == '.') &&
 490                    ((e->d_name[1] == 0) ||
 491                     ((e->d_name[1] == '.') && e->d_name[2] == 0)))
 492                        continue; /* "." and ".." */
 493
 494                namlen = strlen(e->d_name);
 495                if ((len + namlen < PATH_MAX) &&
 496                    strcpy(path + len, e->d_name) &&
 497                    !lstat(path, &st) &&
 498                    S_ISDIR(st.st_mode) &&
 499                    remove_empty_dir_recursive(path, len + namlen))
 500                        continue; /* happy */
 501
 502                /* path too long, stat fails, or non-directory still exists */
 503                ret = -1;
 504                break;
 505        }
 506        closedir(dir);
 507        if (!ret) {
 508                path[len] = 0;
 509                ret = rmdir(path);
 510        }
 511        return ret;
 512}
 513
 514static int remove_empty_directories(char *file)
 515{
 516        /* we want to create a file but there is a directory there;
 517         * if that is an empty directory (or a directory that contains
 518         * only empty directories), remove them.
 519         */
 520        char path[PATH_MAX];
 521        int len = strlen(file);
 522
 523        if (len >= PATH_MAX) /* path too long ;-) */
 524                return -1;
 525        strcpy(path, file);
 526        return remove_empty_dir_recursive(path, len);
 527}
 528
 529static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1)
 530{
 531        char *ref_file;
 532        const char *orig_ref = ref;
 533        struct ref_lock *lock;
 534        struct stat st;
 535        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
 536
 537        lock = xcalloc(1, sizeof(struct ref_lock));
 538        lock->lock_fd = -1;
 539
 540        ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
 541        if (!ref && errno == EISDIR) {
 542                /* we are trying to lock foo but we used to
 543                 * have foo/bar which now does not exist;
 544                 * it is normal for the empty directory 'foo'
 545                 * to remain.
 546                 */
 547                ref_file = git_path("%s", orig_ref);
 548                if (remove_empty_directories(ref_file))
 549                        die("there are still refs under '%s'", orig_ref);
 550                ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
 551        }
 552        if (!ref) {
 553                int last_errno = errno;
 554                error("unable to resolve reference %s: %s",
 555                        orig_ref, strerror(errno));
 556                unlock_ref(lock);
 557                errno = last_errno;
 558                return NULL;
 559        }
 560        lock->lk = xcalloc(1, sizeof(struct lock_file));
 561
 562        lock->ref_name = xstrdup(ref);
 563        lock->log_file = xstrdup(git_path("logs/%s", ref));
 564        ref_file = git_path("%s", ref);
 565        lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
 566
 567        if (safe_create_leading_directories(ref_file))
 568                die("unable to create directory for %s", ref_file);
 569        lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
 570
 571        return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 572}
 573
 574struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
 575{
 576        char refpath[PATH_MAX];
 577        if (check_ref_format(ref))
 578                return NULL;
 579        strcpy(refpath, mkpath("refs/%s", ref));
 580        return lock_ref_sha1_basic(refpath, old_sha1);
 581}
 582
 583struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
 584{
 585        return lock_ref_sha1_basic(ref, old_sha1);
 586}
 587
 588void unlock_ref(struct ref_lock *lock)
 589{
 590        if (lock->lock_fd >= 0) {
 591                close(lock->lock_fd);
 592                /* Do not free lock->lk -- atexit() still looks at them */
 593                if (lock->lk)
 594                        rollback_lock_file(lock->lk);
 595        }
 596        free(lock->ref_name);
 597        free(lock->log_file);
 598        free(lock);
 599}
 600
 601static int log_ref_write(struct ref_lock *lock,
 602        const unsigned char *sha1, const char *msg)
 603{
 604        int logfd, written, oflags = O_APPEND | O_WRONLY;
 605        unsigned maxlen, len;
 606        char *logrec;
 607        const char *committer;
 608
 609        if (log_all_ref_updates) {
 610                if (safe_create_leading_directories(lock->log_file) < 0)
 611                        return error("unable to create directory for %s",
 612                                lock->log_file);
 613                oflags |= O_CREAT;
 614        }
 615
 616        logfd = open(lock->log_file, oflags, 0666);
 617        if (logfd < 0) {
 618                if (!log_all_ref_updates && errno == ENOENT)
 619                        return 0;
 620                return error("Unable to append to %s: %s",
 621                        lock->log_file, strerror(errno));
 622        }
 623
 624        committer = git_committer_info(1);
 625        if (msg) {
 626                maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
 627                logrec = xmalloc(maxlen);
 628                len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
 629                        sha1_to_hex(lock->old_sha1),
 630                        sha1_to_hex(sha1),
 631                        committer,
 632                        msg);
 633        }
 634        else {
 635                maxlen = strlen(committer) + 2*40 + 4;
 636                logrec = xmalloc(maxlen);
 637                len = snprintf(logrec, maxlen, "%s %s %s\n",
 638                        sha1_to_hex(lock->old_sha1),
 639                        sha1_to_hex(sha1),
 640                        committer);
 641        }
 642        written = len <= maxlen ? write(logfd, logrec, len) : -1;
 643        free(logrec);
 644        close(logfd);
 645        if (written != len)
 646                return error("Unable to append to %s", lock->log_file);
 647        return 0;
 648}
 649
 650int write_ref_sha1(struct ref_lock *lock,
 651        const unsigned char *sha1, const char *logmsg)
 652{
 653        static char term = '\n';
 654
 655        if (!lock)
 656                return -1;
 657        if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
 658                unlock_ref(lock);
 659                return 0;
 660        }
 661        if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
 662            write(lock->lock_fd, &term, 1) != 1
 663                || close(lock->lock_fd) < 0) {
 664                error("Couldn't write %s", lock->lk->filename);
 665                unlock_ref(lock);
 666                return -1;
 667        }
 668        if (log_ref_write(lock, sha1, logmsg) < 0) {
 669                unlock_ref(lock);
 670                return -1;
 671        }
 672        if (commit_lock_file(lock->lk)) {
 673                error("Couldn't set %s", lock->ref_name);
 674                unlock_ref(lock);
 675                return -1;
 676        }
 677        lock->lock_fd = -1;
 678        unlock_ref(lock);
 679        return 0;
 680}
 681
 682int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
 683{
 684        const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
 685        char *tz_c;
 686        int logfd, tz;
 687        struct stat st;
 688        unsigned long date;
 689        unsigned char logged_sha1[20];
 690
 691        logfile = git_path("logs/%s", ref);
 692        logfd = open(logfile, O_RDONLY, 0);
 693        if (logfd < 0)
 694                die("Unable to read log %s: %s", logfile, strerror(errno));
 695        fstat(logfd, &st);
 696        if (!st.st_size)
 697                die("Log %s is empty.", logfile);
 698        logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
 699        close(logfd);
 700
 701        lastrec = NULL;
 702        rec = logend = logdata + st.st_size;
 703        while (logdata < rec) {
 704                if (logdata < rec && *(rec-1) == '\n')
 705                        rec--;
 706                lastgt = NULL;
 707                while (logdata < rec && *(rec-1) != '\n') {
 708                        rec--;
 709                        if (*rec == '>')
 710                                lastgt = rec;
 711                }
 712                if (!lastgt)
 713                        die("Log %s is corrupt.", logfile);
 714                date = strtoul(lastgt + 1, &tz_c, 10);
 715                if (date <= at_time) {
 716                        if (lastrec) {
 717                                if (get_sha1_hex(lastrec, logged_sha1))
 718                                        die("Log %s is corrupt.", logfile);
 719                                if (get_sha1_hex(rec + 41, sha1))
 720                                        die("Log %s is corrupt.", logfile);
 721                                if (hashcmp(logged_sha1, sha1)) {
 722                                        tz = strtoul(tz_c, NULL, 10);
 723                                        fprintf(stderr,
 724                                                "warning: Log %s has gap after %s.\n",
 725                                                logfile, show_rfc2822_date(date, tz));
 726                                }
 727                        }
 728                        else if (date == at_time) {
 729                                if (get_sha1_hex(rec + 41, sha1))
 730                                        die("Log %s is corrupt.", logfile);
 731                        }
 732                        else {
 733                                if (get_sha1_hex(rec + 41, logged_sha1))
 734                                        die("Log %s is corrupt.", logfile);
 735                                if (hashcmp(logged_sha1, sha1)) {
 736                                        tz = strtoul(tz_c, NULL, 10);
 737                                        fprintf(stderr,
 738                                                "warning: Log %s unexpectedly ended on %s.\n",
 739                                                logfile, show_rfc2822_date(date, tz));
 740                                }
 741                        }
 742                        munmap((void*)logdata, st.st_size);
 743                        return 0;
 744                }
 745                lastrec = rec;
 746        }
 747
 748        rec = logdata;
 749        while (rec < logend && *rec != '>' && *rec != '\n')
 750                rec++;
 751        if (rec == logend || *rec == '\n')
 752                die("Log %s is corrupt.", logfile);
 753        date = strtoul(rec + 1, &tz_c, 10);
 754        tz = strtoul(tz_c, NULL, 10);
 755        if (get_sha1_hex(logdata, sha1))
 756                die("Log %s is corrupt.", logfile);
 757        munmap((void*)logdata, st.st_size);
 758        fprintf(stderr, "warning: Log %s only goes back to %s.\n",
 759                logfile, show_rfc2822_date(date, tz));
 760        return 0;
 761}