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 * 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 /* 66 * Safe function to copy files, no more system("cp ...") please. 67 */ 68 int safe_cp(const char *file, const int lineno, 69 void (*cleanup_fn)(void), 70 const char *src, const char *dst); 71 72 /* 73 * Safe function to touch a file. 74 * 75 * If the file (pathname) does not exist It will be created with 76 * the specified permission (mode) and the access/modification times (times). 77 * 78 * If mode is 0 then the file is created with (0666 & ~umask) 79 * permission or (if the file exists) the permission is not changed. 80 * 81 * times is a timespec[2] (as for utimensat(2)). If times is NULL then 82 * the access/modification times of the file is set to the current time. 83 */ 84 int safe_touch(const char *file, const int lineno, 85 void (*cleanup_fn)(void), 86 const char *pathname, 87 mode_t mode, const struct timespec times[2]); 88 89 /* helper functions to setup overlayfs mountpoint */ 90 void create_overlay_dirs(void); 91 int mount_overlay(const char *file, const int lineno, int skip); 92 93 #endif /* SAFE_FILE_OPS_FN */ 94