1 #ifndef _WIMLIB_FILE_IO_H
2 #define _WIMLIB_FILE_IO_H
3 
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <sys/types.h>
7 
8 /* Wrapper around a file descriptor that keeps track of offset (including in
9  * pipes, which don't support lseek()) and a cached flag that tells whether the
10  * file descriptor is a pipe or not.  */
11 struct filedes {
12 	int fd;
13 	unsigned int is_pipe : 1;
14 	off_t offset;
15 };
16 
17 extern int
18 full_read(struct filedes *fd, void *buf, size_t n);
19 
20 extern int
21 full_pread(struct filedes *fd, void *buf, size_t nbyte, off_t offset);
22 
23 extern int
24 full_write(struct filedes *fd, const void *buf, size_t n);
25 
26 extern int
27 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset);
28 
29 #ifndef __WIN32__
30 #  define O_BINARY 0
31 #endif
32 
33 extern off_t
34 filedes_seek(struct filedes *fd, off_t offset);
35 
36 extern bool
37 filedes_is_seekable(struct filedes *fd);
38 
filedes_init(struct filedes * fd,int raw_fd)39 static inline void filedes_init(struct filedes *fd, int raw_fd)
40 {
41 	fd->fd = raw_fd;
42 	fd->offset = 0;
43 	fd->is_pipe = 0;
44 }
45 
filedes_invalidate(struct filedes * fd)46 static inline void filedes_invalidate(struct filedes *fd)
47 {
48 	fd->fd = -1;
49 }
50 
51 #define filedes_close(f) close((f)->fd)
52 
53 static inline bool
filedes_valid(const struct filedes * fd)54 filedes_valid(const struct filedes *fd)
55 {
56 	return fd->fd != -1;
57 }
58 
59 #endif /* _WIMLIB_FILE_IO_H */
60