file_io.c File Reference

File opening, unlinking, and closing. More...

`#include "private.h"`
`#include <fcntl.h>`
`#include <poll.h>`
`#include "tuklib_open_stdxxx.h"`

## 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]

## Detailed Description

File opening, unlinking, and closing.

## Function Documentation

## io_init()

void io_init | ( | void |  | ) |

Initialize the I/O module.

## io_write_to_user_abort_pipe()

void io_write_to_user_abort_pipe | ( | void |  | ) |

Write a byte to user_abort_pipe[1].

This is called from a signal handler.

## io_no_sparse()

void io_no_sparse | ( | void |  | ) |

Disable creation of sparse files when decompressing.

References try_sparse.

## io_wait()

static io_wait_ret io_wait | ( | file_pair * | pair,
|  | int | timeout,
|  | bool | is_reading
| ) |  |

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.

References file_pair::dest_fd, file_pair::src_fd, user_abort, and user_abort_pipe.

## io_unlink()

static void io_unlink | ( | const char * | name,
|  | const struct stat * | known_st
| ) |  |

static

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).

## io_copy_attrs()

static void io_copy_attrs | ( | const file_pair * | pair | ) |

static

Copies owner/group and permissions.

Todo:
  ACL and EA support

References file_pair::dest_fd, and file_pair::src_st.

## io_open_src_real()

static bool io_open_src_real | ( | file_pair * | pair | ) |

static

Opens the source file. Returns false on success, true on error.

References file_pair::src_name.

Referenced by io_open_src().

## io_open_src()

file_pair* io_open_src | ( | const char * | src_name | ) |

Open the source file.

References io_open_src_real(), is_empty_filename(), signals_block(), signals_unblock(), file_pair::src_fd, and file_pair::src_name.

Referenced by coder_run().

## io_close_src()

static void io_close_src | ( | file_pair * | pair,
|  | bool | success
| ) |  |

static

Closes source file of the file_pair structure.

Parameters

  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.

## io_open_dest()

bool io_open_dest | ( | file_pair * | pair | ) |

Open the destination file.

References signals_block().

## io_close_dest()

static bool io_close_dest | ( | file_pair * | pair,
|  | bool | success
| ) |  |

static

Closes destination file of the file_pair structure.

Parameters

  pair | File whose dest_fd should be closed
  success | If false, the file will be removed from the disk.

Returns
  Zero if closing succeeds. On error, -1 is returned and error message printed.

## io_close()

void io_close | ( | file_pair * | pair,
|  | bool | success
| ) |  |

Closes the file descriptors and frees possible allocated memory.

The success argument determines if source or destination file gets unlinked:

- false: The destination file is unlinked.
- true: The source file is unlinked unless writing to stdout or –keep was used.

References file_pair::dest_fd, file_pair::dest_pending_sparse, file_pair::dest_try_sparse, and message_error().

## io_fix_src_pos()

void io_fix_src_pos | ( | file_pair * | pair,
|  | size_t | rewind_size
| ) |  |

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.

Parameters

  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.

## io_read()

size_t io_read | ( | file_pair * | pair,
|  | io_buf * | buf,
|  | size_t | size
| ) |  |

Reads from the source file to a buffer.

Parameters

  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

Returns
  On success, number of bytes read is returned. On end of file zero is returned and pair->src_eof set to true. On error, SIZE_MAX is returned and error message printed.

Referenced by coder_passthru().

## io_pread()

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.

This is remotely similar to standard pread(). This uses lseek() though, so the read offset is changed on each call.

Parameters

  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.

Returns
  On success, false is returned. On error, error message is printed and true is returned.

References message_error(), and file_pair::src_fd.

Referenced by parse_check_value().

## io_write()

bool io_write | ( | file_pair * | pair,
|  | const io_buf * | buf,
|  | size_t | size
| ) |  |

Writes a buffer to the destination file.

Parameters

  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

Returns
  On success, zero is returned. On error, -1 is returned and error message printed.

Referenced by coder_passthru().

## Variable Documentation

## try_sparse

bool try_sparse = true

static

If true, try to create sparse files when decompressing.

Referenced by io_no_sparse().

## stdin_flags

int stdin_flags

static

File status flags of standard input. This is used by io_open_src() and io_close_src().

## stdout_flags

int stdout_flags

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.

## user_abort_pipe

int user_abort_pipe[2]

static

Self-pipe used together with the user_abort variable to avoid race conditions with signal handling.

Referenced by io_wait().