]> git.proxmox.com Git - libgit2.git/blob - src/fileops.h
t0020-dirent.c: allow test to be run standalone
[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 /** Force 64 bit off_t size on POSIX. */
10 #define _FILE_OFFSET_BITS 64
11
12 #include "common.h"
13 #include "map.h"
14 #include "dir.h"
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <time.h>
18
19 #if !defined(O_BINARY)
20 #define O_BINARY 0
21 #endif
22
23 #define GITFO_BUF_INIT {NULL, 0}
24
25 typedef int git_file;
26 typedef struct gitfo_cache gitfo_cache;
27
28 typedef struct { /* file io buffer */
29 void *data; /* data bytes */
30 size_t len; /* data length */
31 } gitfo_buf;
32
33 extern int gitfo_exists(const char *path);
34 extern int gitfo_open(const char *path, int flags);
35 extern int gitfo_creat(const char *path, int mode);
36 #define gitfo_close(fd) close(fd)
37
38 extern int gitfo_read(git_file fd, void *buf, size_t cnt);
39 extern int gitfo_write(git_file fd, void *buf, size_t cnt);
40 extern off_t gitfo_size(git_file fd);
41
42 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
43 extern void gitfo_free_buf(gitfo_buf *obj);
44
45 #define gitfo_unlink(p) unlink(p)
46 #define gitfo_rmdir(p) rmdir(p)
47 #define gitfo_chdir(p) chdir(p)
48
49 #ifdef GIT_WIN32
50 #define gitfo_mkdir(p,m) mkdir(p)
51 #else
52 #define gitfo_mkdir(p,m) mkdir(p, m)
53 #endif
54
55 /**
56 * Read-only map all or part of a file into memory.
57 * When possible this function should favor a virtual memory
58 * style mapping over some form of malloc()+read(), as the
59 * data access will be random and is not likely to touch the
60 * majority of the region requested.
61 *
62 * @param out buffer to populate with the mapping information.
63 * @param fd open descriptor to configure the mapping from.
64 * @param begin first byte to map, this should be page aligned.
65 * @param end number of bytes to map.
66 * @return
67 * - GIT_SUCCESS on success;
68 * - GIT_EOSERR on an unspecified OS related error.
69 */
70 extern int gitfo_map_ro(
71 git_map *out,
72 git_file fd,
73 off_t begin,
74 size_t len);
75
76 /**
77 * Release the memory associated with a previous memory mapping.
78 * @param map the mapping description previously configured.
79 */
80 extern void gitfo_free_map(git_map *map);
81
82 /**
83 * Walk each directory entry, except '.' and '..', calling fn(state).
84 *
85 * @param pathbuf buffer the function reads the initial directory
86 * path from, and updates with each successive entry's name.
87 * @param pathmax maximum allocation of pathbuf.
88 * @param fn function to invoke with each entry. The first arg is
89 * the input state and the second arg is pathbuf. The function
90 * may modify the pathbuf, but only by appending new text.
91 * @param state to pass to fn as the first arg.
92 */
93 extern int gitfo_dirent(
94 char *pathbuf,
95 size_t pathmax,
96 int (*fn)(void *, char *),
97 void *state);
98
99 extern gitfo_cache *gitfo_enable_caching(git_file fd, size_t cache_size);
100 extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);
101 extern int gitfo_flush_cached(gitfo_cache *ioc);
102 extern int gitfo_close_cached(gitfo_cache *ioc);
103
104 #endif /* INCLUDE_fileops_h__ */