]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
Merge pull request #234 from Romain-Geissler/entry-count-API-uniformisation
[libgit2.git] / src / fileops.h
1 /*
2 * fileops.h - OS agnostic disk io operations
3 *
4 * This header describes the strictly internal part of the api
5 */
6 #ifndef INCLUDE_fileops_h__
7 #define INCLUDE_fileops_h__
8
9 #include "common.h"
10 #include "map.h"
11 #include "dir.h"
12 #include <fcntl.h>
13 #include <time.h>
14
15 #define GIT_PATH_LIST_SEPARATOR ':'
16
17 #ifdef GIT_WIN32
18 #define GIT_PLATFORM_PATH_SEP '\\'
19 #else
20 #define GIT_PLATFORM_PATH_SEP '/'
21 #endif
22
23 #ifdef GIT_WIN32
24 GIT_INLINE(int) link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
25 {
26 GIT_UNUSED_ARG(old)
27 GIT_UNUSED_ARG(new)
28 errno = ENOSYS;
29 return -1;
30 }
31
32 GIT_INLINE(int) git__mkdir(const char *path, int GIT_UNUSED(mode))
33 {
34 GIT_UNUSED_ARG(mode)
35 return mkdir(path);
36 }
37
38 extern int git__unlink(const char *path);
39 extern int git__mkstemp(char *template);
40 extern int git__fsync(int fd);
41
42 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
43 # define unlink(p) git__unlink(p)
44 # define mkstemp(t) git__mkstemp(t)
45 # define mkdir(p,m) git__mkdir(p, m)
46 # define fsync(fd) git__fsync(fd)
47 # endif
48 #endif /* GIT_WIN32 */
49
50
51 #if !defined(O_BINARY)
52 #define O_BINARY 0
53 #endif
54
55 #define GITFO_BUF_INIT {NULL, 0}
56
57 typedef int git_file;
58
59 typedef struct { /* file io buffer */
60 void *data; /* data bytes */
61 size_t len; /* data length */
62 } gitfo_buf;
63
64 extern int gitfo_exists(const char *path);
65 extern int gitfo_open(const char *path, int flags);
66 extern int gitfo_creat(const char *path, int mode);
67 extern int gitfo_creat_force(const char *path, int mode);
68 extern int gitfo_creat_locked(const char *path, int mode);
69 extern int gitfo_creat_locked_force(const char *path, int mode);
70 extern int gitfo_mktemp(char *path_out, const char *filename);
71 extern int gitfo_isdir(const char *path);
72 extern int gitfo_isfile(const char *path);
73 extern int gitfo_mkdir_recurs(const char *path, int mode);
74 extern int gitfo_mkdir_2file(const char *path);
75 #define gitfo_close(fd) close(fd)
76
77 extern int gitfo_read(git_file fd, void *buf, size_t cnt);
78 extern int gitfo_write(git_file fd, void *buf, size_t cnt);
79 #define gitfo_lseek(f,n,w) lseek(f, n, w)
80 extern git_off_t gitfo_size(git_file fd);
81
82 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
83 extern void gitfo_free_buf(gitfo_buf *obj);
84
85 /* Move (rename) a file; this operation is atomic */
86 extern int gitfo_mv(const char *from, const char *to);
87
88 /* Move a file (forced); this will create the destination
89 * path if it doesn't exist */
90 extern int gitfo_mv_force(const char *from, const char *to);
91
92 #define gitfo_stat(p,b) stat(p, b)
93 #define gitfo_fstat(f,b) fstat(f, b)
94
95 #define gitfo_unlink(p) unlink(p)
96 #define gitfo_rmdir(p) rmdir(p)
97 #define gitfo_chdir(p) chdir(p)
98 #define gitfo_mkdir(p,m) mkdir(p, m)
99
100 #define gitfo_mkstemp(t) mkstemp(t)
101 #define gitfo_fsync(fd) fsync(fd)
102 #define gitfo_chmod(p,m) chmod(p, m)
103
104 /**
105 * Read-only map all or part of a file into memory.
106 * When possible this function should favor a virtual memory
107 * style mapping over some form of malloc()+read(), as the
108 * data access will be random and is not likely to touch the
109 * majority of the region requested.
110 *
111 * @param out buffer to populate with the mapping information.
112 * @param fd open descriptor to configure the mapping from.
113 * @param begin first byte to map, this should be page aligned.
114 * @param end number of bytes to map.
115 * @return
116 * - GIT_SUCCESS on success;
117 * - GIT_EOSERR on an unspecified OS related error.
118 */
119 extern int gitfo_map_ro(
120 git_map *out,
121 git_file fd,
122 git_off_t begin,
123 size_t len);
124
125 /**
126 * Release the memory associated with a previous memory mapping.
127 * @param map the mapping description previously configured.
128 */
129 extern void gitfo_free_map(git_map *map);
130
131 /**
132 * Walk each directory entry, except '.' and '..', calling fn(state).
133 *
134 * @param pathbuf buffer the function reads the initial directory
135 * path from, and updates with each successive entry's name.
136 * @param pathmax maximum allocation of pathbuf.
137 * @param fn function to invoke with each entry. The first arg is
138 * the input state and the second arg is pathbuf. The function
139 * may modify the pathbuf, but only by appending new text.
140 * @param state to pass to fn as the first arg.
141 */
142 extern int gitfo_dirent(
143 char *pathbuf,
144 size_t pathmax,
145 int (*fn)(void *, char *),
146 void *state);
147
148 extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
149 const char *name2, int len2, int isdir2);
150
151 extern int gitfo_getcwd(char *buffer_out, size_t size);
152
153 /**
154 * Clean up a provided absolute or relative directory path.
155 *
156 * This prettification relies on basic operations such as coalescing
157 * multiple forward slashes into a single slash, removing '.' and
158 * './' current directory segments, and removing parent directory
159 * whenever '..' is encountered.
160 *
161 * If not empty, the returned path ends with a forward slash.
162 *
163 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
164 *
165 * This only performs a string based analysis of the path.
166 * No checks are done to make sure the path actually makes sense from
167 * the file system perspective.
168 *
169 * @param buffer_out buffer to populate with the normalized path.
170 * @param size buffer size.
171 * @param path directory path to clean up.
172 * @return
173 * - GIT_SUCCESS on success;
174 * - GIT_ERROR when the input path is invalid or escapes the current directory.
175 */
176 int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path);
177
178 /**
179 * Clean up a provided absolute or relative file path.
180 *
181 * This prettification relies on basic operations such as coalescing
182 * multiple forward slashes into a single slash, removing '.' and
183 * './' current directory segments, and removing parent directory
184 * whenever '..' is encountered.
185 *
186 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
187 *
188 * This only performs a string based analysis of the path.
189 * No checks are done to make sure the path actually makes sense from
190 * the file system perspective.
191 *
192 * @param buffer_out buffer to populate with the normalized path.
193 * @param size buffer size.
194 * @param path file path to clean up.
195 * @return
196 * - GIT_SUCCESS on success;
197 * - GIT_ERROR when the input path is invalid or escapes the current directory.
198 */
199 int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path);
200
201 void gitfo_posixify_path(char *path);
202
203 int gitfo_retrieve_path_root_offset(const char *path);
204
205 #endif /* INCLUDE_fileops_h__ */