CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Utilities/StorageFactory/src/UnixFile.cc

Go to the documentation of this file.
00001 #include "Utilities/StorageFactory/interface/File.h"
00002 #include "Utilities/StorageFactory/src/SysFile.h"
00003 #include "Utilities/StorageFactory/src/Throw.h"
00004 #include <cassert>
00005 
00006 using namespace IOFlags;
00007 
00008 IOFD
00009 File::sysduplicate (IOFD fd)
00010 {
00011   IOFD copyfd;
00012   if ((copyfd = ::dup (fd)) == EDM_IOFD_INVALID)
00013     throwStorageError ("File::sysduplicate()", "dup()", errno);
00014 
00015   return copyfd;
00016 }
00017 
00018 void
00019 File::sysopen (const char *name, int flags, int perms,
00020                IOFD &newfd, unsigned int& /*newflags*/)
00021 {
00022   // Translate our flags to system flags.
00023   int openflags = 0;
00024 
00025   if ((flags & OpenRead) && (flags & OpenWrite))
00026     openflags |= O_RDWR;
00027   else if (flags & OpenRead)
00028     openflags |= O_RDONLY;
00029   else if (flags & OpenWrite)
00030     openflags |= O_WRONLY;
00031 
00032   if (flags & OpenNonBlock)
00033     openflags |= O_NONBLOCK;
00034 
00035   if (flags & OpenAppend)
00036     openflags |= O_APPEND;
00037 
00038 #ifdef O_SYNC
00039   if (flags & OpenUnbuffered)
00040     openflags |= O_SYNC;
00041 #else
00042   if (flags & OpenUnbuffered)
00043     newflags |= OpenUnbuffered;
00044 #endif
00045 
00046   if (flags & OpenCreate)
00047     openflags |= O_CREAT;
00048 
00049   if (flags & OpenExclusive)
00050     openflags |= O_EXCL;
00051 
00052   if (flags & OpenTruncate)
00053     openflags |= O_TRUNC;
00054 
00055   if (flags & OpenNotCTTY)
00056     openflags |= O_NOCTTY;
00057 
00058   if ((newfd = ::open (name, openflags, perms)) == -1)
00059     throwStorageError ("File::sysopen()", "open()", errno);
00060 }
00061 
00062 IOSize
00063 File::read (void *into, IOSize n, IOOffset pos)
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 }
00077 
00078 IOSize
00079 File::write (const void *from, IOSize n, IOOffset pos)
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 }
00097 
00098 IOOffset
00099 File::size (void) const
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 }
00110 
00111 IOOffset
00112 File::position (IOOffset offset, Relative whence /* = SET */)
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 }
00127 
00128 void
00129 File::resize (IOOffset size)
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 }
00137 
00138 void
00139 File::flush (void)
00140 {
00141   IOFD fd = this->fd ();
00142   assert (fd != EDM_IOFD_INVALID);
00143 
00144 #if _POSIX_SYNCHRONIZED_IO > 0
00145   if (fdatasync (fd) == -1)
00146     throwStorageError("File::flush()", "fdatasync()", errno);
00147 #elif _POSIX_FSYNC > 0
00148   if (fsync (fd) == -1)
00149     throwStorageError("File::flush()", "fsync()", errno);
00150 #endif
00151 }
00152 
00153 bool
00154 File::sysclose (IOFD fd, int *error /* = 0 */)
00155 {
00156   int ret = ::close (fd);
00157   if (error) *error = errno;
00158   return ret != -1;
00159 }