Pekka Paalanen | 899b50b | 2015-02-12 12:52:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Collabora, Ltd. |
| 3 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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: |
Pekka Paalanen | 899b50b | 2015-02-12 12:52:21 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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. |
| 15 | * |
| 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 |
| 23 | * SOFTWARE. |
Pekka Paalanen | 899b50b | 2015-02-12 12:52:21 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <errno.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
Derek Foreman | 83b900f | 2018-02-06 15:18:36 -0600 | [diff] [blame^] | 34 | #include <stdlib.h> |
Pekka Paalanen | 899b50b | 2015-02-12 12:52:21 +0200 | [diff] [blame] | 35 | |
| 36 | #include "file-util.h" |
| 37 | |
| 38 | static int |
| 39 | current_time_str(char *str, size_t len, const char *fmt) |
| 40 | { |
| 41 | time_t t; |
| 42 | struct tm *t_local; |
| 43 | int ret; |
| 44 | |
| 45 | t = time(NULL); |
| 46 | t_local = localtime(&t); |
| 47 | if (!t_local) { |
| 48 | errno = ETIME; |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | ret = strftime(str, len, fmt, t_local); |
| 53 | if (ret == 0) { |
| 54 | errno = ETIME; |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | create_file_excl(const char *fname) |
| 63 | { |
| 64 | return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666); |
| 65 | } |
| 66 | |
| 67 | /** Create a unique file with date and time in the name |
| 68 | * |
| 69 | * \param path_prefix Path and file name prefix. |
| 70 | * \param suffix File name suffix. |
| 71 | * \param name_out[out] Buffer for the resulting file name. |
| 72 | * \param name_len Number of bytes usable in name_out. |
| 73 | * \return stdio FILE pointer, or NULL on failure. |
| 74 | * |
| 75 | * Create and open a new file with the name concatenated from |
| 76 | * path_prefix, date and time, and suffix. If a file with this name |
| 77 | * already exists, an counter number is added to the end of the |
| 78 | * date and time sub-string. The counter is increased until a free file |
| 79 | * name is found. |
| 80 | * |
| 81 | * Once creating the file succeeds, the name of the file is in name_out. |
| 82 | * On failure, the contents of name_out are undefined and errno is set. |
| 83 | */ |
| 84 | FILE * |
| 85 | file_create_dated(const char *path_prefix, const char *suffix, |
| 86 | char *name_out, size_t name_len) |
| 87 | { |
| 88 | char timestr[128]; |
| 89 | int ret; |
| 90 | int fd; |
| 91 | int cnt = 0; |
| 92 | |
| 93 | if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0) |
| 94 | return NULL; |
| 95 | |
| 96 | ret = snprintf(name_out, name_len, "%s%s%s", |
| 97 | path_prefix, timestr, suffix); |
| 98 | if (ret < 0 || (size_t)ret >= name_len) { |
| 99 | errno = ENOBUFS; |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | fd = create_file_excl(name_out); |
| 104 | |
| 105 | while (fd == -1 && errno == EEXIST) { |
| 106 | cnt++; |
| 107 | |
| 108 | ret = snprintf(name_out, name_len, "%s%s-%d%s", |
| 109 | path_prefix, timestr, cnt, suffix); |
| 110 | if (ret < 0 || (size_t)ret >= name_len) { |
| 111 | errno = ENOBUFS; |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | fd = create_file_excl(name_out); |
| 116 | } |
| 117 | |
| 118 | if (fd == -1) |
| 119 | return NULL; |
| 120 | |
| 121 | return fdopen(fd, "w"); |
| 122 | } |
Derek Foreman | 83b900f | 2018-02-06 15:18:36 -0600 | [diff] [blame^] | 123 | |
| 124 | char * |
| 125 | file_name_with_datadir(const char *filename) |
| 126 | { |
| 127 | const char *base = getenv("WESTON_DATA_DIR"); |
| 128 | char *out; |
| 129 | int len; |
| 130 | |
| 131 | if (base) |
| 132 | len = asprintf(&out, "%s/%s", base, filename); |
| 133 | else |
| 134 | len = asprintf(&out, "%s/weston/%s", DATADIR, filename); |
| 135 | |
| 136 | if (len == -1) |
| 137 | return NULL; |
| 138 | |
| 139 | return out; |
| 140 | } |