Blame src/fileops.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_fileops_h__
Packit Service 20376f
#define INCLUDE_fileops_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "map.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "pool.h"
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Filebuffer methods
Packit Service 20376f
 *
Packit Service 20376f
 * Read whole files into an in-memory buffer for processing
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_readbuffer(git_buf *obj, const char *path);
Packit Service 20376f
extern int git_futils_readbuffer_updated(
Packit Service 20376f
	git_buf *obj, const char *path, git_oid *checksum, int *updated);
Packit Service 20376f
extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len);
Packit Service 20376f
Packit Service 20376f
/* Additional constants for `git_futils_writebuffer`'s `open_flags`.  We
Packit Service 20376f
 * support these internally and they will be removed before the `open` call.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef O_FSYNC
Packit Service 20376f
# define O_FSYNC (1 << 31)
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
extern int git_futils_writebuffer(
Packit Service 20376f
	const git_buf *buf, const char *path, int open_flags, mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * File utils
Packit Service 20376f
 *
Packit Service 20376f
 * These are custom filesystem-related helper methods. They are
Packit Service 20376f
 * rather high level, and wrap the underlying POSIX methods
Packit Service 20376f
 *
Packit Service 20376f
 * All these methods return 0 on success,
Packit Service 20376f
 * or an error code on failure and an error message is set.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create and open a file, while also
Packit Service 20376f
 * creating all the folders in its path
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create and open a process-locked file
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_creat_locked(const char *path, const mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create and open a process-locked file, while
Packit Service 20376f
 * also creating all the folders in its path
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create a path recursively.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mkdir_r(const char *path, const mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Flags to pass to `git_futils_mkdir`.
Packit Service 20376f
 *
Packit Service 20376f
 * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists.
Packit Service 20376f
 * * GIT_MKDIR_PATH says to make all components in the path.
Packit Service 20376f
 * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation
Packit Service 20376f
 * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path
Packit Service 20376f
 * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path
Packit Service 20376f
 * * GIT_MKDIR_SKIP_LAST2 says to leave off the last 2 elements of the path
Packit Service 20376f
 * * GIT_MKDIR_VERIFY_DIR says confirm final item is a dir, not just EEXIST
Packit Service 20376f
 * * GIT_MKDIR_REMOVE_FILES says to remove files and recreate dirs
Packit Service 20376f
 * * GIT_MKDIR_REMOVE_SYMLINKS says to remove symlinks and recreate dirs
Packit Service 20376f
 *
Packit Service 20376f
 * Note that the chmod options will be executed even if the directory already
Packit Service 20376f
 * exists, unless GIT_MKDIR_EXCL is given.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_MKDIR_EXCL = 1,
Packit Service 20376f
	GIT_MKDIR_PATH = 2,
Packit Service 20376f
	GIT_MKDIR_CHMOD = 4,
Packit Service 20376f
	GIT_MKDIR_CHMOD_PATH = 8,
Packit Service 20376f
	GIT_MKDIR_SKIP_LAST = 16,
Packit Service 20376f
	GIT_MKDIR_SKIP_LAST2 = 32,
Packit Service 20376f
	GIT_MKDIR_VERIFY_DIR = 64,
Packit Service 20376f
	GIT_MKDIR_REMOVE_FILES = 128,
Packit Service 20376f
	GIT_MKDIR_REMOVE_SYMLINKS = 256,
Packit Service 20376f
} git_futils_mkdir_flags;
Packit Service 20376f
Packit Service 20376f
struct git_futils_mkdir_perfdata
Packit Service 20376f
{
Packit Service 20376f
	size_t stat_calls;
Packit Service 20376f
	size_t mkdir_calls;
Packit Service 20376f
	size_t chmod_calls;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct git_futils_mkdir_options
Packit Service 20376f
{
Packit Service 20376f
	git_strmap *dir_map;
Packit Service 20376f
	git_pool *pool;
Packit Service 20376f
	struct git_futils_mkdir_perfdata perfdata;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create a directory or entire path.
Packit Service 20376f
 *
Packit Service 20376f
 * This makes a directory (and the entire path leading up to it if requested),
Packit Service 20376f
 * and optionally chmods the directory immediately after (or each part of the
Packit Service 20376f
 * path if requested).
Packit Service 20376f
 *
Packit Service 20376f
 * @param path The path to create, relative to base.
Packit Service 20376f
 * @param base Root for relative path.  These directories will never be made.
Packit Service 20376f
 * @param mode The mode to use for created directories.
Packit Service 20376f
 * @param flags Combination of the mkdir flags above.
Packit Service 20376f
 * @param opts Extended options, or null.
Packit Service 20376f
 * @return 0 on success, else error code
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mkdir_relative(const char *path, const char *base, mode_t mode, uint32_t flags, struct git_futils_mkdir_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create a directory or entire path.  Similar to `git_futils_mkdir_relative`
Packit Service 20376f
 * without performance data.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mkdir(const char *path, mode_t mode, uint32_t flags);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create all the folders required to contain
Packit Service 20376f
 * the full path of a file
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mkpath2file(const char *path, const mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Flags to pass to `git_futils_rmdir_r`.
Packit Service 20376f
 *
Packit Service 20376f
 * * GIT_RMDIR_EMPTY_HIERARCHY - the default; remove hierarchy of empty
Packit Service 20376f
 *       dirs and generate error if any files are found.
Packit Service 20376f
 * * GIT_RMDIR_REMOVE_FILES    - attempt to remove files in the hierarchy.
Packit Service 20376f
 * * GIT_RMDIR_SKIP_NONEMPTY   - skip non-empty directories with no error.
Packit Service 20376f
 * * GIT_RMDIR_EMPTY_PARENTS   - remove containing directories up to base
Packit Service 20376f
 *       if removing this item leaves them empty
Packit Service 20376f
 * * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
Packit Service 20376f
 * * GIT_RMDIR_SKIP_ROOT       - don't remove root directory itself
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_RMDIR_EMPTY_HIERARCHY = 0,
Packit Service 20376f
	GIT_RMDIR_REMOVE_FILES    = (1 << 0),
Packit Service 20376f
	GIT_RMDIR_SKIP_NONEMPTY   = (1 << 1),
Packit Service 20376f
	GIT_RMDIR_EMPTY_PARENTS   = (1 << 2),
Packit Service 20376f
	GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3),
Packit Service 20376f
	GIT_RMDIR_SKIP_ROOT       = (1 << 4),
Packit Service 20376f
} git_futils_rmdir_flags;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Remove path and any files and directories beneath it.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path Path to the top level directory to process.
Packit Service 20376f
 * @param base Root for relative path.
Packit Service 20376f
 * @param flags Combination of git_futils_rmdir_flags values
Packit Service 20376f
 * @return 0 on success; -1 on error.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create and open a temporary file with a `_git2_` suffix.
Packit Service 20376f
 * Writes the filename into path_out.
Packit Service 20376f
 * @return On success, an open file descriptor, else an error code < 0.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Move a file on the filesystem, create the
Packit Service 20376f
 * destination path if it doesn't exist
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Copy a file
Packit Service 20376f
 *
Packit Service 20376f
 * The filemode will be used for the newly created file.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_cp(
Packit Service 20376f
	const char *from,
Packit Service 20376f
	const char *to,
Packit Service 20376f
	mode_t filemode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set the files atime and mtime to the given time, or the current time
Packit Service 20376f
 * if `ts` is NULL.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_touch(const char *path, time_t *when);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Flags that can be passed to `git_futils_cp_r`.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CPDIR_CREATE_EMPTY_DIRS: create directories even if there are no
Packit Service 20376f
 *   files under them (otherwise directories will only be created lazily
Packit Service 20376f
 *   when a file inside them is copied).
Packit Service 20376f
 * - GIT_CPDIR_COPY_SYMLINKS: copy symlinks, otherwise they are ignored.
Packit Service 20376f
 * - GIT_CPDIR_COPY_DOTFILES: copy files with leading '.', otherwise ignored.
Packit Service 20376f
 * - GIT_CPDIR_OVERWRITE: overwrite pre-existing files with source content,
Packit Service 20376f
 *   otherwise they are silently skipped.
Packit Service 20376f
 * - GIT_CPDIR_CHMOD_DIRS: explicitly chmod directories to `dirmode`
Packit Service 20376f
 * - GIT_CPDIR_SIMPLE_TO_MODE: default tries to replicate the mode of the
Packit Service 20376f
 *   source file to the target; with this flag, always use 0666 (or 0777 if
Packit Service 20376f
 *   source has exec bits set) for target.
Packit Service 20376f
 * - GIT_CPDIR_LINK_FILES will try to use hardlinks for the files
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_CPDIR_CREATE_EMPTY_DIRS = (1u << 0),
Packit Service 20376f
	GIT_CPDIR_COPY_SYMLINKS     = (1u << 1),
Packit Service 20376f
	GIT_CPDIR_COPY_DOTFILES     = (1u << 2),
Packit Service 20376f
	GIT_CPDIR_OVERWRITE         = (1u << 3),
Packit Service 20376f
	GIT_CPDIR_CHMOD_DIRS        = (1u << 4),
Packit Service 20376f
	GIT_CPDIR_SIMPLE_TO_MODE    = (1u << 5),
Packit Service 20376f
	GIT_CPDIR_LINK_FILES        = (1u << 6),
Packit Service 20376f
} git_futils_cpdir_flags;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Copy a directory tree.
Packit Service 20376f
 *
Packit Service 20376f
 * This copies directories and files from one root to another.  You can
Packit Service 20376f
 * pass a combinationof GIT_CPDIR flags as defined above.
Packit Service 20376f
 *
Packit Service 20376f
 * If you pass the CHMOD flag, then the dirmode will be applied to all
Packit Service 20376f
 * directories that are created during the copy, overiding the natural
Packit Service 20376f
 * permissions.  If you do not pass the CHMOD flag, then the dirmode
Packit Service 20376f
 * will actually be copied from the source files and the `dirmode` arg
Packit Service 20376f
 * will be ignored.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_cp_r(
Packit Service 20376f
	const char *from,
Packit Service 20376f
	const char *to,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	mode_t dirmode);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Open a file readonly and set error if needed.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_open_ro(const char *path);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Get the filesize in bytes of a file
Packit Service 20376f
 */
Packit Service 20376f
extern git_off_t git_futils_filesize(git_file fd);
Packit Service 20376f
Packit Service 20376f
#define GIT_PERMS_IS_EXEC(MODE)		(((MODE) & 0111) != 0)
Packit Service 20376f
#define GIT_PERMS_CANONICAL(MODE)	(GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
Packit Service 20376f
#define GIT_PERMS_FOR_WRITE(MODE)   (GIT_PERMS_IS_EXEC(MODE) ? 0777 : 0666)
Packit Service 20376f
Packit Service 20376f
#define GIT_MODE_PERMS_MASK			0777
Packit Service 20376f
#define GIT_MODE_TYPE_MASK			0170000
Packit Service 20376f
#define GIT_MODE_TYPE(MODE)			((MODE) & GIT_MODE_TYPE_MASK)
Packit Service 20376f
#define GIT_MODE_ISBLOB(MODE)		(GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Convert a mode_t from the OS to a legal git mode_t value.
Packit Service 20376f
 */
Packit Service 20376f
extern mode_t git_futils_canonical_mode(mode_t raw_mode);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Read-only map all or part of a file into memory.
Packit Service 20376f
 * When possible this function should favor a virtual memory
Packit Service 20376f
 * style mapping over some form of malloc()+read(), as the
Packit Service 20376f
 * data access will be random and is not likely to touch the
Packit Service 20376f
 * majority of the region requested.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out buffer to populate with the mapping information.
Packit Service 20376f
 * @param fd open descriptor to configure the mapping from.
Packit Service 20376f
 * @param begin first byte to map, this should be page aligned.
Packit Service 20376f
 * @param len number of bytes to map.
Packit Service 20376f
 * @return
Packit Service 20376f
 * - 0 on success;
Packit Service 20376f
 * - -1 on error.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mmap_ro(
Packit Service 20376f
	git_map *out,
Packit Service 20376f
	git_file fd,
Packit Service 20376f
	git_off_t begin,
Packit Service 20376f
	size_t len);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Read-only map an entire file.
Packit Service 20376f
 *
Packit Service 20376f
 * @param out buffer to populate with the mapping information.
Packit Service 20376f
 * @param path path to file to be opened.
Packit Service 20376f
 * @return
Packit Service 20376f
 * - 0 on success;
Packit Service 20376f
 * - GIT_ENOTFOUND if not found;
Packit Service 20376f
 * - -1 on an unspecified OS related error.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_mmap_ro_file(
Packit Service 20376f
	git_map *out,
Packit Service 20376f
	const char *path);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Release the memory associated with a previous memory mapping.
Packit Service 20376f
 * @param map the mapping description previously configured.
Packit Service 20376f
 */
Packit Service 20376f
extern void git_futils_mmap_free(git_map *map);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Create a "fake" symlink (text file containing the target path).
Packit Service 20376f
 *
Packit Service 20376f
 * @param new symlink file to be created
Packit Service 20376f
 * @param old original symlink target
Packit Service 20376f
 * @return 0 on success, -1 on error
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_fake_symlink(const char *new, const char *old);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * A file stamp represents a snapshot of information about a file that can
Packit Service 20376f
 * be used to test if the file changes.  This portable implementation is
Packit Service 20376f
 * based on stat data about that file, but it is possible that OS specific
Packit Service 20376f
 * versions could be implemented in the future.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct {
Packit Service 20376f
	struct timespec mtime;
Packit Service 20376f
	git_off_t  size;
Packit Service 20376f
	unsigned int ino;
Packit Service 20376f
} git_futils_filestamp;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Compare stat information for file with reference info.
Packit Service 20376f
 *
Packit Service 20376f
 * This function updates the file stamp to current data for the given path
Packit Service 20376f
 * and returns 0 if the file is up-to-date relative to the prior setting,
Packit Service 20376f
 * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't
Packit Service 20376f
 * exist.  This will not call giterr_set, so you must set the error if you
Packit Service 20376f
 * plan to return an error.
Packit Service 20376f
 *
Packit Service 20376f
 * @param stamp File stamp to be checked
Packit Service 20376f
 * @param path Path to stat and check if changed
Packit Service 20376f
 * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_filestamp_check(
Packit Service 20376f
	git_futils_filestamp *stamp, const char *path);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set or reset file stamp data
Packit Service 20376f
 *
Packit Service 20376f
 * This writes the target file stamp.  If the source is NULL, this will set
Packit Service 20376f
 * the target stamp to values that will definitely be out of date.  If the
Packit Service 20376f
 * source is not NULL, this copies the source values to the target.
Packit Service 20376f
 *
Packit Service 20376f
 * @param tgt File stamp to write to
Packit Service 20376f
 * @param src File stamp to copy from or NULL to clear the target
Packit Service 20376f
 */
Packit Service 20376f
extern void git_futils_filestamp_set(
Packit Service 20376f
	git_futils_filestamp *tgt, const git_futils_filestamp *src);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set file stamp data from stat structure
Packit Service 20376f
 */
Packit Service 20376f
extern void git_futils_filestamp_set_from_stat(
Packit Service 20376f
	git_futils_filestamp *stamp, struct stat *st);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * `fsync` the parent directory of the given path, if `fsync` is
Packit Service 20376f
 * supported for directories on this platform.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path Path of the directory to sync.
Packit Service 20376f
 * @return 0 on success, -1 on error
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_fsync_dir(const char *path);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * `fsync` the parent directory of the given path, if `fsync` is
Packit Service 20376f
 * supported for directories on this platform.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path Path of the file whose parent directory should be synced.
Packit Service 20376f
 * @return 0 on success, -1 on error
Packit Service 20376f
 */
Packit Service 20376f
extern int git_futils_fsync_parent(const char *path);
Packit Service 20376f
Packit Service 20376f
#endif /* INCLUDE_fileops_h__ */