compositor: Use stdint for specifing integer storage
[src/agl-compositor.git] / shared / file-util.c
1 /*
2  * Copyright © 2015 Collabora, Ltd.
3  *
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:
11  *
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.
24  */
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33
34 #include "file-util.h"
35
36 static int
37 current_time_str(char *str, size_t len, const char *fmt)
38 {
39         time_t t;
40         struct tm *t_local;
41         int ret;
42
43         t = time(NULL);
44         t_local = localtime(&t);
45         if (!t_local) {
46                 errno = ETIME;
47                 return -1;
48         }
49
50         ret = strftime(str, len, fmt, t_local);
51         if (ret == 0) {
52                 errno = ETIME;
53                 return -1;
54         }
55
56         return ret;
57 }
58
59 static int
60 create_file_excl(const char *fname)
61 {
62         return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666);
63 }
64
65 /** Create a unique file with date and time in the name
66  *
67  * \param path File path
68  * \param prefix File name prefix.
69  * \param suffix File name suffix.
70  * \param[out] name_out Buffer for the resulting file name.
71  * \param name_len Number of bytes usable in name_out.
72  * \return stdio FILE pointer, or NULL on failure.
73  *
74  * Create and open a new file with the name concatenated from
75  * path/prefix, date and time, and suffix. If a file with this name
76  * already exists, an counter number is added to the end of the
77  * date and time sub-string. The counter is increased until a free file
78  * name is found.
79  *
80  * Once creating the file succeeds, the name of the file is in name_out.
81  * On failure, the contents of name_out are undefined and errno is set.
82  */
83 FILE *
84 file_create_dated(const char *path, const char *prefix, const char *suffix,
85                   char *name_out, size_t name_len)
86 {
87         char timestr[128];
88         int ret;
89         int fd;
90         int cnt = 0;
91         int with_path;
92
93         with_path = path && path[0];
94
95         if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0)
96                 return NULL;
97
98         ret = snprintf(name_out, name_len, "%s%s%s%s%s",
99                        with_path ? path : "", with_path ? "/" : "",
100                        prefix, timestr, suffix);
101         if (ret < 0 || (size_t)ret >= name_len) {
102                 errno = ENOBUFS;
103                 return NULL;
104         }
105
106         fd = create_file_excl(name_out);
107
108         while (fd == -1 && errno == EEXIST) {
109                 cnt++;
110
111                 ret = snprintf(name_out, name_len, "%s%s%s%s-%d%s",
112                                with_path ? path : "", with_path ? "/" : "",
113                                prefix, timestr, cnt, suffix);
114                 if (ret < 0 || (size_t)ret >= name_len) {
115                         errno = ENOBUFS;
116                         return NULL;
117                 }
118
119                 fd = create_file_excl(name_out);
120         }
121
122         if (fd == -1)
123                 return NULL;
124
125         return fdopen(fd, "w");
126 }