Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/server-info.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / server-info.c` 374 lines ยท 7996 bytes Raw Blame History 1 #define DISABLE_SIGN_COMPARE_WARNINGS 2 3 #include "git-compat-util.h" 4 #include "dir.h" 5 #include "hex.h" 6 #include "repository.h" 7 #include "refs.h" 8 #include "object.h" 9 #include "commit.h" 10 #include "tag.h" 11 #include "packfile.h" 12 #include "path.h" 13 #include "object-file.h" 14 #include "odb.h" 15 #include "server-info.h" 16 #include "strbuf.h" 17 #include "tempfile.h" 18 19 struct update_info_ctx { 20 struct repository *repo; 21 FILE *cur_fp; 22 FILE *old_fp; /* becomes NULL if it differs from cur_fp */ 23 struct strbuf cur_sb; 24 struct strbuf old_sb; 25 }; 26 27 static void uic_mark_stale(struct update_info_ctx *uic) 28 { 29 fclose(uic->old_fp); 30 uic->old_fp = NULL; 31 } 32 33 static int uic_is_stale(const struct update_info_ctx *uic) 34 { 35 return uic->old_fp == NULL; 36 } 37 38 __attribute__((format (printf, 2, 3))) 39 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...) 40 { 41 va_list ap; 42 int ret = -1; 43 44 va_start(ap, fmt); 45 46 if (uic_is_stale(uic)) { 47 ret = vfprintf(uic->cur_fp, fmt, ap); 48 } else { 49 ssize_t r; 50 struct strbuf *cur = &uic->cur_sb; 51 struct strbuf *old = &uic->old_sb; 52 53 strbuf_reset(cur); 54 strbuf_vinsertf(cur, 0, fmt, ap); 55 56 strbuf_reset(old); 57 strbuf_grow(old, cur->len); 58 r = fread(old->buf, 1, cur->len, uic->old_fp); 59 if (r != cur->len || memcmp(old->buf, cur->buf, r)) 60 uic_mark_stale(uic); 61 62 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len) 63 ret = 0; 64 } 65 66 va_end(ap); 67 68 return ret; 69 } 70 71 /* 72 * Create the file "path" by writing to a temporary file and renaming 73 * it into place. The contents of the file come from "generate", which 74 * should return non-zero if it encounters an error. 75 */ 76 static int update_info_file(struct repository *r, char *path, 77 int (*generate)(struct update_info_ctx *), 78 int force) 79 { 80 char *tmp = mkpathdup("%s_XXXXXX", path); 81 struct tempfile *f = NULL; 82 int ret = -1; 83 struct update_info_ctx uic = { 84 .repo = r, 85 .cur_fp = NULL, 86 .old_fp = NULL, 87 .cur_sb = STRBUF_INIT, 88 .old_sb = STRBUF_INIT 89 }; 90 91 safe_create_leading_directories(r, path); 92 f = mks_tempfile_m(tmp, 0666); 93 if (!f) 94 goto out; 95 uic.cur_fp = fdopen_tempfile(f, "w"); 96 if (!uic.cur_fp) 97 goto out; 98 99 /* no problem on ENOENT and old_fp == NULL, it's stale, now */ 100 if (!force) 101 uic.old_fp = fopen_or_warn(path, "r"); 102 103 /* 104 * uic_printf will compare incremental comparison against old_fp 105 * and mark uic as stale if needed 106 */ 107 ret = generate(&uic); 108 if (ret) 109 goto out; 110 111 /* new file may be shorter than the old one, check here */ 112 if (!uic_is_stale(&uic)) { 113 struct stat st; 114 long new_len = ftell(uic.cur_fp); 115 int old_fd = fileno(uic.old_fp); 116 117 if (new_len < 0) { 118 ret = -1; 119 goto out; 120 } 121 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len)) 122 uic_mark_stale(&uic); 123 } 124 125 uic.cur_fp = NULL; 126 127 if (uic_is_stale(&uic)) { 128 if (adjust_shared_perm(r, get_tempfile_path(f)) < 0) 129 goto out; 130 if (rename_tempfile(&f, path) < 0) 131 goto out; 132 } else { 133 delete_tempfile(&f); 134 } 135 ret = 0; 136 137 out: 138 if (ret) { 139 error_errno("unable to update %s", path); 140 if (f) 141 delete_tempfile(&f); 142 } 143 free(tmp); 144 if (uic.old_fp) 145 fclose(uic.old_fp); 146 strbuf_release(&uic.old_sb); 147 strbuf_release(&uic.cur_sb); 148 return ret; 149 } 150 151 static int add_info_ref(const struct reference *ref, void *cb_data) 152 { 153 struct update_info_ctx *uic = cb_data; 154 struct object *o = parse_object(uic->repo, ref->oid); 155 if (!o) 156 return -1; 157 158 if (uic_printf(uic, "%s %s\n", oid_to_hex(ref->oid), ref->name) < 0) 159 return -1; 160 161 if (o->type == OBJ_TAG) { 162 o = deref_tag(uic->repo, o, ref->name, 0); 163 if (o) 164 if (uic_printf(uic, "%s %s^{}\n", 165 oid_to_hex(&o->oid), ref->name) < 0) 166 return -1; 167 } 168 return 0; 169 } 170 171 static int generate_info_refs(struct update_info_ctx *uic) 172 { 173 return refs_for_each_ref(get_main_ref_store(uic->repo), 174 add_info_ref, uic); 175 } 176 177 static int update_info_refs(struct repository *r, int force) 178 { 179 char *path = repo_git_path(r, "info/refs"); 180 int ret = update_info_file(r, path, generate_info_refs, force); 181 free(path); 182 return ret; 183 } 184 185 /* packs */ 186 static struct pack_info { 187 struct packed_git *p; 188 int old_num; 189 int new_num; 190 } **info; 191 static int num_pack; 192 193 static struct pack_info *find_pack_by_name(const char *name) 194 { 195 int i; 196 for (i = 0; i < num_pack; i++) { 197 struct packed_git *p = info[i]->p; 198 if (!strcmp(pack_basename(p), name)) 199 return info[i]; 200 } 201 return NULL; 202 } 203 204 /* Returns non-zero when we detect that the info in the 205 * old file is useless. 206 */ 207 static int parse_pack_def(const char *packname, int old_cnt) 208 { 209 struct pack_info *i = find_pack_by_name(packname); 210 if (i) { 211 i->old_num = old_cnt; 212 return 0; 213 } 214 else { 215 /* The file describes a pack that is no longer here */ 216 return 1; 217 } 218 } 219 220 /* Returns non-zero when we detect that the info in the 221 * old file is useless. 222 */ 223 static int read_pack_info_file(const char *infofile) 224 { 225 FILE *fp; 226 struct strbuf line = STRBUF_INIT; 227 int old_cnt = 0; 228 int stale = 1; 229 230 fp = fopen_or_warn(infofile, "r"); 231 if (!fp) 232 return 1; /* nonexistent is not an error. */ 233 234 while (strbuf_getline(&line, fp) != EOF) { 235 const char *arg; 236 237 if (!line.len) 238 continue; 239 240 if (skip_prefix(line.buf, "P ", &arg)) { 241 /* P name */ 242 if (parse_pack_def(arg, old_cnt++)) 243 goto out_stale; 244 } else if (line.buf[0] == 'D') { 245 /* we used to emit D but that was misguided. */ 246 goto out_stale; 247 } else if (line.buf[0] == 'T') { 248 /* we used to emit T but nobody uses it. */ 249 goto out_stale; 250 } else { 251 error("unrecognized: %s", line.buf); 252 } 253 } 254 stale = 0; 255 256 out_stale: 257 strbuf_release(&line); 258 fclose(fp); 259 return stale; 260 } 261 262 static int compare_info(const void *a_, const void *b_) 263 { 264 struct pack_info *const *a = a_; 265 struct pack_info *const *b = b_; 266 267 if (0 <= (*a)->old_num && 0 <= (*b)->old_num) 268 /* Keep the order in the original */ 269 return (*a)->old_num - (*b)->old_num; 270 else if (0 <= (*a)->old_num) 271 /* Only A existed in the original so B is obviously newer */ 272 return -1; 273 else if (0 <= (*b)->old_num) 274 /* The other way around. */ 275 return 1; 276 277 /* then it does not matter but at least keep the comparison stable */ 278 if ((*a)->p == (*b)->p) 279 return 0; 280 else if ((*a)->p < (*b)->p) 281 return -1; 282 else 283 return 1; 284 } 285 286 static void init_pack_info(struct repository *r, const char *infofile, int force) 287 { 288 struct packed_git *p; 289 int stale; 290 int i; 291 size_t alloc = 0; 292 293 repo_for_each_pack(r, p) { 294 /* we ignore things on alternate path since they are 295 * not available to the pullers in general. 296 */ 297 if (!p->pack_local || !file_exists(p->pack_name)) 298 continue; 299 300 i = num_pack++; 301 ALLOC_GROW(info, num_pack, alloc); 302 CALLOC_ARRAY(info[i], 1); 303 info[i]->p = p; 304 info[i]->old_num = -1; 305 } 306 307 if (infofile && !force) 308 stale = read_pack_info_file(infofile); 309 else 310 stale = 1; 311 312 for (i = 0; i < num_pack; i++) 313 if (stale) 314 info[i]->old_num = -1; 315 316 /* renumber them */ 317 QSORT(info, num_pack, compare_info); 318 for (i = 0; i < num_pack; i++) 319 info[i]->new_num = i; 320 } 321 322 static void free_pack_info(void) 323 { 324 int i; 325 for (i = 0; i < num_pack; i++) 326 free(info[i]); 327 free(info); 328 } 329 330 static int write_pack_info_file(struct update_info_ctx *uic) 331 { 332 int i; 333 for (i = 0; i < num_pack; i++) { 334 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0) 335 return -1; 336 } 337 if (uic_printf(uic, "\n") < 0) 338 return -1; 339 return 0; 340 } 341 342 static int update_info_packs(struct repository *r, int force) 343 { 344 char *infofile = mkpathdup("%s/info/packs", 345 repo_get_object_directory(r)); 346 int ret; 347 348 init_pack_info(r, infofile, force); 349 ret = update_info_file(r, infofile, write_pack_info_file, force); 350 free_pack_info(); 351 free(infofile); 352 return ret; 353 } 354 355 /* public */ 356 int update_server_info(struct repository *r, int force) 357 { 358 /* We would add more dumb-server support files later, 359 * including index of available pack files and their 360 * intended audiences. 361 */ 362 int errs = 0; 363 char *path; 364 365 errs = errs | update_info_refs(r, force); 366 errs = errs | update_info_packs(r, force); 367 368 /* remove leftover rev-cache file if there is any */ 369 path = repo_git_path(r, "info/rev-cache"); 370 unlink_or_warn(path); 371 free(path); 372 373 return errs; 374 }