4 #include "environment.h"
6 #include "repository.h"
12 #include "object-file.h"
13 #include "object-store.h"
17 struct update_info_ctx
{
19 FILE *old_fp
; /* becomes NULL if it differs from cur_fp */
24 static void uic_mark_stale(struct update_info_ctx
*uic
)
30 static int uic_is_stale(const struct update_info_ctx
*uic
)
32 return uic
->old_fp
== NULL
;
35 __attribute__((format (printf
, 2, 3)))
36 static int uic_printf(struct update_info_ctx
*uic
, const char *fmt
, ...)
43 if (uic_is_stale(uic
)) {
44 ret
= vfprintf(uic
->cur_fp
, fmt
, ap
);
47 struct strbuf
*cur
= &uic
->cur_sb
;
48 struct strbuf
*old
= &uic
->old_sb
;
51 strbuf_vinsertf(cur
, 0, fmt
, ap
);
54 strbuf_grow(old
, cur
->len
);
55 r
= fread(old
->buf
, 1, cur
->len
, uic
->old_fp
);
56 if (r
!= cur
->len
|| memcmp(old
->buf
, cur
->buf
, r
))
59 if (fwrite(cur
->buf
, 1, cur
->len
, uic
->cur_fp
) == cur
->len
)
69 * Create the file "path" by writing to a temporary file and renaming
70 * it into place. The contents of the file come from "generate", which
71 * should return non-zero if it encounters an error.
73 static int update_info_file(char *path
,
74 int (*generate
)(struct update_info_ctx
*),
77 char *tmp
= mkpathdup("%s_XXXXXX", path
);
81 struct update_info_ctx uic
= {
84 .cur_sb
= STRBUF_INIT
,
88 safe_create_leading_directories(path
);
89 fd
= git_mkstemp_mode(tmp
, 0666);
92 to_close
= uic
.cur_fp
= fdopen(fd
, "w");
97 /* no problem on ENOENT and old_fp == NULL, it's stale, now */
99 uic
.old_fp
= fopen_or_warn(path
, "r");
102 * uic_printf will compare incremental comparison against old_fp
103 * and mark uic as stale if needed
105 ret
= generate(&uic
);
109 /* new file may be shorter than the old one, check here */
110 if (!uic_is_stale(&uic
)) {
112 long new_len
= ftell(uic
.cur_fp
);
113 int old_fd
= fileno(uic
.old_fp
);
119 if (fstat(old_fd
, &st
) || (st
.st_size
!= (size_t)new_len
))
120 uic_mark_stale(&uic
);
124 if (fclose(to_close
))
127 if (uic_is_stale(&uic
)) {
128 if (adjust_shared_perm(tmp
) < 0)
130 if (rename(tmp
, path
) < 0)
139 error_errno("unable to update %s", path
);
149 strbuf_release(&uic
.old_sb
);
150 strbuf_release(&uic
.cur_sb
);
154 static int add_info_ref(const char *path
, const struct object_id
*oid
,
158 struct update_info_ctx
*uic
= cb_data
;
159 struct object
*o
= parse_object(the_repository
, oid
);
163 if (uic_printf(uic
, "%s %s\n", oid_to_hex(oid
), path
) < 0)
166 if (o
->type
== OBJ_TAG
) {
167 o
= deref_tag(the_repository
, o
, path
, 0);
169 if (uic_printf(uic
, "%s %s^{}\n",
170 oid_to_hex(&o
->oid
), path
) < 0)
176 static int generate_info_refs(struct update_info_ctx
*uic
)
178 return for_each_ref(add_info_ref
, uic
);
181 static int update_info_refs(int force
)
183 char *path
= git_pathdup("info/refs");
184 int ret
= update_info_file(path
, generate_info_refs
, force
);
190 static struct pack_info
{
191 struct packed_git
*p
;
197 static struct pack_info
*find_pack_by_name(const char *name
)
200 for (i
= 0; i
< num_pack
; i
++) {
201 struct packed_git
*p
= info
[i
]->p
;
202 if (!strcmp(pack_basename(p
), name
))
208 /* Returns non-zero when we detect that the info in the
209 * old file is useless.
211 static int parse_pack_def(const char *packname
, int old_cnt
)
213 struct pack_info
*i
= find_pack_by_name(packname
);
215 i
->old_num
= old_cnt
;
219 /* The file describes a pack that is no longer here */
224 /* Returns non-zero when we detect that the info in the
225 * old file is useless.
227 static int read_pack_info_file(const char *infofile
)
230 struct strbuf line
= STRBUF_INIT
;
234 fp
= fopen_or_warn(infofile
, "r");
236 return 1; /* nonexistent is not an error. */
238 while (strbuf_getline(&line
, fp
) != EOF
) {
244 if (skip_prefix(line
.buf
, "P ", &arg
)) {
246 if (parse_pack_def(arg
, old_cnt
++))
248 } else if (line
.buf
[0] == 'D') {
249 /* we used to emit D but that was misguided. */
251 } else if (line
.buf
[0] == 'T') {
252 /* we used to emit T but nobody uses it. */
255 error("unrecognized: %s", line
.buf
);
261 strbuf_release(&line
);
266 static int compare_info(const void *a_
, const void *b_
)
268 struct pack_info
*const *a
= a_
;
269 struct pack_info
*const *b
= b_
;
271 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
272 /* Keep the order in the original */
273 return (*a
)->old_num
- (*b
)->old_num
;
274 else if (0 <= (*a
)->old_num
)
275 /* Only A existed in the original so B is obviously newer */
277 else if (0 <= (*b
)->old_num
)
278 /* The other way around. */
281 /* then it does not matter but at least keep the comparison stable */
282 if ((*a
)->p
== (*b
)->p
)
284 else if ((*a
)->p
< (*b
)->p
)
290 static void init_pack_info(const char *infofile
, int force
)
292 struct packed_git
*p
;
297 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
298 /* we ignore things on alternate path since they are
299 * not available to the pullers in general.
301 if (!p
->pack_local
|| !file_exists(p
->pack_name
))
305 ALLOC_GROW(info
, num_pack
, alloc
);
306 CALLOC_ARRAY(info
[i
], 1);
308 info
[i
]->old_num
= -1;
311 if (infofile
&& !force
)
312 stale
= read_pack_info_file(infofile
);
316 for (i
= 0; i
< num_pack
; i
++)
318 info
[i
]->old_num
= -1;
321 QSORT(info
, num_pack
, compare_info
);
322 for (i
= 0; i
< num_pack
; i
++)
323 info
[i
]->new_num
= i
;
326 static void free_pack_info(void)
329 for (i
= 0; i
< num_pack
; i
++)
334 static int write_pack_info_file(struct update_info_ctx
*uic
)
337 for (i
= 0; i
< num_pack
; i
++) {
338 if (uic_printf(uic
, "P %s\n", pack_basename(info
[i
]->p
)) < 0)
341 if (uic_printf(uic
, "\n") < 0)
346 static int update_info_packs(int force
)
348 char *infofile
= mkpathdup("%s/info/packs", get_object_directory());
351 init_pack_info(infofile
, force
);
352 ret
= update_info_file(infofile
, write_pack_info_file
, force
);
359 int update_server_info(int force
)
361 /* We would add more dumb-server support files later,
362 * including index of available pack files and their
363 * intended audiences.
367 errs
= errs
| update_info_refs(force
);
368 errs
= errs
| update_info_packs(force
);
370 /* remove leftover rev-cache file if there is any */
371 unlink_or_warn(git_path("info/rev-cache"));