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