CMS 3D CMS Logo

File.cc
Go to the documentation of this file.
4 #include <cassert>
5 
6 using namespace edm::storage;
7 using namespace edm::storage::IOFlags;
8 
9 //<<<<<< PRIVATE DEFINES >>>>>>
10 //<<<<<< PRIVATE CONSTANTS >>>>>>
11 //<<<<<< PRIVATE TYPES >>>>>>
12 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>>
13 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>>
14 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>>
15 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>>
16 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>>
17 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>>
18 
54 
59 
63 File::File(IOFD fd, bool autoclose /* = true */) : m_fd(fd) { m_flags = autoclose ? InternalAutoClose : 0; }
64 
66 File::File(IOFD fd, unsigned flags) : m_fd(fd) { m_flags = flags; }
67 
69 File::File(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) { open(name, flags, perms); }
70 
72 File::File(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
73  open(name.c_str(), flags, perms);
74 }
75 
79 File::~File(void) {
81  abort();
82 }
83 
85 
90 void File::setAutoClose(bool autoclose) {
92  if (autoclose)
94 }
95 
97 
102 File *File::duplicate(bool copy) const {
103  File *dup = new File(fd(), copy ? m_flags : 0);
104  return copy ? this->duplicate(dup) : dup;
105 }
106 
110  IOFD fd = m_fd;
112  assert(child);
113  child->m_fd = sysduplicate(fd);
114  child->m_flags = m_flags;
115  return child;
116 }
117 
119 
123 void File::create(const char *name, bool exclusive /*=false*/, int perms /*=0666*/) {
125 }
126 
131 void File::create(const std::string &name, bool exclusive /*=false*/, int perms /*=0666*/) {
132  open(name.c_str(), (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms);
133 }
134 
140 void File::open(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
141  open(name.c_str(), flags, perms);
142 }
143 
148 void File::open(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
149  // is zero and always implied. OTOH, existence check should be
150  // done with Filename::exists() -- see comments there about what
151  // can happen on a WIN32 remote share even if the file doesn't
152  // exist. For now make sure that read or write was asked for.
153 
154  assert(name && *name);
156 
157  // If I am already open, close the old file first.
159  close();
160 
161  IOFD newfd = EDM_IOFD_INVALID;
162  unsigned newflags = InternalAutoClose;
163 
164  sysopen(name, flags, perms, newfd, newflags);
165 
166  m_fd = newfd;
167  m_flags = newflags;
168 }
169 
171  m_fd = fd;
172  m_flags = 0;
173 }
174 
176 
177 bool File::prefetch(const IOPosBuffer *what, IOSize n) {
178  IOFD fd = m_fd;
179  for (IOSize i = 0; i < n; ++i) {
180 #if F_RDADVISE
181  radvisory info;
182  info.ra_offset = what[i].offset();
183  info.ra_count = what[i].size();
184  fcntl(fd, F_RDADVISE, &info);
185 #elif _POSIX_ADVISORY_INFO > 0
186  posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED);
187 #else
188 #error advisory read ahead not available on this platform
189 #endif
190  }
191  return true;
192 }
193 
195 IOSize File::write(const void *from, IOSize n) {
196  // FIXME: This may create a race condition or cause trouble on
197  // remote files. Should be currently needed only on WIN32.
198  if (m_flags & OpenAppend)
199  position(0, END);
200 
201  IOSize s = syswrite(from, n);
202 
203  if (m_flags & OpenUnbuffered)
204  // FIXME: Exception handling?
205  flush();
206 
207  return s;
208 }
209 
211 IOSize File::writev(const IOBuffer *from, IOSize length) {
212  // FIXME: This may create a race condition or cause trouble on
213  // remote files. Should be currently needed only on WIN32.
214  if (m_flags & OpenAppend)
215  position(0, END);
216 
217  IOSize s = syswritev(from, length);
218 
219  if (m_flags & OpenUnbuffered)
220  // FIXME: Exception handling?
221  flush();
222 
223  return s;
224 }
225 
227 void File::close() {
228  IOFD fd = m_fd;
230 
231  int error;
232  if (!sysclose(fd, &error))
233  throwStorageError("FileCloseError", "Calling File::close()", "sysclose", error);
234 
237 }
238 
240 void File::abort() {
241  IOFD fd = m_fd;
242  if (fd != EDM_IOFD_INVALID) {
243  sysclose(fd);
246  }
247 }
IOFD fd() const
Definition: File.h:34
static const TGPicture * info(bool iBackgroundIsBlack)
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
void flush() override
Definition: UnixFile.cc:207
assert(be >=bs)
File * duplicate(bool copy) const
Definition: File.cc:102
IOSize syswrite(const void *from, IOSize n)
Definition: UnixFile.cc:116
IOOffset offset() const
Definition: IOPosBuffer.h:41
virtual void setAutoClose(bool closeit)
Definition: File.cc:90
virtual void abort()
Definition: File.cc:240
~File() override
Definition: File.cc:79
IOSize syswritev(const IOBuffer *from, IOSize length)
Definition: UnixFile.cc:128
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: File.cc:148
bool prefetch(const IOPosBuffer *what, IOSize n) override
Definition: File.cc:177
void close() override
Definition: File.cc:227
unsigned m_flags
Definition: File.h:71
static void sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned &newflags)
Definition: UnixFile.cc:20
static IOFD sysduplicate(IOFD fd)
Definition: UnixFile.cc:12
IOSize write(const void *from, IOSize n) override
Definition: File.cc:195
size_t IOSize
Definition: IOTypes.h:15
IOOffset size() const override
Definition: UnixFile.cc:175
virtual void create(const char *name, bool exclusive=false, int perms=0666)
Definition: File.cc:123
constexpr int EDM_IOFD_INVALID
Definition: IOTypes.h:9
virtual IOOffset position() const
Definition: Storage.cc:504
int IOFD
Definition: IOTypes.h:23
virtual void attach(IOFD fd)
Definition: File.cc:170
static bool sysclose(IOFD fd, int *error=nullptr)
Definition: UnixFile.cc:220
fd
Definition: ztee.py:136
IOSize writev(const IOBuffer *from, IOSize length) override
Definition: File.cc:211