Update date in copyrights
[src/app-framework-main.git] / src / utils-file.c
1 /*
2  Copyright (C) 2017-2019 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include "utils-file.h"
29
30 /* alias for getfile_at(AT_FDCWD, file, content, size) */
31 int getfile(const char *file, char **content, size_t *size)
32 {
33         return getfile_at(AT_FDCWD, file, content, size);
34 }
35
36 /*
37  * Reads the 'file' relative to 'dfd' (see openat) in a freshly
38  * allocated memory and returns it in 'content' and 'size' (if not NULL).
39  * To help in reading text files, a pending null is added at the
40  * end of the content.
41  * Return 0 in case of success or else -1 and set 'errno'.
42  */
43 int getfile_at(int dfd, const char *file, char **content, size_t *size)
44 {
45         int rc, f;
46         struct stat s;
47         char *r;
48         ssize_t rsz;
49         size_t sz, i;
50
51         if (content)
52                 *content = NULL;
53         rc = openat(dfd, file, O_RDONLY);
54         if (rc >= 0) {
55                 f = rc;
56                 rc = fstat(f, &s);
57                 if (rc != 0) {
58                         /* nothing */;
59                 } else if (!S_ISREG(s.st_mode)) {
60                         rc = -1;
61                         errno = EBADF;
62                 } else {
63                         sz = (size_t)s.st_size;
64                         if (content) {
65                                 r = malloc(sz + 1);
66                                 if (!r) {
67                                         errno = ENOMEM;
68                                         rc = -1;
69                                 } else {
70                                         i = 0;
71                                         while (!rc && i < sz) {
72                                                 rsz = read(f, r + i, sz - i);
73                                                 if (rsz == 0)
74                                                         sz = i;
75                                                 else if (rsz > 0)
76                                                         i += (size_t)rsz;
77                                                 else if (errno != EINTR && errno != EAGAIN) {
78                                                         free(r);
79                                                         rc = -1;
80                                                 }
81                                         }
82                                         if (rc == 0) {
83                                                 r[sz] = 0;
84                                                 *content = r;
85                                         }
86                                 }
87                         }
88                         if (size)
89                                 *size = sz;
90                 }
91                 close(f);
92         }
93         return rc;
94 }
95
96 /* alias for putfile_at(AT_FDCWD, file, content, size) */
97 int putfile(const char *file, const void *content, size_t size)
98 {
99         return putfile_at(AT_FDCWD, file, content, size);
100 }
101
102 int putfile_at(int dfd, const char *file, const void *content, size_t size)
103 {
104         int rc, f;
105         ssize_t wsz;
106         size_t i;
107
108         rc = openat(dfd, file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
109         if (rc >= 0) {
110                 if (size == (size_t)(ssize_t)-1)
111                         size = strlen(content);
112                 f = rc;
113                 i = 0;
114                 rc = 0;
115                 while (!rc && i < size) {
116                         wsz = write(f, content + i, size - i);
117                         if (wsz >= 0)
118                                 i += (size_t)wsz;
119                         else if (errno != EINTR && errno != EAGAIN) {
120                                 unlinkat(dfd, file, 0);
121                                 rc = -1;
122                         }
123                 }
124                 close(f);
125         }
126         return rc;
127 }
128