]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
win32: Use an 64-bit file offset type
[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 *old, const char *new)
17 {
18 errno = ENOSYS;
19 return -1;
20 }
21
22 GIT_INLINE(int) git__mkdir(const char *path, int mode)
23 {
24 return mkdir(path);
25 }
26
27 extern int git__unlink(const char *path);
28 extern int git__mkstemp(char *template);
29 extern int git__fsync(int fd);
30
31 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
32 # define unlink(p) git__unlink(p)
33 # define mkstemp(t) git__mkstemp(t)
34 # define mkdir(p,m) git__mkdir(p,m)
35 # define fsync(fd) git__fsync(fd)
36 # endif
37 #endif /* GIT_WIN32 */
38
39
40 #if !defined(O_BINARY)
41 #define O_BINARY 0
42 #endif
43
44 #define GITFO_BUF_INIT {NULL, 0}
45
46 typedef int git_file;
47 typedef struct gitfo_cache gitfo_cache;
48
49 typedef struct { /* file io buffer */
50 void *data; /* data bytes */
51 size_t len; /* data length */
52 } gitfo_buf;
53
54 extern int gitfo_exists(const char *path);
55 extern int gitfo_open(const char *path, int flags);
56 extern int gitfo_creat(const char *path, int mode);
57 #define gitfo_close(fd) close(fd)
58
59 extern int gitfo_read(git_file fd, void *buf, size_t cnt);
60 extern int gitfo_write(git_file fd, void *buf, size_t cnt);
61 extern off_t gitfo_size(git_file fd);
62
63 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
64 extern void gitfo_free_buf(gitfo_buf *obj);
65 extern int gitfo_move_file(char *from, char *to);
66
67 #define gitfo_unlink(p) unlink(p)
68 #define gitfo_rmdir(p) rmdir(p)
69 #define gitfo_chdir(p) chdir(p)
70 #define gitfo_mkdir(p,m) mkdir(p, m)
71
72 #define gitfo_mkstemp(t) mkstemp(t)
73 #define gitfo_fsync(fd) fsync(fd)
74 #define gitfo_chmod(p,m) chmod(p, m)
75
76 /**
77 * Read-only map all or part of a file into memory.
78 * When possible this function should favor a virtual memory
79 * style mapping over some form of malloc()+read(), as the
80 * data access will be random and is not likely to touch the
81 * majority of the region requested.
82 *
83 * @param out buffer to populate with the mapping information.
84 * @param fd open descriptor to configure the mapping from.
85 * @param begin first byte to map, this should be page aligned.
86 * @param end number of bytes to map.
87 * @return
88 * - GIT_SUCCESS on success;
89 * - GIT_EOSERR on an unspecified OS related error.
90 */
91 extern int gitfo_map_ro(
92 git_map *out,
93 git_file fd,
94 off_t begin,
95 size_t len);
96
97 /**
98 * Release the memory associated with a previous memory mapping.
99 * @param map the mapping description previously configured.
100 */
101 extern void gitfo_free_map(git_map *map);
102
103 /**
104 * Walk each directory entry, except '.' and '..', calling fn(state).
105 *
106 * @param pathbuf buffer the function reads the initial directory
107 * path from, and updates with each successive entry's name.
108 * @param pathmax maximum allocation of pathbuf.
109 * @param fn function to invoke with each entry. The first arg is
110 * the input state and the second arg is pathbuf. The function
111 * may modify the pathbuf, but only by appending new text.
112 * @param state to pass to fn as the first arg.
113 */
114 extern int gitfo_dirent(
115 char *pathbuf,
116 size_t pathmax,
117 int (*fn)(void *, char *),
118 void *state);
119
120 extern gitfo_cache *gitfo_enable_caching(git_file fd, size_t cache_size);
121 extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);
122 extern int gitfo_flush_cached(gitfo_cache *ioc);
123 extern int gitfo_close_cached(gitfo_cache *ioc);
124
125 #endif /* INCLUDE_fileops_h__ */