]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
Change git_repository initialization to use a path
[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_isdir(const char *path);
61 #define gitfo_close(fd) close(fd)
62
63 extern int gitfo_read(git_file fd, void *buf, size_t cnt);
64 extern int gitfo_write(git_file fd, void *buf, size_t cnt);
65 #define gitfo_lseek(f,n,w) lseek(f, n, w)
66 extern off_t gitfo_size(git_file fd);
67
68 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
69 extern void gitfo_free_buf(gitfo_buf *obj);
70 extern int gitfo_move_file(char *from, char *to);
71
72 #define gitfo_stat(p,b) stat(p, b)
73 #define gitfo_fstat(f,b) fstat(f, b)
74
75 #define gitfo_unlink(p) unlink(p)
76 #define gitfo_rmdir(p) rmdir(p)
77 #define gitfo_chdir(p) chdir(p)
78 #define gitfo_mkdir(p,m) mkdir(p, m)
79
80 #define gitfo_mkstemp(t) mkstemp(t)
81 #define gitfo_fsync(fd) fsync(fd)
82 #define gitfo_chmod(p,m) chmod(p, m)
83
84 /**
85 * Read-only map all or part of a file into memory.
86 * When possible this function should favor a virtual memory
87 * style mapping over some form of malloc()+read(), as the
88 * data access will be random and is not likely to touch the
89 * majority of the region requested.
90 *
91 * @param out buffer to populate with the mapping information.
92 * @param fd open descriptor to configure the mapping from.
93 * @param begin first byte to map, this should be page aligned.
94 * @param end number of bytes to map.
95 * @return
96 * - GIT_SUCCESS on success;
97 * - GIT_EOSERR on an unspecified OS related error.
98 */
99 extern int gitfo_map_ro(
100 git_map *out,
101 git_file fd,
102 off_t begin,
103 size_t len);
104
105 /**
106 * Release the memory associated with a previous memory mapping.
107 * @param map the mapping description previously configured.
108 */
109 extern void gitfo_free_map(git_map *map);
110
111 /**
112 * Walk each directory entry, except '.' and '..', calling fn(state).
113 *
114 * @param pathbuf buffer the function reads the initial directory
115 * path from, and updates with each successive entry's name.
116 * @param pathmax maximum allocation of pathbuf.
117 * @param fn function to invoke with each entry. The first arg is
118 * the input state and the second arg is pathbuf. The function
119 * may modify the pathbuf, but only by appending new text.
120 * @param state to pass to fn as the first arg.
121 */
122 extern int gitfo_dirent(
123 char *pathbuf,
124 size_t pathmax,
125 int (*fn)(void *, char *),
126 void *state);
127
128 extern gitfo_cache *gitfo_enable_caching(git_file fd, size_t cache_size);
129 extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);
130 extern int gitfo_flush_cached(gitfo_cache *ioc);
131 extern int gitfo_close_cached(gitfo_cache *ioc);
132
133 #endif /* INCLUDE_fileops_h__ */