1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief I/O types and functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
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))
16 # define IO_BUFFER_SIZE 8192
18 # define IO_BUFFER_SIZE (BUFSIZ & ~7U)
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.
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)];
32 /// Name of the source filename (as given on the command line) or
33 /// pointer to static "(stdin)" when reading from standard input.
36 /// Destination filename converted from src_name or pointer to static
37 /// "(stdout)" when writing to standard output.
40 /// File descriptor of the source file
43 /// File descriptor of the target file
46 /// True once end of the source file has been detected.
49 /// If true, we look for long chunks of zeros and try to create
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;
58 /// Stat of the source file.
61 /// Stat of the destination file.
67 /// \brief Initialize the I/O module
68 extern void io_init(void);
71 /// \brief Disable creation of sparse files when decompressing
72 extern void io_no_sparse(void);
75 /// \brief Opens a file pair
76 extern file_pair *io_open(const char *src_name);
79 /// \brief Closes the file descriptors and frees possible allocated memory
81 /// The success argument determines if source or destination file gets
83 /// - false: The destination file is unlinked.
84 /// - true: The source file is unlinked unless writing to stdout or --keep
86 extern void io_close(file_pair *pair, bool success);
89 /// \brief Reads from the source file to a buffer
91 /// \param pair File pair having the source file open for reading
92 /// \param buf Destination buffer to hold the read data
93 /// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
95 /// \return On success, number of bytes read is returned. On end of
96 /// file zero is returned and pair->src_eof set to true.
97 /// On error, SIZE_MAX is returned and error message printed.
98 extern size_t io_read(file_pair *pair, io_buf *buf, size_t size);
101 /// \brief Read from source file from given offset to a buffer
103 /// This is remotely similar to standard pread(). This uses lseek() though,
104 /// so the read offset is changed on each call.
106 /// \param pair Seekable source file
107 /// \param buf Destination buffer
108 /// \param size Amount of data to read
109 /// \param pos Offset relative to the beginning of the file,
110 /// from which the data should be read.
112 /// \return On success, false is returned. On error, error message
113 /// is printed and true is returned.
114 extern bool io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos);
117 /// \brief Writes a buffer to the destination file
119 /// \param pair File pair having the destination file open for writing
120 /// \param buf Buffer containing the data to be written
121 /// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
123 /// \return On success, zero is returned. On error, -1 is returned
124 /// and error message printed.
125 extern bool io_write(file_pair *pair, const io_buf *buf, size_t size);