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_fileops_h__
8 #define INCLUDE_fileops_h__
18 * Read whole files into an in-memory buffer for processing
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
);
25 extern int git_futils_writebuffer(
26 const git_buf
*buf
, const char *path
, int open_flags
, mode_t mode
);
31 * These are custom filesystem-related helper methods. They are
32 * rather high level, and wrap the underlying POSIX methods
34 * All these methods return 0 on success,
35 * or an error code on failure and an error message is set.
39 * Create and open a file, while also
40 * creating all the folders in its path
42 extern int git_futils_creat_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
45 * Create an open a process-locked file
47 extern int git_futils_creat_locked(const char *path
, const mode_t mode
);
50 * Create an open a process-locked file, while
51 * also creating all the folders in its path
53 extern int git_futils_creat_locked_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
56 * Create a path recursively
58 * If a base parameter is being passed, it's expected to be valued with a
59 * path pointing to an already existing directory.
61 extern int git_futils_mkdir_r(const char *path
, const char *base
, const mode_t mode
);
64 * Flags to pass to `git_futils_mkdir`.
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
74 * Note that the chmod options will be executed even if the directory already
75 * exists, unless GIT_MKDIR_EXCL is given.
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
;
88 * Create a directory or entire path.
90 * This makes a directory (and the entire path leading up to it if requested),
91 * and optionally chmods the directory immediately after (or each part of the
94 * @param path The path to create.
95 * @param base Root for relative path. These directories will never be made.
96 * @param mode The mode to use for created directories.
97 * @param flags Combination of the mkdir flags above.
98 * @return 0 on success, else error code
100 extern int git_futils_mkdir(const char *path
, const char *base
, mode_t mode
, uint32_t flags
);
103 * Create all the folders required to contain
104 * the full path of a file
106 extern int git_futils_mkpath2file(const char *path
, const mode_t mode
);
109 * Flags to pass to `git_futils_rmdir_r`.
111 * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty
112 * dirs and generate error if any files are found.
113 * * GIT_RMDIR_REMOVE_FILES - attempt to remove files in the hierarchy.
114 * * GIT_RMDIR_SKIP_NONEMPTY - skip non-empty directories with no error.
115 * * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base
116 * if removing this item leaves them empty
117 * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
118 * * GIT_RMDIR_SKIP_ROOT - don't remove root directory itself
121 GIT_RMDIR_EMPTY_HIERARCHY
= 0,
122 GIT_RMDIR_REMOVE_FILES
= (1 << 0),
123 GIT_RMDIR_SKIP_NONEMPTY
= (1 << 1),
124 GIT_RMDIR_EMPTY_PARENTS
= (1 << 2),
125 GIT_RMDIR_REMOVE_BLOCKERS
= (1 << 3),
126 GIT_RMDIR_SKIP_ROOT
= (1 << 4),
127 } git_futils_rmdir_flags
;
130 * Remove path and any files and directories beneath it.
132 * @param path Path to the top level directory to process.
133 * @param base Root for relative path.
134 * @param flags Combination of git_futils_rmdir_flags values
135 * @return 0 on success; -1 on error.
137 extern int git_futils_rmdir_r(const char *path
, const char *base
, uint32_t flags
);
140 * Create and open a temporary file with a `_git2_` suffix.
141 * Writes the filename into path_out.
142 * @return On success, an open file descriptor, else an error code < 0.
144 extern int git_futils_mktmp(git_buf
*path_out
, const char *filename
, mode_t mode
);
147 * Move a file on the filesystem, create the
148 * destination path if it doesn't exist
150 extern int git_futils_mv_withpath(const char *from
, const char *to
, const mode_t dirmode
);
155 * The filemode will be used for the newly created file.
157 extern int git_futils_cp(
163 * Flags that can be passed to `git_futils_cp_r`.
165 * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no
166 * files under them (otherwise directories will only be created lazily
167 * when a file inside them is copied).
168 * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored.
169 * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored.
170 * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content,
171 * otherwise they are silently skipped.
172 * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode`
173 * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the
174 * source file to the target; with this flag, always use 0666 (or 0777 if
175 * source has exec bits set) for target.
178 GIT_CPDIR_CREATE_EMPTY_DIRS
= (1u << 0),
179 GIT_CPDIR_COPY_SYMLINKS
= (1u << 1),
180 GIT_CPDIR_COPY_DOTFILES
= (1u << 2),
181 GIT_CPDIR_OVERWRITE
= (1u << 3),
182 GIT_CPDIR_CHMOD_DIRS
= (1u << 4),
183 GIT_CPDIR_SIMPLE_TO_MODE
= (1u << 5),
184 } git_futils_cpdir_flags
;
187 * Copy a directory tree.
189 * This copies directories and files from one root to another. You can
190 * pass a combinationof GIT_CPDIR flags as defined above.
192 * If you pass the CHMOD flag, then the dirmode will be applied to all
193 * directories that are created during the copy, overiding the natural
194 * permissions. If you do not pass the CHMOD flag, then the dirmode
195 * will actually be copied from the source files and the `dirmode` arg
198 extern int git_futils_cp_r(
205 * Open a file readonly and set error if needed.
207 extern int git_futils_open_ro(const char *path
);
210 * Get the filesize in bytes of a file
212 extern git_off_t
git_futils_filesize(git_file fd
);
214 #define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0111) != 0)
215 #define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
216 #define GIT_PERMS_FOR_WRITE(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0777 : 0666)
218 #define GIT_MODE_PERMS_MASK 0777
219 #define GIT_MODE_TYPE_MASK 0170000
220 #define GIT_MODE_TYPE(MODE) ((MODE) & GIT_MODE_TYPE_MASK)
221 #define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
224 * Convert a mode_t from the OS to a legal git mode_t value.
226 extern mode_t
git_futils_canonical_mode(mode_t raw_mode
);
230 * Read-only map all or part of a file into memory.
231 * When possible this function should favor a virtual memory
232 * style mapping over some form of malloc()+read(), as the
233 * data access will be random and is not likely to touch the
234 * majority of the region requested.
236 * @param out buffer to populate with the mapping information.
237 * @param fd open descriptor to configure the mapping from.
238 * @param begin first byte to map, this should be page aligned.
239 * @param len number of bytes to map.
244 extern int git_futils_mmap_ro(
251 * Read-only map an entire file.
253 * @param out buffer to populate with the mapping information.
254 * @param path path to file to be opened.
257 * - GIT_ENOTFOUND if not found;
258 * - -1 on an unspecified OS related error.
260 extern int git_futils_mmap_ro_file(
265 * Release the memory associated with a previous memory mapping.
266 * @param map the mapping description previously configured.
268 extern void git_futils_mmap_free(git_map
*map
);
271 * Create a "fake" symlink (text file containing the target path).
273 * @param new symlink file to be created
274 * @param old original symlink target
275 * @return 0 on success, -1 on error
277 extern int git_futils_fake_symlink(const char *new, const char *old
);
280 * A file stamp represents a snapshot of information about a file that can
281 * be used to test if the file changes. This portable implementation is
282 * based on stat data about that file, but it is possible that OS specific
283 * versions could be implemented in the future.
289 } git_futils_filestamp
;
292 * Compare stat information for file with reference info.
294 * This function updates the file stamp to current data for the given path
295 * and returns 0 if the file is up-to-date relative to the prior setting or
296 * 1 if the file has been changed. (This also may return GIT_ENOTFOUND if
297 * the file doesn't exist.)
299 * @param stamp File stamp to be checked
300 * @param path Path to stat and check if changed
301 * @return 0 if up-to-date, 1 if out-of-date, <0 on error
303 extern int git_futils_filestamp_check(
304 git_futils_filestamp
*stamp
, const char *path
);
307 * Set or reset file stamp data
309 * This writes the target file stamp. If the source is NULL, this will set
310 * the target stamp to values that will definitely be out of date. If the
311 * source is not NULL, this copies the source values to the target.
313 * @param tgt File stamp to write to
314 * @param src File stamp to copy from or NULL to clear the target
316 extern void git_futils_filestamp_set(
317 git_futils_filestamp
*tgt
, const git_futils_filestamp
*src
);
319 #endif /* INCLUDE_fileops_h__ */