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