]> wimlib.net Git - wimlib/blob - include/wimlib/file_io.h
resource.c: Cleanup and refactor
[wimlib] / include / wimlib / file_io.h
1 #ifndef _WIMLIB_FILE_IO_H
2 #define _WIMLIB_FILE_IO_H
3
4 #include <stddef.h>
5 #include <sys/types.h>
6
7 /* Wrapper around a file descriptor that keeps track of offset (including in
8  * pipes, which don't support lseek()) and a cached flag that tells whether the
9  * file descriptor is a pipe or not.  */
10 struct filedes {
11         int fd;
12         unsigned int is_pipe : 1;
13         off_t offset;
14 };
15
16 extern int
17 full_read(struct filedes *fd, void *buf, size_t n);
18
19 extern int
20 full_pread(struct filedes *fd, void *buf, size_t nbyte, off_t offset);
21
22 extern int
23 full_write(struct filedes *fd, const void *buf, size_t n);
24
25 extern int
26 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset);
27
28 extern ssize_t
29 raw_pread(struct filedes *fd, void *buf, size_t nbyte, off_t offset);
30
31 extern ssize_t
32 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset);
33
34 #ifdef __WIN32__
35 struct iovec {
36         void *iov_base;
37         size_t iov_len;
38 };
39 #else
40 struct iovec;
41 #endif
42
43 extern int
44 full_writev(struct filedes *fd, struct iovec *iov, int iovcnt);
45
46 #ifndef __WIN32__
47 #  define O_BINARY 0
48 #endif
49
50 extern off_t
51 filedes_seek(struct filedes *fd, off_t offset);
52
53 extern bool
54 filedes_is_seekable(struct filedes *fd);
55
56 static inline void filedes_init(struct filedes *fd, int raw_fd)
57 {
58         fd->fd = raw_fd;
59         fd->offset = 0;
60         fd->is_pipe = 0;
61 }
62
63 static inline void filedes_invalidate(struct filedes *fd)
64 {
65         fd->fd = -1;
66 }
67
68 static inline void filedes_copy(struct filedes *dst, const struct filedes *src)
69 {
70         *dst = *src;
71 }
72
73 #define filedes_close(f) close((f)->fd)
74
75 static inline bool
76 filedes_valid(const struct filedes *fd)
77 {
78         return fd->fd != -1;
79 }
80
81 #endif /* _WIMLIB_FILE_IO_H */