1 #include "git-compat-util.h"
2 #include "tmp-objdir.h"
4 #include "chdir-notify.h"
6 #include "environment.h"
7 #include "object-file.h"
9 #include "string-list.h"
13 #include "object-store-ll.h"
18 struct object_directory
*prev_odb
;
23 * Allow only one tmp_objdir at a time in a running process, which simplifies
24 * our atexit cleanup routines. It's doubtful callers will ever need
25 * more than one, and we can expand later if so. You can have many such
26 * tmp_objdirs simultaneously in many processes, of course.
28 static struct tmp_objdir
*the_tmp_objdir
;
30 static void tmp_objdir_free(struct tmp_objdir
*t
)
32 strbuf_release(&t
->path
);
33 strvec_clear(&t
->env
);
37 int tmp_objdir_destroy(struct tmp_objdir
*t
)
44 if (t
== the_tmp_objdir
)
45 the_tmp_objdir
= NULL
;
48 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
50 err
= remove_dir_recursively(&t
->path
, 0);
57 static void remove_tmp_objdir(void)
59 tmp_objdir_destroy(the_tmp_objdir
);
62 void tmp_objdir_discard_objects(struct tmp_objdir
*t
)
64 remove_dir_recursively(&t
->path
, REMOVE_DIR_KEEP_TOPLEVEL
);
68 * These env_* functions are for setting up the child environment; the
69 * "replace" variant overrides the value of any existing variable with that
70 * "key". The "append" variant puts our new value at the end of a list,
71 * separated by PATH_SEP (which is what separate values in
72 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
74 static void env_append(struct strvec
*env
, const char *key
, const char *val
)
76 struct strbuf quoted
= STRBUF_INIT
;
80 * Avoid quoting if it's not necessary, for maximum compatibility
81 * with older parsers which don't understand the quoting.
83 if (*val
== '"' || strchr(val
, PATH_SEP
)) {
84 strbuf_addch("ed
, '"');
85 quote_c_style(val
, "ed
, NULL
, 1);
86 strbuf_addch("ed
, '"');
92 strvec_pushf(env
, "%s=%s", key
, val
);
94 strvec_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
96 strbuf_release("ed
);
99 static void env_replace(struct strvec
*env
, const char *key
, const char *val
)
101 strvec_pushf(env
, "%s=%s", key
, val
);
104 static int setup_tmp_objdir(const char *root
)
109 path
= xstrfmt("%s/pack", root
);
110 ret
= mkdir(path
, 0777);
116 struct tmp_objdir
*tmp_objdir_create(const char *prefix
)
118 static int installed_handlers
;
119 struct tmp_objdir
*t
;
122 BUG("only one tmp_objdir can be used at a time");
124 t
= xcalloc(1, sizeof(*t
));
125 strbuf_init(&t
->path
, 0);
126 strvec_init(&t
->env
);
129 * Use a string starting with tmp_ so that the builtin/prune.c code
130 * can recognize any stale objdirs left behind by a crash and delete
133 strbuf_addf(&t
->path
, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix
);
135 if (!mkdtemp(t
->path
.buf
)) {
136 /* free, not destroy, as we never touched the filesystem */
142 if (!installed_handlers
) {
143 atexit(remove_tmp_objdir
);
144 installed_handlers
++;
147 if (setup_tmp_objdir(t
->path
.buf
)) {
148 tmp_objdir_destroy(t
);
152 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
153 absolute_path(get_object_directory()));
154 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
155 env_replace(&t
->env
, GIT_QUARANTINE_ENVIRONMENT
,
156 absolute_path(t
->path
.buf
));
162 * Make sure we copy packfiles and their associated metafiles in the correct
163 * order. All of these ends_with checks are slightly expensive to do in
164 * the midst of a sorting routine, but in practice it shouldn't matter.
165 * We will have a relatively small number of packfiles to order, and loose
166 * objects exit early in the first line.
168 static int pack_copy_priority(const char *name
)
170 if (!starts_with(name
, "pack"))
172 if (ends_with(name
, ".keep"))
174 if (ends_with(name
, ".pack"))
176 if (ends_with(name
, ".rev"))
178 if (ends_with(name
, ".idx"))
183 static int pack_copy_cmp(const char *a
, const char *b
)
185 return pack_copy_priority(a
) - pack_copy_priority(b
);
188 static int read_dir_paths(struct string_list
*out
, const char *path
)
197 while ((de
= readdir(dh
)))
198 if (de
->d_name
[0] != '.')
199 string_list_append(out
, de
->d_name
);
205 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
);
207 static int migrate_one(struct strbuf
*src
, struct strbuf
*dst
)
211 if (stat(src
->buf
, &st
) < 0)
213 if (S_ISDIR(st
.st_mode
)) {
214 if (!mkdir(dst
->buf
, 0777)) {
215 if (adjust_shared_perm(dst
->buf
))
217 } else if (errno
!= EEXIST
)
219 return migrate_paths(src
, dst
);
221 return finalize_object_file(src
->buf
, dst
->buf
);
224 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
)
226 size_t src_len
= src
->len
, dst_len
= dst
->len
;
227 struct string_list paths
= STRING_LIST_INIT_DUP
;
231 if (read_dir_paths(&paths
, src
->buf
) < 0)
233 paths
.cmp
= pack_copy_cmp
;
234 string_list_sort(&paths
);
236 for (i
= 0; i
< paths
.nr
; i
++) {
237 const char *name
= paths
.items
[i
].string
;
239 strbuf_addf(src
, "/%s", name
);
240 strbuf_addf(dst
, "/%s", name
);
242 ret
|= migrate_one(src
, dst
);
244 strbuf_setlen(src
, src_len
);
245 strbuf_setlen(dst
, dst_len
);
248 string_list_clear(&paths
, 0);
252 int tmp_objdir_migrate(struct tmp_objdir
*t
)
254 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
261 if (the_repository
->objects
->odb
->will_destroy
)
262 BUG("migrating an ODB that was marked for destruction");
263 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
267 strbuf_addbuf(&src
, &t
->path
);
268 strbuf_addstr(&dst
, get_object_directory());
270 ret
= migrate_paths(&src
, &dst
);
272 strbuf_release(&src
);
273 strbuf_release(&dst
);
275 tmp_objdir_destroy(t
);
279 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
286 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
288 add_to_alternates_memory(t
->path
.buf
);
291 void tmp_objdir_replace_primary_odb(struct tmp_objdir
*t
, int will_destroy
)
294 BUG("the primary object database is already replaced");
295 t
->prev_odb
= set_temporary_primary_odb(t
->path
.buf
, will_destroy
);
296 t
->will_destroy
= will_destroy
;
299 struct tmp_objdir
*tmp_objdir_unapply_primary_odb(void)
301 if (!the_tmp_objdir
|| !the_tmp_objdir
->prev_odb
)
304 restore_primary_odb(the_tmp_objdir
->prev_odb
, the_tmp_objdir
->path
.buf
);
305 the_tmp_objdir
->prev_odb
= NULL
;
306 return the_tmp_objdir
;
309 void tmp_objdir_reapply_primary_odb(struct tmp_objdir
*t
, const char *old_cwd
,
314 path
= reparent_relative_path(old_cwd
, new_cwd
, t
->path
.buf
);
315 strbuf_reset(&t
->path
);
316 strbuf_addstr(&t
->path
, path
);
318 tmp_objdir_replace_primary_odb(t
, t
->will_destroy
);