#include <File.h>
Public Member Functions | |
virtual void | abort (void) |
virtual void | attach (IOFD fd) |
virtual void | close (void) |
virtual void | create (const char *name, bool exclusive=false, int perms=0666) |
virtual void | create (const std::string &name, bool exclusive=false, int perms=0666) |
File (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666) | |
File (IOFD fd, bool autoclose=true) | |
File (const char *name, int flags=IOFlags::OpenRead, int perms=0666) | |
File (void) | |
virtual void | flush (void) |
virtual void | open (const char *name, int flags=IOFlags::OpenRead, int perms=0666) |
virtual void | open (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666) |
virtual IOOffset | position (IOOffset offset, Relative whence=SET) |
virtual bool | prefetch (const IOPosBuffer *what, IOSize n) |
virtual IOSize | read (void *into, IOSize n) |
virtual IOSize | read (void *into, IOSize n, IOOffset pos) |
virtual IOSize | readv (IOBuffer *into, IOSize length) |
virtual void | resize (IOOffset size) |
virtual void | setAutoClose (bool closeit) |
virtual IOOffset | size (void) const |
virtual IOSize | write (const void *from, IOSize n) |
virtual IOSize | write (const void *from, IOSize n, IOOffset pos) |
virtual IOSize | writev (const IOBuffer *from, IOSize length) |
~File (void) | |
Private Types | |
enum | { InternalAutoClose = 4096 } |
Private Member Functions | |
File * | duplicate (bool copy) const |
File * | duplicate (File *child) const |
File (IOFD fd, unsigned flags) | |
Static Private Member Functions | |
static bool | sysclose (IOFD fd, int *error=0) |
static IOFD | sysduplicate (IOFD fd) |
static void | sysopen (const char *name, int flags, int perms, IOFD &newfd, unsigned &newflags) |
Private Attributes | |
unsigned | m_flags |
anonymous enum [private] |
Definition at line 61 of file File.h.
{ InternalAutoClose = 4096 }; //< Close on delete
File::File | ( | void | ) |
Create a new file object without a file attached to it.
Definition at line 57 of file File.cc.
References EDM_IOFD_INVALID.
{ fd (EDM_IOFD_INVALID); m_flags = 0; }
File::File | ( | IOFD | fd, |
bool | autoclose = true |
||
) |
File::File | ( | const char * | name, |
int | flags = IOFlags::OpenRead , |
||
int | perms = 0666 |
||
) |
File::File | ( | const std::string & | name, |
int | flags = IOFlags::OpenRead , |
||
int | perms = 0666 |
||
) |
File::~File | ( | void | ) |
File::File | ( | IOFD | fd, |
unsigned | flags | ||
) | [private] |
void File::abort | ( | void | ) | [virtual] |
Close the file and ignore all errors.
Reimplemented in RemoteFile.
Definition at line 292 of file File.cc.
References EDM_IOFD_INVALID.
{ IOFD fd = this->fd (); if (fd != EDM_IOFD_INVALID) { sysclose (fd); m_flags &= ~InternalAutoClose; this->fd (EDM_IOFD_INVALID); } }
void File::attach | ( | IOFD | fd | ) | [virtual] |
void File::close | ( | void | ) | [virtual] |
Close the file.
Reimplemented from IOChannel.
Reimplemented in RemoteFile.
Definition at line 277 of file File.cc.
References EDM_IOFD_INVALID, error, and throwStorageError().
Referenced by LocalCacheFile::close().
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); int error; if (! sysclose (fd, &error)) throwStorageError("File::sysclose()", "close()", error); m_flags &= ~InternalAutoClose; this->fd (EDM_IOFD_INVALID); }
void File::create | ( | const char * | name, |
bool | exclusive = false , |
||
int | perms = 0666 |
||
) | [virtual] |
Create and open the file name in write mode. If exclusive, the creation fails if the file already exists, otherwise if the file exists, it will be truncated. The new file will have the permissions perms.
Definition at line 142 of file File.cc.
References IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.
{ open (name, (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms); }
void File::create | ( | const std::string & | name, |
bool | exclusive = false , |
||
int | perms = 0666 |
||
) | [virtual] |
Create and open the file name in write mode. If exclusive, the creation fails if the file already exists, otherwise if the file exists, it will be truncated. The new file will have the permissions perms.
Definition at line 155 of file File.cc.
References IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.
{ open (name.c_str (), (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms); }
File * File::duplicate | ( | bool | copy | ) | const [private] |
Duplicate the file object. If copy, also duplicates the underlying file descriptor, otherwise the two will point to the same descriptor. If the file descriptor is not copied, the copy will not close its file descriptor on destruction, the original object (this
) will.
Definition at line 117 of file File.cc.
References reco_application_tbsim_DetSim-Digi_cfg::File.
Internal implementation of duplicate() to actually duplicate the file handle into child.
Definition at line 126 of file File.cc.
References EDM_IOFD_INVALID, IOChannel::fd(), and m_flags.
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); assert (child); child->fd (sysduplicate (fd)); child->m_flags = m_flags; return child; }
void File::flush | ( | void | ) | [virtual] |
Flush the system's file system buffers for this file.
Reimplemented from Storage.
Definition at line 139 of file UnixFile.cc.
References EDM_IOFD_INVALID, and throwStorageError().
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); #if _POSIX_SYNCHRONIZED_IO > 0 if (fdatasync (fd) == -1) throwStorageError("File::flush()", "fdatasync()", errno); #elif _POSIX_FSYNC > 0 if (fsync (fd) == -1) throwStorageError("File::flush()", "fsync()", errno); #endif }
void File::open | ( | const std::string & | name, |
int | flags = IOFlags::OpenRead , |
||
int | perms = 0666 |
||
) | [virtual] |
Open or possibly create the file name with options specified in flags. If the file is to be created, it will be given the permissions perms. If this object already has a file open, it is closed first. Redirected to the overloaded method taking a "const char *" argument.
Definition at line 169 of file File.cc.
References flags.
void File::open | ( | const char * | name, |
int | flags = IOFlags::OpenRead , |
||
int | perms = 0666 |
||
) | [virtual] |
Open or possibly create the file name with options specified in flags. If the file is to be created, it will be given the permissions perms. If this object already has a file open, it is closed first.
Definition at line 177 of file File.cc.
References EDM_IOFD_INVALID, IOFlags::OpenRead, and IOFlags::OpenWrite.
{ // is zero and always implied. OTOH, existence check should be // done with Filename::exists() -- see comments there about what // can happen on a WIN32 remote share even if the file doesn't // exist. For now make sure that read or write was asked for. assert (name && *name); assert (flags & (OpenRead | OpenWrite)); // If I am already open, close the old file first. if (fd () != EDM_IOFD_INVALID && (m_flags & InternalAutoClose)) close (); IOFD newfd = EDM_IOFD_INVALID; unsigned newflags = InternalAutoClose; sysopen (name, flags, perms, newfd, newflags); fd (newfd); m_flags = newflags; }
Implements Storage.
Definition at line 112 of file UnixFile.cc.
References EDM_IOFD_INVALID, query::result, and throwStorageError().
Referenced by LocalCacheFile::position(), LocalCacheFile::read(), and LocalCacheFile::readv().
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); assert (whence == CURRENT || whence == SET || whence == END); IOOffset result; int mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END); if ((result = ::lseek (fd, offset, mywhence)) == -1) throwStorageError("File::position()", "lseek()", errno); return result; }
bool File::prefetch | ( | const IOPosBuffer * | what, |
IOSize | n | ||
) | [virtual] |
Prefetch data for the file.
Reimplemented from Storage.
Definition at line 210 of file File.cc.
References i, info, n, IOPosBuffer::offset(), evf::evtn::offset(), findQualityFiles::size, and IOPosBuffer::size().
Referenced by LocalCacheFile::prefetch().
{ IOFD fd = this->fd(); for (IOSize i = 0; i < n; ++i) { #if F_RDADVISE radvisory info; info.ra_offset = what[i].offset(); info.ra_count = what[i].size(); fcntl(fd, F_RDADVISE, &info); #elif _POSIX_ADVISORY_INFO > 0 posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED); #else # error advisory read ahead not available on this platform #endif } return true; }
Reimplemented from Storage.
Definition at line 63 of file UnixFile.cc.
References n, pos, asciidump::s, and throwStorageError().
Read from the file.
Reimplemented from IOChannel.
Definition at line 231 of file File.cc.
References SiPixelLorentzAngle_cfi::read.
Referenced by LocalCacheFile::read().
{ return IOChannel::read (into, n); }
Read from the file.
Reimplemented from IOChannel.
Definition at line 236 of file File.cc.
References IOChannel::readv().
Referenced by LocalCacheFile::readv().
{ return IOChannel::readv (into, length); }
void File::resize | ( | IOOffset | size | ) | [virtual] |
Resize to the file to size. If size is less than the file's current size, the file is truncated. If size is larger than the file's current size, the file is extended with zero bytes. Does not change the current file pointer.
Implements Storage.
Definition at line 129 of file UnixFile.cc.
References EDM_IOFD_INVALID, and throwStorageError().
Referenced by LocalCacheFile::LocalCacheFile().
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); if (ftruncate (fd, size) == -1) throwStorageError("File::resize()", "ftruncate()", errno); }
void File::setAutoClose | ( | bool | autoclose | ) | [virtual] |
Set the autoclose flag of the file. If autoclose is true
, the destructor will automatically try to close the underlying file descriptor. Otherwise the file descriptor will be left open. Set the flag off if the file descriptor is originally owned by someone else.
Definition at line 103 of file File.cc.
{ m_flags &= ~InternalAutoClose; if (autoclose) m_flags |= InternalAutoClose; }
IOOffset File::size | ( | void | ) | const [virtual] |
Get the size of the file.
Reimplemented from Storage.
Definition at line 99 of file UnixFile.cc.
References EDM_IOFD_INVALID, and throwStorageError().
{ IOFD fd = this->fd (); assert (fd != EDM_IOFD_INVALID); struct stat info; if (fstat (fd, &info) == -1) throwStorageError("File::size()", "fstat()", errno); return info.st_size; }
bool File::sysclose | ( | IOFD | fd, |
int * | error = 0 |
||
) | [static, private] |
Actually close a file handle and return error code.
Reimplemented from IOChannel.
Definition at line 154 of file UnixFile.cc.
References runTheMatrix::ret.
Definition at line 9 of file UnixFile.cc.
References EDM_IOFD_INVALID, and throwStorageError().
{ IOFD copyfd; if ((copyfd = ::dup (fd)) == EDM_IOFD_INVALID) throwStorageError ("File::sysduplicate()", "dup()", errno); return copyfd; }
void File::sysopen | ( | const char * | name, |
int | flags, | ||
int | perms, | ||
IOFD & | newfd, | ||
unsigned & | newflags | ||
) | [static, private] |
Definition at line 19 of file UnixFile.cc.
References O_NONBLOCK, IOFlags::OpenAppend, IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenNonBlock, IOFlags::OpenNotCTTY, IOFlags::OpenRead, IOFlags::OpenTruncate, IOFlags::OpenUnbuffered, IOFlags::OpenWrite, and throwStorageError().
{ // Translate our flags to system flags. int openflags = 0; if ((flags & OpenRead) && (flags & OpenWrite)) openflags |= O_RDWR; else if (flags & OpenRead) openflags |= O_RDONLY; else if (flags & OpenWrite) openflags |= O_WRONLY; if (flags & OpenNonBlock) openflags |= O_NONBLOCK; if (flags & OpenAppend) openflags |= O_APPEND; #ifdef O_SYNC if (flags & OpenUnbuffered) openflags |= O_SYNC; #else if (flags & OpenUnbuffered) newflags |= OpenUnbuffered; #endif if (flags & OpenCreate) openflags |= O_CREAT; if (flags & OpenExclusive) openflags |= O_EXCL; if (flags & OpenTruncate) openflags |= O_TRUNC; if (flags & OpenNotCTTY) openflags |= O_NOCTTY; if ((newfd = ::open (name, openflags, perms)) == -1) throwStorageError ("File::sysopen()", "open()", errno); }
Write to the file.
Reimplemented from IOChannel.
Definition at line 241 of file File.cc.
References IOFlags::OpenAppend, IOFlags::OpenUnbuffered, position, asciidump::s, and IOChannel::write().
{ // FIXME: This may create a race condition or cause trouble on // remote files. Should be currently needed only on WIN32. if (m_flags & OpenAppend) position (0, END); IOSize s = IOChannel::write (from, n); if (m_flags & OpenUnbuffered) // FIXME: Exception handling? flush (); return s; }
Reimplemented from Storage.
Definition at line 79 of file UnixFile.cc.
References Capri::details::from(), n, IOFlags::OpenUnbuffered, pos, asciidump::s, and throwStorageError().
Write to the file.
Reimplemented from IOChannel.
Definition at line 259 of file File.cc.
References IOFlags::OpenAppend, IOFlags::OpenUnbuffered, position, asciidump::s, and IOChannel::writev().
{ // FIXME: This may create a race condition or cause trouble on // remote files. Should be currently needed only on WIN32. if (m_flags & OpenAppend) position (0, END); IOSize s = IOChannel::writev (from, length); if (m_flags & OpenUnbuffered) // FIXME: Exception handling? flush (); return s; }
unsigned File::m_flags [private] |
Definition at line 72 of file File.h.
Referenced by duplicate().