1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "repository.h"
13 #include "object-file.h"
14 #include "object-store-ll.h"
15 #include "server-info.h"
19 struct update_info_ctx
{
21 FILE *old_fp
; /* becomes NULL if it differs from cur_fp */
26 static void uic_mark_stale(struct update_info_ctx
*uic
)
32 static int uic_is_stale(const struct update_info_ctx
*uic
)
34 return uic
->old_fp
== NULL
;
37 __attribute__((format (printf
, 2, 3)))
38 static int uic_printf(struct update_info_ctx
*uic
, const char *fmt
, ...)
45 if (uic_is_stale(uic
)) {
46 ret
= vfprintf(uic
->cur_fp
, fmt
, ap
);
49 struct strbuf
*cur
= &uic
->cur_sb
;
50 struct strbuf
*old
= &uic
->old_sb
;
53 strbuf_vinsertf(cur
, 0, fmt
, ap
);
56 strbuf_grow(old
, cur
->len
);
57 r
= fread(old
->buf
, 1, cur
->len
, uic
->old_fp
);
58 if (r
!= cur
->len
|| memcmp(old
->buf
, cur
->buf
, r
))
61 if (fwrite(cur
->buf
, 1, cur
->len
, uic
->cur_fp
) == cur
->len
)
71 * Create the file "path" by writing to a temporary file and renaming
72 * it into place. The contents of the file come from "generate", which
73 * should return non-zero if it encounters an error.
75 static int update_info_file(char *path
,
76 int (*generate
)(struct update_info_ctx
*),
79 char *tmp
= mkpathdup("%s_XXXXXX", path
);
80 struct tempfile
*f
= NULL
;
82 struct update_info_ctx uic
= {
85 .cur_sb
= STRBUF_INIT
,
89 safe_create_leading_directories(path
);
90 f
= mks_tempfile_m(tmp
, 0666);
93 uic
.cur_fp
= fdopen_tempfile(f
, "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
);
125 if (uic_is_stale(&uic
)) {
126 if (adjust_shared_perm(get_tempfile_path(f
)) < 0)
128 if (rename_tempfile(&f
, path
) < 0)
137 error_errno("unable to update %s", path
);
144 strbuf_release(&uic
.old_sb
);
145 strbuf_release(&uic
.cur_sb
);
149 static int add_info_ref(const char *path
, const char *referent UNUSED
, const struct object_id
*oid
,
153 struct update_info_ctx
*uic
= cb_data
;
154 struct object
*o
= parse_object(the_repository
, oid
);
158 if (uic_printf(uic
, "%s %s\n", oid_to_hex(oid
), path
) < 0)
161 if (o
->type
== OBJ_TAG
) {
162 o
= deref_tag(the_repository
, o
, path
, 0);
164 if (uic_printf(uic
, "%s %s^{}\n",
165 oid_to_hex(&o
->oid
), path
) < 0)
171 static int generate_info_refs(struct update_info_ctx
*uic
)
173 return refs_for_each_ref(get_main_ref_store(the_repository
),
177 static int update_info_refs(int force
)
179 char *path
= git_pathdup("info/refs");
180 int ret
= update_info_file(path
, generate_info_refs
, force
);
186 static struct pack_info
{
187 struct packed_git
*p
;
193 static struct pack_info
*find_pack_by_name(const char *name
)
196 for (i
= 0; i
< num_pack
; i
++) {
197 struct packed_git
*p
= info
[i
]->p
;
198 if (!strcmp(pack_basename(p
), name
))
204 /* Returns non-zero when we detect that the info in the
205 * old file is useless.
207 static int parse_pack_def(const char *packname
, int old_cnt
)
209 struct pack_info
*i
= find_pack_by_name(packname
);
211 i
->old_num
= old_cnt
;
215 /* The file describes a pack that is no longer here */
220 /* Returns non-zero when we detect that the info in the
221 * old file is useless.
223 static int read_pack_info_file(const char *infofile
)
226 struct strbuf line
= STRBUF_INIT
;
230 fp
= fopen_or_warn(infofile
, "r");
232 return 1; /* nonexistent is not an error. */
234 while (strbuf_getline(&line
, fp
) != EOF
) {
240 if (skip_prefix(line
.buf
, "P ", &arg
)) {
242 if (parse_pack_def(arg
, old_cnt
++))
244 } else if (line
.buf
[0] == 'D') {
245 /* we used to emit D but that was misguided. */
247 } else if (line
.buf
[0] == 'T') {
248 /* we used to emit T but nobody uses it. */
251 error("unrecognized: %s", line
.buf
);
257 strbuf_release(&line
);
262 static int compare_info(const void *a_
, const void *b_
)
264 struct pack_info
*const *a
= a_
;
265 struct pack_info
*const *b
= b_
;
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 */
273 else if (0 <= (*b
)->old_num
)
274 /* The other way around. */
277 /* then it does not matter but at least keep the comparison stable */
278 if ((*a
)->p
== (*b
)->p
)
280 else if ((*a
)->p
< (*b
)->p
)
286 static void init_pack_info(const char *infofile
, int force
)
288 struct packed_git
*p
;
293 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
294 /* we ignore things on alternate path since they are
295 * not available to the pullers in general.
297 if (!p
->pack_local
|| !file_exists(p
->pack_name
))
301 ALLOC_GROW(info
, num_pack
, alloc
);
302 CALLOC_ARRAY(info
[i
], 1);
304 info
[i
]->old_num
= -1;
307 if (infofile
&& !force
)
308 stale
= read_pack_info_file(infofile
);
312 for (i
= 0; i
< num_pack
; i
++)
314 info
[i
]->old_num
= -1;
317 QSORT(info
, num_pack
, compare_info
);
318 for (i
= 0; i
< num_pack
; i
++)
319 info
[i
]->new_num
= i
;
322 static void free_pack_info(void)
325 for (i
= 0; i
< num_pack
; i
++)
330 static int write_pack_info_file(struct update_info_ctx
*uic
)
333 for (i
= 0; i
< num_pack
; i
++) {
334 if (uic_printf(uic
, "P %s\n", pack_basename(info
[i
]->p
)) < 0)
337 if (uic_printf(uic
, "\n") < 0)
342 static int update_info_packs(int force
)
344 char *infofile
= mkpathdup("%s/info/packs",
345 repo_get_object_directory(the_repository
));
348 init_pack_info(infofile
, force
);
349 ret
= update_info_file(infofile
, write_pack_info_file
, force
);
356 int update_server_info(int force
)
358 /* We would add more dumb-server support files later,
359 * including index of available pack files and their
360 * intended audiences.
364 errs
= errs
| update_info_refs(force
);
365 errs
= errs
| update_info_packs(force
);
367 /* remove leftover rev-cache file if there is any */
368 unlink_or_warn(git_path("info/rev-cache"));