]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
Merge pull request #3491 from libgit2/cmn/config-checksum
[libgit2.git] / src / fileops.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_fileops_h__
8 #define INCLUDE_fileops_h__
9
10 #include "common.h"
11 #include "map.h"
12 #include "posix.h"
13 #include "path.h"
14 #include "pool.h"
15 #include "strmap.h"
16 #include "oid.h"
17
18 /**
19 * Filebuffer methods
20 *
21 * Read whole files into an in-memory buffer for processing
22 */
23 extern int git_futils_readbuffer(git_buf *obj, const char *path);
24 extern int git_futils_readbuffer_updated(
25 git_buf *obj, const char *path, git_oid *checksum, int *updated);
26 extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len);
27
28 extern int git_futils_writebuffer(
29 const git_buf *buf, const char *path, int open_flags, mode_t mode);
30
31 /**
32 * File utils
33 *
34 * These are custom filesystem-related helper methods. They are
35 * rather high level, and wrap the underlying POSIX methods
36 *
37 * All these methods return 0 on success,
38 * or an error code on failure and an error message is set.
39 */
40
41 /**
42 * Create and open a file, while also
43 * creating all the folders in its path
44 */
45 extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode);
46
47 /**
48 * Create an open a process-locked file
49 */
50 extern int git_futils_creat_locked(const char *path, const mode_t mode);
51
52 /**
53 * Create an open a process-locked file, while
54 * also creating all the folders in its path
55 */
56 extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode);
57
58 /**
59 * Create a path recursively.
60 */
61 extern int git_futils_mkdir_r(const char *path, const mode_t mode);
62
63 /**
64 * Flags to pass to `git_futils_mkdir`.
65 *
66 * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists.
67 * * GIT_MKDIR_PATH says to make all components in the path.
68 * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation
69 * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path
70 * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path
71 * * GIT_MKDIR_SKIP_LAST2 says to leave off the last 2 elements of the path
72 * * GIT_MKDIR_VERIFY_DIR says confirm final item is a dir, not just EEXIST
73 * * GIT_MKDIR_REMOVE_FILES says to remove files and recreate dirs
74 * * GIT_MKDIR_REMOVE_SYMLINKS says to remove symlinks and recreate dirs
75 *
76 * Note that the chmod options will be executed even if the directory already
77 * exists, unless GIT_MKDIR_EXCL is given.
78 */
79 typedef enum {
80 GIT_MKDIR_EXCL = 1,
81 GIT_MKDIR_PATH = 2,
82 GIT_MKDIR_CHMOD = 4,
83 GIT_MKDIR_CHMOD_PATH = 8,
84 GIT_MKDIR_SKIP_LAST = 16,
85 GIT_MKDIR_SKIP_LAST2 = 32,
86 GIT_MKDIR_VERIFY_DIR = 64,
87 GIT_MKDIR_REMOVE_FILES = 128,
88 GIT_MKDIR_REMOVE_SYMLINKS = 256,
89 } git_futils_mkdir_flags;
90
91 struct git_futils_mkdir_perfdata
92 {
93 size_t stat_calls;
94 size_t mkdir_calls;
95 size_t chmod_calls;
96 };
97
98 struct git_futils_mkdir_options
99 {
100 git_strmap *dir_map;
101 git_pool *pool;
102 struct git_futils_mkdir_perfdata perfdata;
103 };
104
105 /**
106 * Create a directory or entire path.
107 *
108 * This makes a directory (and the entire path leading up to it if requested),
109 * and optionally chmods the directory immediately after (or each part of the
110 * path if requested).
111 *
112 * @param path The path to create, relative to base.
113 * @param base Root for relative path. These directories will never be made.
114 * @param mode The mode to use for created directories.
115 * @param flags Combination of the mkdir flags above.
116 * @param opts Extended options, or null.
117 * @return 0 on success, else error code
118 */
119 extern int git_futils_mkdir_relative(const char *path, const char *base, mode_t mode, uint32_t flags, struct git_futils_mkdir_options *opts);
120
121 /**
122 * Create a directory or entire path. Similar to `git_futils_mkdir_relative`
123 * without performance data.
124 */
125 extern int git_futils_mkdir(const char *path, mode_t mode, uint32_t flags);
126
127 /**
128 * Create all the folders required to contain
129 * the full path of a file
130 */
131 extern int git_futils_mkpath2file(const char *path, const mode_t mode);
132
133 /**
134 * Flags to pass to `git_futils_rmdir_r`.
135 *
136 * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty
137 * dirs and generate error if any files are found.
138 * * GIT_RMDIR_REMOVE_FILES - attempt to remove files in the hierarchy.
139 * * GIT_RMDIR_SKIP_NONEMPTY - skip non-empty directories with no error.
140 * * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base
141 * if removing this item leaves them empty
142 * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
143 * * GIT_RMDIR_SKIP_ROOT - don't remove root directory itself
144 */
145 typedef enum {
146 GIT_RMDIR_EMPTY_HIERARCHY = 0,
147 GIT_RMDIR_REMOVE_FILES = (1 << 0),
148 GIT_RMDIR_SKIP_NONEMPTY = (1 << 1),
149 GIT_RMDIR_EMPTY_PARENTS = (1 << 2),
150 GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3),
151 GIT_RMDIR_SKIP_ROOT = (1 << 4),
152 } git_futils_rmdir_flags;
153
154 /**
155 * Remove path and any files and directories beneath it.
156 *
157 * @param path Path to the top level directory to process.
158 * @param base Root for relative path.
159 * @param flags Combination of git_futils_rmdir_flags values
160 * @return 0 on success; -1 on error.
161 */
162 extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags);
163
164 /**
165 * Create and open a temporary file with a `_git2_` suffix.
166 * Writes the filename into path_out.
167 * @return On success, an open file descriptor, else an error code < 0.
168 */
169 extern int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode);
170
171 /**
172 * Move a file on the filesystem, create the
173 * destination path if it doesn't exist
174 */
175 extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
176
177 /**
178 * Copy a file
179 *
180 * The filemode will be used for the newly created file.
181 */
182 extern int git_futils_cp(
183 const char *from,
184 const char *to,
185 mode_t filemode);
186
187 /**
188 * Flags that can be passed to `git_futils_cp_r`.
189 *
190 * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no
191 * files under them (otherwise directories will only be created lazily
192 * when a file inside them is copied).
193 * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored.
194 * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored.
195 * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content,
196 * otherwise they are silently skipped.
197 * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode`
198 * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the
199 * source file to the target; with this flag, always use 0666 (or 0777 if
200 * source has exec bits set) for target.
201 * - GIT_CPDIR_LINK_FILES will try to use hardlinks for the files
202 */
203 typedef enum {
204 GIT_CPDIR_CREATE_EMPTY_DIRS = (1u << 0),
205 GIT_CPDIR_COPY_SYMLINKS = (1u << 1),
206 GIT_CPDIR_COPY_DOTFILES = (1u << 2),
207 GIT_CPDIR_OVERWRITE = (1u << 3),
208 GIT_CPDIR_CHMOD_DIRS = (1u << 4),
209 GIT_CPDIR_SIMPLE_TO_MODE = (1u << 5),
210 GIT_CPDIR_LINK_FILES = (1u << 6),
211 } git_futils_cpdir_flags;
212
213 /**
214 * Copy a directory tree.
215 *
216 * This copies directories and files from one root to another. You can
217 * pass a combinationof GIT_CPDIR flags as defined above.
218 *
219 * If you pass the CHMOD flag, then the dirmode will be applied to all
220 * directories that are created during the copy, overiding the natural
221 * permissions. If you do not pass the CHMOD flag, then the dirmode
222 * will actually be copied from the source files and the `dirmode` arg
223 * will be ignored.
224 */
225 extern int git_futils_cp_r(
226 const char *from,
227 const char *to,
228 uint32_t flags,
229 mode_t dirmode);
230
231 /**
232 * Open a file readonly and set error if needed.
233 */
234 extern int git_futils_open_ro(const char *path);
235
236 /**
237 * Get the filesize in bytes of a file
238 */
239 extern git_off_t git_futils_filesize(git_file fd);
240
241 #define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0111) != 0)
242 #define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
243 #define GIT_PERMS_FOR_WRITE(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0777 : 0666)
244
245 #define GIT_MODE_PERMS_MASK 0777
246 #define GIT_MODE_TYPE_MASK 0170000
247 #define GIT_MODE_TYPE(MODE) ((MODE) & GIT_MODE_TYPE_MASK)
248 #define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
249
250 /**
251 * Convert a mode_t from the OS to a legal git mode_t value.
252 */
253 extern mode_t git_futils_canonical_mode(mode_t raw_mode);
254
255
256 /**
257 * Read-only map all or part of a file into memory.
258 * When possible this function should favor a virtual memory
259 * style mapping over some form of malloc()+read(), as the
260 * data access will be random and is not likely to touch the
261 * majority of the region requested.
262 *
263 * @param out buffer to populate with the mapping information.
264 * @param fd open descriptor to configure the mapping from.
265 * @param begin first byte to map, this should be page aligned.
266 * @param len number of bytes to map.
267 * @return
268 * - 0 on success;
269 * - -1 on error.
270 */
271 extern int git_futils_mmap_ro(
272 git_map *out,
273 git_file fd,
274 git_off_t begin,
275 size_t len);
276
277 /**
278 * Read-only map an entire file.
279 *
280 * @param out buffer to populate with the mapping information.
281 * @param path path to file to be opened.
282 * @return
283 * - 0 on success;
284 * - GIT_ENOTFOUND if not found;
285 * - -1 on an unspecified OS related error.
286 */
287 extern int git_futils_mmap_ro_file(
288 git_map *out,
289 const char *path);
290
291 /**
292 * Release the memory associated with a previous memory mapping.
293 * @param map the mapping description previously configured.
294 */
295 extern void git_futils_mmap_free(git_map *map);
296
297 /**
298 * Create a "fake" symlink (text file containing the target path).
299 *
300 * @param new symlink file to be created
301 * @param old original symlink target
302 * @return 0 on success, -1 on error
303 */
304 extern int git_futils_fake_symlink(const char *new, const char *old);
305
306 /**
307 * A file stamp represents a snapshot of information about a file that can
308 * be used to test if the file changes. This portable implementation is
309 * based on stat data about that file, but it is possible that OS specific
310 * versions could be implemented in the future.
311 */
312 typedef struct {
313 git_time_t mtime;
314 git_off_t size;
315 unsigned int ino;
316 } git_futils_filestamp;
317
318 /**
319 * Compare stat information for file with reference info.
320 *
321 * This function updates the file stamp to current data for the given path
322 * and returns 0 if the file is up-to-date relative to the prior setting,
323 * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't
324 * exist. This will not call giterr_set, so you must set the error if you
325 * plan to return an error.
326 *
327 * @param stamp File stamp to be checked
328 * @param path Path to stat and check if changed
329 * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat
330 */
331 extern int git_futils_filestamp_check(
332 git_futils_filestamp *stamp, const char *path);
333
334 /**
335 * Set or reset file stamp data
336 *
337 * This writes the target file stamp. If the source is NULL, this will set
338 * the target stamp to values that will definitely be out of date. If the
339 * source is not NULL, this copies the source values to the target.
340 *
341 * @param tgt File stamp to write to
342 * @param src File stamp to copy from or NULL to clear the target
343 */
344 extern void git_futils_filestamp_set(
345 git_futils_filestamp *tgt, const git_futils_filestamp *src);
346
347 /**
348 * Set file stamp data from stat structure
349 */
350 extern void git_futils_filestamp_set_from_stat(
351 git_futils_filestamp *stamp, struct stat *st);
352
353 #endif /* INCLUDE_fileops_h__ */