]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
GIT_MODE_TYPE should exclude setgid bits
[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 /**
88 * Create a directory or entire path.
89 *
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
92 * path if requested).
93 *
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
99 */
100 extern int git_futils_mkdir(const char *path, const char *base, mode_t mode, uint32_t flags);
101
102 /**
103 * Create all the folders required to contain
104 * the full path of a file
105 */
106 extern int git_futils_mkpath2file(const char *path, const mode_t mode);
107
108 /**
109 * Flags to pass to `git_futils_rmdir_r`.
110 *
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 *
119 * The old values translate into the new as follows:
120 *
121 * * GIT_DIRREMOVAL_EMPTY_HIERARCHY == GIT_RMDIR_EMPTY_HIERARCHY
122 * * GIT_DIRREMOVAL_FILES_AND_DIRS ~= GIT_RMDIR_REMOVE_FILES
123 * * GIT_DIRREMOVAL_ONLY_EMPTY_DIRS == GIT_RMDIR_SKIP_NONEMPTY
124 */
125 typedef enum {
126 GIT_RMDIR_EMPTY_HIERARCHY = 0,
127 GIT_RMDIR_REMOVE_FILES = (1 << 0),
128 GIT_RMDIR_SKIP_NONEMPTY = (1 << 1),
129 GIT_RMDIR_EMPTY_PARENTS = (1 << 2),
130 GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3),
131 } git_futils_rmdir_flags;
132
133 /**
134 * Remove path and any files and directories beneath it.
135 *
136 * @param path Path to the top level directory to process.
137 * @param base Root for relative path.
138 * @param flags Combination of git_futils_rmdir_flags values
139 * @return 0 on success; -1 on error.
140 */
141 extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags);
142
143 /**
144 * Remove all files and directories beneath the specified path.
145 *
146 * @param path Path to the top level directory to process.
147 * @return 0 on success; -1 on error.
148 */
149 extern int git_futils_cleanupdir_r(const char *path);
150
151 /**
152 * Create and open a temporary file with a `_git2_` suffix.
153 * Writes the filename into path_out.
154 * @return On success, an open file descriptor, else an error code < 0.
155 */
156 extern int git_futils_mktmp(git_buf *path_out, const char *filename);
157
158 /**
159 * Move a file on the filesystem, create the
160 * destination path if it doesn't exist
161 */
162 extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
163
164 /**
165 * Copy a file
166 *
167 * The filemode will be used for the newly created file.
168 */
169 extern int git_futils_cp(
170 const char *from,
171 const char *to,
172 mode_t filemode);
173
174 /**
175 * Flags that can be passed to `git_futils_cp_r`.
176 *
177 * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no
178 * files under them (otherwise directories will only be created lazily
179 * when a file inside them is copied).
180 * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored.
181 * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored.
182 * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content,
183 * otherwise they are silently skipped.
184 * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode`
185 * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the
186 * source file to the target; with this flag, always use 0666 (or 0777 if
187 * source has exec bits set) for target.
188 */
189 typedef enum {
190 GIT_CPDIR_CREATE_EMPTY_DIRS = (1u << 0),
191 GIT_CPDIR_COPY_SYMLINKS = (1u << 1),
192 GIT_CPDIR_COPY_DOTFILES = (1u << 2),
193 GIT_CPDIR_OVERWRITE = (1u << 3),
194 GIT_CPDIR_CHMOD_DIRS = (1u << 4),
195 GIT_CPDIR_SIMPLE_TO_MODE = (1u << 5),
196 } git_futils_cpdir_flags;
197
198 /**
199 * Copy a directory tree.
200 *
201 * This copies directories and files from one root to another. You can
202 * pass a combinationof GIT_CPDIR flags as defined above.
203 *
204 * If you pass the CHMOD flag, then the dirmode will be applied to all
205 * directories that are created during the copy, overiding the natural
206 * permissions. If you do not pass the CHMOD flag, then the dirmode
207 * will actually be copied from the source files and the `dirmode` arg
208 * will be ignored.
209 */
210 extern int git_futils_cp_r(
211 const char *from,
212 const char *to,
213 uint32_t flags,
214 mode_t dirmode);
215
216 /**
217 * Open a file readonly and set error if needed.
218 */
219 extern int git_futils_open_ro(const char *path);
220
221 /**
222 * Get the filesize in bytes of a file
223 */
224 extern git_off_t git_futils_filesize(git_file fd);
225
226 #define GIT_PERMS_EXECUTABLE(MODE) (((MODE) & 0111) != 0)
227 #define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_EXECUTABLE(MODE) ? 0755 : 0644)
228 #define GIT_PERMS_FOR_WRITE(MODE) (GIT_PERMS_EXECUTABLE(MODE) ? 0777 : 0666)
229
230 #define GIT_MODE_PERMS_MASK 0777
231 #define GIT_MODE_TYPE_MASK 0170000
232 #define GIT_MODE_TYPE(MODE) ((MODE) & GIT_MODE_TYPE_MASK)
233 #define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
234
235 /**
236 * Convert a mode_t from the OS to a legal git mode_t value.
237 */
238 extern mode_t git_futils_canonical_mode(mode_t raw_mode);
239
240
241 /**
242 * Read-only map all or part of a file into memory.
243 * When possible this function should favor a virtual memory
244 * style mapping over some form of malloc()+read(), as the
245 * data access will be random and is not likely to touch the
246 * majority of the region requested.
247 *
248 * @param out buffer to populate with the mapping information.
249 * @param fd open descriptor to configure the mapping from.
250 * @param begin first byte to map, this should be page aligned.
251 * @param len number of bytes to map.
252 * @return
253 * - 0 on success;
254 * - -1 on error.
255 */
256 extern int git_futils_mmap_ro(
257 git_map *out,
258 git_file fd,
259 git_off_t begin,
260 size_t len);
261
262 /**
263 * Read-only map an entire file.
264 *
265 * @param out buffer to populate with the mapping information.
266 * @param path path to file to be opened.
267 * @return
268 * - 0 on success;
269 * - GIT_ENOTFOUND if not found;
270 * - -1 on an unspecified OS related error.
271 */
272 extern int git_futils_mmap_ro_file(
273 git_map *out,
274 const char *path);
275
276 /**
277 * Release the memory associated with a previous memory mapping.
278 * @param map the mapping description previously configured.
279 */
280 extern void git_futils_mmap_free(git_map *map);
281
282 /**
283 * Find a "global" file (i.e. one in a user's home directory).
284 *
285 * @param path buffer to write the full path into
286 * @param filename name of file to find in the home directory
287 * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
288 */
289 extern int git_futils_find_global_file(git_buf *path, const char *filename);
290
291 /**
292 * Find an "XDG" file (i.e. one in user's XDG config path).
293 *
294 * @param path buffer to write the full path into
295 * @param filename name of file to find in the home directory
296 * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
297 */
298 extern int git_futils_find_xdg_file(git_buf *path, const char *filename);
299
300 /**
301 * Find a "system" file (i.e. one shared for all users of the system).
302 *
303 * @param path buffer to write the full path into
304 * @param filename name of file to find in the home directory
305 * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
306 */
307 extern int git_futils_find_system_file(git_buf *path, const char *filename);
308
309 typedef enum {
310 GIT_FUTILS_DIR_SYSTEM = 0,
311 GIT_FUTILS_DIR_GLOBAL = 1,
312 GIT_FUTILS_DIR_XDG = 2,
313 GIT_FUTILS_DIR__MAX = 3,
314 } git_futils_dir_t;
315
316 /**
317 * Configures global data for configuration file search paths.
318 *
319 * @return 0 on success, <0 on failure
320 */
321 extern int git_futils_dirs_global_init(void);
322
323 /**
324 * Get the search path for global/system/xdg files
325 *
326 * @param out pointer to git_buf containing search path
327 * @param which which list of paths to return
328 * @return 0 on success, <0 on failure
329 */
330 extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which);
331
332 /**
333 * Get search path into a preallocated buffer
334 *
335 * @param out String buffer to write into
336 * @param outlen Size of string buffer
337 * @param which Which search path to return
338 * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
339 */
340
341 extern int git_futils_dirs_get_str(
342 char *out, size_t outlen, git_futils_dir_t which);
343
344 /**
345 * Set search paths for global/system/xdg files
346 *
347 * The first occurrence of the magic string "$PATH" in the new value will
348 * be replaced with the old value of the search path.
349 *
350 * @param which Which search path to modify
351 * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
352 * @return 0 on success, <0 on failure (allocation error)
353 */
354 extern int git_futils_dirs_set(git_futils_dir_t which, const char *paths);
355
356 /**
357 * Release / reset all search paths
358 */
359 extern void git_futils_dirs_free(void);
360
361 /**
362 * Create a "fake" symlink (text file containing the target path).
363 *
364 * @param new symlink file to be created
365 * @param old original symlink target
366 * @return 0 on success, -1 on error
367 */
368 extern int git_futils_fake_symlink(const char *new, const char *old);
369
370 /**
371 * A file stamp represents a snapshot of information about a file that can
372 * be used to test if the file changes. This portable implementation is
373 * based on stat data about that file, but it is possible that OS specific
374 * versions could be implemented in the future.
375 */
376 typedef struct {
377 git_time_t mtime;
378 git_off_t size;
379 unsigned int ino;
380 } git_futils_filestamp;
381
382 /**
383 * Compare stat information for file with reference info.
384 *
385 * This function updates the file stamp to current data for the given path
386 * and returns 0 if the file is up-to-date relative to the prior setting or
387 * 1 if the file has been changed. (This also may return GIT_ENOTFOUND if
388 * the file doesn't exist.)
389 *
390 * @param stamp File stamp to be checked
391 * @param path Path to stat and check if changed
392 * @return 0 if up-to-date, 1 if out-of-date, <0 on error
393 */
394 extern int git_futils_filestamp_check(
395 git_futils_filestamp *stamp, const char *path);
396
397 /**
398 * Set or reset file stamp data
399 *
400 * This writes the target file stamp. If the source is NULL, this will set
401 * the target stamp to values that will definitely be out of date. If the
402 * source is not NULL, this copies the source values to the target.
403 *
404 * @param tgt File stamp to write to
405 * @param src File stamp to copy from or NULL to clear the target
406 */
407 extern void git_futils_filestamp_set(
408 git_futils_filestamp *tgt, const git_futils_filestamp *src);
409
410 #endif /* INCLUDE_fileops_h__ */