• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012-2016 Cyril Hrubis <chrubis@suse.cz>
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  * All-in-one function to scanf value(s) from a file.
28  */
29 int file_scanf(const char *file, const int lineno,
30 		const char *path, const char *fmt, ...)
31 		__attribute__ ((format (scanf, 4, 5)));
32 
33 void safe_file_scanf(const char *file, const int lineno,
34                      void (*cleanup_fn)(void),
35 		     const char *path, const char *fmt, ...)
36 		     __attribute__ ((format (scanf, 5, 6)));
37 
38 int file_lines_scanf(const char *file, const int lineno,
39 		     void (*cleanup_fn)(void), int strict,
40 		     const char *path, const char *fmt, ...)
41 		     __attribute__ ((format (scanf, 6, 7)));
42 
43 /*
44  * All-in-one function that lets you printf directly into a file.
45  */
46 int file_printf(const char *file, const int lineno,
47                       const char *path, const char *fmt, ...)
48                       __attribute__ ((format (printf, 4, 5)));
49 
50 void safe_file_printf(const char *file, const int lineno,
51                       void (*cleanup_fn)(void),
52                       const char *path, const char *fmt, ...)
53                       __attribute__ ((format (printf, 5, 6)));
54 
55 /*
56  * Safe function to copy files, no more system("cp ...") please.
57  */
58 void safe_cp(const char *file, const int lineno,
59              void (*cleanup_fn)(void),
60 	     const char *src, const char *dst);
61 
62 /*
63  * Safe function to touch a file.
64  *
65  * If the file (pathname) does not exist It will be created with
66  * the specified permission (mode) and the access/modification times (times).
67  *
68  * If mode is 0 then the file is created with (0666 & ~umask)
69  * permission or (if the file exists) the permission is not changed.
70  *
71  * times is a timespec[2] (as for utimensat(2)). If times is NULL then
72  * the access/modification times of the file is set to the current time.
73  */
74 void safe_touch(const char *file, const int lineno,
75 		void (*cleanup_fn)(void),
76 		const char *pathname,
77 		mode_t mode, const struct timespec times[2]);
78 
79 #endif /* SAFE_FILE_OPS_FN */
80