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