2 * Copyright © 2015 Collabora, Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <sys/types.h>
36 #include "file-util.h"
39 current_time_str(char *str, size_t len, const char *fmt)
46 t_local = localtime(&t);
52 ret = strftime(str, len, fmt, t_local);
62 create_file_excl(const char *fname)
64 return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666);
67 /** Create a unique file with date and time in the name
69 * \param path File path
70 * \param prefix File name prefix.
71 * \param suffix File name suffix.
72 * \param name_out[out] Buffer for the resulting file name.
73 * \param name_len Number of bytes usable in name_out.
74 * \return stdio FILE pointer, or NULL on failure.
76 * Create and open a new file with the name concatenated from
77 * path/prefix, date and time, and suffix. If a file with this name
78 * already exists, an counter number is added to the end of the
79 * date and time sub-string. The counter is increased until a free file
82 * Once creating the file succeeds, the name of the file is in name_out.
83 * On failure, the contents of name_out are undefined and errno is set.
86 file_create_dated(const char *path, const char *prefix, const char *suffix,
87 char *name_out, size_t name_len)
95 with_path = path && path[0];
97 if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0)
100 ret = snprintf(name_out, name_len, "%s%s%s%s%s",
101 with_path ? path : "", with_path ? "/" : "",
102 prefix, timestr, suffix);
103 if (ret < 0 || (size_t)ret >= name_len) {
108 fd = create_file_excl(name_out);
110 while (fd == -1 && errno == EEXIST) {
113 ret = snprintf(name_out, name_len, "%s%s%s%s-%d%s",
114 with_path ? path : "", with_path ? "/" : "",
115 prefix, timestr, cnt, suffix);
116 if (ret < 0 || (size_t)ret >= name_len) {
121 fd = create_file_excl(name_out);
127 return fdopen(fd, "w");
131 file_name_with_datadir(const char *filename)
133 const char *base = getenv("WESTON_DATA_DIR");
138 len = asprintf(&out, "%s/%s", base, filename);
140 len = asprintf(&out, "%s/weston/%s", DATADIR, filename);