xapian-core  2.0.0
Functions | Variables
io_utils.cc File Reference

Wrappers for low-level POSIX I/O routines. More...

#include <config.h>
#include "io_utils.h"
#include "posixy_wrapper.h"
#include "safeunistd.h"
#include <cerrno>
#include <cstring>
#include <limits>
#include <string>
#include <xapian/error.h>
#include "omassert.h"
#include "str.h"
+ Include dependency graph for io_utils.cc:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool io_unlink (const std::string &filename)
 Delete a file. More...
 
static int move_to_higher_fd_ (int fd)
 
static int move_to_higher_fd (int fd)
 
static void protect_from_write (int fd)
 Protect against stray writes to fds we use pwrite() on. More...
 
int io_open_block_wr (const char *filename, bool anew)
 Open a block-based file for writing. More...
 
int io_open_stream_wr (const char *filename, bool anew)
 Open a stream-based file for writing. More...
 
size_t io_read (int fd, char *p, size_t n, size_t min)
 Read n bytes (or until EOF) into block pointed to by p from file descriptor fd. More...
 
void io_write (int fd, const char *p, size_t n)
 Write n bytes from block pointed to by p to file descriptor fd. More...
 
size_t io_pread (int fd, char *p, size_t n, off_t o, size_t min)
 Read n bytes (or until EOF) into block pointed to by p from file descriptor fd starting at position o. More...
 
void io_pwrite (int fd, const char *p, size_t n, off_t o)
 Write n bytes from block pointed to by p to file descriptor fd starting at position o. More...
 
static void throw_block_error (const char *s, off_t b, int e=0)
 
void io_read_block (int fd, char *p, size_t n, off_t b, off_t o)
 Read block b size n bytes into buffer p from file descriptor fd, offset o. More...
 
void io_write_block (int fd, const char *p, size_t n, off_t b, off_t o)
 Write block b size n bytes from buffer p to file descriptor fd, offset o. More...
 
bool io_tmp_rename (const std::string &tmp_file, const std::string &real_file)
 Rename a temporary file to its final position. More...
 

Variables

const int MIN_WRITE_FD = 3
 

Detailed Description

Wrappers for low-level POSIX I/O routines.

Definition in file io_utils.cc.

Function Documentation

◆ io_open_block_wr()

int io_open_block_wr ( const char *  filename,
bool  anew 
)

Open a block-based file for writing.

Parameters
filenameThe path of the file to open.
anewIf true, open the file anew (create or truncate it).

Definition at line 192 of file io_utils.cc.

References move_to_higher_fd(), O_BINARY, O_CLOEXEC, Xapian::Remote::open(), and protect_from_write().

Referenced by GlassTable::do_open_to_write(), io_open_block_wr(), and test_ioblock1().

◆ io_open_stream_wr()

int io_open_stream_wr ( const char *  filename,
bool  anew 
)

Open a stream-based file for writing.

Parameters
filenameThe path of the file to open.
anewIf true, open the file anew (create or truncate it).

Definition at line 231 of file io_utils.cc.

References move_to_higher_fd(), O_BINARY, O_CLOEXEC, and Xapian::Remote::open().

Referenced by io_open_stream_wr(), and BufferedFile::open().

◆ io_pread()

size_t io_pread ( int  fd,
char *  p,
size_t  n,
off_t  o,
size_t  min = 0 
)

Read n bytes (or until EOF) into block pointed to by p from file descriptor fd starting at position o.

If a read error occurs, throws DatabaseError.

If min is specified and EOF is reached after less than min bytes, throws DatabaseCorruptError.

The current file position may or may not be updated.

Returns the number of bytes actually read.

Definition at line 277 of file io_utils.cc.

References p, rare, and usual.

Referenced by BufferedFile::read().

◆ io_pwrite()

void io_pwrite ( int  fd,
const char *  p,
size_t  n,
off_t  o 
)

Write n bytes from block pointed to by p to file descriptor fd starting at position o.

If a read error occurs, throws DatabaseError.

The current file position may or may not be updated.

Definition at line 363 of file io_utils.cc.

References io_write(), p, rare, and usual.

◆ io_read()

size_t io_read ( int  fd,
char *  p,
size_t  n,
size_t  min = 0 
)

Read n bytes (or until EOF) into block pointed to by p from file descriptor fd.

If a read error occurs, throws DatabaseError.

If min is specified and EOF is reached after less than min bytes, throws DatabaseCorruptError.

Returns the number of bytes actually read.

Definition at line 241 of file io_utils.cc.

References p.

Referenced by GlassChanges::check(), GlassDatabase::get_changeset_revisions(), GlassVersion::read(), HoneyVersion::read(), and test_if_single_file_db_().

◆ io_read_block()

void io_read_block ( int  fd,
char *  p,
size_t  n,
off_t  b,
off_t  o 
)

Read block b size n bytes into buffer p from file descriptor fd, offset o.

Definition at line 432 of file io_utils.cc.

References p, rare, throw_block_error(), and usual.

Referenced by GlassTable::read_block(), and test_ioblock1().

◆ io_tmp_rename()

bool io_tmp_rename ( const std::string &  tmp_file,
const std::string &  real_file 
)

Rename a temporary file to its final position.

Attempts to deal with NFS infelicities. If the rename fails, the temporary file is removed.

Returns
true if the rename succeeded; false if it failed (and errno will be set appropriately).

Definition at line 573 of file io_utils.cc.

References posixy_rename.

Referenced by GlassChanges::commit(), Xapian::Database::compact_(), GlassDatabaseReplicator::process_changeset_chunk_version(), GlassVersion::sync(), HoneyVersion::sync(), and Xapian::DatabaseReplica::Internal::update_stub_database().

◆ io_unlink()

bool io_unlink ( const std::string &  filename)

Delete a file.

Parameters
filenameThe file to delete.
Exceptions
Xapian::DatabaseErroris thrown if filename existed but couldn't be unlinked.
Returns
true if filename was successfully removed; false if it didn't exist. If the file is on NFS, false may be returned even if the file was removed (if the server fails after removing the file but before telling the client, and the client then retries).

Definition at line 56 of file io_utils.cc.

References posixy_unlink.

Referenced by GlassChanges::commit(), GlassTable::create_and_open(), test_ioblock1(), and GlassChanges::~GlassChanges().

◆ io_write()

void io_write ( int  fd,
const char *  p,
size_t  n 
)

◆ io_write_block()

void io_write_block ( int  fd,
const char *  p,
size_t  n,
off_t  b,
off_t  o 
)

Write block b size n bytes from buffer p to file descriptor fd, offset o.

Definition at line 507 of file io_utils.cc.

References p, rare, throw_block_error(), and usual.

Referenced by io_write_block(), GlassDatabaseReplicator::process_changeset_chunk_blocks(), test_ioblock1(), and GlassTable::write_block().

◆ move_to_higher_fd()

static int move_to_higher_fd ( int  fd)
inlinestatic

Definition at line 122 of file io_utils.cc.

References MIN_WRITE_FD, move_to_higher_fd_(), and usual.

Referenced by io_open_block_wr(), and io_open_stream_wr().

◆ move_to_higher_fd_()

static int move_to_higher_fd_ ( int  fd)
static

Definition at line 75 of file io_utils.cc.

References Assert, close(), and MIN_WRITE_FD.

Referenced by move_to_higher_fd().

◆ protect_from_write()

static void protect_from_write ( int  fd)
inlinestatic

Protect against stray writes to fds we use pwrite() on.

Set the file position high to protect against user code or other libraries accidentally trying to write to our fd. To avoid problems we're rolling this out gradually on platforms we've tested it on.

Definition at line 135 of file io_utils.cc.

Referenced by io_open_block_wr().

◆ throw_block_error()

static void throw_block_error ( const char *  s,
off_t  b,
int  e = 0 
)
static

Definition at line 413 of file io_utils.cc.

References Xapian::Internal::str().

Referenced by io_read_block(), and io_write_block().

Variable Documentation

◆ MIN_WRITE_FD

const int MIN_WRITE_FD = 3

Definition at line 72 of file io_utils.cc.

Referenced by move_to_higher_fd(), and move_to_higher_fd_().