Debian Codegraph @

src/lib/util/file.h

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 #ifndef _FILE_H_
22 #define _FILE_H_
23
24 #include <stdbool.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28
29 #include "common/errno_t.h"
30
31 /**
32 * Check that a file @filepath is a regular file.
33 *
34 * It also checks that the file is owned by given user and group and it has
35 * given access mode. Result of these tests is stored in @_result.
36 *
37 * @param filepath Path to the file that should be checked.
38 * @param uid Expected UID. Use (uid_t)-1 to allow any user.
39 * @param gid Expected GID. Use (gid_t)-1 to allow any group.
40 * @param access_mode Expected access mode.
41 *
42 * @param _result True if all conditions were met, false otherwise.
43 *
44 * @return On success, EOK is returned and @_result contains result of the
45 * tests. Other errno code is returned on failure.
46 */
47 errno_t
48 file_is_regular(const char *filepath,
49 uid_t uid,
50 gid_t gid,
51 mode_t access_mode,
52 bool *_result);
53
54 /**
55 * Check that @linkpath is a link to @destpath.
56 *
57 * The result is stored in @_result.
58 *
59 * @param linkpath Path to the file that should be checked.
60 * @param destpath Expected link destination.
61 *
62 * @param _result True if all conditions were met, false otherwise.
63 *
64 * @return On success, EOK is returned and @_result contains result of the
65 * tests. Other errno code is returned on failure.
66 */
67 errno_t
68 file_links_to(const char *linkpath,
69 const char *destpath,
70 bool *_result);
71
72 /**
73 * Check that @linkpath is not a link or it does not link to @destpath.
74 *
75 * The result is stored in @_result.
76 *
77 * @param linkpath Path to the file that should be checked.
78 * @param destpath Destination to which the link must not point.
79 * @param error_mode If true, ERROR is yielded instead of INFO.
80 *
81 * @param _result True if all conditions were met, false otherwise.
82 *
83 * @return On success, EOK is returned and @_result contains result of the
84 * tests. Other errno code is returned on failure.
85 */
86 errno_t
87 file_does_not_link_to(const char *linkpath,
88 const char *destpath,
89 bool error_mode,
90 bool *_result);
91
92 /**
93 * Check file access mode.
94 *
95 * @param path Path to the file that should be checked.
96 * @param mode Desired mode. See access(): R_OK, W_OK, X_OK, F_OK
97 *
98 * @return EOK if the file can be access with desired mode. ENOENT if the file
99 * does not exist. Other errno code on error.
100 */
101 errno_t
102 file_check_access(const char *path, int mode);
103
104 /**
105 * Check if file exists.
106 *
107 * @param path Path to the file that should be checked.
108 *
109 * @return EOK if the file exists. ENOENT if the file does not exist.
110 * Other errno code on error.
111 */
112 errno_t
113 file_exists(const char *path);
114
115 /**
116 * Return file basename from path.
117 *
118 * @param filepath Path to the file.
119 *
120 * @return Basename or NULL if none is present in @filepath.
121 */
122 const char *
123 file_get_basename(const char *filepath);
124
125 /**
126 * Return parent directory of a file.
127 *
128 * @param path Path to the file.
129 *
130 * @return Parent directory of the file or NULL on error.
131 */
132 char *
133 file_get_parent_directory(const char *filepath);
134
135 /**
136 * Create all directories in a path. Path must end with a directory not a file.
137 *
138 * @param path Path to the file whose directories should be created.
139 * @param mode Directory mode.
140 *
141 * @return EOK on success, other errno code on error.
142 */
143 errno_t
144 file_make_path(const char *path, mode_t mode);
145
146 /**
147 * Make temporary file for @path so it can be first written and then safely
148 * renamed to @path.
149 *
150 * @param path Path to the file whose directories should be created.
151 * @param mode Temporary file mode.
152 * @param _tmpfile Path to created temporary file.
153 */
154 errno_t
155 file_mktmp_for(const char *path, mode_t mode, char **_tmpfile);
156
157 /**
158 * Make copy of a file @source and store it in temporary file
159 * @destdir/@destname.XXXXXX keeping its owner and permissions.
160 *
161 * @param source Source file name.
162 * @param destdir Destination directory.
163 * @param destname Destination file name.
164 * @param dir_mode Access mode of destination directory if it is created.
165 * @param _tmpfile Path to created temporary file.
166 */
167 errno_t
168 file_mktmp_copy(const char *source,
169 const char *destdir,
170 const char *destname,
171 mode_t dir_mode,
172 char **_tmpfile);
173
174 /**
175 * Copy file to destination. Directory is created if it does not exist.
176 * The original owner and permissions of the source file are kept.
177 *
178 * @param source Source file name.
179 * @param destdir Destination directory.
180 * @param destname Destination file name.
181 * @param dir_mode Access mode of destination directory if it is created.
182 *
183 * @return EOK on success, other errno code on error.
184 */
185 errno_t
186 file_copy(const char *source,
187 const char *destdir,
188 const char *destname,
189 mode_t dir_mode);
190
191 #endif /* _FILE_H_ */