]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
index/fileops: Correctly process symbolic links
[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 #define S_IFGITLINK 0160000
24 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
25
26 #ifdef GIT_WIN32
27 GIT_INLINE(int) link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
28 {
29 GIT_UNUSED_ARG(old)
30 GIT_UNUSED_ARG(new)
31 errno = ENOSYS;
32 return -1;
33 }
34
35 GIT_INLINE(int) git__mkdir(const char *path, int GIT_UNUSED(mode))
36 {
37 GIT_UNUSED_ARG(mode)
38 return mkdir(path);
39 }
40
41 extern int git__unlink(const char *path);
42 extern int git__mkstemp(char *template);
43 extern int git__fsync(int fd);
44
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)
50 # endif
51 #endif /* GIT_WIN32 */
52
53
54 #if !defined(O_BINARY)
55 #define O_BINARY 0
56 #endif
57
58 #define GITFO_BUF_INIT {NULL, 0}
59
60 typedef int git_file;
61
62 typedef struct { /* file io buffer */
63 void *data; /* data bytes */
64 size_t len; /* data length */
65 } gitfo_buf;
66
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)
79
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);
84
85 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
86 extern void gitfo_free_buf(gitfo_buf *obj);
87
88 /* Move (rename) a file; this operation is atomic */
89 extern int gitfo_mv(const char *from, const char *to);
90
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);
94
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)
98
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)
103
104 #define gitfo_mkstemp(t) mkstemp(t)
105 #define gitfo_fsync(fd) fsync(fd)
106 #define gitfo_chmod(p,m) chmod(p, m)
107
108 /**
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.
114 *
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.
119 * @return
120 * - GIT_SUCCESS on success;
121 * - GIT_EOSERR on an unspecified OS related error.
122 */
123 extern int gitfo_map_ro(
124 git_map *out,
125 git_file fd,
126 git_off_t begin,
127 size_t len);
128
129 /**
130 * Release the memory associated with a previous memory mapping.
131 * @param map the mapping description previously configured.
132 */
133 extern void gitfo_free_map(git_map *map);
134
135 /**
136 * Walk each directory entry, except '.' and '..', calling fn(state).
137 *
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.
145 */
146 extern int gitfo_dirent(
147 char *pathbuf,
148 size_t pathmax,
149 int (*fn)(void *, char *),
150 void *state);
151
152 extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
153 const char *name2, int len2, int isdir2);
154
155 extern int gitfo_getcwd(char *buffer_out, size_t size);
156
157 /**
158 * Clean up a provided absolute or relative directory path.
159 *
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.
164 *
165 * If not empty, the returned path ends with a forward slash.
166 *
167 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
168 *
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.
172 *
173 * @param buffer_out buffer to populate with the normalized path.
174 * @param size buffer size.
175 * @param path directory path to clean up.
176 * @return
177 * - GIT_SUCCESS on success;
178 * - GIT_ERROR when the input path is invalid or escapes the current directory.
179 */
180 int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path);
181
182 /**
183 * Clean up a provided absolute or relative file path.
184 *
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.
189 *
190 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
191 *
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.
195 *
196 * @param buffer_out buffer to populate with the normalized path.
197 * @param size buffer size.
198 * @param path file path to clean up.
199 * @return
200 * - GIT_SUCCESS on success;
201 * - GIT_ERROR when the input path is invalid or escapes the current directory.
202 */
203 int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path);
204
205 void gitfo_posixify_path(char *path);
206
207 int gitfo_retrieve_path_root_offset(const char *path);
208
209 #endif /* INCLUDE_fileops_h__ */