1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file       file_io.h
4/// \brief      I/O types and functions
5//
6//  Author:     Lasse Collin
7//
8//  This file has been put into the public domain.
9//  You can do whatever you want with this file.
10//
11///////////////////////////////////////////////////////////////////////////////
12
13// Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
14// We also need that IO_BUFFER_SIZE is a multiple of 8 (sizeof(uint64_t))
15#if BUFSIZ <= 1024
16#	define IO_BUFFER_SIZE 8192
17#else
18#	define IO_BUFFER_SIZE (BUFSIZ & ~7U)
19#endif
20
21
22/// is_sparse() accesses the buffer as uint64_t for maximum speed.
23/// The u32 and u64 members must only be access through this union
24/// to avoid strict aliasing violations. Taking a pointer of u8
25/// should be fine as long as uint8_t maps to unsigned char which
26/// can alias anything.
27typedef union {
28	uint8_t u8[IO_BUFFER_SIZE];
29	uint32_t u32[IO_BUFFER_SIZE / sizeof(uint32_t)];
30	uint64_t u64[IO_BUFFER_SIZE / sizeof(uint64_t)];
31} io_buf;
32
33
34typedef struct {
35	/// Name of the source filename (as given on the command line) or
36	/// pointer to static "(stdin)" when reading from standard input.
37	const char *src_name;
38
39	/// Destination filename converted from src_name or pointer to static
40	/// "(stdout)" when writing to standard output.
41	char *dest_name;
42
43	/// File descriptor of the source file
44	int src_fd;
45
46	/// File descriptor of the target file
47	int dest_fd;
48
49	/// True once end of the source file has been detected.
50	bool src_eof;
51
52	/// For --flush-timeout: True if at least one byte has been read
53	/// since the previous flush or the start of the file.
54	bool src_has_seen_input;
55
56	/// For --flush-timeout: True when flushing is needed.
57	bool flush_needed;
58
59	/// If true, we look for long chunks of zeros and try to create
60	/// a sparse file.
61	bool dest_try_sparse;
62
63	/// This is used only if dest_try_sparse is true. This holds the
64	/// number of zero bytes we haven't written out, because we plan
65	/// to make that byte range a sparse chunk.
66	off_t dest_pending_sparse;
67
68	/// Stat of the source file.
69	struct stat src_st;
70
71	/// Stat of the destination file.
72	struct stat dest_st;
73
74} file_pair;
75
76
77/// \brief      Initialize the I/O module
78extern void io_init(void);
79
80
81#ifndef TUKLIB_DOSLIKE
82/// \brief      Write a byte to user_abort_pipe[1]
83///
84/// This is called from a signal handler.
85extern void io_write_to_user_abort_pipe(void);
86#endif
87
88
89/// \brief      Disable creation of sparse files when decompressing
90extern void io_no_sparse(void);
91
92
93#ifdef ENABLE_SANDBOX
94/// \brief      main() calls this if conditions for sandboxing have been met.
95extern void io_allow_sandbox(void);
96#endif
97
98
99/// \brief      Open the source file
100extern file_pair *io_open_src(const char *src_name);
101
102
103/// \brief      Open the destination file
104extern bool io_open_dest(file_pair *pair);
105
106
107/// \brief      Closes the file descriptors and frees possible allocated memory
108///
109/// The success argument determines if source or destination file gets
110/// unlinked:
111///  - false: The destination file is unlinked.
112///  - true: The source file is unlinked unless writing to stdout or --keep
113///    was used.
114extern void io_close(file_pair *pair, bool success);
115
116
117/// \brief      Reads from the source file to a buffer
118///
119/// \param      pair    File pair having the source file open for reading
120/// \param      buf     Destination buffer to hold the read data
121/// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
122///
123/// \return     On success, number of bytes read is returned. On end of
124///             file zero is returned and pair->src_eof set to true.
125///             On error, SIZE_MAX is returned and error message printed.
126extern size_t io_read(file_pair *pair, io_buf *buf, size_t size);
127
128
129/// \brief      Fix the position in src_fd
130///
131/// This is used when --single-thream has been specified and decompression
132/// is successful. If the input file descriptor supports seeking, this
133/// function fixes the input position to point to the next byte after the
134/// decompressed stream.
135///
136/// \param      pair        File pair having the source file open for reading
137/// \param      rewind_size How many bytes of extra have been read i.e.
138///                         how much to seek backwards.
139extern void io_fix_src_pos(file_pair *pair, size_t rewind_size);
140
141
142/// \brief      Read from source file from given offset to a buffer
143///
144/// This is remotely similar to standard pread(). This uses lseek() though,
145/// so the read offset is changed on each call.
146///
147/// \param      pair    Seekable source file
148/// \param      buf     Destination buffer
149/// \param      size    Amount of data to read
150/// \param      pos     Offset relative to the beginning of the file,
151///                     from which the data should be read.
152///
153/// \return     On success, false is returned. On error, error message
154///             is printed and true is returned.
155extern bool io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos);
156
157
158/// \brief      Writes a buffer to the destination file
159///
160/// \param      pair    File pair having the destination file open for writing
161/// \param      buf     Buffer containing the data to be written
162/// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
163///
164/// \return     On success, zero is returned. On error, -1 is returned
165///             and error message printed.
166extern bool io_write(file_pair *pair, const io_buf *buf, size_t size);
167