2 #include "tmp-objdir.h"
4 #include "chdir-notify.h"
6 #include "environment.h"
8 #include "string-list.h"
12 #include "object-store.h"
17 struct object_directory
*prev_odb
;
22 * Allow only one tmp_objdir at a time in a running process, which simplifies
23 * our atexit cleanup routines. It's doubtful callers will ever need
24 * more than one, and we can expand later if so. You can have many such
25 * tmp_objdirs simultaneously in many processes, of course.
27 static struct tmp_objdir
*the_tmp_objdir
;
29 static void tmp_objdir_free(struct tmp_objdir
*t
)
31 strbuf_release(&t
->path
);
32 strvec_clear(&t
->env
);
36 int tmp_objdir_destroy(struct tmp_objdir
*t
)
43 if (t
== the_tmp_objdir
)
44 the_tmp_objdir
= NULL
;
47 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
49 err
= remove_dir_recursively(&t
->path
, 0);
56 static void remove_tmp_objdir(void)
58 tmp_objdir_destroy(the_tmp_objdir
);
61 void tmp_objdir_discard_objects(struct tmp_objdir
*t
)
63 remove_dir_recursively(&t
->path
, REMOVE_DIR_KEEP_TOPLEVEL
);
67 * These env_* functions are for setting up the child environment; the
68 * "replace" variant overrides the value of any existing variable with that
69 * "key". The "append" variant puts our new value at the end of a list,
70 * separated by PATH_SEP (which is what separate values in
71 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
73 static void env_append(struct strvec
*env
, const char *key
, const char *val
)
75 struct strbuf quoted
= STRBUF_INIT
;
79 * Avoid quoting if it's not necessary, for maximum compatibility
80 * with older parsers which don't understand the quoting.
82 if (*val
== '"' || strchr(val
, PATH_SEP
)) {
83 strbuf_addch("ed
, '"');
84 quote_c_style(val
, "ed
, NULL
, 1);
85 strbuf_addch("ed
, '"');
91 strvec_pushf(env
, "%s=%s", key
, val
);
93 strvec_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
95 strbuf_release("ed
);
98 static void env_replace(struct strvec
*env
, const char *key
, const char *val
)
100 strvec_pushf(env
, "%s=%s", key
, val
);
103 static int setup_tmp_objdir(const char *root
)
108 path
= xstrfmt("%s/pack", root
);
109 ret
= mkdir(path
, 0777);
115 struct tmp_objdir
*tmp_objdir_create(const char *prefix
)
117 static int installed_handlers
;
118 struct tmp_objdir
*t
;
121 BUG("only one tmp_objdir can be used at a time");
123 t
= xcalloc(1, sizeof(*t
));
124 strbuf_init(&t
->path
, 0);
125 strvec_init(&t
->env
);
128 * Use a string starting with tmp_ so that the builtin/prune.c code
129 * can recognize any stale objdirs left behind by a crash and delete
132 strbuf_addf(&t
->path
, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix
);
134 if (!mkdtemp(t
->path
.buf
)) {
135 /* free, not destroy, as we never touched the filesystem */
141 if (!installed_handlers
) {
142 atexit(remove_tmp_objdir
);
143 installed_handlers
++;
146 if (setup_tmp_objdir(t
->path
.buf
)) {
147 tmp_objdir_destroy(t
);
151 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
152 absolute_path(get_object_directory()));
153 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
154 env_replace(&t
->env
, GIT_QUARANTINE_ENVIRONMENT
,
155 absolute_path(t
->path
.buf
));
161 * Make sure we copy packfiles and their associated metafiles in the correct
162 * order. All of these ends_with checks are slightly expensive to do in
163 * the midst of a sorting routine, but in practice it shouldn't matter.
164 * We will have a relatively small number of packfiles to order, and loose
165 * objects exit early in the first line.
167 static int pack_copy_priority(const char *name
)
169 if (!starts_with(name
, "pack"))
171 if (ends_with(name
, ".keep"))
173 if (ends_with(name
, ".pack"))
175 if (ends_with(name
, ".rev"))
177 if (ends_with(name
, ".idx"))
182 static int pack_copy_cmp(const char *a
, const char *b
)
184 return pack_copy_priority(a
) - pack_copy_priority(b
);
187 static int read_dir_paths(struct string_list
*out
, const char *path
)
196 while ((de
= readdir(dh
)))
197 if (de
->d_name
[0] != '.')
198 string_list_append(out
, de
->d_name
);
204 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
);
206 static int migrate_one(struct strbuf
*src
, struct strbuf
*dst
)
210 if (stat(src
->buf
, &st
) < 0)
212 if (S_ISDIR(st
.st_mode
)) {
213 if (!mkdir(dst
->buf
, 0777)) {
214 if (adjust_shared_perm(dst
->buf
))
216 } else if (errno
!= EEXIST
)
218 return migrate_paths(src
, dst
);
220 return finalize_object_file(src
->buf
, dst
->buf
);
223 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
)
225 size_t src_len
= src
->len
, dst_len
= dst
->len
;
226 struct string_list paths
= STRING_LIST_INIT_DUP
;
230 if (read_dir_paths(&paths
, src
->buf
) < 0)
232 paths
.cmp
= pack_copy_cmp
;
233 string_list_sort(&paths
);
235 for (i
= 0; i
< paths
.nr
; i
++) {
236 const char *name
= paths
.items
[i
].string
;
238 strbuf_addf(src
, "/%s", name
);
239 strbuf_addf(dst
, "/%s", name
);
241 ret
|= migrate_one(src
, dst
);
243 strbuf_setlen(src
, src_len
);
244 strbuf_setlen(dst
, dst_len
);
247 string_list_clear(&paths
, 0);
251 int tmp_objdir_migrate(struct tmp_objdir
*t
)
253 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
260 if (the_repository
->objects
->odb
->will_destroy
)
261 BUG("migrating an ODB that was marked for destruction");
262 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
266 strbuf_addbuf(&src
, &t
->path
);
267 strbuf_addstr(&dst
, get_object_directory());
269 ret
= migrate_paths(&src
, &dst
);
271 strbuf_release(&src
);
272 strbuf_release(&dst
);
274 tmp_objdir_destroy(t
);
278 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
285 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
287 add_to_alternates_memory(t
->path
.buf
);
290 void tmp_objdir_replace_primary_odb(struct tmp_objdir
*t
, int will_destroy
)
293 BUG("the primary object database is already replaced");
294 t
->prev_odb
= set_temporary_primary_odb(t
->path
.buf
, will_destroy
);
295 t
->will_destroy
= will_destroy
;
298 struct tmp_objdir
*tmp_objdir_unapply_primary_odb(void)
300 if (!the_tmp_objdir
|| !the_tmp_objdir
->prev_odb
)
303 restore_primary_odb(the_tmp_objdir
->prev_odb
, the_tmp_objdir
->path
.buf
);
304 the_tmp_objdir
->prev_odb
= NULL
;
305 return the_tmp_objdir
;
308 void tmp_objdir_reapply_primary_odb(struct tmp_objdir
*t
, const char *old_cwd
,
313 path
= reparent_relative_path(old_cwd
, new_cwd
, t
->path
.buf
);
314 strbuf_reset(&t
->path
);
315 strbuf_addstr(&t
->path
, path
);
317 tmp_objdir_replace_primary_odb(t
, t
->will_destroy
);