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