Debian Codegraph @

src/lib/util/textfile.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
29 #include "lib/util/file.h"cxx . . $ `<file>/src/lib/util/file.h`/
30 #include "lib/util/textfile.h"cxx . . $ `<file>/src/lib/util/textfile.h`/
31
32 static errno_tcxx . . $ errno_t#
33 textfile_read_contentcxx . . $ textfile_read_content(7d69dc7af7f7d0).(FILEcxx . . $ FILE# *file,
34 const char *filename,
35 unsigned int limit_KiB,
36 char **_content)
37 {
38 size_t read_bytes;
39 char *buffer;
40 long filelen;
41 errno_tcxx . . $ errno_t# ret;
42
43 ret = fseekcxx . . $ fseek(543fae30d249f6b1).(file, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:116:9`!);
44 if (ret != 0) {
45 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
46 goto done;
47 }
48
49 filelen = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(file);
50 if (filelen == -1) {
51 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
52 goto done;
53 }
54
55 if (filelen > limit_KiB * 1024) {
56 ERRORcxx . . $ `src/common/common.h:71:9`!("File [%s] is bigger than %uKiB!", filename, limit_KiB);
57 ret = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
58 goto done;
59 }
60
61 rewindcxx . . $ rewind(8d4262d45fc644ab).(file);
62
63 buffer = malloc_zero_arraycxx . . $ `src/common/common.h:41:9`!(char, filelen + 1);
64 if (buffer == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
65 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
66 goto done;
67 }
68
69 read_bytes = freadcxx . . $ fread(738fc8ac1d711e58).(buffer, sizeof(char), filelen, file);
70 if (read_bytes != filelen) {
71 freecxx . . $ free(5d22bdacc48458e8).(buffer);
72 ret = EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!;
73 goto done;
74 }
75
76 *_content = buffer;
77
78 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
79
80 done:
81 /* File descriptor is closed when file is closed. */
82 fclosecxx . . $ fclose(89bc1efcdac0ca22).(file);
83
84 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
85 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read file [%s] [%d]: %s",
86 filename, ret, strerror(ret));
87 }
88
89 return ret;
90 }
91
92 errno_tcxx . . $ errno_t#
93 textfile_readcxx . . $ textfile_read(d26d230207164ac8).(const char *filepath,
94 unsigned int limit_KiB,
95 char **_content)
96 {
97 FILEcxx . . $ FILE# *file;
98
99 file = fopencxx . . $ fopen(82ec0787cd37e0a3).(filepath, "r");
100 if (file == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
101 return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
102 }
103
104 return textfile_read_contentcxx . . $ textfile_read_content(7d69dc7af7f7d0).(file, filepath, limit_KiB, _content);
105 }
106
107 errno_tcxx . . $ errno_t#
108 textfile_read_dirfdcxx . . $ textfile_read_dirfd(4ff8903a8e11e2fc).(int dirfd,
109 const char *dirpath,
110 const char *filename,
111 unsigned int limit_KiB,
112 char **_content)
113 {
114 errno_tcxx . . $ errno_t# ret;
115 FILEcxx . . $ FILE# *file;
116 int fd;
117
118 fd = openatcxx . . $ openat(5e925858040f3251).(dirfd, filename, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
119 if (fd == -1) {
120 return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
121 }
122
123 file = fdopencxx . . $ fdopen(8f4e2d7350ebd077).(fd, "r");
124 if (file == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
125 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
126 closecxx . . $ close(7864480464b09eea).(fd);
127 return ret;
128 }
129
130 return textfile_read_contentcxx . . $ textfile_read_content(7d69dc7af7f7d0).(file, filename, limit_KiB, _content);
131 }
132
133 errno_tcxx . . $ errno_t#
134 textfile_writecxx . . $ textfile_write(54fb1e1f0004e0cf).(const char *filepath,
135 const char *content,
136 mode_tcxx . . $ mode_t# mode)
137 {
138 FILEcxx . . $ FILE# *file;
139 mode_tcxx . . $ mode_t# oldmask;
140 size_t written;
141 size_t len;
142 errno_tcxx . . $ errno_t# ret;
143
144 if (filepath == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! || filepath[0] == '\0') {
145 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
146 }
147
148 /* Create an empty file if no content is given. */
149 if (content == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
150 content = "";
151 }
152
153 oldmask = umaskcxx . . $ umask(2141b656ce2cc063).(~mode & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!);
154
155 file = fopencxx . . $ fopen(82ec0787cd37e0a3).(filepath, "w");
156 if (file == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
157 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
158 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to open file [%s] [%d]: %s",
159 filepath, ret, strerror(ret));
160 goto done;
161 }
162
163 len = strlencxx . . $ strlen(751346f8406fd082).(content);
164 written = fwritecxx . . $ fwrite(ab0609fa07c47d6e).(content, sizeof(char), len, file);
165 if (written != len) {
166 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
167 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to write data [%s] [%d]: %s",
168 filepath, ret, strerror(ret));
169 goto done;
170 }
171
172 ret = fchmodcxx . . $ fchmod(b223e862534b1804).(filenocxx . . $ fileno(89bc1efcdac0ca22).(file), mode);
173 if (ret != 0) {
174 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
175 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to chmod file [%s] [%d]: %s",
176 filepath, ret, strerror(ret));
177 goto done;
178 }
179
180 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
181
182 done:
183 umaskcxx . . $ umask(2141b656ce2cc063).(oldmask);
184 if (file != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) {
185 fclosecxx . . $ fclose(89bc1efcdac0ca22).(file);
186 }
187
188 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
189 unlinkcxx . . $ unlink(5c4306d8553b3696).(filepath);
190 }
191
192 return ret;
193 }