Debian Codegraph @

src/lib/util/file.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 <unistd.h>
24 #include <libgen.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30
31 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
32 #include "lib/util/file.h"cxx . . $ `<file>/src/lib/util/file.h`/
33
34 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
35 file_check_typecxx . . $ file_check_type(e5d071369ce9a39).(struct statcxx . . $ stat# *statbuf,
36 const char *name,
37 mode_tcxx . . $ mode_t# mode)
38 {
39 mode_tcxx . . $ mode_t# exp_type = mode & S_IFMTcxx . . $ `<external>/531885356569766532/stat.h:104:10`!;
40 mode_tcxx . . $ mode_t# real_type;
41
42 if (statbuf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
43 ERRORcxx . . $ `src/common/common.h:71:9`!("Internal error: stat cannot be NULL!");
44 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
45 }
46
47 real_type = statbuf->st_modecxx . . $ stat#st_mode. & S_IFMTcxx . . $ `<external>/531885356569766532/stat.h:104:10`!;
48 if (exp_type != (real_type)) {
49 switch (exp_type) {
50 case S_IFDIRcxx . . $ `<external>/531885356569766532/stat.h:105:10`!:
51 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] is not a directory!", name);
52 break;
53 case S_IFREGcxx . . $ `<external>/531885356569766532/stat.h:108:10`!:
54 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] is not a regular file!", name);
55 break;
56 case S_IFLNKcxx . . $ `<external>/531885356569766532/stat.h:113:11`!:
57 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] is not a symbolic link!", name);
58 break;
59 default:
60 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] has wrong type [%.7o], expected [%.7o]!",
61 name, real_type, exp_type);
62 break;
63 }
64
65 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
66 }
67
68 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
69 }
70
71 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
72 file_check_modecxx . . $ file_check_mode(b45490cf11566f00).(struct statcxx . . $ stat# *statbuf,
73 const char *name,
74 uid_tcxx . . $ uid_t# uid,
75 gid_tcxx . . $ gid_t# gid,
76 mode_tcxx . . $ mode_t# mode)
77 {
78 mode_tcxx . . $ mode_t# exp_perm = mode & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!;
79 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! bret;
80
81 bret = file_check_typecxx . . $ file_check_type(e5d071369ce9a39).(statbuf, name, mode);
82 if (!bret) {
83 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
84 }
85
86 if (exp_perm != (statbuf->st_modecxx . . $ stat#st_mode. & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!)) {
87 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] has wrong mode [%.4o], expected [%.4o]!",
88 name, statbuf->st_mode & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!, exp_perm);
89 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
90 }
91
92 if (uid != (uid_tcxx . . $ uid_t#)(-1) && statbuf->st_uidcxx . . $ stat#st_uid. != uid) {
93 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] has wrong owner [%u], expected [%u]!",
94 name, statbuf->st_uid, uid);
95 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
96 }
97
98 if (gid != (gid_tcxx . . $ gid_t#)(-1) && statbuf->st_gidcxx . . $ stat#st_gid. != gid) {
99 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] has wrong group [%u], expected [%u]!",
100 name, statbuf->st_gid, gid);
101 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
102 }
103
104 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
105 }
106
107 static errno_tcxx . . $ errno_t#
108 file_check_attributescxx . . $ file_check_attributes(4567836c9036030d).(const char *filepath,
109 uid_tcxx . . $ uid_t# uid,
110 gid_tcxx . . $ gid_t# gid,
111 mode_tcxx . . $ mode_t# mode,
112 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *_result)
113 {
114 struct statcxx . . $ stat# statbuf;
115 errno_tcxx . . $ errno_t# ret;
116
117 ret = lstatcxx . . $ lstat(a9cd1e23e96f423c).(filepath, &statbuf);
118 if (ret == -1) {
119 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
120 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
121 ERRORcxx . . $ `src/common/common.h:71:9`!("[%s] does not exist!", filepath);
122 *_result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
123 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
124 }
125
126 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to stat [%s] [%d]: %s", filepath, ret, strerror(ret));
127 return ret;
128 }
129
130 *_result = file_check_modecxx . . $ file_check_mode(b45490cf11566f00).(&statbuf, filepath, uid, gid, mode);
131
132 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
133 }
134
135 errno_tcxx . . $ errno_t#
136 file_is_regularcxx . . $ file_is_regular(4567836c9036030d).(const char *filepath,
137 uid_tcxx . . $ uid_t# uid,
138 gid_tcxx . . $ gid_t# gid,
139 mode_tcxx . . $ mode_t# access_mode,
140 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *_result)
141 {
142 return file_check_attributescxx . . $ file_check_attributes(4567836c9036030d).(filepath, uid, gid, S_IFREGcxx . . $ `<external>/531885356569766532/stat.h:108:10`! | access_mode,
143 _result);
144 }
145
146 errno_tcxx . . $ errno_t#
147 file_links_tocxx . . $ file_links_to(9f2194e17b4de24f).(const char *linkpath,
148 const char *destpath,
149 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *_result)
150 {
151 char linkbuf[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1];
152 ssize_tcxx . . $ ssize_t# len;
153 errno_tcxx . . $ errno_t# ret;
154
155 ret = file_check_attributescxx . . $ file_check_attributes(4567836c9036030d).(linkpath, (uid_tcxx . . $ uid_t#)-1, (gid_tcxx . . $ gid_t#)-1,
156 S_IFLNKcxx . . $ `<external>/531885356569766532/stat.h:113:11`! | ACCESSPERMScxx . . $ `<external>/531885356569766532/stat.h:195:10`!, _result);
157 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`! || *_result == falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!) {
158 return ret;
159 }
160
161 len = readlinkcxx . . $ readlink(373c9bd46766ee5).(linkpath, linkbuf, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1);
162 if (len == -1) {
163 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
164 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read link destination [%s] [%d]: %s",
165 linkpath, ret, strerror(ret));
166 return ret;
167 }
168
169 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(linkbuf, destpath, len) != 0) {
170 ERRORcxx . . $ `src/common/common.h:71:9`!("Link [%s] does not point to [%s]", linkpath, destpath);
171 *_result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
172 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
173 }
174
175 *_result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
176 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
177 }
178
179 errno_tcxx . . $ errno_t#
180 file_does_not_link_tocxx . . $ file_does_not_link_to(b4f44182c5fb6adc).(const char *linkpath,
181 const char *destpath,
182 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! error_mode,
183 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *_result)
184 {
185 char linkbuf[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1];
186 struct statcxx . . $ stat# statbuf;
187 ssize_tcxx . . $ ssize_t# len;
188 errno_tcxx . . $ errno_t# ret;
189
190 ret = lstatcxx . . $ lstat(a9cd1e23e96f423c).(linkpath, &statbuf);
191 if (ret == -1) {
192 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
193
194 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
195 *_result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
196 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
197 }
198
199 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to stat [%s] [%d]: %s", linkpath, ret, strerror(ret));
200 return ret;
201 }
202
203 if (!S_ISLNKcxx . . $ `<external>/531885356569766532/stat.h:133:10`!(statbuf.st_mode)) {
204 *_result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
205 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
206 }
207
208 len = readlinkcxx . . $ readlink(373c9bd46766ee5).(linkpath, linkbuf, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1);
209 if (len == -1) {
210 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
211 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read link destination [%s] [%d]: %s",
212 linkpath, ret, strerror(ret));
213 return ret;
214 }
215
216 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(linkbuf, destpath, len) == 0) {
217 if (error_mode) {
218 ERRORcxx . . $ `src/common/common.h:71:9`!("Link [%s] points to [%s]", linkpath, destpath);
219 } else {
220 INFOcxx . . $ `src/common/common.h:63:9`!("Link [%s] points to [%s]", linkpath, destpath);
221 }
222 *_result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
223 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
224 }
225
226 *_result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
227 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
228 }
229
230 errno_tcxx . . $ errno_t#
231 file_check_accesscxx . . $ file_check_access(cb7559238bb26814).(const char *path, int mode)
232 {
233 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
234 if (accesscxx . . $ access(cb7559238bb26814).(path, mode) == 0) {
235 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
236 }
237
238 /* ENOENT is returned if a file is missing. */
239 return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
240 }
241
242 errno_tcxx . . $ errno_t#
243 file_existscxx . . $ file_exists(5c4306d8553b3696).(const char *path)
244 {
245 return file_check_accesscxx . . $ file_check_access(cb7559238bb26814).(path, F_OKcxx . . $ `<external>/12628407106812493757/unistd.h:284:9`!);
246 }
247
248 const char *
249 file_get_basenamecxx . . $ file_get_basename(fb58d2e09a2a1ce6).(const char *filepath)
250 {
251 const char *filename;
252
253 if (filepath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
254 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
255 }
256
257 filename = strrchrcxx . . $ strrchr(2050848944e00d54).(filepath, '/');
258 if (filename == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
259 /* There is no slash. */
260 return filepath;
261 }
262
263 if (filename[0] == '\0' || filename[1] == '\0') {
264 /* There is a slash but no file name. */
265 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
266 }
267
268 return filename + 1;
269
270 }
271
272 char *
273 file_get_parent_directorycxx . . $ file_get_parent_directory(a8b8e32347182212).(const char *filepath)
274 {
275 char *copy = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
276 char *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
277 char *dir;
278 errno_tcxx . . $ errno_t# ret;
279
280 if (filepath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
281 ERRORcxx . . $ `src/common/common.h:71:9`!("Internal error: filepath cannot be NULL!");
282 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
283 }
284
285 copy = strdupcxx . . $ strdup(a8b8e32347182212).(filepath);
286 if (copy == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
287 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
288 goto done;
289 }
290
291 /* Function dirname() may modify input argument,
292 * it may also return pointer to static memory. */
293 dir = dirnamecxx . . $ dirname(7a9722d75b371431).(copy);
294 if (dir == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
295 ret = ENOTDIRcxx . . $ `<external>/8987895939074593727/errno-base.h:24:9`!;
296 goto done;
297 }
298
299 out = strdupcxx . . $ strdup(a8b8e32347182212).(dir);
300 if (out == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
301 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
302 goto done;
303 }
304
305 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
306
307 done:
308 if (copy != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
309 freecxx . . $ free(5d22bdacc48458e8).(copy);
310 }
311
312 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
313 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to get parent directory of [%s] [%d]: %s",
314 filepath, ret, strerror(ret));
315 }
316
317 return out;
318 }
319
320 errno_tcxx . . $ errno_t#
321 file_make_pathcxx . . $ file_make_path(1df18506ef0a631b).(const char *path, mode_tcxx . . $ mode_t# mode)
322 {
323 char *parent;
324 errno_tcxx . . $ errno_t# ret;
325
326 ret = file_existscxx . . $ file_exists(5c4306d8553b3696).(path);
327 if (ret != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
328 return ret;
329 }
330
331 parent = file_get_parent_directorycxx . . $ file_get_parent_directory(a8b8e32347182212).(path);
332 if (parent != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
333 ret = file_make_pathcxx . . $ file_make_path(1df18506ef0a631b).(parent, mode);
334 freecxx . . $ free(5d22bdacc48458e8).(parent);
335 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
336 return ret;
337 }
338 }
339
340 ret = mkdircxx . . $ mkdir(1df18506ef0a631b).(path, mode);
341 if (ret != 0) {
342 return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
343 }
344
345 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
346
347 }
348
349 errno_tcxx . . $ errno_t#
350 file_mktmp_forcxx . . $ file_mktmp_for(d26d230207164ac8).(const char *path, mode_tcxx . . $ mode_t# mode, char **_tmpfile)
351 {
352 mode_tcxx . . $ mode_t# oldmask;
353 char *tmpfile;
354 errno_tcxx . . $ errno_t# ret;
355 int fd;
356
357 oldmask = umaskcxx . . $ umask(2141b656ce2cc063).(~mode & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!);
358
359 tmpfile = formatcxx . . $ format(a849077ff1b8d812).("%s.XXXXXX", path);
360 if (tmpfile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
361 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
362 goto done;
363 }
364
365 fd = mkstempcxx . . $ mkstemp(1ef8e6ec083aabb9).(tmpfile);
366 if (fd == -1) {
367 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
368 /* To silence static analyzers that assumes that errno can be 0 here. */
369 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
370 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
371 }
372 goto done;
373 }
374
375 closecxx . . $ close(7864480464b09eea).(fd);
376
377 *_tmpfile = tmpfile;
378
379 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
380
381 done:
382 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
383 freecxx . . $ free(5d22bdacc48458e8).(tmpfile);
384 }
385
386 umaskcxx . . $ umask(2141b656ce2cc063).(oldmask);
387
388 return ret;
389 }
390
391 static errno_tcxx . . $ errno_t#
392 file_mktmp_atcxx . . $ file_mktmp_at(5071347881743dfd).(const char *path, const char *name, mode_tcxx . . $ mode_t# mode, char **_tmpfile)
393 {
394 char *fullpath;
395 errno_tcxx . . $ errno_t# ret;
396
397 fullpath = formatcxx . . $ format(a849077ff1b8d812).("%s/%s", path, name);
398 if (fullpath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
399 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
400 }
401
402 ret = file_mktmp_forcxx . . $ file_mktmp_for(d26d230207164ac8).(fullpath, mode, _tmpfile);
403 freecxx . . $ free(5d22bdacc48458e8).(fullpath);
404
405 return ret;
406 }
407
408 errno_tcxx . . $ errno_t#
409 file_mktmp_copycxx . . $ file_mktmp_copy(dbcc8002fb6c5cd0).(const char *source,
410 const char *destdir,
411 const char *destname,
412 mode_tcxx . . $ mode_t# dir_mode,
413 char **_tmpfile)
414 {
415 const char *tmpname;
416 char *tmpfile;
417 errno_tcxx . . $ errno_t# ret;
418
419 ret = file_make_pathcxx . . $ file_make_path(1df18506ef0a631b).(destdir, dir_mode);
420 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
421 return ret;
422 }
423
424 ret = file_mktmp_atcxx . . $ file_mktmp_at(5071347881743dfd).(destdir, destname, 0600, &tmpfile);
425 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
426 return ret;
427 }
428
429 tmpname = file_get_basenamecxx . . $ file_get_basename(fb58d2e09a2a1ce6).(tmpfile);
430 if (tmpname == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
431 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
432 goto done;
433 }
434
435 ret = file_copycxx . . $ file_copy(f8e1a36fd0133965).(source, destdir, tmpname, dir_mode);
436 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
437 goto done;
438 }
439
440 *_tmpfile = tmpfile;
441
442 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
443
444 done:
445 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
446 unlinkcxx . . $ unlink(5c4306d8553b3696).(tmpfile);
447 freecxx . . $ free(5d22bdacc48458e8).(tmpfile);
448 }
449
450 return ret;
451 }
452
453 errno_tcxx . . $ errno_t#
454 file_copycxx . . $ file_copy(f8e1a36fd0133965).(const char *source,
455 const char *destdir,
456 const char *destname,
457 mode_tcxx . . $ mode_t# dir_mode)
458 {
459 struct statcxx . . $ stat# statbuf;
460 FILEcxx . . $ FILE# *fsource = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
461 FILEcxx . . $ FILE# *fdest = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
462 size_t bytes_written;
463 size_t bytes_read;
464 mode_tcxx . . $ mode_t# oldmask;
465 char *destpath;
466 char buf[32];
467 errno_tcxx . . $ errno_t# ret;
468
469 ret = file_make_pathcxx . . $ file_make_path(1df18506ef0a631b).(destdir, dir_mode);
470 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
471 return ret;
472 }
473
474 destpath = formatcxx . . $ format(a849077ff1b8d812).("%s/%s", destdir, destname);
475 if (destpath == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
476 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
477 }
478
479 /* Temporary umask before we change the owner and permissions. */
480 oldmask = umaskcxx . . $ umask(2141b656ce2cc063).(0177);
481
482 fsource = fopencxx . . $ fopen(82ec0787cd37e0a3).(source, "r");
483 if (fsource == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
484 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
485 goto done;
486 }
487
488 ret = fstatcxx . . $ fstat(a43660ce2f600cb5).(filenocxx . . $ fileno(89bc1efcdac0ca22).(fsource), &statbuf);
489 if (ret == -1) {
490 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
491 goto done;
492 }
493
494 fdest = fopencxx . . $ fopen(82ec0787cd37e0a3).(destpath, "w");
495 if (fdest == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
496 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
497 goto done;
498 }
499
500 do {
501 bytes_read = freadcxx . . $ fread(738fc8ac1d711e58).(buf, sizeof(char), sizeof(buf), fsource);
502 if (bytes_read != sizeof(buf)) {
503 if (ferrorcxx . . $ ferror(89bc1efcdac0ca22).(fsource) != 0) {
504 ret = EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!;
505 goto done;
506 }
507 /* eof not error */
508 }
509
510 bytes_written = fwritecxx . . $ fwrite(ab0609fa07c47d6e).(buf, sizeof(char), bytes_read, fdest);
511 if (bytes_written != bytes_read) {
512 if (ferrorcxx . . $ ferror(89bc1efcdac0ca22).(fdest) != 0) {
513 ret = EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!;
514 goto done;
515 }
516 }
517 } while (!feofcxx . . $ feof(89bc1efcdac0ca22).(fsource));
518
519 /* Restore original owner and mode. Errors here are not fatal, since we
520 * have the original content already stored and owned by root. */
521 ret = fchmodcxx . . $ fchmod(b223e862534b1804).(filenocxx . . $ fileno(89bc1efcdac0ca22).(fdest), statbuf.st_modecxx . . $ stat#st_mode. & ALLPERMScxx . . $ `<external>/531885356569766532/stat.h:196:10`!);
522 if (ret != 0) {
523 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
524 WARNcxx . . $ `src/common/common.h:67:9`!("Unable to chmod file [%s] [%d]: %s",
525 destpath, ret, strerror(ret));
526 }
527
528 ret = fchowncxx . . $ fchown(3edbacac9ec58d6c).(filenocxx . . $ fileno(89bc1efcdac0ca22).(fdest), statbuf.st_uidcxx . . $ stat#st_uid., statbuf.st_gidcxx . . $ stat#st_gid.);
529 if (ret != 0) {
530 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
531 WARNcxx . . $ `src/common/common.h:67:9`!("Unable to chown file [%s] [%d]: %s",
532 destpath, ret, strerror(ret));
533 }
534
535 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
536
537 done:
538 freecxx . . $ free(5d22bdacc48458e8).(destpath);
539 umaskcxx . . $ umask(2141b656ce2cc063).(oldmask);
540 if (fsource != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
541 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fsource);
542 }
543
544 if (fdest != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
545 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fdest);
546 }
547
548 return ret;
549 }