2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_futils_h__
8 #define INCLUDE_futils_h__
22 * Read whole files into an in-memory buffer for processing
24 extern int git_futils_readbuffer(git_buf
*obj
, const char *path
);
25 extern int git_futils_readbuffer_updated(
26 git_buf
*obj
, const char *path
, git_oid
*checksum
, int *updated
);
27 extern int git_futils_readbuffer_fd(git_buf
*obj
, git_file fd
, size_t len
);
29 /* Additional constants for `git_futils_writebuffer`'s `open_flags`. We
30 * support these internally and they will be removed before the `open` call.
33 # define O_FSYNC (1 << 31)
36 extern int git_futils_writebuffer(
37 const git_buf
*buf
, const char *path
, int open_flags
, mode_t mode
);
42 * These are custom filesystem-related helper methods. They are
43 * rather high level, and wrap the underlying POSIX methods
45 * All these methods return 0 on success,
46 * or an error code on failure and an error message is set.
50 * Create and open a file, while also
51 * creating all the folders in its path
53 extern int git_futils_creat_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
56 * Create and open a process-locked file
58 extern int git_futils_creat_locked(const char *path
, const mode_t mode
);
61 * Create and open a process-locked file, while
62 * also creating all the folders in its path
64 extern int git_futils_creat_locked_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
67 * Create a path recursively.
69 extern int git_futils_mkdir_r(const char *path
, const mode_t mode
);
72 * Flags to pass to `git_futils_mkdir`.
74 * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists.
75 * * GIT_MKDIR_PATH says to make all components in the path.
76 * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation
77 * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path
78 * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path
79 * * GIT_MKDIR_SKIP_LAST2 says to leave off the last 2 elements of the path
80 * * GIT_MKDIR_VERIFY_DIR says confirm final item is a dir, not just EEXIST
81 * * GIT_MKDIR_REMOVE_FILES says to remove files and recreate dirs
82 * * GIT_MKDIR_REMOVE_SYMLINKS says to remove symlinks and recreate dirs
84 * Note that the chmod options will be executed even if the directory already
85 * exists, unless GIT_MKDIR_EXCL is given.
91 GIT_MKDIR_CHMOD_PATH
= 8,
92 GIT_MKDIR_SKIP_LAST
= 16,
93 GIT_MKDIR_SKIP_LAST2
= 32,
94 GIT_MKDIR_VERIFY_DIR
= 64,
95 GIT_MKDIR_REMOVE_FILES
= 128,
96 GIT_MKDIR_REMOVE_SYMLINKS
= 256,
97 } git_futils_mkdir_flags
;
99 struct git_futils_mkdir_perfdata
106 struct git_futils_mkdir_options
110 struct git_futils_mkdir_perfdata perfdata
;
114 * Create a directory or entire path.
116 * This makes a directory (and the entire path leading up to it if requested),
117 * and optionally chmods the directory immediately after (or each part of the
118 * path if requested).
120 * @param path The path to create, relative to base.
121 * @param base Root for relative path. These directories will never be made.
122 * @param mode The mode to use for created directories.
123 * @param flags Combination of the mkdir flags above.
124 * @param opts Extended options, or null.
125 * @return 0 on success, else error code
127 extern int git_futils_mkdir_relative(const char *path
, const char *base
, mode_t mode
, uint32_t flags
, struct git_futils_mkdir_options
*opts
);
130 * Create a directory or entire path. Similar to `git_futils_mkdir_relative`
131 * without performance data.
133 extern int git_futils_mkdir(const char *path
, mode_t mode
, uint32_t flags
);
136 * Create all the folders required to contain
137 * the full path of a file
139 extern int git_futils_mkpath2file(const char *path
, const mode_t mode
);
142 * Flags to pass to `git_futils_rmdir_r`.
144 * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty
145 * dirs and generate error if any files are found.
146 * * GIT_RMDIR_REMOVE_FILES - attempt to remove files in the hierarchy.
147 * * GIT_RMDIR_SKIP_NONEMPTY - skip non-empty directories with no error.
148 * * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base
149 * if removing this item leaves them empty
150 * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
151 * * GIT_RMDIR_SKIP_ROOT - don't remove root directory itself
154 GIT_RMDIR_EMPTY_HIERARCHY
= 0,
155 GIT_RMDIR_REMOVE_FILES
= (1 << 0),
156 GIT_RMDIR_SKIP_NONEMPTY
= (1 << 1),
157 GIT_RMDIR_EMPTY_PARENTS
= (1 << 2),
158 GIT_RMDIR_REMOVE_BLOCKERS
= (1 << 3),
159 GIT_RMDIR_SKIP_ROOT
= (1 << 4),
160 } git_futils_rmdir_flags
;
163 * Remove path and any files and directories beneath it.
165 * @param path Path to the top level directory to process.
166 * @param base Root for relative path.
167 * @param flags Combination of git_futils_rmdir_flags values
168 * @return 0 on success; -1 on error.
170 extern int git_futils_rmdir_r(const char *path
, const char *base
, uint32_t flags
);
173 * Create and open a temporary file with a `_git2_` suffix.
174 * Writes the filename into path_out.
175 * @return On success, an open file descriptor, else an error code < 0.
177 extern int git_futils_mktmp(git_buf
*path_out
, const char *filename
, mode_t mode
);
180 * Move a file on the filesystem, create the
181 * destination path if it doesn't exist
183 extern int git_futils_mv_withpath(const char *from
, const char *to
, const mode_t dirmode
);
188 * The filemode will be used for the newly created file.
190 extern int git_futils_cp(
196 * Set the files atime and mtime to the given time, or the current time
199 extern int git_futils_touch(const char *path
, time_t *when
);
202 * Flags that can be passed to `git_futils_cp_r`.
204 * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no
205 * files under them (otherwise directories will only be created lazily
206 * when a file inside them is copied).
207 * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored.
208 * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored.
209 * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content,
210 * otherwise they are silently skipped.
211 * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode`
212 * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the
213 * source file to the target; with this flag, always use 0666 (or 0777 if
214 * source has exec bits set) for target.
215 * - GIT_CPDIR_LINK_FILES will try to use hardlinks for the files
218 GIT_CPDIR_CREATE_EMPTY_DIRS
= (1u << 0),
219 GIT_CPDIR_COPY_SYMLINKS
= (1u << 1),
220 GIT_CPDIR_COPY_DOTFILES
= (1u << 2),
221 GIT_CPDIR_OVERWRITE
= (1u << 3),
222 GIT_CPDIR_CHMOD_DIRS
= (1u << 4),
223 GIT_CPDIR_SIMPLE_TO_MODE
= (1u << 5),
224 GIT_CPDIR_LINK_FILES
= (1u << 6),
225 } git_futils_cpdir_flags
;
228 * Copy a directory tree.
230 * This copies directories and files from one root to another. You can
231 * pass a combinationof GIT_CPDIR flags as defined above.
233 * If you pass the CHMOD flag, then the dirmode will be applied to all
234 * directories that are created during the copy, overiding the natural
235 * permissions. If you do not pass the CHMOD flag, then the dirmode
236 * will actually be copied from the source files and the `dirmode` arg
239 extern int git_futils_cp_r(
246 * Open a file readonly and set error if needed.
248 extern int git_futils_open_ro(const char *path
);
251 * Truncate a file, creating it if it doesn't exist.
253 extern int git_futils_truncate(const char *path
, int mode
);
256 * Get the filesize in bytes of a file
258 extern int git_futils_filesize(uint64_t *out
, git_file fd
);
260 #define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0111) != 0)
261 #define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
262 #define GIT_PERMS_FOR_WRITE(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0777 : 0666)
264 #define GIT_MODE_PERMS_MASK 0777
265 #define GIT_MODE_TYPE_MASK 0170000
266 #define GIT_MODE_TYPE(MODE) ((MODE) & GIT_MODE_TYPE_MASK)
267 #define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
270 * Convert a mode_t from the OS to a legal git mode_t value.
272 extern mode_t
git_futils_canonical_mode(mode_t raw_mode
);
276 * Read-only map all or part of a file into memory.
277 * When possible this function should favor a virtual memory
278 * style mapping over some form of malloc()+read(), as the
279 * data access will be random and is not likely to touch the
280 * majority of the region requested.
282 * @param out buffer to populate with the mapping information.
283 * @param fd open descriptor to configure the mapping from.
284 * @param begin first byte to map, this should be page aligned.
285 * @param len number of bytes to map.
290 extern int git_futils_mmap_ro(
297 * Read-only map an entire file.
299 * @param out buffer to populate with the mapping information.
300 * @param path path to file to be opened.
303 * - GIT_ENOTFOUND if not found;
304 * - -1 on an unspecified OS related error.
306 extern int git_futils_mmap_ro_file(
311 * Release the memory associated with a previous memory mapping.
312 * @param map the mapping description previously configured.
314 extern void git_futils_mmap_free(git_map
*map
);
317 * Create a "fake" symlink (text file containing the target path).
319 * @param target original symlink target
320 * @param path symlink file to be created
321 * @return 0 on success, -1 on error
323 extern int git_futils_fake_symlink(const char *target
, const char *path
);
326 * A file stamp represents a snapshot of information about a file that can
327 * be used to test if the file changes. This portable implementation is
328 * based on stat data about that file, but it is possible that OS specific
329 * versions could be implemented in the future.
332 struct timespec mtime
;
335 } git_futils_filestamp
;
338 * Compare stat information for file with reference info.
340 * This function updates the file stamp to current data for the given path
341 * and returns 0 if the file is up-to-date relative to the prior setting,
342 * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't
343 * exist. This will not call git_error_set, so you must set the error if you
344 * plan to return an error.
346 * @param stamp File stamp to be checked
347 * @param path Path to stat and check if changed
348 * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat
350 extern int git_futils_filestamp_check(
351 git_futils_filestamp
*stamp
, const char *path
);
354 * Set or reset file stamp data
356 * This writes the target file stamp. If the source is NULL, this will set
357 * the target stamp to values that will definitely be out of date. If the
358 * source is not NULL, this copies the source values to the target.
360 * @param tgt File stamp to write to
361 * @param src File stamp to copy from or NULL to clear the target
363 extern void git_futils_filestamp_set(
364 git_futils_filestamp
*tgt
, const git_futils_filestamp
*src
);
367 * Set file stamp data from stat structure
369 extern void git_futils_filestamp_set_from_stat(
370 git_futils_filestamp
*stamp
, struct stat
*st
);
373 * `fsync` the parent directory of the given path, if `fsync` is
374 * supported for directories on this platform.
376 * @param path Path of the directory to sync.
377 * @return 0 on success, -1 on error
379 extern int git_futils_fsync_dir(const char *path
);
382 * `fsync` the parent directory of the given path, if `fsync` is
383 * supported for directories on this platform.
385 * @param path Path of the file whose parent directory should be synced.
386 * @return 0 on success, -1 on error
388 extern int git_futils_fsync_parent(const char *path
);