2 #include "tmp-objdir.h"
3 #include "chdir-notify.h"
6 #include "string-list.h"
10 #include "object-store.h"
15 struct object_directory
*prev_odb
;
20 * Allow only one tmp_objdir at a time in a running process, which simplifies
21 * our atexit cleanup routines. It's doubtful callers will ever need
22 * more than one, and we can expand later if so. You can have many such
23 * tmp_objdirs simultaneously in many processes, of course.
25 static struct tmp_objdir
*the_tmp_objdir
;
27 static void tmp_objdir_free(struct tmp_objdir
*t
)
29 strbuf_release(&t
->path
);
30 strvec_clear(&t
->env
);
34 int tmp_objdir_destroy(struct tmp_objdir
*t
)
41 if (t
== the_tmp_objdir
)
42 the_tmp_objdir
= NULL
;
45 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
47 err
= remove_dir_recursively(&t
->path
, 0);
54 static void remove_tmp_objdir(void)
56 tmp_objdir_destroy(the_tmp_objdir
);
59 void tmp_objdir_discard_objects(struct tmp_objdir
*t
)
61 remove_dir_recursively(&t
->path
, REMOVE_DIR_KEEP_TOPLEVEL
);
65 * These env_* functions are for setting up the child environment; the
66 * "replace" variant overrides the value of any existing variable with that
67 * "key". The "append" variant puts our new value at the end of a list,
68 * separated by PATH_SEP (which is what separate values in
69 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
71 static void env_append(struct strvec
*env
, const char *key
, const char *val
)
73 struct strbuf quoted
= STRBUF_INIT
;
77 * Avoid quoting if it's not necessary, for maximum compatibility
78 * with older parsers which don't understand the quoting.
80 if (*val
== '"' || strchr(val
, PATH_SEP
)) {
81 strbuf_addch("ed
, '"');
82 quote_c_style(val
, "ed
, NULL
, 1);
83 strbuf_addch("ed
, '"');
89 strvec_pushf(env
, "%s=%s", key
, val
);
91 strvec_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
93 strbuf_release("ed
);
96 static void env_replace(struct strvec
*env
, const char *key
, const char *val
)
98 strvec_pushf(env
, "%s=%s", key
, val
);
101 static int setup_tmp_objdir(const char *root
)
106 path
= xstrfmt("%s/pack", root
);
107 ret
= mkdir(path
, 0777);
113 struct tmp_objdir
*tmp_objdir_create(const char *prefix
)
115 static int installed_handlers
;
116 struct tmp_objdir
*t
;
119 BUG("only one tmp_objdir can be used at a time");
121 t
= xcalloc(1, sizeof(*t
));
122 strbuf_init(&t
->path
, 0);
123 strvec_init(&t
->env
);
126 * Use a string starting with tmp_ so that the builtin/prune.c code
127 * can recognize any stale objdirs left behind by a crash and delete
130 strbuf_addf(&t
->path
, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix
);
132 if (!mkdtemp(t
->path
.buf
)) {
133 /* free, not destroy, as we never touched the filesystem */
139 if (!installed_handlers
) {
140 atexit(remove_tmp_objdir
);
141 installed_handlers
++;
144 if (setup_tmp_objdir(t
->path
.buf
)) {
145 tmp_objdir_destroy(t
);
149 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
150 absolute_path(get_object_directory()));
151 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
152 env_replace(&t
->env
, GIT_QUARANTINE_ENVIRONMENT
,
153 absolute_path(t
->path
.buf
));
159 * Make sure we copy packfiles and their associated metafiles in the correct
160 * order. All of these ends_with checks are slightly expensive to do in
161 * the midst of a sorting routine, but in practice it shouldn't matter.
162 * We will have a relatively small number of packfiles to order, and loose
163 * objects exit early in the first line.
165 static int pack_copy_priority(const char *name
)
167 if (!starts_with(name
, "pack"))
169 if (ends_with(name
, ".keep"))
171 if (ends_with(name
, ".pack"))
173 if (ends_with(name
, ".rev"))
175 if (ends_with(name
, ".idx"))
180 static int pack_copy_cmp(const char *a
, const char *b
)
182 return pack_copy_priority(a
) - pack_copy_priority(b
);
185 static int read_dir_paths(struct string_list
*out
, const char *path
)
194 while ((de
= readdir(dh
)))
195 if (de
->d_name
[0] != '.')
196 string_list_append(out
, de
->d_name
);
202 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
);
204 static int migrate_one(struct strbuf
*src
, struct strbuf
*dst
)
208 if (stat(src
->buf
, &st
) < 0)
210 if (S_ISDIR(st
.st_mode
)) {
211 if (!mkdir(dst
->buf
, 0777)) {
212 if (adjust_shared_perm(dst
->buf
))
214 } else if (errno
!= EEXIST
)
216 return migrate_paths(src
, dst
);
218 return finalize_object_file(src
->buf
, dst
->buf
);
221 static int migrate_paths(struct strbuf
*src
, struct strbuf
*dst
)
223 size_t src_len
= src
->len
, dst_len
= dst
->len
;
224 struct string_list paths
= STRING_LIST_INIT_DUP
;
228 if (read_dir_paths(&paths
, src
->buf
) < 0)
230 paths
.cmp
= pack_copy_cmp
;
231 string_list_sort(&paths
);
233 for (i
= 0; i
< paths
.nr
; i
++) {
234 const char *name
= paths
.items
[i
].string
;
236 strbuf_addf(src
, "/%s", name
);
237 strbuf_addf(dst
, "/%s", name
);
239 ret
|= migrate_one(src
, dst
);
241 strbuf_setlen(src
, src_len
);
242 strbuf_setlen(dst
, dst_len
);
245 string_list_clear(&paths
, 0);
249 int tmp_objdir_migrate(struct tmp_objdir
*t
)
251 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
258 if (the_repository
->objects
->odb
->will_destroy
)
259 BUG("migrating an ODB that was marked for destruction");
260 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
264 strbuf_addbuf(&src
, &t
->path
);
265 strbuf_addstr(&dst
, get_object_directory());
267 ret
= migrate_paths(&src
, &dst
);
269 strbuf_release(&src
);
270 strbuf_release(&dst
);
272 tmp_objdir_destroy(t
);
276 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
283 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
285 add_to_alternates_memory(t
->path
.buf
);
288 void tmp_objdir_replace_primary_odb(struct tmp_objdir
*t
, int will_destroy
)
291 BUG("the primary object database is already replaced");
292 t
->prev_odb
= set_temporary_primary_odb(t
->path
.buf
, will_destroy
);
293 t
->will_destroy
= will_destroy
;
296 struct tmp_objdir
*tmp_objdir_unapply_primary_odb(void)
298 if (!the_tmp_objdir
|| !the_tmp_objdir
->prev_odb
)
301 restore_primary_odb(the_tmp_objdir
->prev_odb
, the_tmp_objdir
->path
.buf
);
302 the_tmp_objdir
->prev_odb
= NULL
;
303 return the_tmp_objdir
;
306 void tmp_objdir_reapply_primary_odb(struct tmp_objdir
*t
, const char *old_cwd
,
311 path
= reparent_relative_path(old_cwd
, new_cwd
, t
->path
.buf
);
312 strbuf_reset(&t
->path
);
313 strbuf_addstr(&t
->path
, path
);
315 tmp_objdir_replace_primary_odb(t
, t
->will_destroy
);