]>
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__
16 #define GIT_PLATFORM_PATH_SEP '\\'
18 #define GIT_PLATFORM_PATH_SEP '/'
21 #define S_IFGITLINK 0160000
22 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
25 GIT_INLINE(int) link(const char *GIT_UNUSED(old
), const char *GIT_UNUSED(new))
33 GIT_INLINE(int) git__mkdir(const char *path
, int GIT_UNUSED(mode
))
39 extern int git__unlink(const char *path
);
40 extern int git__mkstemp(char *template);
41 extern int git__fsync(int fd
);
43 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
44 # define unlink(p) git__unlink(p)
45 # define mkstemp(t) git__mkstemp(t)
46 # define mkdir(p,m) git__mkdir(p, m)
47 # define fsync(fd) git__fsync(fd)
49 #endif /* GIT_WIN32 */
52 #if !defined(O_BINARY)
56 #define GITFO_BUF_INIT {NULL, 0}
60 typedef struct { /* file io buffer */
61 void *data
; /* data bytes */
62 size_t len
; /* data length */
65 extern int gitfo_exists(const char *path
);
66 extern int gitfo_open(const char *path
, int flags
);
67 extern int gitfo_creat(const char *path
, int mode
);
68 extern int gitfo_creat_force(const char *path
, int mode
);
69 extern int gitfo_creat_locked(const char *path
, int mode
);
70 extern int gitfo_creat_locked_force(const char *path
, int mode
);
71 extern int gitfo_mktemp(char *path_out
, const char *filename
);
72 extern int gitfo_isdir(const char *path
);
73 extern int gitfo_isfile(const char *path
);
74 extern int gitfo_mkdir_recurs(const char *path
, int mode
);
75 extern int gitfo_mkdir_2file(const char *path
);
76 #define gitfo_close(fd) close(fd)
78 extern int gitfo_read(git_file fd
, void *buf
, size_t cnt
);
79 extern int gitfo_write(git_file fd
, void *buf
, size_t cnt
);
80 #define gitfo_lseek(f,n,w) lseek(f, n, w)
81 extern git_off_t
gitfo_size(git_file fd
);
83 extern int gitfo_read_file(gitfo_buf
*obj
, const char *path
);
84 extern void gitfo_free_buf(gitfo_buf
*obj
);
86 /* Move (rename) a file; this operation is atomic */
87 extern int gitfo_mv(const char *from
, const char *to
);
89 /* Move a file (forced); this will create the destination
90 * path if it doesn't exist */
91 extern int gitfo_mv_force(const char *from
, const char *to
);
93 #define gitfo_stat(p,b) stat(p, b)
94 #define gitfo_fstat(f,b) fstat(f, b)
97 # define gitfo_lstat(p,b) gitfo_lstat__w32(p,b)
98 # define gitfo_readlink(a, b, c) gitfo_readlink__w32(a, b, c)
100 extern int gitfo_lstat__w32(const char *file_name
, struct stat
*buf
);
101 extern int gitfo_readlink__w32(const char *link
, char *target
, size_t target_len
);
103 # define gitfo_lstat(p,b) lstat(p,b)
104 # define gitfo_readlink(a, b, c) readlink(a, b, c)
107 #define gitfo_unlink(p) unlink(p)
108 #define gitfo_rmdir(p) rmdir(p)
109 #define gitfo_chdir(p) chdir(p)
110 #define gitfo_mkdir(p,m) mkdir(p, m)
112 #define gitfo_mkstemp(t) mkstemp(t)
113 #define gitfo_fsync(fd) fsync(fd)
114 #define gitfo_chmod(p,m) chmod(p, m)
117 * Read-only map all or part of a file into memory.
118 * When possible this function should favor a virtual memory
119 * style mapping over some form of malloc()+read(), as the
120 * data access will be random and is not likely to touch the
121 * majority of the region requested.
123 * @param out buffer to populate with the mapping information.
124 * @param fd open descriptor to configure the mapping from.
125 * @param begin first byte to map, this should be page aligned.
126 * @param end number of bytes to map.
128 * - GIT_SUCCESS on success;
129 * - GIT_EOSERR on an unspecified OS related error.
131 extern int gitfo_map_ro(
138 * Release the memory associated with a previous memory mapping.
139 * @param map the mapping description previously configured.
141 extern void gitfo_free_map(git_map
*map
);
144 * Walk each directory entry, except '.' and '..', calling fn(state).
146 * @param pathbuf buffer the function reads the initial directory
147 * path from, and updates with each successive entry's name.
148 * @param pathmax maximum allocation of pathbuf.
149 * @param fn function to invoke with each entry. The first arg is
150 * the input state and the second arg is pathbuf. The function
151 * may modify the pathbuf, but only by appending new text.
152 * @param state to pass to fn as the first arg.
154 extern int gitfo_dirent(
157 int (*fn
)(void *, char *),
160 extern int gitfo_cmp_path(const char *name1
, int len1
, int isdir1
,
161 const char *name2
, int len2
, int isdir2
);
163 extern int gitfo_getcwd(char *buffer_out
, size_t size
);
166 * Clean up a provided absolute or relative directory path.
168 * This prettification relies on basic operations such as coalescing
169 * multiple forward slashes into a single slash, removing '.' and
170 * './' current directory segments, and removing parent directory
171 * whenever '..' is encountered.
173 * If not empty, the returned path ends with a forward slash.
175 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
177 * This only performs a string based analysis of the path.
178 * No checks are done to make sure the path actually makes sense from
179 * the file system perspective.
181 * @param buffer_out buffer to populate with the normalized path.
182 * @param size buffer size.
183 * @param path directory path to clean up.
185 * - GIT_SUCCESS on success;
186 * - GIT_ERROR when the input path is invalid or escapes the current directory.
188 int gitfo_prettify_dir_path(char *buffer_out
, size_t size
, const char *path
, const char *base_path
);
191 * Clean up a provided absolute or relative file path.
193 * This prettification relies on basic operations such as coalescing
194 * multiple forward slashes into a single slash, removing '.' and
195 * './' current directory segments, and removing parent directory
196 * whenever '..' is encountered.
198 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
200 * This only performs a string based analysis of the path.
201 * No checks are done to make sure the path actually makes sense from
202 * the file system perspective.
204 * @param buffer_out buffer to populate with the normalized path.
205 * @param size buffer size.
206 * @param path file path to clean up.
208 * - GIT_SUCCESS on success;
209 * - GIT_ERROR when the input path is invalid or escapes the current directory.
211 int gitfo_prettify_file_path(char *buffer_out
, size_t size
, const char *path
, const char *base_path
);
213 void gitfo_posixify_path(char *path
);
215 int gitfo_retrieve_path_root_offset(const char *path
);
217 #endif /* INCLUDE_fileops_h__ */