CMS 3D CMS Logo

File Class Reference

Basic file-related functions. More...

#include <Utilities/StorageFactory/interface/File.h>

Inheritance diagram for File:

IOChannel Storage IOInput IOOutput IOInput IOOutput RemoteFile

List of all members.

Public Member Functions

virtual void abort (void)
 Close the file and ignore all errors.
virtual void attach (IOFD fd)
virtual void close (void)
 Close the file.
virtual void create (const std::string &name, bool exclusive=false, int perms=0666)
 Create and open the file name in write mode.
virtual void create (const char *name, bool exclusive=false, int perms=0666)
 Create and open the file name in write mode.
 File (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 Create a new file object by calling open() with the given arguments.
 File (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 Create a new file object by calling open() with the given arguments.
 File (IOFD fd, bool autoclose=true)
 Create a new file object from a file descriptor.
 File (void)
 Create a new file object without a file attached to it.
virtual void flush (void)
 Flush the system's file system buffers for this file.
virtual void open (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 Open or possibly create the file name with options specified in flags.
virtual void open (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 Open or possibly create the file name with options specified in flags.
virtual IOOffset position (IOOffset offset, Relative whence=SET)
 Move the current file pointer to offset relative to whence.
virtual bool prefetch (const IOPosBuffer *what, IOSize n)
 Prefetch data for the file.
virtual IOSize read (void *into, IOSize n, IOOffset pos)
virtual IOSize read (void *into, IOSize n)
 Read from the file.
virtual IOSize readv (IOBuffer *into, IOSize length)
 Read from the file.
virtual void resize (IOOffset size)
 Resize to the file to size.
virtual void setAutoClose (bool closeit)
 Set the autoclose flag of the file.
virtual IOOffset size (void) const
 Get the size of the file.
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
virtual IOSize write (const void *from, IOSize n)
 Write to the file.
virtual IOSize writev (const IOBuffer *from, IOSize length)
 Write to the file.
 ~File (void)
 Release the resources held by the file object.

Private Types

enum  { InternalAutoClose = 4096 }

Private Member Functions

Fileduplicate (File *child) const
 Internal implementation of duplicate() to actually duplicate the file handle into child.
Fileduplicate (bool copy) const
 Duplicate the file object.
 File (IOFD fd, unsigned flags)
 Internal function for copying file objects to retain the state flags.

Static Private Member Functions

static bool sysclose (IOFD fd, int *error=0)
 Actually close a file handle and return error code.
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.

00061 { 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, IOChannel::fd(), and m_flags.

Referenced by duplicate().

00058 {
00059   fd (EDM_IOFD_INVALID);
00060   m_flags = 0;
00061 }

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.

References IOChannel::fd(), InternalAutoClose, and m_flags.

00067 {
00068   this->fd (fd);
00069   m_flags = autoclose ? InternalAutoClose : 0;
00070 }

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.

References open().

00081 { 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 open().

00085 { 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.

References abort(), InternalAutoClose, and m_flags.

00091 {
00092   if (m_flags & InternalAutoClose)
00093     abort ();
00094 }

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 IOChannel::fd(), and m_flags.

00074 {
00075   this->fd (fd);
00076   m_flags = flags;
00077 }


Member Function Documentation

void File::abort ( void   )  [virtual]

Close the file and ignore all errors.

Reimplemented in RemoteFile.

Definition at line 281 of file File.cc.

References EDM_IOFD_INVALID, IOChannel::fd(), InternalAutoClose, m_flags, and sysclose().

Referenced by RemoteFile::abort(), and ~File().

00282 {
00283   IOFD fd = this->fd ();
00284   if (fd != EDM_IOFD_INVALID)
00285   {
00286     sysclose (fd);
00287     m_flags &= ~InternalAutoClose;
00288     this->fd (EDM_IOFD_INVALID);
00289   }
00290 }

void File::attach ( IOFD  fd  )  [virtual]

Definition at line 201 of file File.cc.

References IOChannel::fd(), and m_flags.

00202 {
00203   this->fd (fd);
00204   m_flags = 0;
00205 }

void File::close ( void   )  [virtual]

Close the file.

Reimplemented from IOChannel.

Reimplemented in RemoteFile.

Definition at line 266 of file File.cc.

References EDM_IOFD_INVALID, error, IOChannel::fd(), InternalAutoClose, m_flags, sysclose(), and throwStorageError().

Referenced by RemoteFile::close(), LocalCacheFile::close(), open(), and sysclose().

00267 {
00268   IOFD fd = this->fd ();
00269   assert (fd != EDM_IOFD_INVALID);
00270 
00271   int error;
00272   if (! sysclose (fd, &error))
00273     throwStorageError("File::sysclose()", "close()", error);
00274 
00275   m_flags &= ~InternalAutoClose;
00276   this->fd (EDM_IOFD_INVALID);
00277 }

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 open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

00156 {
00157   open (name.c_str (),
00158         (OpenCreate | OpenWrite | OpenTruncate
00159          | (exclusive ? OpenExclusive : 0)),
00160         perms);
00161 }

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 open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

00143 {
00144   open (name,
00145         (OpenCreate | OpenWrite | OpenTruncate
00146          | (exclusive ? OpenExclusive : 0)),
00147         perms);
00148 }

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(), m_flags, and sysduplicate().

00127 {
00128   IOFD fd = this->fd ();
00129   assert (fd != EDM_IOFD_INVALID);
00130   assert (child);
00131   child->fd (sysduplicate (fd));
00132   child->m_flags = m_flags;
00133   return child;
00134 }

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 dup, IOChannel::fd(), File(), and m_flags.

00118 {
00119   File *dup = new File (fd (), copy ? m_flags : 0);
00120   return copy ? this->duplicate (dup) : dup;
00121 }

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, IOChannel::fd(), and throwStorageError().

Referenced by write(), and writev().

00140 {
00141   IOFD fd = this->fd ();
00142   assert (fd != EDM_IOFD_INVALID);
00143 
00144   if (fdatasync (fd) == -1)
00145     throwStorageError("File::flush()", "fdatasync()", errno);
00146 }

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

00170 { 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 close(), EDM_IOFD_INVALID, IOChannel::fd(), InternalAutoClose, m_flags, IOFlags::OpenRead, IOFlags::OpenWrite, and sysopen().

Referenced by create(), File(), and open().

00178 {
00179   // is zero and always implied.  OTOH, existence check should be
00180   // done with Filename::exists() -- see comments there about what
00181   // can happen on a WIN32 remote share even if the file doesn't
00182   // exist.  For now make sure that read or write was asked for.
00183 
00184   assert (name && *name);
00185   assert (flags & (OpenRead | OpenWrite));
00186 
00187   // If I am already open, close the old file first.
00188   if (fd () != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
00189     close ();
00190     
00191   IOFD          newfd = EDM_IOFD_INVALID;
00192   unsigned      newflags = InternalAutoClose;
00193 
00194   sysopen (name, flags, perms, newfd, newflags);
00195 
00196   fd (newfd);
00197   m_flags = newflags;
00198 }

IOOffset File::position ( IOOffset  offset,
Relative  whence = SET 
) [virtual]

Move the current file pointer to offset relative to whence.

Returns the new file offset relative to the beginning of the file.

Implements Storage.

Definition at line 112 of file UnixFile.cc.

References Storage::CURRENT, EDM_IOFD_INVALID, Storage::END, IOChannel::fd(), HLT_VtxMuL3::result, Storage::SET, and throwStorageError().

Referenced by LocalCacheFile::position(), LocalCacheFile::read(), and LocalCacheFile::readv().

00113 {
00114   IOFD fd = this->fd ();
00115   assert (fd != EDM_IOFD_INVALID);
00116   assert (whence == CURRENT || whence == SET || whence == END);
00117 
00118   IOOffset result;
00119   int      mywhence = (whence == SET ? SEEK_SET
00120                        : whence == CURRENT ? SEEK_CUR
00121                        : SEEK_END);
00122   if ((result = ::lseek (fd, offset, mywhence)) == -1)
00123     throwStorageError("File::position()", "lseek()", errno);
00124 
00125   return result;
00126 }

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 IOChannel::fd(), i, offset, and size().

Referenced by LocalCacheFile::prefetch().

00211 {
00212   IOFD fd = this->fd();
00213   for (IOSize i = 0; i < n; ++i)
00214     posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED);
00215   return true;
00216 }

IOSize File::read ( void into,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 63 of file UnixFile.cc.

References IOChannel::fd(), s, and throwStorageError().

00064 {
00065   assert (pos >= 0);
00066 
00067   ssize_t s;
00068   do
00069     s = ::pread (fd (), into, n, pos);
00070   while (s == -1 && errno == EINTR);
00071 
00072   if (s == -1)
00073     throwStorageError("File::read()", "pread()", errno);
00074 
00075   return s;
00076 }

IOSize File::read ( void into,
IOSize  n 
) [virtual]

Read from the file.

Reimplemented from IOChannel.

Definition at line 220 of file File.cc.

References IOInput::read().

Referenced by LocalCacheFile::read().

00221 { return IOChannel::read (into, n); }

IOSize File::readv ( IOBuffer into,
IOSize  length 
) [virtual]

Read from the file.

Reimplemented from IOChannel.

Definition at line 225 of file File.cc.

References IOChannel::readv().

Referenced by LocalCacheFile::readv().

00226 { 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, IOChannel::fd(), and throwStorageError().

Referenced by LocalCacheFile::LocalCacheFile().

00130 {
00131   IOFD fd = this->fd ();
00132   assert (fd != EDM_IOFD_INVALID);
00133 
00134   if (ftruncate (fd, size) == -1)
00135     throwStorageError("File::resize()", "ftruncate()", errno);
00136 }

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.

References InternalAutoClose, and m_flags.

00104 {
00105   m_flags &= ~InternalAutoClose;
00106   if (autoclose)
00107     m_flags |= InternalAutoClose;
00108 }

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, IOChannel::fd(), and throwStorageError().

Referenced by prefetch().

00100 {
00101   IOFD fd = this->fd ();
00102   assert (fd != EDM_IOFD_INVALID);
00103 
00104   struct stat info;
00105   if (fstat (fd, &info) == -1)
00106     throwStorageError("File::size()", "fstat()", errno);
00107 
00108   return info.st_size;
00109 }

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 149 of file UnixFile.cc.

References close().

Referenced by abort(), and close().

00150 {
00151   int ret = ::close (fd);
00152   if (error) *error = errno;
00153   return ret != -1;
00154 }

IOFD File::sysduplicate ( IOFD  fd  )  [static, private]

Definition at line 9 of file UnixFile.cc.

References dup, EDM_IOFD_INVALID, and throwStorageError().

Referenced by duplicate().

00010 {
00011   IOFD copyfd;
00012   if ((copyfd = ::dup (fd)) == EDM_IOFD_INVALID)
00013     throwStorageError ("File::sysduplicate()", "dup()", errno);
00014 
00015   return copyfd;
00016 }

static void File::sysopen ( const char *  name,
int  flags,
int  perms,
IOFD newfd,
unsigned &  newflags 
) [static, private]

Referenced by open().

IOSize File::write ( const void from,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 79 of file UnixFile.cc.

References IOChannel::fd(), flush(), m_flags, s, and throwStorageError().

00080 {
00081   assert (pos >= 0);
00082 
00083   ssize_t s;
00084   do
00085     s = ::pwrite (fd (), from, n, pos);
00086   while (s == -1 && errno == EINTR);
00087 
00088   if (s == -1)
00089     throwStorageError("File::write()", "pwrite()", errno);
00090 
00091   if (m_flags & OpenUnbuffered)
00092     // FIXME: Exception handling?
00093     flush ();
00094 
00095   return s;
00096 }

IOSize File::write ( const void from,
IOSize  n 
) [virtual]

Write to the file.

Reimplemented from IOChannel.

Definition at line 230 of file File.cc.

References Storage::END, flush(), m_flags, IOFlags::OpenAppend, IOFlags::OpenUnbuffered, Storage::position(), s, and IOChannel::write().

00231 {
00232   // FIXME: This may create a race condition or cause trouble on
00233   // remote files.  Should be currently needed only on WIN32.
00234   if (m_flags & OpenAppend)
00235     position (0, END);
00236 
00237   IOSize s = IOChannel::write (from, n);
00238 
00239   if (m_flags & OpenUnbuffered)
00240     // FIXME: Exception handling?
00241     flush ();
00242 
00243   return s;
00244 }

IOSize File::writev ( const IOBuffer from,
IOSize  length 
) [virtual]

Write to the file.

Reimplemented from IOChannel.

Definition at line 248 of file File.cc.

References Storage::END, flush(), m_flags, IOFlags::OpenAppend, IOFlags::OpenUnbuffered, Storage::position(), s, and IOChannel::writev().

00249 {
00250   // FIXME: This may create a race condition or cause trouble on
00251   // remote files.  Should be currently needed only on WIN32.
00252   if (m_flags & OpenAppend)
00253     position (0, END);
00254 
00255   IOSize s = IOChannel::writev (from, length);
00256 
00257   if (m_flags & OpenUnbuffered)
00258     // FIXME: Exception handling?
00259     flush ();
00260 
00261   return s;
00262 }


Member Data Documentation

unsigned File::m_flags [private]

Definition at line 72 of file File.h.

Referenced by abort(), attach(), close(), duplicate(), File(), open(), setAutoClose(), write(), writev(), and ~File().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:20:52 2009 for CMSSW by  doxygen 1.5.4