2 #include "tmp-objdir.h"
5 #include "string-list.h"
7 #include "argv-array.h"
12 struct argv_array env
;
16 * Allow only one tmp_objdir at a time in a running process, which simplifies
17 * our signal/atexit cleanup routines. It's doubtful callers will ever need
18 * more than one, and we can expand later if so. You can have many such
19 * tmp_objdirs simultaneously in many processes, of course.
21 static struct tmp_objdir
*the_tmp_objdir
;
23 static void tmp_objdir_free(struct tmp_objdir
*t
)
25 strbuf_release(&t
->path
);
26 argv_array_clear(&t
->env
);
30 static int tmp_objdir_destroy_1(struct tmp_objdir
*t
, int on_signal
)
37 if (t
== the_tmp_objdir
)
38 the_tmp_objdir
= NULL
;
41 * This may use malloc via strbuf_grow(), but we should
42 * have pre-grown t->path sufficiently so that this
43 * doesn't happen in practice.
45 err
= remove_dir_recursively(&t
->path
, 0);
48 * When we are cleaning up due to a signal, we won't bother
49 * freeing memory; it may cause a deadlock if the signal
50 * arrived while libc's allocator lock is held.
57 int tmp_objdir_destroy(struct tmp_objdir
*t
)
59 return tmp_objdir_destroy_1(t
, 0);
62 static void remove_tmp_objdir(void)
64 tmp_objdir_destroy(the_tmp_objdir
);
67 static void remove_tmp_objdir_on_signal(int signo
)
69 tmp_objdir_destroy_1(the_tmp_objdir
, 1);
75 * These env_* functions are for setting up the child environment; the
76 * "replace" variant overrides the value of any existing variable with that
77 * "key". The "append" variant puts our new value at the end of a list,
78 * separated by PATH_SEP (which is what separate values in
79 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
81 static void env_append(struct argv_array
*env
, const char *key
, const char *val
)
83 struct strbuf quoted
= STRBUF_INIT
;
87 * Avoid quoting if it's not necessary, for maximum compatibility
88 * with older parsers which don't understand the quoting.
90 if (*val
== '"' || strchr(val
, PATH_SEP
)) {
91 strbuf_addch("ed
, '"');
92 quote_c_style(val
, "ed
, NULL
, 1);
93 strbuf_addch("ed
, '"');
99 argv_array_pushf(env
, "%s=%s", key
, val
);
101 argv_array_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
103 strbuf_release("ed
);
106 static void env_replace(struct argv_array
*env
, const char *key
, const char *val
)
108 argv_array_pushf(env
, "%s=%s", key
, val
);
111 static int setup_tmp_objdir(const char *root
)
116 path
= xstrfmt("%s/pack", root
);
117 ret
= mkdir(path
, 0777);
123 struct tmp_objdir
*tmp_objdir_create(void)
125 static int installed_handlers
;
126 struct tmp_objdir
*t
;
129 die("BUG: only one tmp_objdir can be used at a time");
131 t
= xmalloc(sizeof(*t
));
132 strbuf_init(&t
->path
, 0);
133 argv_array_init(&t
->env
);
135 strbuf_addf(&t
->path
, "%s/incoming-XXXXXX", get_object_directory());
138 * Grow the strbuf beyond any filename we expect to be placed in it.
139 * If tmp_objdir_destroy() is called by a signal handler, then
140 * we should be able to use the strbuf to remove files without
141 * having to call malloc.
143 strbuf_grow(&t
->path
, 1024);
145 if (!mkdtemp(t
->path
.buf
)) {
146 /* free, not destroy, as we never touched the filesystem */
152 if (!installed_handlers
) {
153 atexit(remove_tmp_objdir
);
154 sigchain_push_common(remove_tmp_objdir_on_signal
);
155 installed_handlers
++;
158 if (setup_tmp_objdir(t
->path
.buf
)) {
159 tmp_objdir_destroy(t
);
163 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
164 absolute_path(get_object_directory()));
165 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
166 env_replace(&t
->env
, GIT_QUARANTINE_ENVIRONMENT
,
167 absolute_path(t
->path
.buf
));
173 * Make sure we copy packfiles and their associated metafiles in the correct
174 * order. All of these ends_with checks are slightly expensive to do in
175 * the midst of a sorting routine, but in practice it shouldn't matter.
176 * We will have a relatively small number of packfiles to order, and loose
177 * objects exit early in the first line.
179 static int pack_copy_priority(const char *name
)
181 if (!starts_with(name
, "pack"))
183 if (ends_with(name
, ".keep"))
185 if (ends_with(name
, ".pack"))
187 if (ends_with(name
, ".idx"))
192 static int pack_copy_cmp(const char *a
, const char *b
)
194 return pack_copy_priority(a
) - pack_copy_priority(b
);
197 static int read_dir_paths(struct string_list
*out
, const char *path
)
206 while ((de
= readdir(dh
)))
207 if (de
->d_name
[0] != '.')
208 string_list_append(out
, de
->d_name
);
214 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
);
216 static int migrate_one(struct strbuf
*src
, struct strbuf
*dst
)
220 if (stat(src
->buf
, &st
) < 0)
222 if (S_ISDIR(st
.st_mode
)) {
223 if (!mkdir(dst
->buf
, 0777)) {
224 if (adjust_shared_perm(dst
->buf
))
226 } else if (errno
!= EEXIST
)
228 return migrate_paths(src
, dst
);
230 return finalize_object_file(src
->buf
, dst
->buf
);
233 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
)
235 size_t src_len
= src
->len
, dst_len
= dst
->len
;
236 struct string_list paths
= STRING_LIST_INIT_DUP
;
240 if (read_dir_paths(&paths
, src
->buf
) < 0)
242 paths
.cmp
= pack_copy_cmp
;
243 string_list_sort(&paths
);
245 for (i
= 0; i
< paths
.nr
; i
++) {
246 const char *name
= paths
.items
[i
].string
;
248 strbuf_addf(src
, "/%s", name
);
249 strbuf_addf(dst
, "/%s", name
);
251 ret
|= migrate_one(src
, dst
);
253 strbuf_setlen(src
, src_len
);
254 strbuf_setlen(dst
, dst_len
);
257 string_list_clear(&paths
, 0);
261 int tmp_objdir_migrate(struct tmp_objdir
*t
)
263 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
269 strbuf_addbuf(&src
, &t
->path
);
270 strbuf_addstr(&dst
, get_object_directory());
272 ret
= migrate_paths(&src
, &dst
);
274 strbuf_release(&src
);
275 strbuf_release(&dst
);
277 tmp_objdir_destroy(t
);
281 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
288 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
290 add_to_alternates_memory(t
->path
.buf
);