]>
git.proxmox.com Git - libgit2.git/blob - src/fileops.h
2 * fileops.h - OS agnostic disk io operations
4 * This header describes the strictly internal part of the api
6 #ifndef INCLUDE_fileops_h__
7 #define INCLUDE_fileops_h__
15 #define GIT_PATH_LIST_SEPARATOR ':'
18 #define GIT_PLATFORM_PATH_SEP '\\'
20 #define GIT_PLATFORM_PATH_SEP '/'
23 #define S_IFGITLINK 0160000
24 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
27 GIT_INLINE(int) link(const char *GIT_UNUSED(old
), const char *GIT_UNUSED(new))
35 GIT_INLINE(int) git__mkdir(const char *path
, int GIT_UNUSED(mode
))
41 extern int git__unlink(const char *path
);
42 extern int git__mkstemp(char *template);
43 extern int git__fsync(int fd
);
45 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
46 # define unlink(p) git__unlink(p)
47 # define mkstemp(t) git__mkstemp(t)
48 # define mkdir(p,m) git__mkdir(p, m)
49 # define fsync(fd) git__fsync(fd)
51 #endif /* GIT_WIN32 */
54 #if !defined(O_BINARY)
58 #define GITFO_BUF_INIT {NULL, 0}
62 typedef struct { /* file io buffer */
63 void *data
; /* data bytes */
64 size_t len
; /* data length */
67 extern int gitfo_exists(const char *path
);
68 extern int gitfo_open(const char *path
, int flags
);
69 extern int gitfo_creat(const char *path
, int mode
);
70 extern int gitfo_creat_force(const char *path
, int mode
);
71 extern int gitfo_creat_locked(const char *path
, int mode
);
72 extern int gitfo_creat_locked_force(const char *path
, int mode
);
73 extern int gitfo_mktemp(char *path_out
, const char *filename
);
74 extern int gitfo_isdir(const char *path
);
75 extern int gitfo_isfile(const char *path
);
76 extern int gitfo_mkdir_recurs(const char *path
, int mode
);
77 extern int gitfo_mkdir_2file(const char *path
);
78 #define gitfo_close(fd) close(fd)
80 extern int gitfo_read(git_file fd
, void *buf
, size_t cnt
);
81 extern int gitfo_write(git_file fd
, void *buf
, size_t cnt
);
82 #define gitfo_lseek(f,n,w) lseek(f, n, w)
83 extern git_off_t
gitfo_size(git_file fd
);
85 extern int gitfo_read_file(gitfo_buf
*obj
, const char *path
);
86 extern void gitfo_free_buf(gitfo_buf
*obj
);
88 /* Move (rename) a file; this operation is atomic */
89 extern int gitfo_mv(const char *from
, const char *to
);
91 /* Move a file (forced); this will create the destination
92 * path if it doesn't exist */
93 extern int gitfo_mv_force(const char *from
, const char *to
);
95 #define gitfo_stat(p,b) stat(p, b)
96 #define gitfo_fstat(f,b) fstat(f, b)
97 #define gitfo_lstat(p,b) lstat(p,b)
99 #define gitfo_unlink(p) unlink(p)
100 #define gitfo_rmdir(p) rmdir(p)
101 #define gitfo_chdir(p) chdir(p)
102 #define gitfo_mkdir(p,m) mkdir(p, m)
104 #define gitfo_mkstemp(t) mkstemp(t)
105 #define gitfo_fsync(fd) fsync(fd)
106 #define gitfo_chmod(p,m) chmod(p, m)
109 * Read-only map all or part of a file into memory.
110 * When possible this function should favor a virtual memory
111 * style mapping over some form of malloc()+read(), as the
112 * data access will be random and is not likely to touch the
113 * majority of the region requested.
115 * @param out buffer to populate with the mapping information.
116 * @param fd open descriptor to configure the mapping from.
117 * @param begin first byte to map, this should be page aligned.
118 * @param end number of bytes to map.
120 * - GIT_SUCCESS on success;
121 * - GIT_EOSERR on an unspecified OS related error.
123 extern int gitfo_map_ro(
130 * Release the memory associated with a previous memory mapping.
131 * @param map the mapping description previously configured.
133 extern void gitfo_free_map(git_map
*map
);
136 * Walk each directory entry, except '.' and '..', calling fn(state).
138 * @param pathbuf buffer the function reads the initial directory
139 * path from, and updates with each successive entry's name.
140 * @param pathmax maximum allocation of pathbuf.
141 * @param fn function to invoke with each entry. The first arg is
142 * the input state and the second arg is pathbuf. The function
143 * may modify the pathbuf, but only by appending new text.
144 * @param state to pass to fn as the first arg.
146 extern int gitfo_dirent(
149 int (*fn
)(void *, char *),
152 extern int gitfo_cmp_path(const char *name1
, int len1
, int isdir1
,
153 const char *name2
, int len2
, int isdir2
);
155 extern int gitfo_getcwd(char *buffer_out
, size_t size
);
158 * Clean up a provided absolute or relative directory path.
160 * This prettification relies on basic operations such as coalescing
161 * multiple forward slashes into a single slash, removing '.' and
162 * './' current directory segments, and removing parent directory
163 * whenever '..' is encountered.
165 * If not empty, the returned path ends with a forward slash.
167 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
169 * This only performs a string based analysis of the path.
170 * No checks are done to make sure the path actually makes sense from
171 * the file system perspective.
173 * @param buffer_out buffer to populate with the normalized path.
174 * @param size buffer size.
175 * @param path directory path to clean up.
177 * - GIT_SUCCESS on success;
178 * - GIT_ERROR when the input path is invalid or escapes the current directory.
180 int gitfo_prettify_dir_path(char *buffer_out
, size_t size
, const char *path
, const char *base_path
);
183 * Clean up a provided absolute or relative file path.
185 * This prettification relies on basic operations such as coalescing
186 * multiple forward slashes into a single slash, removing '.' and
187 * './' current directory segments, and removing parent directory
188 * whenever '..' is encountered.
190 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
192 * This only performs a string based analysis of the path.
193 * No checks are done to make sure the path actually makes sense from
194 * the file system perspective.
196 * @param buffer_out buffer to populate with the normalized path.
197 * @param size buffer size.
198 * @param path file path to clean up.
200 * - GIT_SUCCESS on success;
201 * - GIT_ERROR when the input path is invalid or escapes the current directory.
203 int gitfo_prettify_file_path(char *buffer_out
, size_t size
, const char *path
, const char *base_path
);
205 void gitfo_posixify_path(char *path
);
207 int gitfo_retrieve_path_root_offset(const char *path
);
209 #endif /* INCLUDE_fileops_h__ */