file_io.h revision 292588
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/// Use an union to make sure that the buffer is properly aligned.
24typedef union {
25	uint8_t u8[IO_BUFFER_SIZE];
26	uint32_t u32[IO_BUFFER_SIZE / sizeof(uint32_t)];
27	uint64_t u64[IO_BUFFER_SIZE / sizeof(uint64_t)];
28} io_buf;
29
30
31typedef struct {
32	/// Name of the source filename (as given on the command line) or
33	/// pointer to static "(stdin)" when reading from standard input.
34	const char *src_name;
35
36	/// Destination filename converted from src_name or pointer to static
37	/// "(stdout)" when writing to standard output.
38	char *dest_name;
39
40	/// File descriptor of the source file
41	int src_fd;
42
43	/// File descriptor of the target file
44	int dest_fd;
45
46	/// True once end of the source file has been detected.
47	bool src_eof;
48
49	/// If true, we look for long chunks of zeros and try to create
50	/// a sparse file.
51	bool dest_try_sparse;
52
53	/// This is used only if dest_try_sparse is true. This holds the
54	/// number of zero bytes we haven't written out, because we plan
55	/// to make that byte range a sparse chunk.
56	off_t dest_pending_sparse;
57
58	/// Stat of the source file.
59	struct stat src_st;
60
61	/// Stat of the destination file.
62	struct stat dest_st;
63
64} file_pair;
65
66
67/// \brief      Initialize the I/O module
68extern void io_init(void);
69
70
71#ifndef TUKLIB_DOSLIKE
72/// \brief      Write a byte to user_abort_pipe[1]
73///
74/// This is called from a signal handler.
75extern void io_write_to_user_abort_pipe(void);
76#endif
77
78
79/// \brief      Disable creation of sparse files when decompressing
80extern void io_no_sparse(void);
81
82
83/// \brief      Open the source file
84extern file_pair *io_open_src(const char *src_name);
85
86
87/// \brief      Open the destination file
88extern bool io_open_dest(file_pair *pair);
89
90
91/// \brief      Closes the file descriptors and frees possible allocated memory
92///
93/// The success argument determines if source or destination file gets
94/// unlinked:
95///  - false: The destination file is unlinked.
96///  - true: The source file is unlinked unless writing to stdout or --keep
97///    was used.
98extern void io_close(file_pair *pair, bool success);
99
100
101/// \brief      Reads from the source file to a buffer
102///
103/// \param      pair    File pair having the source file open for reading
104/// \param      buf     Destination buffer to hold the read data
105/// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
106///
107/// \return     On success, number of bytes read is returned. On end of
108///             file zero is returned and pair->src_eof set to true.
109///             On error, SIZE_MAX is returned and error message printed.
110extern size_t io_read(file_pair *pair, io_buf *buf, size_t size);
111
112
113/// \brief      Fix the position in src_fd
114///
115/// This is used when --single-thream has been specified and decompression
116/// is successful. If the input file descriptor supports seeking, this
117/// function fixes the input position to point to the next byte after the
118/// decompressed stream.
119///
120/// \param      pair        File pair having the source file open for reading
121/// \param      rewind_size How many bytes of extra have been read i.e.
122///                         how much to seek backwards.
123extern void io_fix_src_pos(file_pair *pair, size_t rewind_size);
124
125
126/// \brief      Read from source file from given offset to a buffer
127///
128/// This is remotely similar to standard pread(). This uses lseek() though,
129/// so the read offset is changed on each call.
130///
131/// \param      pair    Seekable source file
132/// \param      buf     Destination buffer
133/// \param      size    Amount of data to read
134/// \param      pos     Offset relative to the beginning of the file,
135///                     from which the data should be read.
136///
137/// \return     On success, false is returned. On error, error message
138///             is printed and true is returned.
139extern bool io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos);
140
141
142/// \brief      Writes a buffer to the destination file
143///
144/// \param      pair    File pair having the destination file open for writing
145/// \param      buf     Buffer containing the data to be written
146/// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
147///
148/// \return     On success, zero is returned. On error, -1 is returned
149///             and error message printed.
150extern bool io_write(file_pair *pair, const io_buf *buf, size_t size);
151