2 #include "tmp-objdir.h"
5 #include "string-list.h"
7 #include "argv-array.h"
11 struct argv_array env
;
15 * Allow only one tmp_objdir at a time in a running process, which simplifies
16 * our signal/atexit cleanup routines. It's doubtful callers will ever need
17 * more than one, and we can expand later if so. You can have many such
18 * tmp_objdirs simultaneously in many processes, of course.
20 static struct tmp_objdir
*the_tmp_objdir
;
22 static void tmp_objdir_free(struct tmp_objdir
*t
)
24 strbuf_release(&t
->path
);
25 argv_array_clear(&t
->env
);
29 static int tmp_objdir_destroy_1(struct tmp_objdir
*t
, int on_signal
)
36 if (t
== the_tmp_objdir
)
37 the_tmp_objdir
= NULL
;
40 * This may use malloc via strbuf_grow(), but we should
41 * have pre-grown t->path sufficiently so that this
42 * doesn't happen in practice.
44 err
= remove_dir_recursively(&t
->path
, 0);
47 * When we are cleaning up due to a signal, we won't bother
48 * freeing memory; it may cause a deadlock if the signal
49 * arrived while libc's allocator lock is held.
56 int tmp_objdir_destroy(struct tmp_objdir
*t
)
58 return tmp_objdir_destroy_1(t
, 0);
61 static void remove_tmp_objdir(void)
63 tmp_objdir_destroy(the_tmp_objdir
);
66 static void remove_tmp_objdir_on_signal(int signo
)
68 tmp_objdir_destroy_1(the_tmp_objdir
, 1);
74 * These env_* functions are for setting up the child environment; the
75 * "replace" variant overrides the value of any existing variable with that
76 * "key". The "append" variant puts our new value at the end of a list,
77 * separated by PATH_SEP (which is what separate values in
78 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
80 static void env_append(struct argv_array
*env
, const char *key
, const char *val
)
82 const char *old
= getenv(key
);
85 argv_array_pushf(env
, "%s=%s", key
, val
);
87 argv_array_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
90 static void env_replace(struct argv_array
*env
, const char *key
, const char *val
)
92 argv_array_pushf(env
, "%s=%s", key
, val
);
95 static int setup_tmp_objdir(const char *root
)
100 path
= xstrfmt("%s/pack", root
);
101 ret
= mkdir(path
, 0777);
107 struct tmp_objdir
*tmp_objdir_create(void)
109 static int installed_handlers
;
110 struct tmp_objdir
*t
;
113 die("BUG: only one tmp_objdir can be used at a time");
115 t
= xmalloc(sizeof(*t
));
116 strbuf_init(&t
->path
, 0);
117 argv_array_init(&t
->env
);
119 strbuf_addf(&t
->path
, "%s/incoming-XXXXXX", get_object_directory());
122 * Grow the strbuf beyond any filename we expect to be placed in it.
123 * If tmp_objdir_destroy() is called by a signal handler, then
124 * we should be able to use the strbuf to remove files without
125 * having to call malloc.
127 strbuf_grow(&t
->path
, 1024);
129 if (!mkdtemp(t
->path
.buf
)) {
130 /* free, not destroy, as we never touched the filesystem */
136 if (!installed_handlers
) {
137 atexit(remove_tmp_objdir
);
138 sigchain_push_common(remove_tmp_objdir_on_signal
);
139 installed_handlers
++;
142 if (setup_tmp_objdir(t
->path
.buf
)) {
143 tmp_objdir_destroy(t
);
147 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
148 absolute_path(get_object_directory()));
149 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
155 * Make sure we copy packfiles and their associated metafiles in the correct
156 * order. All of these ends_with checks are slightly expensive to do in
157 * the midst of a sorting routine, but in practice it shouldn't matter.
158 * We will have a relatively small number of packfiles to order, and loose
159 * objects exit early in the first line.
161 static int pack_copy_priority(const char *name
)
163 if (!starts_with(name
, "pack"))
165 if (ends_with(name
, ".keep"))
167 if (ends_with(name
, ".pack"))
169 if (ends_with(name
, ".idx"))
174 static int pack_copy_cmp(const char *a
, const char *b
)
176 return pack_copy_priority(a
) - pack_copy_priority(b
);
179 static int read_dir_paths(struct string_list
*out
, const char *path
)
188 while ((de
= readdir(dh
)))
189 if (!is_dot_or_dotdot(de
->d_name
))
190 string_list_append(out
, de
->d_name
);
196 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
);
198 static int migrate_one(struct strbuf
*src
, struct strbuf
*dst
)
202 if (stat(src
->buf
, &st
) < 0)
204 if (S_ISDIR(st
.st_mode
)) {
205 if (!mkdir(dst
->buf
, 0777)) {
206 if (adjust_shared_perm(dst
->buf
))
208 } else if (errno
!= EEXIST
)
210 return migrate_paths(src
, dst
);
212 return finalize_object_file(src
->buf
, dst
->buf
);
215 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
)
217 size_t src_len
= src
->len
, dst_len
= dst
->len
;
218 struct string_list paths
= STRING_LIST_INIT_DUP
;
222 if (read_dir_paths(&paths
, src
->buf
) < 0)
224 paths
.cmp
= pack_copy_cmp
;
225 string_list_sort(&paths
);
227 for (i
= 0; i
< paths
.nr
; i
++) {
228 const char *name
= paths
.items
[i
].string
;
230 strbuf_addf(src
, "/%s", name
);
231 strbuf_addf(dst
, "/%s", name
);
233 ret
|= migrate_one(src
, dst
);
235 strbuf_setlen(src
, src_len
);
236 strbuf_setlen(dst
, dst_len
);
239 string_list_clear(&paths
, 0);
243 int tmp_objdir_migrate(struct tmp_objdir
*t
)
245 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
251 strbuf_addbuf(&src
, &t
->path
);
252 strbuf_addstr(&dst
, get_object_directory());
254 ret
= migrate_paths(&src
, &dst
);
256 strbuf_release(&src
);
257 strbuf_release(&dst
);
259 tmp_objdir_destroy(t
);
263 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
270 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
272 add_to_alternates_memory(t
->path
.buf
);