Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 1 | #ifndef TMP_OBJDIR_H |
| 2 | #define TMP_OBJDIR_H |
| 3 | |
| 4 | /* |
| 5 | * This API allows you to create a temporary object directory, advertise it to |
| 6 | * sub-processes via GIT_OBJECT_DIRECTORY and GIT_ALTERNATE_OBJECT_DIRECTORIES, |
| 7 | * and then either migrate its object into the main object directory, or remove |
| 8 | * it. The library handles unexpected signal/exit death by cleaning up the |
| 9 | * temporary directory. |
| 10 | * |
| 11 | * Example: |
| 12 | * |
René Scharfe | eb5b6b5 | 2022-10-30 12:51:55 +0100 | [diff] [blame] | 13 | * struct child_process child = CHILD_PROCESS_INIT; |
Neeraj Singh | b3cecf4 | 2021-12-06 22:05:04 +0000 | [diff] [blame] | 14 | * struct tmp_objdir *t = tmp_objdir_create("incoming"); |
René Scharfe | eb5b6b5 | 2022-10-30 12:51:55 +0100 | [diff] [blame] | 15 | * strvec_push(&child.args, cmd); |
| 16 | * strvec_pushv(&child.env, tmp_objdir_env(t)); |
| 17 | * if (!run_command(&child)) && !tmp_objdir_migrate(t)) |
Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 18 | * printf("success!\n"); |
| 19 | * else |
| 20 | * die("failed...tmp_objdir will clean up for us"); |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | struct tmp_objdir; |
| 25 | |
| 26 | /* |
Neeraj Singh | b3cecf4 | 2021-12-06 22:05:04 +0000 | [diff] [blame] | 27 | * Create a new temporary object directory with the specified prefix; |
| 28 | * returns NULL on failure. |
Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 29 | */ |
Neeraj Singh | b3cecf4 | 2021-12-06 22:05:04 +0000 | [diff] [blame] | 30 | struct tmp_objdir *tmp_objdir_create(const char *prefix); |
Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Return a list of environment strings, suitable for use with |
| 34 | * child_process.env, that can be passed to child programs to make use of the |
| 35 | * temporary object directory. |
| 36 | */ |
| 37 | const char **tmp_objdir_env(const struct tmp_objdir *); |
| 38 | |
| 39 | /* |
| 40 | * Finalize a temporary object directory by migrating its objects into the main |
| 41 | * object database, removing the temporary directory, and freeing any |
| 42 | * associated resources. |
| 43 | */ |
| 44 | int tmp_objdir_migrate(struct tmp_objdir *); |
| 45 | |
| 46 | /* |
| 47 | * Destroy a temporary object directory, discarding any objects it contains. |
| 48 | */ |
| 49 | int tmp_objdir_destroy(struct tmp_objdir *); |
| 50 | |
| 51 | /* |
Elijah Newren | 7b90ab4 | 2022-02-02 02:37:29 +0000 | [diff] [blame] | 52 | * Remove all objects from the temporary object directory, while leaving it |
| 53 | * around so more objects can be added. |
| 54 | */ |
| 55 | void tmp_objdir_discard_objects(struct tmp_objdir *); |
| 56 | |
| 57 | /* |
Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 58 | * Add the temporary object directory as an alternate object store in the |
| 59 | * current process. |
| 60 | */ |
| 61 | void tmp_objdir_add_as_alternate(const struct tmp_objdir *); |
| 62 | |
Neeraj Singh | b3cecf4 | 2021-12-06 22:05:04 +0000 | [diff] [blame] | 63 | /* |
| 64 | * Replaces the writable object store in the current process with the temporary |
| 65 | * object directory and makes the former main object store an alternate. |
| 66 | * If will_destroy is nonzero, the object directory may not be migrated. |
| 67 | */ |
| 68 | void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy); |
| 69 | |
| 70 | /* |
| 71 | * If the primary object database was replaced by a temporary object directory, |
| 72 | * restore it to its original value while keeping the directory contents around. |
| 73 | * Returns NULL if the primary object database was not replaced. |
| 74 | */ |
| 75 | struct tmp_objdir *tmp_objdir_unapply_primary_odb(void); |
| 76 | |
| 77 | /* |
| 78 | * Reapplies the former primary temporary object database, after potentially |
| 79 | * changing its relative path. |
| 80 | */ |
| 81 | void tmp_objdir_reapply_primary_odb(struct tmp_objdir *, const char *old_cwd, |
| 82 | const char *new_cwd); |
| 83 | |
| 84 | |
Jeff King | 2564d99 | 2016-10-03 16:49:11 -0400 | [diff] [blame] | 85 | #endif /* TMP_OBJDIR_H */ |