2 * Copyright © 2015 Collabora, Ltd.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
32 #include "file-util.h"
35 current_time_str(char *str, size_t len, const char *fmt)
42 t_local = localtime(&t);
48 ret = strftime(str, len, fmt, t_local);
58 create_file_excl(const char *fname)
60 return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666);
63 /** Create a unique file with date and time in the name
65 * \param path_prefix Path and file name prefix.
66 * \param suffix File name suffix.
67 * \param name_out[out] Buffer for the resulting file name.
68 * \param name_len Number of bytes usable in name_out.
69 * \return stdio FILE pointer, or NULL on failure.
71 * Create and open a new file with the name concatenated from
72 * path_prefix, date and time, and suffix. If a file with this name
73 * already exists, an counter number is added to the end of the
74 * date and time sub-string. The counter is increased until a free file
77 * Once creating the file succeeds, the name of the file is in name_out.
78 * On failure, the contents of name_out are undefined and errno is set.
81 file_create_dated(const char *path_prefix, const char *suffix,
82 char *name_out, size_t name_len)
89 if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0)
92 ret = snprintf(name_out, name_len, "%s%s%s",
93 path_prefix, timestr, suffix);
94 if (ret < 0 || (size_t)ret >= name_len) {
99 fd = create_file_excl(name_out);
101 while (fd == -1 && errno == EEXIST) {
104 ret = snprintf(name_out, name_len, "%s%s-%d%s",
105 path_prefix, timestr, cnt, suffix);
106 if (ret < 0 || (size_t)ret >= name_len) {
111 fd = create_file_excl(name_out);
117 return fdopen(fd, "w");