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