blob: cdb0f7436909dcb2ffa04b7ae8e50d676452c2e5 [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
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 */
29int file_scanf(const char *file, const int lineno,
30 const char *path, const char *fmt, ...)
31 __attribute__ ((format (scanf, 4, 5)));
32
33void 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/*
39 * All-in-one function that lets you printf directly into a file.
40 */
41int file_printf(const char *file, const int lineno,
42 const char *path, const char *fmt, ...)
43 __attribute__ ((format (printf, 4, 5)));
44
45void safe_file_printf(const char *file, const int lineno,
46 void (*cleanup_fn)(void),
47 const char *path, const char *fmt, ...)
48 __attribute__ ((format (printf, 5, 6)));
49
50/*
51 * Safe function to copy files, no more system("cp ...") please.
52 */
53void safe_cp(const char *file, const int lineno,
54 void (*cleanup_fn)(void),
55 const char *src, const char *dst);
56
57/*
58 * Safe function to touch a file.
59 *
60 * If the file (pathname) does not exist It will be created with
61 * the specified permission (mode) and the access/modification times (times).
62 *
63 * If mode is 0 then the file is created with (0666 & ~umask)
64 * permission or (if the file exists) the permission is not changed.
65 *
66 * times is a timespec[2] (as for utimensat(2)). If times is NULL then
67 * the access/modification times of the file is set to the current time.
68 */
69void safe_touch(const char *file, const int lineno,
70 void (*cleanup_fn)(void),
71 const char *pathname,
72 mode_t mode, const struct timespec times[2]);
73
74#endif /* SAFE_FILE_OPS_FN */