Rizin
unix-like reverse engineering framework and cli tools
|
File opening, unlinking, and closing. More...
Go to the source code of this file.
Macros | |
#define | O_BINARY 0 |
#define | O_NOCTTY 0 |
#define | IS_EAGAIN_OR_EWOULDBLOCK(e) ((e) == EAGAIN) |
Enumerations | |
enum | io_wait_ret { IO_WAIT_MORE , IO_WAIT_ERROR , IO_WAIT_TIMEOUT } |
Functions | |
static bool | io_write_buf (file_pair *pair, const uint8_t *buf, size_t size) |
void | io_init (void) |
Initialize the I/O module. More... | |
void | io_write_to_user_abort_pipe (void) |
Write a byte to user_abort_pipe[1]. More... | |
void | io_no_sparse (void) |
Disable creation of sparse files when decompressing. More... | |
static io_wait_ret | io_wait (file_pair *pair, int timeout, bool is_reading) |
Waits for input or output to become available or for a signal. More... | |
static void | io_unlink (const char *name, const struct stat *known_st) |
Unlink a file. More... | |
static void | io_copy_attrs (const file_pair *pair) |
Copies owner/group and permissions. More... | |
static bool | io_open_src_real (file_pair *pair) |
Opens the source file. Returns false on success, true on error. More... | |
file_pair * | io_open_src (const char *src_name) |
Open the source file. More... | |
static void | io_close_src (file_pair *pair, bool success) |
Closes source file of the file_pair structure. More... | |
static bool | io_open_dest_real (file_pair *pair) |
bool | io_open_dest (file_pair *pair) |
Open the destination file. More... | |
static bool | io_close_dest (file_pair *pair, bool success) |
Closes destination file of the file_pair structure. More... | |
void | io_close (file_pair *pair, bool success) |
Closes the file descriptors and frees possible allocated memory. More... | |
void | io_fix_src_pos (file_pair *pair, size_t rewind_size) |
Fix the position in src_fd. More... | |
size_t | io_read (file_pair *pair, io_buf *buf, size_t size) |
Reads from the source file to a buffer. More... | |
bool | io_pread (file_pair *pair, io_buf *buf, size_t size, off_t pos) |
Read from source file from given offset to a buffer. More... | |
static bool | is_sparse (const io_buf *buf) |
bool | io_write (file_pair *pair, const io_buf *buf, size_t size) |
Writes a buffer to the destination file. More... | |
Variables | |
static bool | warn_fchown |
static bool | try_sparse = true |
If true, try to create sparse files when decompressing. More... | |
static int | stdin_flags |
static bool | restore_stdin_flags = false |
static int | stdout_flags |
static bool | restore_stdout_flags = false |
static int | user_abort_pipe [2] |
File opening, unlinking, and closing.
Definition in file file_io.c.
enum io_wait_ret |
Closes the file descriptors and frees possible allocated memory.
The success argument determines if source or destination file gets unlinked:
Definition at line 1052 of file file_io.c.
References _, file_pair::dest_fd, file_pair::dest_name, file_pair::dest_pending_sparse, file_pair::dest_try_sparse, io_close_dest(), io_close_src(), io_copy_attrs(), io_write_buf(), lseek, message_error(), SEEK_CUR, signals_block(), signals_unblock(), and STDOUT_FILENO.
Referenced by coder_run(), and list_file().
Closes destination file of the file_pair structure.
pair | File whose dest_fd should be closed |
success | If false, the file will be removed from the disk. |
Definition at line 1008 of file file_io.c.
References _, assert(), close, file_pair::dest_fd, file_pair::dest_name, file_pair::dest_st, F_SETFL, fcntl, free(), io_unlink(), message_error(), restore_stdout_flags, STDOUT_FILENO, and stdout_flags.
Referenced by io_close().
Closes source file of the file_pair structure.
pair | File whose src_fd should be closed |
success | If true, the file will be removed from the disk if closing succeeds and –keep hasn't been used. |
Definition at line 783 of file file_io.c.
References _, assert(), close, F_SETFL, fcntl, io_unlink(), message_error(), opt_keep_original, restore_stdin_flags, file_pair::src_fd, file_pair::src_name, file_pair::src_st, STDIN_FILENO, and stdin_flags.
Referenced by io_close().
Copies owner/group and permissions.
Definition at line 352 of file file_io.c.
References _, file_pair::dest_fd, file_pair::dest_name, fchmod, message_warning(), NULL, file_pair::src_st, tv, utime, and warn_fchown.
Referenced by io_close().
Fix the position in src_fd.
This is used when –single-thream has been specified and decompression is successful. If the input file descriptor supports seeking, this function fixes the input position to point to the next byte after the decompressed stream.
pair | File pair having the source file open for reading |
rewind_size | How many bytes of extra have been read i.e. how much to seek backwards. |
Definition at line 1101 of file file_io.c.
References assert(), IO_BUFFER_SIZE, lseek, SEEK_CUR, and file_pair::src_fd.
Referenced by coder_normal().
void io_init | ( | void | ) |
Initialize the I/O module.
Definition at line 95 of file file_io.c.
References _, E_ERROR, F_GETFL, F_SETFL, fcntl, flags, i, message_fatal(), O_NONBLOCK, pipe, tuklib_open_stdxxx(), user_abort_pipe, and warn_fchown.
Referenced by main().
void io_no_sparse | ( | void | ) |
Disable creation of sparse files when decompressing.
Definition at line 151 of file file_io.c.
References try_sparse.
Referenced by parse_real().
Open the destination file.
Definition at line 991 of file file_io.c.
References io_open_dest_real(), signals_block(), and signals_unblock().
Referenced by coder_run().
Definition at line 822 of file file_io.c.
References _, file_pair::dest_fd, file_pair::dest_name, file_pair::dest_st, file_pair::dest_try_sparse, ENOENT, F_GETFL, F_SETFL, fcntl, flags, free(), fstat, lseek, message_error(), MODE_DECOMPRESS, NULL, O_APPEND, O_BINARY, O_CREAT, O_EXCL, O_NOCTTY, O_NONBLOCK, O_WRONLY, opt_force, opt_mode, opt_stdout, restore_stdout_flags, S_ISREG, SEEK_CUR, SEEK_END, file_pair::src_fd, file_pair::src_name, file_pair::src_st, stat, STDIN_FILENO, STDOUT_FILENO, stdout_flags, suffix_get_dest_name(), try_sparse, and unlink.
Referenced by io_open_dest().
Open the source file.
Definition at line 741 of file file_io.c.
References error(), io_open_src_real(), is_empty_filename(), NULL, signals_block(), signals_unblock(), file_pair::src_fd, and file_pair::src_name.
Referenced by coder_run(), and list_file().
Opens the source file. Returns false on success, true on error.
Definition at line 502 of file file_io.c.
References _, assert(), close, EFTYPE, EINTR, EMLINK, error(), F_GETFL, F_SETFL, fcntl, flags, fstat, io_wait(), IO_WAIT_MORE, message_error(), message_warning(), MODE_LIST, O_BINARY, O_NOCTTY, O_NOFOLLOW, O_NONBLOCK, O_RDONLY, opt_force, opt_mode, opt_stdout, restore_stdin_flags, S_ISDIR, S_ISREG, signals_block(), signals_unblock(), file_pair::src_fd, file_pair::src_name, file_pair::src_st, stat, stdin_filename, STDIN_FILENO, and stdin_flags.
Referenced by io_open_src().
Read from source file from given offset to a buffer.
This is remotely similar to standard pread(). This uses lseek() though, so the read offset is changed on each call.
pair | Seekable source file |
buf | Destination buffer |
size | Amount of data to read |
pos | Offset relative to the beginning of the file, from which the data should be read. |
Definition at line 1186 of file file_io.c.
References _, io_read(), lseek, message_error(), pos, SEEK_SET, SIZE_MAX, file_pair::src_fd, and file_pair::src_name.
Referenced by parse_block_header(), parse_check_value(), and parse_indexes().
Reads from the source file to a buffer.
pair | File pair having the source file open for reading |
buf | Destination buffer to hold the read data |
size | Size of the buffer; assumed be smaller than SSIZE_MAX |
Definition at line 1116 of file file_io.c.
References _, assert(), EINTR, file_pair::flush_needed, io_wait(), IO_WAIT_ERROR, IO_WAIT_MORE, IO_WAIT_TIMEOUT, IS_EAGAIN_OR_EWOULDBLOCK, message_bug(), message_error(), mytime_get_flush_timeout(), mytime_set_flush_time(), pos, read(), SIZE_MAX, file_pair::src_eof, file_pair::src_fd, file_pair::src_has_seen_input, file_pair::src_name, SSIZE_MAX, timeout, and user_abort.
Referenced by coder_normal(), coder_passthru(), coder_run(), and io_pread().
Unlink a file.
This tries to verify that the file being unlinked really is the file that we want to unlink by verifying device and inode numbers. There's still a small unavoidable race, but this is much better than nothing (the file could have been moved/replaced even hours earlier).
Definition at line 290 of file file_io.c.
References _, message_error(), opt_force, stat, and unlink.
Referenced by io_close_dest(), and io_close_src().
|
static |
Waits for input or output to become available or for a signal.
This uses the self-pipe trick to avoid a race condition that can occur if a signal is caught after user_abort has been checked but before e.g. read() has been called. In that situation read() could block unless non-blocking I/O is used. With non-blocking I/O something like select() or poll() is needed to avoid a busy-wait loop, and the same race condition pops up again. There are pselect() (POSIX-1.2001) and ppoll() (not in POSIX) but neither is portable enough in 2013. The self-pipe trick is old and very portable.
Definition at line 241 of file file_io.c.
References _, file_pair::dest_fd, file_pair::dest_name, EAGAIN, EINTR, IO_WAIT_ERROR, IO_WAIT_MORE, IO_WAIT_TIMEOUT, message_error(), poll, file_pair::src_fd, file_pair::src_name, timeout, user_abort, and user_abort_pipe.
Referenced by io_open_src_real(), io_read(), and io_write_buf().
Writes a buffer to the destination file.
pair | File pair having the destination file open for writing |
buf | Buffer containing the data to be written |
size | Size of the buffer; assumed be smaller than SSIZE_MAX |
Definition at line 1275 of file file_io.c.
References _, assert(), CHAR_BIT, file_pair::dest_fd, file_pair::dest_name, file_pair::dest_pending_sparse, file_pair::dest_try_sparse, IO_BUFFER_SIZE, io_write_buf(), is_sparse(), lseek, message_error(), and SEEK_CUR.
Referenced by coder_passthru(), and coder_write_output().
Definition at line 1224 of file file_io.c.
References _, assert(), file_pair::dest_fd, file_pair::dest_name, EINTR, EPIPE, io_wait(), IO_WAIT_MORE, IS_EAGAIN_OR_EWOULDBLOCK, message_error(), SSIZE_MAX, user_abort, and write.
Referenced by io_close(), and io_write().
void io_write_to_user_abort_pipe | ( | void | ) |
Write a byte to user_abort_pipe[1].
This is called from a signal handler.
Definition at line 135 of file file_io.c.
References b, user_abort_pipe, and write.
Referenced by signal_handler().
Definition at line 1211 of file file_io.c.
References ARRAY_SIZE, assert(), i, and IO_BUFFER_SIZE.
Referenced by io_write().
Definition at line 78 of file file_io.c.
Referenced by io_close_src(), and io_open_src_real().
Definition at line 83 of file file_io.c.
Referenced by io_close_dest(), and io_open_dest_real().
|
static |
File status flags of standard input. This is used by io_open_src() and io_close_src().
Definition at line 77 of file file_io.c.
Referenced by io_close_src(), and io_open_src_real().
|
static |
Original file status flags of standard output. This is used by io_open_dest() and io_close_dest() to save and restore the flags.
Definition at line 82 of file file_io.c.
Referenced by io_close_dest(), and io_open_dest_real().
If true, try to create sparse files when decompressing.
Definition at line 67 of file file_io.c.
Referenced by io_no_sparse(), and io_open_dest_real().
|
static |
Self-pipe used together with the user_abort variable to avoid race conditions with signal handling.
Definition at line 87 of file file_io.c.
Referenced by io_init(), io_wait(), and io_write_to_user_abort_pipe().
|
static |
Definition at line 21 of file file_io.c.
Referenced by io_copy_attrs(), and io_init().