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