tempfile.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #ifndef TEMPFILE_H
  2. #define TEMPFILE_H
  3. #include "list.h"
  4. #include "strbuf.h"
  5. /*
  6. * Handle temporary files.
  7. *
  8. * The tempfile API allows temporary files to be created, deleted, and
  9. * atomically renamed. Temporary files that are still active when the
  10. * program ends are cleaned up automatically. Lockfiles (see
  11. * "lockfile.h") are built on top of this API.
  12. *
  13. *
  14. * Calling sequence
  15. * ----------------
  16. *
  17. * The caller:
  18. *
  19. * * Attempts to create a temporary file by calling
  20. * `create_tempfile()`. The resources used for the temporary file are
  21. * managed by the tempfile API.
  22. *
  23. * * Writes new content to the file by either:
  24. *
  25. * * writing to the `tempfile->fd` file descriptor
  26. *
  27. * * calling `fdopen_tempfile()` to get a `FILE` pointer for the
  28. * open file and writing to the file using stdio.
  29. *
  30. * Note that the file descriptor created by create_tempfile()
  31. * is marked O_CLOEXEC, so the new contents must be written by
  32. * the current process, not any spawned one.
  33. *
  34. * When finished writing, the caller can:
  35. *
  36. * * Close the file descriptor and remove the temporary file by
  37. * calling `delete_tempfile()`.
  38. *
  39. * * Close the temporary file and rename it atomically to a specified
  40. * filename by calling `rename_tempfile()`. This relinquishes
  41. * control of the file.
  42. *
  43. * * Close the file descriptor without removing or renaming the
  44. * temporary file by calling `close_tempfile_gently()`, and later call
  45. * `delete_tempfile()` or `rename_tempfile()`.
  46. *
  47. * After the temporary file is renamed or deleted, the `tempfile`
  48. * object is no longer valid and should not be reused.
  49. *
  50. * If the program exits before `rename_tempfile()` or
  51. * `delete_tempfile()` is called, an `atexit(3)` handler will close
  52. * and remove the temporary file.
  53. *
  54. * If you need to close the file descriptor yourself, do so by calling
  55. * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)`
  56. * yourself, otherwise the `struct tempfile` structure would still
  57. * think that the file descriptor needs to be closed, and a later
  58. * cleanup would result in duplicate calls to `close(2)`. Worse yet,
  59. * if you close and then later open another file descriptor for a
  60. * completely different purpose, then the unrelated file descriptor
  61. * might get closed.
  62. *
  63. *
  64. * Error handling
  65. * --------------
  66. *
  67. * `create_tempfile()` returns an allocated tempfile on success or NULL
  68. * on failure. On errors, `errno` describes the reason for failure.
  69. *
  70. * `rename_tempfile()` and `close_tempfile_gently()` return 0 on success.
  71. * On failure they set `errno` appropriately and return -1.
  72. * `delete_tempfile()` and `rename` (but not `close`) do their best to
  73. * delete the temporary file before returning.
  74. */
  75. struct tempfile {
  76. volatile struct volatile_list_head list;
  77. volatile sig_atomic_t active;
  78. volatile int fd;
  79. FILE *volatile fp;
  80. volatile pid_t owner;
  81. struct strbuf filename;
  82. };
  83. /*
  84. * Attempt to create a temporary file at the specified `path`. Return
  85. * a tempfile (whose "fd" member can be used for writing to it), or
  86. * NULL on error. It is an error if a file already exists at that path.
  87. * Note that `mode` will be further modified by the umask, and possibly
  88. * `core.sharedRepository`, so it is not guaranteed to have the given
  89. * mode.
  90. */
  91. struct tempfile *create_tempfile_mode(const char *path, int mode);
  92. static inline struct tempfile *create_tempfile(const char *path)
  93. {
  94. return create_tempfile_mode(path, 0666);
  95. }
  96. /*
  97. * Register an existing file as a tempfile, meaning that it will be
  98. * deleted when the program exits. The tempfile is considered closed,
  99. * but it can be worked with like any other closed tempfile (for
  100. * example, it can be opened using reopen_tempfile()).
  101. */
  102. struct tempfile *register_tempfile(const char *path);
  103. /*
  104. * mks_tempfile functions
  105. *
  106. * The following functions attempt to create and open temporary files
  107. * with names derived automatically from a template, in the manner of
  108. * mkstemps(), and arrange for them to be deleted if the program ends
  109. * before they are deleted explicitly. There is a whole family of such
  110. * functions, named according to the following pattern:
  111. *
  112. * x?mks_tempfile_t?s?m?()
  113. *
  114. * The optional letters have the following meanings:
  115. *
  116. * x - die if the temporary file cannot be created.
  117. *
  118. * t - create the temporary file under $TMPDIR (as opposed to
  119. * relative to the current directory). When these variants are
  120. * used, template should be the pattern for the filename alone,
  121. * without a path.
  122. *
  123. * s - template includes a suffix that is suffixlen characters long.
  124. *
  125. * m - the temporary file should be created with the specified mode
  126. * (otherwise, the mode is set to 0600).
  127. *
  128. * None of these functions modify template. If the caller wants to
  129. * know the (absolute) path of the file that was created, it can be
  130. * read from tempfile->filename.
  131. *
  132. * On success, the functions return a tempfile whose "fd" member is open
  133. * for writing the temporary file. On errors, they return NULL and set
  134. * errno appropriately (except for the "x" variants, which die() on
  135. * errors).
  136. */
  137. /* See "mks_tempfile functions" above. */
  138. struct tempfile *mks_tempfile_sm(const char *filename_template,
  139. int suffixlen, int mode);
  140. /* See "mks_tempfile functions" above. */
  141. static inline struct tempfile *mks_tempfile_s(const char *filename_template,
  142. int suffixlen)
  143. {
  144. return mks_tempfile_sm(filename_template, suffixlen, 0600);
  145. }
  146. /* See "mks_tempfile functions" above. */
  147. static inline struct tempfile *mks_tempfile_m(const char *filename_template, int mode)
  148. {
  149. return mks_tempfile_sm(filename_template, 0, mode);
  150. }
  151. /* See "mks_tempfile functions" above. */
  152. static inline struct tempfile *mks_tempfile(const char *filename_template)
  153. {
  154. return mks_tempfile_sm(filename_template, 0, 0600);
  155. }
  156. /* See "mks_tempfile functions" above. */
  157. struct tempfile *mks_tempfile_tsm(const char *filename_template,
  158. int suffixlen, int mode);
  159. /* See "mks_tempfile functions" above. */
  160. static inline struct tempfile *mks_tempfile_ts(const char *filename_template,
  161. int suffixlen)
  162. {
  163. return mks_tempfile_tsm(filename_template, suffixlen, 0600);
  164. }
  165. /* See "mks_tempfile functions" above. */
  166. static inline struct tempfile *mks_tempfile_tm(const char *filename_template, int mode)
  167. {
  168. return mks_tempfile_tsm(filename_template, 0, mode);
  169. }
  170. /* See "mks_tempfile functions" above. */
  171. static inline struct tempfile *mks_tempfile_t(const char *filename_template)
  172. {
  173. return mks_tempfile_tsm(filename_template, 0, 0600);
  174. }
  175. /* See "mks_tempfile functions" above. */
  176. struct tempfile *xmks_tempfile_m(const char *filename_template, int mode);
  177. /* See "mks_tempfile functions" above. */
  178. static inline struct tempfile *xmks_tempfile(const char *filename_template)
  179. {
  180. return xmks_tempfile_m(filename_template, 0600);
  181. }
  182. /*
  183. * Associate a stdio stream with the temporary file (which must still
  184. * be open). Return `NULL` (*without* deleting the file) on error. The
  185. * stream is closed automatically when `close_tempfile_gently()` is called or
  186. * when the file is deleted or renamed.
  187. */
  188. FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
  189. static inline int is_tempfile_active(struct tempfile *tempfile)
  190. {
  191. return tempfile && tempfile->active;
  192. }
  193. /*
  194. * Return the path of the lockfile. The return value is a pointer to a
  195. * field within the lock_file object and should not be freed.
  196. */
  197. const char *get_tempfile_path(struct tempfile *tempfile);
  198. int get_tempfile_fd(struct tempfile *tempfile);
  199. FILE *get_tempfile_fp(struct tempfile *tempfile);
  200. /*
  201. * If the temporary file is still open, close it (and the file pointer
  202. * too, if it has been opened using `fdopen_tempfile()`) without
  203. * deleting the file. Return 0 upon success. On failure to `close(2)`,
  204. * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
  205. * should eventually be called regardless of whether `close_tempfile_gently()`
  206. * succeeds.
  207. */
  208. int close_tempfile_gently(struct tempfile *tempfile);
  209. /*
  210. * Re-open a temporary file that has been closed using
  211. * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
  212. * to implement a sequence of operations like the following:
  213. *
  214. * * Create temporary file.
  215. *
  216. * * Write new contents to file, then `close_tempfile_gently()` to cause the
  217. * contents to be written to disk.
  218. *
  219. * * Pass the name of the temporary file to another program to allow
  220. * it (and nobody else) to inspect or even modify the file's
  221. * contents.
  222. *
  223. * * `reopen_tempfile()` to reopen the temporary file, truncating the existing
  224. * contents. Write out the new contents.
  225. *
  226. * * `rename_tempfile()` to move the file to its permanent location.
  227. */
  228. int reopen_tempfile(struct tempfile *tempfile);
  229. /*
  230. * Close the file descriptor and/or file pointer and remove the
  231. * temporary file associated with `tempfile`. It is a NOOP to call
  232. * `delete_tempfile()` for a `tempfile` object that has already been
  233. * deleted or renamed.
  234. */
  235. void delete_tempfile(struct tempfile **tempfile_p);
  236. /*
  237. * Close the file descriptor and/or file pointer if they are still
  238. * open, and atomically rename the temporary file to `path`. `path`
  239. * must be on the same filesystem as the lock file. Return 0 on
  240. * success. On failure, delete the temporary file and return -1, with
  241. * `errno` set to the value from the failing call to `close(2)` or
  242. * `rename(2)`. It is a bug to call `rename_tempfile()` for a
  243. * `tempfile` object that is not currently active.
  244. */
  245. int rename_tempfile(struct tempfile **tempfile_p, const char *path);
  246. #endif /* TEMPFILE_H */