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