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 #ifndef TUKLIB_DOSLIKE 59 /// File descriptor of the directory of the target file (which is 60 /// also the directory of the source file) 61 int dir_fd; 62 #endif 63 64 /// True once end of the source file has been detected. 65 bool src_eof; 66 67 /// For --flush-timeout: True if at least one byte has been read 68 /// since the previous flush or the start of the file. 69 bool src_has_seen_input; 70 71 /// For --flush-timeout: True when flushing is needed. 72 bool flush_needed; 73 74 /// If true, we look for long chunks of zeros and try to create 75 /// a sparse file. 76 bool dest_try_sparse; 77 78 /// This is used only if dest_try_sparse is true. This holds the 79 /// number of zero bytes we haven't written out, because we plan 80 /// to make that byte range a sparse chunk. 81 off_t dest_pending_sparse; 82 83 /// Stat of the source file. 84 struct stat src_st; 85 86 /// Stat of the destination file. 87 struct stat dest_st; 88 89 } file_pair; 90 91 92 /// \brief Initialize the I/O module 93 extern void io_init(void); 94 95 96 #ifndef TUKLIB_DOSLIKE 97 /// \brief Write a byte to user_abort_pipe[1] 98 /// 99 /// This is called from a signal handler. 100 extern void io_write_to_user_abort_pipe(void); 101 #endif 102 103 104 /// \brief Disable creation of sparse files when decompressing 105 extern void io_no_sparse(void); 106 107 108 /// \brief Open the source file 109 extern file_pair *io_open_src(const char *src_name); 110 111 112 /// \brief Open the destination file 113 extern bool io_open_dest(file_pair *pair); 114 115 116 /// \brief Closes the file descriptors and frees possible allocated memory 117 /// 118 /// The success argument determines if source or destination file gets 119 /// unlinked: 120 /// - false: The destination file is unlinked. 121 /// - true: The source file is unlinked unless writing to stdout or --keep 122 /// was used. 123 extern void io_close(file_pair *pair, bool success); 124 125 126 /// \brief Reads from the source file to a buffer 127 /// 128 /// \param pair File pair having the source file open for reading 129 /// \param buf Destination buffer to hold the read data 130 /// \param size Size of the buffer; must be at most IO_BUFFER_SIZE 131 /// 132 /// \return On success, number of bytes read is returned. On end of 133 /// file zero is returned and pair->src_eof set to true. 134 /// On error, SIZE_MAX is returned and error message printed. 135 extern size_t io_read(file_pair *pair, io_buf *buf, size_t size); 136 137 138 /// \brief Fix the position in src_fd 139 /// 140 /// This is used when --single-thream has been specified and decompression 141 /// is successful. If the input file descriptor supports seeking, this 142 /// function fixes the input position to point to the next byte after the 143 /// decompressed stream. 144 /// 145 /// \param pair File pair having the source file open for reading 146 /// \param rewind_size How many bytes of extra have been read i.e. 147 /// how much to seek backwards. 148 extern void io_fix_src_pos(file_pair *pair, size_t rewind_size); 149 150 151 /// \brief Seek to the given absolute position in the source file 152 /// 153 /// This calls lseek() and also clears pair->src_eof. 154 /// 155 /// \param pair Seekable source file 156 /// \param pos Offset relative to the beginning of the file, 157 /// from which the data should be read. 158 /// 159 /// \return On success, false is returned. On error, error message 160 /// is printed and true is returned. 161 extern bool io_seek_src(file_pair *pair, uint64_t pos); 162 163 164 /// \brief Read from source file from given offset to a buffer 165 /// 166 /// This is remotely similar to standard pread(). This uses lseek() though, 167 /// so the read offset is changed on each call. 168 /// 169 /// \param pair Seekable source file 170 /// \param buf Destination buffer 171 /// \param size Amount of data to read 172 /// \param pos Offset relative to the beginning of the file, 173 /// from which the data should be read. 174 /// 175 /// \return On success, false is returned. On error, error message 176 /// is printed and true is returned. 177 extern bool io_pread(file_pair *pair, io_buf *buf, size_t size, uint64_t pos); 178 179 180 /// \brief Writes a buffer to the destination file 181 /// 182 /// \param pair File pair having the destination file open for writing 183 /// \param buf Buffer containing the data to be written 184 /// \param size Size of the buffer; must be at most IO_BUFFER_SIZE 185 /// 186 /// \return On success, false is returned. On error, error message 187 /// is printed and true is returned. 188 extern bool io_write(file_pair *pair, const io_buf *buf, size_t size); 189