xref: /aosp_15_r20/external/ltp/include/safe_file_ops_fn.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2012-2016 Cyril Hrubis <[email protected]>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SAFE_FILE_OPS_FN
19 #define SAFE_FILE_OPS_FN
20 
21 #include <sys/stat.h>
22 #include <time.h>
23 
24 #include "lapi/utime.h"
25 
26 /*
27  * Count number of expected assigned conversions. Any conversion starts with '%'.
28  * The '%%' matches % and no assignment is done. The %*x matches as x would do but
29  * the assignment is suppressed.
30  *
31  * NOTE: This is not 100% correct for complex scanf strings, but will do for
32  *       all of our intended usage.
33  */
34 int tst_count_scanf_conversions(const char *fmt);
35 
36 /*
37  * All-in-one function to scanf value(s) from a file.
38  */
39 int file_scanf(const char *file, const int lineno,
40 		const char *path, const char *fmt, ...)
41 		__attribute__ ((format (scanf, 4, 5)));
42 
43 void safe_file_scanf(const char *file, const int lineno,
44                      void (*cleanup_fn)(void),
45 		     const char *path, const char *fmt, ...)
46 		     __attribute__ ((format (scanf, 5, 6)));
47 
48 int file_lines_scanf(const char *file, const int lineno,
49 		     void (*cleanup_fn)(void), int strict,
50 		     const char *path, const char *fmt, ...)
51 		     __attribute__ ((format (scanf, 6, 7)));
52 
53 /*
54  * All-in-one function that lets you printf directly into a file.
55  */
56 int file_printf(const char *file, const int lineno,
57                       const char *path, const char *fmt, ...)
58                       __attribute__ ((format (printf, 4, 5)));
59 
60 void safe_file_printf(const char *file, const int lineno,
61                       void (*cleanup_fn)(void),
62                       const char *path, const char *fmt, ...)
63                       __attribute__ ((format (printf, 5, 6)));
64 
65 void safe_try_file_printf(const char *file, const int lineno,
66 	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
67 	__attribute__ ((format (printf, 5, 6)));
68 
69 /*
70  * Safe function to copy files, no more system("cp ...") please.
71  */
72 int safe_cp(const char *file, const int lineno,
73              void (*cleanup_fn)(void),
74 	     const char *src, const char *dst);
75 
76 /*
77  * Safe function to touch a file.
78  *
79  * If the file (pathname) does not exist It will be created with
80  * the specified permission (mode) and the access/modification times (times).
81  *
82  * If mode is 0 then the file is created with (0666 & ~umask)
83  * permission or (if the file exists) the permission is not changed.
84  *
85  * times is a timespec[2] (as for utimensat(2)). If times is NULL then
86  * the access/modification times of the file is set to the current time.
87  */
88 int safe_touch(const char *file, const int lineno,
89 		void (*cleanup_fn)(void),
90 		const char *pathname,
91 		mode_t mode, const struct timespec times[2]);
92 
93 #endif /* SAFE_FILE_OPS_FN */
94