CMS 3D CMS Logo

Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes

File Class Reference

#include <File.h>

Inheritance diagram for File:
IOChannel Storage IOInput IOOutput IOInput IOOutput RemoteFile

List of all members.

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

Fileduplicate (bool copy) const
Fileduplicate (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

Detailed Description

Basic file-related functions. Nicked from SEAL.

Definition at line 11 of file File.h.


Member Enumeration Documentation

anonymous enum [private]
Enumerator:
InternalAutoClose 

Definition at line 61 of file File.h.

{ InternalAutoClose = 4096 }; //< Close on delete

Constructor & Destructor Documentation

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.

File::File ( IOFD  fd,
bool  autoclose = true 
)

Create a new file object from a file descriptor. The descriptor will be closed automatically when the file object is destructed if autoclose is true (the default).

Definition at line 66 of file File.cc.

{
  this->fd (fd);
  m_flags = autoclose ? InternalAutoClose : 0;
}
File::File ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Create a new file object by calling open() with the given arguments.

Definition at line 80 of file File.cc.

{ open (name, flags, perms); }
File::File ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Create a new file object by calling open() with the given arguments.

Definition at line 84 of file File.cc.

References flags.

{ open (name.c_str (), flags, perms); }
File::~File ( void  )

Release the resources held by the file object. If the object holds a valid file descriptor given to it through the constructor or obtained by calling open(), the descriptor will be closed.

Definition at line 90 of file File.cc.

File::File ( IOFD  fd,
unsigned  flags 
) [private]

Internal function for copying file objects to retain the state flags.

Definition at line 73 of file File.cc.

References flags.

{
  this->fd (fd);
  m_flags = flags;
}

Member Function Documentation

void File::abort ( void  ) [virtual]

Close the file and ignore all errors.

Reimplemented in RemoteFile.

Definition at line 293 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]

Definition at line 201 of file File.cc.

{
  this->fd (fd);
  m_flags = 0;
}
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("FileCloseError", "Calling File::close()",
                      "sysclose", 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.

{
  File *dup = new File (fd (), copy ? m_flags : 0);
  return copy ? this->duplicate (dup) : dup;
}
File * File::duplicate ( File child) const [private]

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 140 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("FileFlushError", "Calling File::flush()", "fdatasync()", errno);
#elif _POSIX_FSYNC > 0
  if (fsync (fd) == -1)
    throwStorageError("FileFlushError", "Calling 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.

{ open (name.c_str (), flags, perms); }
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;
}
IOOffset File::position ( IOOffset  offset,
Relative  whence = SET 
) [virtual]

Implements Storage.

Definition at line 113 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("FilePositionError", "Calling 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;
}
IOSize File::read ( void *  into,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 64 of file UnixFile.cc.

References edm::errors::FileReadError, n, pos, alignCSCRings::s, and throwStorageError().

{
  assert (pos >= 0);

  ssize_t s;
  do
    s = ::pread (fd (), into, n, pos);
  while (s == -1 && errno == EINTR);

  if (s == -1)
    throwStorageError(edm::errors::FileReadError, "Calling File::read()", "pread()", errno);

  return s;
}
IOSize File::read ( void *  into,
IOSize  n 
) [virtual]

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); }
IOSize File::readv ( IOBuffer into,
IOSize  length 
) [virtual]

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 130 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("FileResizeError", "Calling 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 100 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("FileSizeError", "Calling 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 155 of file UnixFile.cc.

References run_regression::ret.

{
  int ret = ::close (fd);
  if (error) *error = errno;
  return ret != -1;
}
IOFD File::sysduplicate ( IOFD  fd) [static, private]

Definition at line 10 of file UnixFile.cc.

References EDM_IOFD_INVALID, and throwStorageError().

{
  IOFD copyfd;
  if ((copyfd = ::dup (fd)) == EDM_IOFD_INVALID)
    throwStorageError ("FileDuplicateError", "Calling 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 20 of file UnixFile.cc.

References edm::errors::FileOpenError, 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 (edm::errors::FileOpenError, "Calling File::sysopen()", "open()", errno);
}
IOSize File::write ( const void *  from,
IOSize  n 
) [virtual]

Write to the file.

Reimplemented from IOChannel.

Definition at line 241 of file File.cc.

References IOFlags::OpenAppend, IOFlags::OpenUnbuffered, position, alignCSCRings::s, and TablePrint::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;
}
IOSize File::write ( const void *  from,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 80 of file UnixFile.cc.

References n, IOFlags::OpenUnbuffered, pos, alignCSCRings::s, and throwStorageError().

{
  assert (pos >= 0);

  ssize_t s;
  do
    s = ::pwrite (fd (), from, n, pos);
  while (s == -1 && errno == EINTR);

  if (s == -1)
    throwStorageError("FileWriteError", "Calling File::write()", "pwrite()", errno);

  if (m_flags & OpenUnbuffered)
    // FIXME: Exception handling?
    flush ();

  return s;
}
IOSize File::writev ( const IOBuffer from,
IOSize  length 
) [virtual]

Write to the file.

Reimplemented from IOChannel.

Definition at line 259 of file File.cc.

References IOFlags::OpenAppend, IOFlags::OpenUnbuffered, position, alignCSCRings::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;
}

Member Data Documentation

unsigned File::m_flags [private]

Definition at line 72 of file File.h.

Referenced by duplicate().