]> git.proxmox.com Git - libgit2.git/blame - src/fileops.h
pkt-line: parse other-ref lines
[libgit2.git] / src / fileops.h
CommitLineData
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
bb88da7f 15#ifdef GIT_WIN32
04fdc10d 16#define GIT_PLATFORM_PATH_SEP '\\'
bb88da7f 17#else
18#define GIT_PLATFORM_PATH_SEP '/'
04fdc10d
RG
19#endif
20
ff9a4c13
RG
21#define S_IFGITLINK 0160000
22#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
23
4319860c 24#ifdef GIT_WIN32
1a7bae4d 25GIT_INLINE(int) link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
4319860c 26{
73dcf287
RJ
27 GIT_UNUSED_ARG(old)
28 GIT_UNUSED_ARG(new)
4319860c
RJ
29 errno = ENOSYS;
30 return -1;
31}
32
1a7bae4d 33GIT_INLINE(int) git__mkdir(const char *path, int GIT_UNUSED(mode))
4319860c 34{
73dcf287 35 GIT_UNUSED_ARG(mode)
4319860c
RJ
36 return mkdir(path);
37}
38
e035685f
VM
39extern int git__unlink(const char *path);
40extern int git__mkstemp(char *template);
41extern int git__fsync(int fd);
4319860c
RJ
42
43# ifndef GIT__WIN32_NO_HIDE_FILEOPS
44# define unlink(p) git__unlink(p)
45# define mkstemp(t) git__mkstemp(t)
1e5dd572 46# define mkdir(p,m) git__mkdir(p, m)
c79dded3 47# define fsync(fd) git__fsync(fd)
4319860c
RJ
48# endif
49#endif /* GIT_WIN32 */
50
51
7a6cf815
RJ
52#if !defined(O_BINARY)
53#define O_BINARY 0
54#endif
55
75d58430
RJ
56#define GITFO_BUF_INIT {NULL, 0}
57
ec250c6e 58typedef int git_file;
ec250c6e 59
75d58430
RJ
60typedef struct { /* file io buffer */
61 void *data; /* data bytes */
62 size_t len; /* data length */
63} gitfo_buf;
64
e035685f
VM
65extern int gitfo_exists(const char *path);
66extern int gitfo_open(const char *path, int flags);
67extern int gitfo_creat(const char *path, int mode);
55ffebe3 68extern int gitfo_creat_force(const char *path, int mode);
1549cba9
RG
69extern int gitfo_creat_locked(const char *path, int mode);
70extern int gitfo_creat_locked_force(const char *path, int mode);
f6f72d7e 71extern int gitfo_mktemp(char *path_out, const char *filename);
e035685f 72extern int gitfo_isdir(const char *path);
bc648491 73extern int gitfo_isfile(const char *path);
e035685f 74extern int gitfo_mkdir_recurs(const char *path, int mode);
72a3fe42 75extern int gitfo_mkdir_2file(const char *path);
ec250c6e
AE
76#define gitfo_close(fd) close(fd)
77
e035685f
VM
78extern int gitfo_read(git_file fd, void *buf, size_t cnt);
79extern int gitfo_write(git_file fd, void *buf, size_t cnt);
3cc60635 80#define gitfo_lseek(f,n,w) lseek(f, n, w)
f0bde7fa 81extern git_off_t gitfo_size(git_file fd);
ec250c6e 82
e035685f
VM
83extern int gitfo_read_file(gitfo_buf *obj, const char *path);
84extern void gitfo_free_buf(gitfo_buf *obj);
19a30a3f
VM
85
86/* Move (rename) a file; this operation is atomic */
87extern int gitfo_mv(const char *from, const char *to);
88
89/* Move a file (forced); this will create the destination
90 * path if it doesn't exist */
91extern int gitfo_mv_force(const char *from, const char *to);
75d58430 92
3cc60635
RJ
93#define gitfo_stat(p,b) stat(p, b)
94#define gitfo_fstat(f,b) fstat(f, b)
ae496955
VM
95
96#ifdef GIT_WIN32
97# define gitfo_lstat(p,b) gitfo_lstat__w32(p,b)
98# define gitfo_readlink(a, b, c) gitfo_readlink__w32(a, b, c)
e6480970
SS
99
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);
ae496955
VM
102#else
103# define gitfo_lstat(p,b) lstat(p,b)
104# define gitfo_readlink(a, b, c) readlink(a, b, c)
105#endif
3cc60635 106
cfe96f31 107#define gitfo_unlink(p) unlink(p)
cfe96f31 108#define gitfo_rmdir(p) rmdir(p)
498bc090 109#define gitfo_chdir(p) chdir(p)
502acd16 110#define gitfo_mkdir(p,m) mkdir(p, m)
4319860c
RJ
111
112#define gitfo_mkstemp(t) mkstemp(t)
113#define gitfo_fsync(fd) fsync(fd)
114#define gitfo_chmod(p,m) chmod(p, m)
502acd16 115
20e7f426
SP
116/**
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.
122 *
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.
127 * @return
128 * - GIT_SUCCESS on success;
129 * - GIT_EOSERR on an unspecified OS related error.
130 */
e035685f 131extern int gitfo_map_ro(
79ca2edc 132 git_map *out,
20e7f426 133 git_file fd,
f0bde7fa 134 git_off_t begin,
20e7f426
SP
135 size_t len);
136
137/**
138 * Release the memory associated with a previous memory mapping.
139 * @param map the mapping description previously configured.
140 */
e035685f 141extern void gitfo_free_map(git_map *map);
20e7f426 142
5690f02e
SP
143/**
144 * Walk each directory entry, except '.' and '..', calling fn(state).
145 *
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.
153 */
e035685f 154extern int gitfo_dirent(
5690f02e
SP
155 char *pathbuf,
156 size_t pathmax,
157 int (*fn)(void *, char *),
158 void *state);
159
ccef1c9d
VM
160extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
161 const char *name2, int len2, int isdir2);
162
677a3c07 163extern int gitfo_getcwd(char *buffer_out, size_t size);
164
170d3f2f 165/**
166 * Clean up a provided absolute or relative directory path.
167 *
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.
172 *
173 * If not empty, the returned path ends with a forward slash.
174 *
175 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
176 *
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.
180 *
181 * @param buffer_out buffer to populate with the normalized path.
c90292ce 182 * @param size buffer size.
170d3f2f 183 * @param path directory path to clean up.
184 * @return
185 * - GIT_SUCCESS on success;
186 * - GIT_ERROR when the input path is invalid or escapes the current directory.
187 */
26a98ec8 188int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path);
170d3f2f 189
618818dc 190/**
191 * Clean up a provided absolute or relative file path.
192 *
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.
197 *
198 * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
199 *
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.
203 *
204 * @param buffer_out buffer to populate with the normalized path.
c90292ce 205 * @param size buffer size.
618818dc 206 * @param path file path to clean up.
207 * @return
208 * - GIT_SUCCESS on success;
209 * - GIT_ERROR when the input path is invalid or escapes the current directory.
210 */
26a98ec8 211int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path);
618818dc 212
f2e6b877
RG
213void gitfo_posixify_path(char *path);
214
04fdc10d
RG
215int gitfo_retrieve_path_root_offset(const char *path);
216
ec250c6e 217#endif /* INCLUDE_fileops_h__ */