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__
20 * Read whole files into an in-memory buffer for processing
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
);
27 extern int git_futils_writebuffer(
28 const git_buf
*buf
, const char *path
, int open_flags
, mode_t mode
);
33 * These are custom filesystem-related helper methods. They are
34 * rather high level, and wrap the underlying POSIX methods
36 * All these methods return 0 on success,
37 * or an error code on failure and an error message is set.
41 * Create and open a file, while also
42 * creating all the folders in its path
44 extern int git_futils_creat_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
47 * Create an open a process-locked file
49 extern int git_futils_creat_locked(const char *path
, const mode_t mode
);
52 * Create an open a process-locked file, while
53 * also creating all the folders in its path
55 extern int git_futils_creat_locked_withpath(const char *path
, const mode_t dirmode
, const mode_t mode
);
58 * Create a path recursively.
60 extern int git_futils_mkdir_r(const char *path
, const mode_t mode
);
63 * Flags to pass to `git_futils_mkdir`.
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
75 * Note that the chmod options will be executed even if the directory already
76 * exists, unless GIT_MKDIR_EXCL is given.
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
;
90 struct git_futils_mkdir_perfdata
97 struct git_futils_mkdir_options
101 struct git_futils_mkdir_perfdata perfdata
;
105 * Create a directory or entire path.
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).
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
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
);
121 * Create a directory or entire path. Similar to `git_futils_mkdir_relative`
122 * without performance data.
124 extern int git_futils_mkdir(const char *path
, mode_t mode
, uint32_t flags
);
127 * Create all the folders required to contain
128 * the full path of a file
130 extern int git_futils_mkpath2file(const char *path
, const mode_t mode
);
133 * Flags to pass to `git_futils_rmdir_r`.
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
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
;
154 * Remove path and any files and directories beneath it.
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.
161 extern int git_futils_rmdir_r(const char *path
, const char *base
, uint32_t flags
);
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.
168 extern int git_futils_mktmp(git_buf
*path_out
, const char *filename
, mode_t mode
);
171 * Move a file on the filesystem, create the
172 * destination path if it doesn't exist
174 extern int git_futils_mv_withpath(const char *from
, const char *to
, const mode_t dirmode
);
179 * The filemode will be used for the newly created file.
181 extern int git_futils_cp(
187 * Flags that can be passed to `git_futils_cp_r`.
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
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
;
213 * Copy a directory tree.
215 * This copies directories and files from one root to another. You can
216 * pass a combinationof GIT_CPDIR flags as defined above.
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
224 extern int git_futils_cp_r(
231 * Open a file readonly and set error if needed.
233 extern int git_futils_open_ro(const char *path
);
236 * Get the filesize in bytes of a file
238 extern git_off_t
git_futils_filesize(git_file fd
);
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)
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))
250 * Convert a mode_t from the OS to a legal git mode_t value.
252 extern mode_t
git_futils_canonical_mode(mode_t raw_mode
);
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.
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.
270 extern int git_futils_mmap_ro(
277 * Read-only map an entire file.
279 * @param out buffer to populate with the mapping information.
280 * @param path path to file to be opened.
283 * - GIT_ENOTFOUND if not found;
284 * - -1 on an unspecified OS related error.
286 extern int git_futils_mmap_ro_file(
291 * Release the memory associated with a previous memory mapping.
292 * @param map the mapping description previously configured.
294 extern void git_futils_mmap_free(git_map
*map
);
297 * Create a "fake" symlink (text file containing the target path).
299 * @param new symlink file to be created
300 * @param old original symlink target
301 * @return 0 on success, -1 on error
303 extern int git_futils_fake_symlink(const char *new, const char *old
);
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.
315 } git_futils_filestamp
;
318 * Compare stat information for file with reference info.
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.
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
330 extern int git_futils_filestamp_check(
331 git_futils_filestamp
*stamp
, const char *path
);
334 * Set or reset file stamp data
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.
340 * @param tgt File stamp to write to
341 * @param src File stamp to copy from or NULL to clear the target
343 extern void git_futils_filestamp_set(
344 git_futils_filestamp
*tgt
, const git_futils_filestamp
*src
);
347 * Set file stamp data from stat structure
349 extern void git_futils_filestamp_set_from_stat(
350 git_futils_filestamp
*stamp
, struct stat
*st
);
352 #endif /* INCLUDE_fileops_h__ */