file_io.h revision 278433
1104477Ssam/////////////////////////////////////////////////////////////////////////////// 2104477Ssam// 3139749Simp/// \file file_io.h 4104477Ssam/// \brief I/O types and functions 5104477Ssam// 6104477Ssam// Author: Lasse Collin 7104477Ssam// 8104477Ssam// This file has been put into the public domain. 9120915Ssam// You can do whatever you want with this file. 10104477Ssam// 11104477Ssam/////////////////////////////////////////////////////////////////////////////// 12104477Ssam 13104477Ssam// Some systems have suboptimal BUFSIZ. Use a bit bigger value on them. 14104477Ssam// We also need that IO_BUFFER_SIZE is a multiple of 8 (sizeof(uint64_t)) 15104477Ssam#if BUFSIZ <= 1024 16104477Ssam# define IO_BUFFER_SIZE 8192 17104477Ssam#else 18104477Ssam# define IO_BUFFER_SIZE (BUFSIZ & ~7U) 19104477Ssam#endif 20104477Ssam 21104477Ssam 22104477Ssam/// is_sparse() accesses the buffer as uint64_t for maximum speed. 23104477Ssam/// Use an union to make sure that the buffer is properly aligned. 24104477Ssamtypedef union { 25104477Ssam uint8_t u8[IO_BUFFER_SIZE]; 26104477Ssam uint32_t u32[IO_BUFFER_SIZE / sizeof(uint32_t)]; 27104477Ssam uint64_t u64[IO_BUFFER_SIZE / sizeof(uint64_t)]; 28104477Ssam} io_buf; 29104477Ssam 30104477Ssam 31104477Ssamtypedef struct { 32104477Ssam /// Name of the source filename (as given on the command line) or 33104477Ssam /// pointer to static "(stdin)" when reading from standard input. 34104477Ssam const char *src_name; 35104477Ssam 36104477Ssam /// Destination filename converted from src_name or pointer to static 37104477Ssam /// "(stdout)" when writing to standard output. 38104477Ssam char *dest_name; 39104477Ssam 40104477Ssam /// File descriptor of the source file 41104477Ssam int src_fd; 42104477Ssam 43119418Sobrien /// File descriptor of the target file 44119418Sobrien int dest_fd; 45119418Sobrien 46104477Ssam /// True once end of the source file has been detected. 47120915Ssam bool src_eof; 48104477Ssam 49112124Ssam /// If true, we look for long chunks of zeros and try to create 50104477Ssam /// a sparse file. 51104477Ssam bool dest_try_sparse; 52104477Ssam 53104477Ssam /// This is used only if dest_try_sparse is true. This holds the 54104477Ssam /// number of zero bytes we haven't written out, because we plan 55104477Ssam /// to make that byte range a sparse chunk. 56104477Ssam off_t dest_pending_sparse; 57129879Sphk 58104477Ssam /// Stat of the source file. 59104477Ssam struct stat src_st; 60104477Ssam 61104477Ssam /// Stat of the destination file. 62104477Ssam struct stat dest_st; 63104477Ssam 64104477Ssam} file_pair; 65104477Ssam 66104477Ssam 67104477Ssam/// \brief Initialize the I/O module 68104477Ssamextern void io_init(void); 69104477Ssam 70104477Ssam 71104477Ssam#ifndef TUKLIB_DOSLIKE 72104477Ssam/// \brief Write a byte to user_abort_pipe[1] 73104477Ssam/// 74119280Simp/// This is called from a signal handler. 75119280Simpextern void io_write_to_user_abort_pipe(void); 76112124Ssam#endif 77112124Ssam 78112124Ssam 79112124Ssam/// \brief Disable creation of sparse files when decompressing 80104477Ssamextern void io_no_sparse(void); 81104477Ssam 82104477Ssam 83104477Ssam/// \brief Open the source file 84104477Ssamextern file_pair *io_open_src(const char *src_name); 85104477Ssam 86104477Ssam 87104477Ssam/// \brief Open the destination file 88104477Ssamextern bool io_open_dest(file_pair *pair); 89104477Ssam 90104477Ssam 91104477Ssam/// \brief Closes the file descriptors and frees possible allocated memory 92104477Ssam/// 93104477Ssam/// The success argument determines if source or destination file gets 94104477Ssam/// unlinked: 95104477Ssam/// - false: The destination file is unlinked. 96104477Ssam/// - true: The source file is unlinked unless writing to stdout or --keep 97104477Ssam/// was used. 98104477Ssamextern void io_close(file_pair *pair, bool success); 99104477Ssam 100104477Ssam 101104477Ssam/// \brief Reads from the source file to a buffer 102104477Ssam/// 103104477Ssam/// \param pair File pair having the source file open for reading 104104477Ssam/// \param buf Destination buffer to hold the read data 105104477Ssam/// \param size Size of the buffer; assumed be smaller than SSIZE_MAX 106104477Ssam/// 107104477Ssam/// \return On success, number of bytes read is returned. On end of 108104477Ssam/// file zero is returned and pair->src_eof set to true. 109104477Ssam/// On error, SIZE_MAX is returned and error message printed. 110104477Ssamextern size_t io_read(file_pair *pair, io_buf *buf, size_t size); 111104477Ssam 112104477Ssam 113104477Ssam/// \brief Fix the position in src_fd 114104477Ssam/// 115104477Ssam/// This is used when --single-thream has been specified and decompression 116105251Smarkm/// is successful. If the input file descriptor supports seeking, this 117112124Ssam/// function fixes the input position to point to the next byte after the 118112124Ssam/// decompressed stream. 119112124Ssam/// 120104477Ssam/// \param pair File pair having the source file open for reading 121104477Ssam/// \param rewind_size How many bytes of extra have been read i.e. 122104477Ssam/// how much to seek backwards. 123104477Ssamextern void io_fix_src_pos(file_pair *pair, size_t rewind_size); 124104477Ssam 125104477Ssam 126104477Ssam/// \brief Read from source file from given offset to a buffer 127104477Ssam/// 128104477Ssam/// This is remotely similar to standard pread(). This uses lseek() though, 129104477Ssam/// so the read offset is changed on each call. 130104477Ssam/// 131104477Ssam/// \param pair Seekable source file 132104477Ssam/// \param buf Destination buffer 133104477Ssam/// \param size Amount of data to read 134104477Ssam/// \param pos Offset relative to the beginning of the file, 135104477Ssam/// from which the data should be read. 136104477Ssam/// 137104477Ssam/// \return On success, false is returned. On error, error message 138104477Ssam/// is printed and true is returned. 139104477Ssamextern bool io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos); 140104477Ssam 141104477Ssam 142104477Ssam/// \brief Writes a buffer to the destination file 143104477Ssam/// 144104477Ssam/// \param pair File pair having the destination file open for writing 145104477Ssam/// \param buf Buffer containing the data to be written 146104477Ssam/// \param size Size of the buffer; assumed be smaller than SSIZE_MAX 147104477Ssam/// 148104477Ssam/// \return On success, zero is returned. On error, -1 is returned 149104477Ssam/// and error message printed. 150104477Ssamextern bool io_write(file_pair *pair, const io_buf *buf, size_t size); 151104477Ssam