4 int gitfo_open(const char *path
, int flags
)
6 int fd
= open(path
, flags
| O_BINARY
);
7 return fd
>= 0 ? fd
: git_os_error();
10 int gitfo_creat(const char *path
, int mode
)
12 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
13 return fd
>= 0 ? fd
: git_os_error();
16 int gitfo_read(git_file fd
, void *buf
, size_t cnt
)
20 ssize_t r
= read(fd
, b
, cnt
);
22 if (errno
== EINTR
|| errno
== EAGAIN
)
24 return git_os_error();
28 return git_os_error();
36 int gitfo_write(git_file fd
, void *buf
, size_t cnt
)
40 ssize_t r
= write(fd
, b
, cnt
);
42 if (errno
== EINTR
|| errno
== EAGAIN
)
44 return git_os_error();
48 return git_os_error();
56 int gitfo_exists(const char *path
)
58 return access(path
, F_OK
);
61 off_t
gitfo_size(git_file fd
)
65 return git_os_error();
69 int gitfo_read_file(gitfo_buf
*obj
, const char *path
)
75 assert(obj
&& path
&& *path
);
77 if ((fd
= gitfo_open(path
, O_RDONLY
)) < 0)
80 if (((len
= gitfo_size(fd
)) < 0)
81 || ((buff
= git__malloc(len
+ 1)) == NULL
)) {
86 if (gitfo_read(fd
, buff
, len
) < 0) {
101 void gitfo_free_buf(gitfo_buf
*obj
)
108 int gitfo_map_ro(git_map
*out
, git_file fd
, off_t begin
, size_t len
)
110 if (git__mmap(out
, len
, GIT_PROT_READ
, GIT_MAP_SHARED
, fd
, begin
) < 0)
111 return git_os_error();
115 void gitfo_free_map(git_map
*out
)
123 unsigned int cache_size
, pos
;
124 unsigned char *cache
;
127 gitfo_cache
*gitfo_enable_caching(git_file fd
, size_t cache_size
)
131 ioc
= git__malloc(sizeof(*ioc
));
136 ioc
->cache_size
= cache_size
;
137 ioc
->cache
= git__malloc(cache_size
);
146 GIT_INLINE(void) gitfo_add_to_cache(gitfo_cache
*ioc
, void *buf
, size_t len
)
148 memcpy(ioc
->cache
+ ioc
->pos
, buf
, len
);
152 int gitfo_flush_cached(gitfo_cache
*ioc
)
154 int result
= GIT_SUCCESS
;
157 result
= gitfo_write(ioc
->fd
, ioc
->cache
, ioc
->pos
);
164 int gitfo_write_cached(gitfo_cache
*ioc
, void *buff
, size_t len
)
166 unsigned char *buf
= buff
;
169 size_t space_left
= ioc
->cache_size
- ioc
->pos
;
170 /* cache if it's small */
171 if (space_left
> len
) {
172 gitfo_add_to_cache(ioc
, buf
, len
);
176 /* flush the cache if it doesn't fit */
179 gitfo_add_to_cache(ioc
, buf
, space_left
);
180 rc
= gitfo_flush_cached(ioc
);
188 /* write too-large chunks immediately */
189 if (len
> ioc
->cache_size
)
190 return gitfo_write(ioc
->fd
, buf
, len
);
195 int gitfo_close_cached(gitfo_cache
*ioc
)
199 if (gitfo_flush_cached(ioc
) < 0)
206 return gitfo_close(fd
);
212 int (*fn
)(void *, char *),
215 size_t wd_len
= strlen(path
);
219 if (!wd_len
|| path_sz
< wd_len
+ 2)
222 while (path
[wd_len
- 1] == '/')
224 path
[wd_len
++] = '/';
229 return git_os_error();
231 while ((de
= readdir(dir
)) != NULL
) {
235 /* always skip '.' and '..' */
236 if (de
->d_name
[0] == '.') {
237 if (de
->d_name
[1] == '\0')
239 if (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0')
243 de_len
= strlen(de
->d_name
);
244 if (path_sz
< wd_len
+ de_len
+ 1) {
249 strcpy(path
+ wd_len
, de
->d_name
);
250 result
= fn(arg
, path
);