17
*
18
* The caller:
19
*
20
- * * Allocates a `struct tempfile`. Once the structure is passed to
21
- * `create_tempfile()`, its storage must remain valid until
22
- * `delete_tempfile()` or `rename_tempfile()` is called on it.
23
- *
20
* * Attempts to create a temporary file by calling
25
- * `create_tempfile()`.
21
+ * `create_tempfile()`. The resources used for the temporary file are
22
+ * managed by the tempfile API.
23
*
24
* * Writes new content to the file by either:
25
*
29
- * * writing to the file descriptor returned by `create_tempfile()`
30
- * (also available via `tempfile->fd`).
26
+ * * writing to the `tempfile->fd` file descriptor
27
*
28
* * calling `fdopen_tempfile()` to get a `FILE` pointer for the
29
* open file and writing to the file using stdio.
30
*
35
- * Note that the file descriptor returned by create_tempfile()
31
+ * Note that the file descriptor created by create_tempfile()
32
* is marked O_CLOEXEC, so the new contents must be written by
33
* the current process, not any spawned one.
34
*
46
* `delete_tempfile()` or `rename_tempfile()`.
47
*
48
* After the temporary file is renamed or deleted, the `tempfile`
53
- * object may be reused or freed.
49
+ * object is no longer valid and should not be reused.
50
*
51
* If the program exits before `rename_tempfile()` or
52
* `delete_tempfile()` is called, an `atexit(3)` handler will close
65
* Error handling
66
* --------------
67
*
72
- * `create_tempfile()` returns a file descriptor on success or -1 on
73
- * failure. On errors, `errno` describes the reason for failure.
68
+ * `create_tempfile()` returns an allocated tempfile on success or NULL
69
+ * on failure. On errors, `errno` describes the reason for failure.
70
*
71
* `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile_gently()`
72
* return 0 on success. On failure they set `errno` appropriately and return
85
86
/*
87
* Attempt to create a temporary file at the specified `path`. Return
92
- * a file descriptor for writing to it, or -1 on error. It is an error
93
- * if a file already exists at that path.
88
+ * a tempfile (whose "fd" member can be used for writing to it), or
89
+ * NULL on error. It is an error if a file already exists at that path.
90
*/
95
-extern int create_tempfile(struct tempfile *tempfile, const char *path);
91
+extern struct tempfile *create_tempfile(const char *path);
92
93
/*
94
* Register an existing file as a tempfile, meaning that it will be
96
* but it can be worked with like any other closed tempfile (for
97
* example, it can be opened using reopen_tempfile()).
98
*/
103
-extern void register_tempfile(struct tempfile *tempfile, const char *path);
99
+extern struct tempfile *register_tempfile(const char *path);
100
101
102
/*
128
* know the (absolute) path of the file that was created, it can be
129
* read from tempfile->filename.
130
*
135
- * On success, the functions return a file descriptor that is open for
136
- * writing the temporary file. On errors, they return -1 and set errno
137
- * appropriately (except for the "x" variants, which die() on errors).
131
+ * On success, the functions return a tempfile whose "fd" member is open
132
+ * for writing the temporary file. On errors, they return NULL and set
133
+ * errno appropriately (except for the "x" variants, which die() on
134
+ * errors).
135
*/
136
137
/* See "mks_tempfile functions" above. */
141
-extern int mks_tempfile_sm(struct tempfile *tempfile,
142
- const char *template, int suffixlen, int mode);
138
+extern struct tempfile *mks_tempfile_sm(const char *template,
139
+ int suffixlen, int mode);
140
141
/* See "mks_tempfile functions" above. */
145
-static inline int mks_tempfile_s(struct tempfile *tempfile,
146
- const char *template, int suffixlen)
142
+static inline struct tempfile *mks_tempfile_s(const char *template,
143
+ int suffixlen)
144
{
148
- return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
145
+ return mks_tempfile_sm(template, suffixlen, 0600);
146
}
147
148
/* See "mks_tempfile functions" above. */
152
-static inline int mks_tempfile_m(struct tempfile *tempfile,
153
- const char *template, int mode)
149
+static inline struct tempfile *mks_tempfile_m(const char *template, int mode)
150
{
155
- return mks_tempfile_sm(tempfile, template, 0, mode);
151
+ return mks_tempfile_sm(template, 0, mode);
152
}
153
154
/* See "mks_tempfile functions" above. */
159
-static inline int mks_tempfile(struct tempfile *tempfile,
160
- const char *template)
155
+static inline struct tempfile *mks_tempfile(const char *template)
156
{
162
- return mks_tempfile_sm(tempfile, template, 0, 0600);
157
+ return mks_tempfile_sm(template, 0, 0600);
158
}
159
160
/* See "mks_tempfile functions" above. */
166
-extern int mks_tempfile_tsm(struct tempfile *tempfile,
167
- const char *template, int suffixlen, int mode);
161
+extern struct tempfile *mks_tempfile_tsm(const char *template,
162
+ int suffixlen, int mode);
163
164
/* See "mks_tempfile functions" above. */
170
-static inline int mks_tempfile_ts(struct tempfile *tempfile,
171
- const char *template, int suffixlen)
165
+static inline struct tempfile *mks_tempfile_ts(const char *template,
166
+ int suffixlen)
167
{
173
- return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
168
+ return mks_tempfile_tsm(template, suffixlen, 0600);
169
}
170
171
/* See "mks_tempfile functions" above. */
177
-static inline int mks_tempfile_tm(struct tempfile *tempfile,
178
- const char *template, int mode)
172
+static inline struct tempfile *mks_tempfile_tm(const char *template, int mode)
173
{
180
- return mks_tempfile_tsm(tempfile, template, 0, mode);
174
+ return mks_tempfile_tsm(template, 0, mode);
175
}
176
177
/* See "mks_tempfile functions" above. */
184
-static inline int mks_tempfile_t(struct tempfile *tempfile,
185
- const char *template)
178
+static inline struct tempfile *mks_tempfile_t(const char *template)
179
{
187
- return mks_tempfile_tsm(tempfile, template, 0, 0600);
180
+ return mks_tempfile_tsm(template, 0, 0600);
181
}
182
183
/* See "mks_tempfile functions" above. */
191
-extern int xmks_tempfile_m(struct tempfile *tempfile,
192
- const char *template, int mode);
184
+extern struct tempfile *xmks_tempfile_m(const char *template, int mode);
185
186
/* See "mks_tempfile functions" above. */
195
-static inline int xmks_tempfile(struct tempfile *tempfile,
196
- const char *template)
187
+static inline struct tempfile *xmks_tempfile(const char *template)
188
{
198
- return xmks_tempfile_m(tempfile, template, 0600);
189
+ return xmks_tempfile_m(template, 0600);
190
}
191
192
/*
248
* `delete_tempfile()` for a `tempfile` object that has already been
249
* deleted or renamed.
250
*/
260
-extern void delete_tempfile(struct tempfile *tempfile);
251
+extern void delete_tempfile(struct tempfile **tempfile_p);
252
253
/*
254
* Close the file descriptor and/or file pointer if they are still
259
* `rename(2)`. It is a bug to call `rename_tempfile()` for a
260
* `tempfile` object that is not currently active.
261
*/
271
-extern int rename_tempfile(struct tempfile *tempfile, const char *path);
262
+extern int rename_tempfile(struct tempfile **tempfile_p, const char *path);
263
264
#endif /* TEMPFILE_H */