]> wimlib.net Git - wimlib/blob - include/wimlib/file_io.h
New helper macro: ALIGN()
[wimlib] / include / wimlib / file_io.h
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 extern ssize_t
30 raw_pread(struct filedes *fd, void *buf, size_t nbyte, off_t offset);
31
32 extern ssize_t
33 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset);
34
35 #ifndef __WIN32__
36 #  define O_BINARY 0
37 #endif
38
39 extern off_t
40 filedes_seek(struct filedes *fd, off_t offset);
41
42 extern bool
43 filedes_is_seekable(struct filedes *fd);
44
45 static inline void filedes_init(struct filedes *fd, int raw_fd)
46 {
47         fd->fd = raw_fd;
48         fd->offset = 0;
49         fd->is_pipe = 0;
50 }
51
52 static inline void filedes_invalidate(struct filedes *fd)
53 {
54         fd->fd = -1;
55 }
56
57 #define filedes_close(f) close((f)->fd)
58
59 static inline bool
60 filedes_valid(const struct filedes *fd)
61 {
62         return fd->fd != -1;
63 }
64
65 #endif /* _WIMLIB_FILE_IO_H */