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.h"
14 #include "repository.h"
17 struct repository
*repo
;
20 struct object_directory
*prev_odb
;
25 * Allow only one tmp_objdir at a time in a running process, which simplifies
26 * our atexit cleanup routines. It's doubtful callers will ever need
27 * more than one, and we can expand later if so. You can have many such
28 * tmp_objdirs simultaneously in many processes, of course.
30 static struct tmp_objdir
*the_tmp_objdir
;
32 static void tmp_objdir_free(struct tmp_objdir
*t
)
34 strbuf_release(&t
->path
);
35 strvec_clear(&t
->env
);
39 int tmp_objdir_destroy(struct tmp_objdir
*t
)
46 if (t
== the_tmp_objdir
)
47 the_tmp_objdir
= NULL
;
50 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
52 err
= remove_dir_recursively(&t
->path
, 0);
59 static void remove_tmp_objdir(void)
61 tmp_objdir_destroy(the_tmp_objdir
);
64 void tmp_objdir_discard_objects(struct tmp_objdir
*t
)
66 remove_dir_recursively(&t
->path
, REMOVE_DIR_KEEP_TOPLEVEL
);
70 * These env_* functions are for setting up the child environment; the
71 * "replace" variant overrides the value of any existing variable with that
72 * "key". The "append" variant puts our new value at the end of a list,
73 * separated by PATH_SEP (which is what separate values in
74 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
76 static void env_append(struct strvec
*env
, const char *key
, const char *val
)
78 struct strbuf quoted
= STRBUF_INIT
;
82 * Avoid quoting if it's not necessary, for maximum compatibility
83 * with older parsers which don't understand the quoting.
85 if (*val
== '"' || strchr(val
, PATH_SEP
)) {
86 strbuf_addch("ed
, '"');
87 quote_c_style(val
, "ed
, NULL
, 1);
88 strbuf_addch("ed
, '"');
94 strvec_pushf(env
, "%s=%s", key
, val
);
96 strvec_pushf(env
, "%s=%s%c%s", key
, old
, PATH_SEP
, val
);
98 strbuf_release("ed
);
101 static void env_replace(struct strvec
*env
, const char *key
, const char *val
)
103 strvec_pushf(env
, "%s=%s", key
, val
);
106 static int setup_tmp_objdir(const char *root
)
111 path
= xstrfmt("%s/pack", root
);
112 ret
= mkdir(path
, 0777);
118 struct tmp_objdir
*tmp_objdir_create(struct repository
*r
,
121 static int installed_handlers
;
122 struct tmp_objdir
*t
;
125 BUG("only one tmp_objdir can be used at a time");
127 t
= xcalloc(1, sizeof(*t
));
129 strbuf_init(&t
->path
, 0);
130 strvec_init(&t
->env
);
133 * Use a string starting with tmp_ so that the builtin/prune.c code
134 * can recognize any stale objdirs left behind by a crash and delete
137 strbuf_addf(&t
->path
, "%s/tmp_objdir-%s-XXXXXX",
138 repo_get_object_directory(r
), prefix
);
140 if (!mkdtemp(t
->path
.buf
)) {
141 /* free, not destroy, as we never touched the filesystem */
147 if (!installed_handlers
) {
148 atexit(remove_tmp_objdir
);
149 installed_handlers
++;
152 if (setup_tmp_objdir(t
->path
.buf
)) {
153 tmp_objdir_destroy(t
);
157 env_append(&t
->env
, ALTERNATE_DB_ENVIRONMENT
,
158 absolute_path(repo_get_object_directory(r
)));
159 env_replace(&t
->env
, DB_ENVIRONMENT
, absolute_path(t
->path
.buf
));
160 env_replace(&t
->env
, GIT_QUARANTINE_ENVIRONMENT
,
161 absolute_path(t
->path
.buf
));
167 * Make sure we copy packfiles and their associated metafiles in the correct
168 * order. All of these ends_with checks are slightly expensive to do in
169 * the midst of a sorting routine, but in practice it shouldn't matter.
170 * We will have a relatively small number of packfiles to order, and loose
171 * objects exit early in the first line.
173 static int pack_copy_priority(const char *name
)
175 if (!starts_with(name
, "pack"))
177 if (ends_with(name
, ".keep"))
179 if (ends_with(name
, ".pack"))
181 if (ends_with(name
, ".rev"))
183 if (ends_with(name
, ".idx"))
188 static int pack_copy_cmp(const char *a
, const char *b
)
190 return pack_copy_priority(a
) - pack_copy_priority(b
);
193 static int read_dir_paths(struct string_list
*out
, const char *path
)
202 while ((de
= readdir(dh
)))
203 if (de
->d_name
[0] != '.')
204 string_list_append(out
, de
->d_name
);
210 static int migrate_paths(struct tmp_objdir
*t
,
211 struct strbuf
*src
, struct strbuf
*dst
,
212 enum finalize_object_file_flags flags
);
214 static int migrate_one(struct tmp_objdir
*t
,
215 struct strbuf
*src
, struct strbuf
*dst
,
216 enum finalize_object_file_flags flags
)
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(t
->repo
, dst
->buf
))
226 } else if (errno
!= EEXIST
)
228 return migrate_paths(t
, src
, dst
, flags
);
230 return finalize_object_file_flags(src
->buf
, dst
->buf
, flags
);
233 static int is_loose_object_shard(const char *name
)
235 return strlen(name
) == 2 && isxdigit(name
[0]) && isxdigit(name
[1]);
238 static int migrate_paths(struct tmp_objdir
*t
,
239 struct strbuf
*src
, struct strbuf
*dst
,
240 enum finalize_object_file_flags flags
)
242 size_t src_len
= src
->len
, dst_len
= dst
->len
;
243 struct string_list paths
= STRING_LIST_INIT_DUP
;
246 if (read_dir_paths(&paths
, src
->buf
) < 0)
248 paths
.cmp
= pack_copy_cmp
;
249 string_list_sort(&paths
);
251 for (size_t i
= 0; i
< paths
.nr
; i
++) {
252 const char *name
= paths
.items
[i
].string
;
253 enum finalize_object_file_flags flags_copy
= flags
;
255 strbuf_addf(src
, "/%s", name
);
256 strbuf_addf(dst
, "/%s", name
);
258 if (is_loose_object_shard(name
))
259 flags_copy
|= FOF_SKIP_COLLISION_CHECK
;
261 ret
|= migrate_one(t
, src
, dst
, flags_copy
);
263 strbuf_setlen(src
, src_len
);
264 strbuf_setlen(dst
, dst_len
);
267 string_list_clear(&paths
, 0);
271 int tmp_objdir_migrate(struct tmp_objdir
*t
)
273 struct strbuf src
= STRBUF_INIT
, dst
= STRBUF_INIT
;
280 if (t
->repo
->objects
->odb
->will_destroy
)
281 BUG("migrating an ODB that was marked for destruction");
282 restore_primary_odb(t
->prev_odb
, t
->path
.buf
);
286 strbuf_addbuf(&src
, &t
->path
);
287 strbuf_addstr(&dst
, repo_get_object_directory(t
->repo
));
289 ret
= migrate_paths(t
, &src
, &dst
, 0);
291 strbuf_release(&src
);
292 strbuf_release(&dst
);
294 tmp_objdir_destroy(t
);
298 const char **tmp_objdir_env(const struct tmp_objdir
*t
)
305 void tmp_objdir_add_as_alternate(const struct tmp_objdir
*t
)
307 add_to_alternates_memory(t
->path
.buf
);
310 void tmp_objdir_replace_primary_odb(struct tmp_objdir
*t
, int will_destroy
)
313 BUG("the primary object database is already replaced");
314 t
->prev_odb
= set_temporary_primary_odb(t
->path
.buf
, will_destroy
);
315 t
->will_destroy
= will_destroy
;
318 struct tmp_objdir
*tmp_objdir_unapply_primary_odb(void)
320 if (!the_tmp_objdir
|| !the_tmp_objdir
->prev_odb
)
323 restore_primary_odb(the_tmp_objdir
->prev_odb
, the_tmp_objdir
->path
.buf
);
324 the_tmp_objdir
->prev_odb
= NULL
;
325 return the_tmp_objdir
;
328 void tmp_objdir_reapply_primary_odb(struct tmp_objdir
*t
, const char *old_cwd
,
333 path
= reparent_relative_path(old_cwd
, new_cwd
, t
->path
.buf
);
334 strbuf_reset(&t
->path
);
335 strbuf_addstr(&t
->path
, path
);
337 tmp_objdir_replace_primary_odb(t
, t
->will_destroy
);