Merge branch 'ew/server-info-remove-crufts'
authorJunio C Hamano <gitster@pobox.com>
Mon, 17 Jun 2019 17:15:15 +0000 (10:15 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Jun 2019 17:15:15 +0000 (10:15 -0700)
"git update-server-info" used to leave stale packfiles in its
output, which has been corrected.

* ew/server-info-remove-crufts:
server-info: do not list unlinked packs

1  2 
server-info.c
diff --combined server-info.c
index e68f785c2f9cf02c077c5ebe8883a24ed5823350,92187c70db650ee0e4d5ea4bf3435e84a51c44b2..4d8199b1d916f70f136b767132916fdebb73318e
@@@ -1,4 -1,5 +1,5 @@@
  #include "cache.h"
+ #include "dir.h"
  #include "repository.h"
  #include "refs.h"
  #include "object.h"
  #include "tag.h"
  #include "packfile.h"
  #include "object-store.h"
 +#include "strbuf.h"
 +
 +struct update_info_ctx {
 +      FILE *cur_fp;
 +      FILE *old_fp; /* becomes NULL if it differs from cur_fp */
 +      struct strbuf cur_sb;
 +      struct strbuf old_sb;
 +};
 +
 +static void uic_mark_stale(struct update_info_ctx *uic)
 +{
 +      fclose(uic->old_fp);
 +      uic->old_fp = NULL;
 +}
 +
 +static int uic_is_stale(const struct update_info_ctx *uic)
 +{
 +      return uic->old_fp == NULL;
 +}
 +
 +static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
 +{
 +      va_list ap;
 +      int ret = -1;
 +
 +      va_start(ap, fmt);
 +
 +      if (uic_is_stale(uic)) {
 +              ret = vfprintf(uic->cur_fp, fmt, ap);
 +      } else {
 +              ssize_t r;
 +              struct strbuf *cur = &uic->cur_sb;
 +              struct strbuf *old = &uic->old_sb;
 +
 +              strbuf_reset(cur);
 +              strbuf_vinsertf(cur, 0, fmt, ap);
 +
 +              strbuf_reset(old);
 +              strbuf_grow(old, cur->len);
 +              r = fread(old->buf, 1, cur->len, uic->old_fp);
 +              if (r != cur->len || memcmp(old->buf, cur->buf, r))
 +                      uic_mark_stale(uic);
 +
 +              if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
 +                      ret = 0;
 +      }
 +
 +      va_end(ap);
 +
 +      return ret;
 +}
  
  /*
   * Create the file "path" by writing to a temporary file and renaming
   * it into place. The contents of the file come from "generate", which
   * should return non-zero if it encounters an error.
   */
 -static int update_info_file(char *path, int (*generate)(FILE *))
 +static int update_info_file(char *path,
 +                      int (*generate)(struct update_info_ctx *),
 +                      int force)
  {
        char *tmp = mkpathdup("%s_XXXXXX", path);
        int ret = -1;
        int fd = -1;
 -      FILE *fp = NULL, *to_close;
 +      FILE *to_close;
 +      struct update_info_ctx uic = {
 +              .cur_fp = NULL,
 +              .old_fp = NULL,
 +              .cur_sb = STRBUF_INIT,
 +              .old_sb = STRBUF_INIT
 +      };
  
        safe_create_leading_directories(path);
        fd = git_mkstemp_mode(tmp, 0666);
        if (fd < 0)
                goto out;
 -      to_close = fp = fdopen(fd, "w");
 -      if (!fp)
 +      to_close = uic.cur_fp = fdopen(fd, "w");
 +      if (!uic.cur_fp)
                goto out;
        fd = -1;
 -      ret = generate(fp);
 +
 +      /* no problem on ENOENT and old_fp == NULL, it's stale, now */
 +      if (!force)
 +              uic.old_fp = fopen_or_warn(path, "r");
 +
 +      /*
 +       * uic_printf will compare incremental comparison aginst old_fp
 +       * and mark uic as stale if needed
 +       */
 +      ret = generate(&uic);
        if (ret)
                goto out;
 -      fp = NULL;
 +
 +      /* new file may be shorter than the old one, check here */
 +      if (!uic_is_stale(&uic)) {
 +              struct stat st;
 +              long new_len = ftell(uic.cur_fp);
 +              int old_fd = fileno(uic.old_fp);
 +
 +              if (new_len < 0) {
 +                      ret = -1;
 +                      goto out;
 +              }
 +              if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
 +                      uic_mark_stale(&uic);
 +      }
 +
 +      uic.cur_fp = NULL;
        if (fclose(to_close))
                goto out;
 -      if (adjust_shared_perm(tmp) < 0)
 -              goto out;
 -      if (rename(tmp, path) < 0)
 -              goto out;
 +
 +      if (uic_is_stale(&uic)) {
 +              if (adjust_shared_perm(tmp) < 0)
 +                      goto out;
 +              if (rename(tmp, path) < 0)
 +                      goto out;
 +      } else {
 +              unlink(tmp);
 +      }
        ret = 0;
  
  out:
        if (ret) {
                error_errno("unable to update %s", path);
 -              if (fp)
 -                      fclose(fp);
 +              if (uic.cur_fp)
 +                      fclose(uic.cur_fp);
                else if (fd >= 0)
                        close(fd);
                unlink(tmp);
        }
        free(tmp);
 +      if (uic.old_fp)
 +              fclose(uic.old_fp);
 +      strbuf_release(&uic.old_sb);
 +      strbuf_release(&uic.cur_sb);
        return ret;
  }
  
  static int add_info_ref(const char *path, const struct object_id *oid,
                        int flag, void *cb_data)
  {
 -      FILE *fp = cb_data;
 +      struct update_info_ctx *uic = cb_data;
        struct object *o = parse_object(the_repository, oid);
        if (!o)
                return -1;
  
 -      if (fprintf(fp, "%s     %s\n", oid_to_hex(oid), path) < 0)
 +      if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
                return -1;
  
        if (o->type == OBJ_TAG) {
                o = deref_tag(the_repository, o, path, 0);
                if (o)
 -                      if (fprintf(fp, "%s     %s^{}\n",
 +                      if (uic_printf(uic, "%s %s^{}\n",
                                oid_to_hex(&o->oid), path) < 0)
                                return -1;
        }
        return 0;
  }
  
 -static int generate_info_refs(FILE *fp)
 +static int generate_info_refs(struct update_info_ctx *uic)
  {
 -      return for_each_ref(add_info_ref, fp);
 +      return for_each_ref(add_info_ref, uic);
  }
  
 -static int update_info_refs(void)
 +static int update_info_refs(int force)
  {
        char *path = git_pathdup("info/refs");
 -      int ret = update_info_file(path, generate_info_refs);
 +      int ret = update_info_file(path, generate_info_refs, force);
        free(path);
        return ret;
  }
@@@ -283,26 -192,21 +284,21 @@@ static void init_pack_info(const char *
  {
        struct packed_git *p;
        int stale;
-       int i = 0;
+       int i;
+       size_t alloc = 0;
  
        for (p = get_all_packs(the_repository); p; p = p->next) {
                /* we ignore things on alternate path since they are
                 * not available to the pullers in general.
                 */
-               if (!p->pack_local)
-                       continue;
-               i++;
-       }
-       num_pack = i;
-       info = xcalloc(num_pack, sizeof(struct pack_info *));
-       for (i = 0, p = get_all_packs(the_repository); p; p = p->next) {
-               if (!p->pack_local)
+               if (!p->pack_local || !file_exists(p->pack_name))
                        continue;
-               assert(i < num_pack);
+               i = num_pack++;
+               ALLOC_GROW(info, num_pack, alloc);
                info[i] = xcalloc(1, sizeof(struct pack_info));
                info[i]->p = p;
                info[i]->old_num = -1;
-               i++;
        }
  
        if (infofile && !force)
@@@ -328,14 -232,14 +324,14 @@@ static void free_pack_info(void
        free(info);
  }
  
 -static int write_pack_info_file(FILE *fp)
 +static int write_pack_info_file(struct update_info_ctx *uic)
  {
        int i;
        for (i = 0; i < num_pack; i++) {
 -              if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0)
 +              if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
                        return -1;
        }
 -      if (fputc('\n', fp) == EOF)
 +      if (uic_printf(uic, "\n") < 0)
                return -1;
        return 0;
  }
@@@ -346,7 -250,7 +342,7 @@@ static int update_info_packs(int force
        int ret;
  
        init_pack_info(infofile, force);
 -      ret = update_info_file(infofile, write_pack_info_file);
 +      ret = update_info_file(infofile, write_pack_info_file, force);
        free_pack_info();
        free(infofile);
        return ret;
@@@ -361,7 -265,7 +357,7 @@@ int update_server_info(int force
         */
        int errs = 0;
  
 -      errs = errs | update_info_refs();
 +      errs = errs | update_info_refs(force);
        errs = errs | update_info_packs(force);
  
        /* remove leftover rev-cache file if there is any */