fileops.h 5.85 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * fileops.h - OS agnostic disk io operations
 *
 * This header describes the strictly internal part of the api
 */
#ifndef INCLUDE_fileops_h__
#define INCLUDE_fileops_h__

9
#include "common.h"
10
#include "map.h"
11
#include "dir.h"
12 13 14
#include <fcntl.h>
#include <time.h>

15
#ifdef GIT_WIN32
16
GIT_INLINE(int) link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
17
{
18 19
	GIT_UNUSED_ARG(old)
	GIT_UNUSED_ARG(new)
20 21 22 23
	errno = ENOSYS;
	return -1;
}

24
GIT_INLINE(int) git__mkdir(const char *path, int GIT_UNUSED(mode))
25
{
26
	GIT_UNUSED_ARG(mode)
27 28 29
	return mkdir(path);
}

30 31 32
extern int git__unlink(const char *path);
extern int git__mkstemp(char *template);
extern int git__fsync(int fd);
33 34 35 36

# ifndef GIT__WIN32_NO_HIDE_FILEOPS
#  define unlink(p) git__unlink(p)
#  define mkstemp(t) git__mkstemp(t)
37
#  define mkdir(p,m) git__mkdir(p, m)
38
#  define fsync(fd) git__fsync(fd)
39 40 41 42
# endif
#endif  /* GIT_WIN32 */


43 44 45 46
#if !defined(O_BINARY)
#define O_BINARY 0
#endif

47 48
#define GITFO_BUF_INIT {NULL, 0}

49 50
typedef int git_file;

51 52 53 54 55
typedef struct {  /* file io buffer  */
	void *data;  /* data bytes   */
	size_t len;  /* data length  */
} gitfo_buf;

56 57 58
extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
extern int gitfo_creat(const char *path, int mode);
59
extern int gitfo_creat_force(const char *path, int mode);
60
extern int gitfo_mktemp(char *path_out, const char *filename);
61 62
extern int gitfo_isdir(const char *path);
extern int gitfo_mkdir_recurs(const char *path, int mode);
Vicent Marti committed
63
extern int gitfo_mkdir_2file(const char *path);
64 65
#define gitfo_close(fd) close(fd)

66 67
extern int gitfo_read(git_file fd, void *buf, size_t cnt);
extern int gitfo_write(git_file fd, void *buf, size_t cnt);
68
#define gitfo_lseek(f,n,w) lseek(f, n, w)
69
extern git_off_t gitfo_size(git_file fd);
70

71 72
extern int gitfo_read_file(gitfo_buf *obj, const char *path);
extern void gitfo_free_buf(gitfo_buf *obj);
73 74 75 76 77 78 79

/* Move (rename) a file; this operation is atomic */
extern int gitfo_mv(const char *from, const char *to);

/* Move a file (forced); this will create the destination
 * path if it doesn't exist */
extern int gitfo_mv_force(const char *from, const char *to);
80

81 82 83
#define gitfo_stat(p,b) stat(p, b)
#define gitfo_fstat(f,b) fstat(f, b)

84 85
#define gitfo_unlink(p) unlink(p)
#define gitfo_rmdir(p) rmdir(p)
86
#define gitfo_chdir(p) chdir(p)
87
#define gitfo_mkdir(p,m) mkdir(p, m)
88 89 90 91

#define gitfo_mkstemp(t) mkstemp(t)
#define gitfo_fsync(fd) fsync(fd)
#define gitfo_chmod(p,m) chmod(p, m)
92

93
/**
94 95 96 97 98 99 100 101 102 103 104 105 106 107
 * Read-only map all or part of a file into memory.
 * When possible this function should favor a virtual memory
 * style mapping over some form of malloc()+read(), as the
 * data access will be random and is not likely to touch the
 * majority of the region requested.
 *
 * @param out buffer to populate with the mapping information.
 * @param fd open descriptor to configure the mapping from.
 * @param begin first byte to map, this should be page aligned.
 * @param end number of bytes to map.
 * @return
 * - GIT_SUCCESS on success;
 * - GIT_EOSERR on an unspecified OS related error.
 */
108
extern int gitfo_map_ro(
109
	git_map *out,
110
	git_file fd,
111
	git_off_t begin,
112 113 114 115 116 117
	size_t len);

/**
 * Release the memory associated with a previous memory mapping.
 * @param map the mapping description previously configured.
 */
118
extern void gitfo_free_map(git_map *map);
119 120

/**
121 122 123 124 125 126 127 128 129 130
 * Walk each directory entry, except '.' and '..', calling fn(state).
 *
 * @param pathbuf buffer the function reads the initial directory
 * 		path from, and updates with each successive entry's name.
 * @param pathmax maximum allocation of pathbuf.
 * @param fn function to invoke with each entry.  The first arg is
 *		the input state and the second arg is pathbuf.  The function
 *		may modify the pathbuf, but only by appending new text.
 * @param state to pass to fn as the first arg.
 */
131
extern int gitfo_dirent(
132 133 134 135 136
	char *pathbuf,
	size_t pathmax,
	int (*fn)(void *, char *),
	void *state);

137 138 139
extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
		const char *name2, int len2, int isdir2);

140 141
extern int gitfo_getcwd(char *buffer_out, size_t size);

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**
 * Clean up a provided absolute or relative directory path.
 * 
 * This prettification relies on basic operations such as coalescing 
 * multiple forward slashes into a single slash, removing '.' and 
 * './' current directory segments, and removing parent directory 
 * whenever '..' is encountered.
 *
 * If not empty, the returned path ends with a forward slash.
 *
 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
 *
 * This only performs a string based analysis of the path.
 * No checks are done to make sure the path actually makes sense from 
 * the file system perspective.
 *
 * @param buffer_out buffer to populate with the normalized path.
159
 * @param size buffer size.
160 161 162 163 164
 * @param path directory path to clean up.
 * @return
 * - GIT_SUCCESS on success;
 * - GIT_ERROR when the input path is invalid or escapes the current directory.
 */
165
int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path);
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/**
 * Clean up a provided absolute or relative file path.
 * 
 * This prettification relies on basic operations such as coalescing 
 * multiple forward slashes into a single slash, removing '.' and 
 * './' current directory segments, and removing parent directory 
 * whenever '..' is encountered.
 *
 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
 *
 * This only performs a string based analysis of the path.
 * No checks are done to make sure the path actually makes sense from 
 * the file system perspective.
 *
 * @param buffer_out buffer to populate with the normalized path.
182
 * @param size buffer size.
183 184 185 186 187
 * @param path file path to clean up.
 * @return
 * - GIT_SUCCESS on success;
 * - GIT_ERROR when the input path is invalid or escapes the current directory.
 */
188
int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path);
189

190
int retrieve_path_root_offset(const char *path);
191
#endif /* INCLUDE_fileops_h__ */