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