server-info.con commit t7800: readlink may not be available (d2addc3)
   1#include "cache.h"
   2#include "refs.h"
   3#include "object.h"
   4#include "commit.h"
   5#include "tag.h"
   6
   7/*
   8 * Create the file "path" by writing to a temporary file and renaming
   9 * it into place. The contents of the file come from "generate", which
  10 * should return non-zero if it encounters an error.
  11 */
  12static int update_info_file(char *path, int (*generate)(FILE *))
  13{
  14        char *tmp = mkpathdup("%s_XXXXXX", path);
  15        int ret = -1;
  16        int fd = -1;
  17        FILE *fp = NULL;
  18
  19        safe_create_leading_directories(path);
  20        fd = git_mkstemp_mode(tmp, 0666);
  21        if (fd < 0)
  22                goto out;
  23        fp = fdopen(fd, "w");
  24        if (!fp)
  25                goto out;
  26        ret = generate(fp);
  27        if (ret)
  28                goto out;
  29        if (fclose(fp))
  30                goto out;
  31        if (adjust_shared_perm(tmp) < 0)
  32                goto out;
  33        if (rename(tmp, path) < 0)
  34                goto out;
  35        ret = 0;
  36
  37out:
  38        if (ret) {
  39                error("unable to update %s: %s", path, strerror(errno));
  40                if (fp)
  41                        fclose(fp);
  42                else if (fd >= 0)
  43                        close(fd);
  44                unlink(tmp);
  45        }
  46        free(tmp);
  47        return ret;
  48}
  49
  50static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  51{
  52        FILE *fp = cb_data;
  53        struct object *o = parse_object(sha1);
  54        if (!o)
  55                return -1;
  56
  57        if (fprintf(fp, "%s     %s\n", sha1_to_hex(sha1), path) < 0)
  58                return -1;
  59
  60        if (o->type == OBJ_TAG) {
  61                o = deref_tag(o, path, 0);
  62                if (o)
  63                        if (fprintf(fp, "%s     %s^{}\n",
  64                                sha1_to_hex(o->sha1), path) < 0)
  65                                return -1;
  66        }
  67        return 0;
  68}
  69
  70static int generate_info_refs(FILE *fp)
  71{
  72        return for_each_ref(add_info_ref, fp);
  73}
  74
  75static int update_info_refs(int force)
  76{
  77        char *path = git_pathdup("info/refs");
  78        int ret = update_info_file(path, generate_info_refs);
  79        free(path);
  80        return ret;
  81}
  82
  83/* packs */
  84static struct pack_info {
  85        struct packed_git *p;
  86        int old_num;
  87        int new_num;
  88        int nr_alloc;
  89        int nr_heads;
  90        unsigned char (*head)[20];
  91} **info;
  92static int num_pack;
  93static const char *objdir;
  94static int objdirlen;
  95
  96static struct pack_info *find_pack_by_name(const char *name)
  97{
  98        int i;
  99        for (i = 0; i < num_pack; i++) {
 100                struct packed_git *p = info[i]->p;
 101                /* skip "/pack/" after ".git/objects" */
 102                if (!strcmp(p->pack_name + objdirlen + 6, name))
 103                        return info[i];
 104        }
 105        return NULL;
 106}
 107
 108/* Returns non-zero when we detect that the info in the
 109 * old file is useless.
 110 */
 111static int parse_pack_def(const char *line, int old_cnt)
 112{
 113        struct pack_info *i = find_pack_by_name(line + 2);
 114        if (i) {
 115                i->old_num = old_cnt;
 116                return 0;
 117        }
 118        else {
 119                /* The file describes a pack that is no longer here */
 120                return 1;
 121        }
 122}
 123
 124/* Returns non-zero when we detect that the info in the
 125 * old file is useless.
 126 */
 127static int read_pack_info_file(const char *infofile)
 128{
 129        FILE *fp;
 130        char line[1000];
 131        int old_cnt = 0;
 132
 133        fp = fopen(infofile, "r");
 134        if (!fp)
 135                return 1; /* nonexistent is not an error. */
 136
 137        while (fgets(line, sizeof(line), fp)) {
 138                int len = strlen(line);
 139                if (len && line[len-1] == '\n')
 140                        line[--len] = 0;
 141
 142                if (!len)
 143                        continue;
 144
 145                switch (line[0]) {
 146                case 'P': /* P name */
 147                        if (parse_pack_def(line, old_cnt++))
 148                                goto out_stale;
 149                        break;
 150                case 'D': /* we used to emit D but that was misguided. */
 151                case 'T': /* we used to emit T but nobody uses it. */
 152                        goto out_stale;
 153                default:
 154                        error("unrecognized: %s", line);
 155                        break;
 156                }
 157        }
 158        fclose(fp);
 159        return 0;
 160 out_stale:
 161        fclose(fp);
 162        return 1;
 163}
 164
 165static int compare_info(const void *a_, const void *b_)
 166{
 167        struct pack_info *const *a = a_;
 168        struct pack_info *const *b = b_;
 169
 170        if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
 171                /* Keep the order in the original */
 172                return (*a)->old_num - (*b)->old_num;
 173        else if (0 <= (*a)->old_num)
 174                /* Only A existed in the original so B is obviously newer */
 175                return -1;
 176        else if (0 <= (*b)->old_num)
 177                /* The other way around. */
 178                return 1;
 179
 180        /* then it does not matter but at least keep the comparison stable */
 181        if ((*a)->p == (*b)->p)
 182                return 0;
 183        else if ((*a)->p < (*b)->p)
 184                return -1;
 185        else
 186                return 1;
 187}
 188
 189static void init_pack_info(const char *infofile, int force)
 190{
 191        struct packed_git *p;
 192        int stale;
 193        int i = 0;
 194
 195        objdir = get_object_directory();
 196        objdirlen = strlen(objdir);
 197
 198        prepare_packed_git();
 199        for (p = packed_git; p; p = p->next) {
 200                /* we ignore things on alternate path since they are
 201                 * not available to the pullers in general.
 202                 */
 203                if (!p->pack_local)
 204                        continue;
 205                i++;
 206        }
 207        num_pack = i;
 208        info = xcalloc(num_pack, sizeof(struct pack_info *));
 209        for (i = 0, p = packed_git; p; p = p->next) {
 210                if (!p->pack_local)
 211                        continue;
 212                info[i] = xcalloc(1, sizeof(struct pack_info));
 213                info[i]->p = p;
 214                info[i]->old_num = -1;
 215                i++;
 216        }
 217
 218        if (infofile && !force)
 219                stale = read_pack_info_file(infofile);
 220        else
 221                stale = 1;
 222
 223        for (i = 0; i < num_pack; i++) {
 224                if (stale) {
 225                        info[i]->old_num = -1;
 226                        info[i]->nr_heads = 0;
 227                }
 228        }
 229
 230        /* renumber them */
 231        qsort(info, num_pack, sizeof(info[0]), compare_info);
 232        for (i = 0; i < num_pack; i++)
 233                info[i]->new_num = i;
 234}
 235
 236static void free_pack_info(void)
 237{
 238        int i;
 239        for (i = 0; i < num_pack; i++)
 240                free(info[i]);
 241        free(info);
 242}
 243
 244static int write_pack_info_file(FILE *fp)
 245{
 246        int i;
 247        for (i = 0; i < num_pack; i++) {
 248                if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
 249                        return -1;
 250        }
 251        if (fputc('\n', fp) == EOF)
 252                return -1;
 253        return 0;
 254}
 255
 256static int update_info_packs(int force)
 257{
 258        char *infofile = mkpathdup("%s/info/packs", get_object_directory());
 259        int ret;
 260
 261        init_pack_info(infofile, force);
 262        ret = update_info_file(infofile, write_pack_info_file);
 263        free_pack_info();
 264        free(infofile);
 265        return ret;
 266}
 267
 268/* public */
 269int update_server_info(int force)
 270{
 271        /* We would add more dumb-server support files later,
 272         * including index of available pack files and their
 273         * intended audiences.
 274         */
 275        int errs = 0;
 276
 277        errs = errs | update_info_refs(force);
 278        errs = errs | update_info_packs(force);
 279
 280        /* remove leftover rev-cache file if there is any */
 281        unlink_or_warn(git_path("info/rev-cache"));
 282
 283        return errs;
 284}