CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/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 "FWCore/Utilities/interface/EDMException.h"
00005 #include <cassert>
00006 
00007 using namespace IOFlags;
00008 
00009 IOFD
00010 File::sysduplicate (IOFD fd)
00011 {
00012   IOFD copyfd;
00013   if ((copyfd = ::dup (fd)) == EDM_IOFD_INVALID)
00014     throwStorageError ("FileDuplicateError", "Calling File::sysduplicate()", "dup()", errno);
00015 
00016   return copyfd;
00017 }
00018 
00019 void
00020 File::sysopen (const char *name, int flags, int perms,
00021                IOFD &newfd, unsigned int& /*newflags*/)
00022 {
00023   // Translate our flags to system flags.
00024   int openflags = 0;
00025 
00026   if ((flags & OpenRead) && (flags & OpenWrite))
00027     openflags |= O_RDWR;
00028   else if (flags & OpenRead)
00029     openflags |= O_RDONLY;
00030   else if (flags & OpenWrite)
00031     openflags |= O_WRONLY;
00032 
00033   if (flags & OpenNonBlock)
00034     openflags |= O_NONBLOCK;
00035 
00036   if (flags & OpenAppend)
00037     openflags |= O_APPEND;
00038 
00039 #ifdef O_SYNC
00040   if (flags & OpenUnbuffered)
00041     openflags |= O_SYNC;
00042 #else
00043   if (flags & OpenUnbuffered)
00044     newflags |= OpenUnbuffered;
00045 #endif
00046 
00047   if (flags & OpenCreate)
00048     openflags |= O_CREAT;
00049 
00050   if (flags & OpenExclusive)
00051     openflags |= O_EXCL;
00052 
00053   if (flags & OpenTruncate)
00054     openflags |= O_TRUNC;
00055 
00056   if (flags & OpenNotCTTY)
00057     openflags |= O_NOCTTY;
00058 
00059   if ((newfd = ::open (name, openflags, perms)) == -1)
00060     throwStorageError (edm::errors::FileOpenError, "Calling File::sysopen()", "open()", errno);
00061 }
00062 
00063 IOSize
00064 File::read (void *into, IOSize n, IOOffset pos)
00065 {
00066   assert (pos >= 0);
00067 
00068   ssize_t s;
00069   do
00070     s = ::pread (fd (), into, n, pos);
00071   while (s == -1 && errno == EINTR);
00072 
00073   if (s == -1)
00074     throwStorageError(edm::errors::FileReadError, "Calling File::read()", "pread()", errno);
00075 
00076   return s;
00077 }
00078 
00079 IOSize
00080 File::write (const void *from, IOSize n, IOOffset pos)
00081 {
00082   assert (pos >= 0);
00083 
00084   ssize_t s;
00085   do
00086     s = ::pwrite (fd (), from, n, pos);
00087   while (s == -1 && errno == EINTR);
00088 
00089   if (s == -1)
00090     throwStorageError("FileWriteError", "Calling File::write()", "pwrite()", errno);
00091 
00092   if (m_flags & OpenUnbuffered)
00093     // FIXME: Exception handling?
00094     flush ();
00095 
00096   return s;
00097 }
00098 
00099 IOOffset
00100 File::size (void) const
00101 {
00102   IOFD fd = this->fd ();
00103   assert (fd != EDM_IOFD_INVALID);
00104 
00105   struct stat info;
00106   if (fstat (fd, &info) == -1)
00107     throwStorageError("FileSizeError", "Calling File::size()", "fstat()", errno);
00108 
00109   return info.st_size;
00110 }
00111 
00112 IOOffset
00113 File::position (IOOffset offset, Relative whence /* = SET */)
00114 {
00115   IOFD fd = this->fd ();
00116   assert (fd != EDM_IOFD_INVALID);
00117   assert (whence == CURRENT || whence == SET || whence == END);
00118 
00119   IOOffset result;
00120   int      mywhence = (whence == SET ? SEEK_SET
00121                        : whence == CURRENT ? SEEK_CUR
00122                        : SEEK_END);
00123   if ((result = ::lseek (fd, offset, mywhence)) == -1)
00124     throwStorageError("FilePositionError", "Calling File::position()", "lseek()", errno);
00125 
00126   return result;
00127 }
00128 
00129 void
00130 File::resize (IOOffset size)
00131 {
00132   IOFD fd = this->fd ();
00133   assert (fd != EDM_IOFD_INVALID);
00134 
00135   if (ftruncate (fd, size) == -1)
00136     throwStorageError("FileResizeError", "Calling File::resize()", "ftruncate()", errno);
00137 }
00138 
00139 void
00140 File::flush (void)
00141 {
00142   IOFD fd = this->fd ();
00143   assert (fd != EDM_IOFD_INVALID);
00144 
00145 #if _POSIX_SYNCHRONIZED_IO > 0
00146   if (fdatasync (fd) == -1)
00147     throwStorageError("FileFlushError", "Calling File::flush()", "fdatasync()", errno);
00148 #elif _POSIX_FSYNC > 0
00149   if (fsync (fd) == -1)
00150     throwStorageError("FileFlushError", "Calling File::flush()", "fsync()", errno);
00151 #endif
00152 }
00153 
00154 bool
00155 File::sysclose (IOFD fd, int *error /* = 0 */)
00156 {
00157   int ret = ::close (fd);
00158   if (error) *error = errno;
00159   return ret != -1;
00160 }