CMS 3D CMS Logo

File.cc
Go to the documentation of this file.
4 #include <cassert>
5 
6 using namespace IOFlags;
7 
8 //<<<<<< PRIVATE DEFINES >>>>>>
9 //<<<<<< PRIVATE CONSTANTS >>>>>>
10 //<<<<<< PRIVATE TYPES >>>>>>
11 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>>
12 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>>
13 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>>
14 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>>
15 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>>
16 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>>
17 
53 
57 File::File(void) {
59  m_flags = 0;
60 }
61 
65 File::File(IOFD fd, bool autoclose /* = true */) {
66  this->fd(fd);
67  m_flags = autoclose ? InternalAutoClose : 0;
68 }
69 
71 File::File(IOFD fd, unsigned flags) {
72  this->fd(fd);
73  m_flags = flags;
74 }
75 
77 File::File(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) { open(name, flags, perms); }
78 
80 File::File(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
81  open(name.c_str(), flags, perms);
82 }
83 
87 File::~File(void) {
88  if (m_flags & InternalAutoClose)
89  abort();
90 }
91 
93 
98 void File::setAutoClose(bool autoclose) {
99  m_flags &= ~InternalAutoClose;
100  if (autoclose)
101  m_flags |= InternalAutoClose;
102 }
103 
105 
110 File *File::duplicate(bool copy) const {
111  File *dup = new File(fd(), copy ? m_flags : 0);
112  return copy ? this->duplicate(dup) : dup;
113 }
114 
118  IOFD fd = this->fd();
119  assert(fd != EDM_IOFD_INVALID);
120  assert(child);
121  child->fd(sysduplicate(fd));
122  child->m_flags = m_flags;
123  return child;
124 }
125 
127 
131 void File::create(const char *name, bool exclusive /*=false*/, int perms /*=0666*/) {
132  open(name, (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms);
133 }
134 
139 void File::create(const std::string &name, bool exclusive /*=false*/, int perms /*=0666*/) {
140  open(name.c_str(), (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms);
141 }
142 
148 void File::open(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
149  open(name.c_str(), flags, perms);
150 }
151 
156 void File::open(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
157  // is zero and always implied. OTOH, existence check should be
158  // done with Filename::exists() -- see comments there about what
159  // can happen on a WIN32 remote share even if the file doesn't
160  // exist. For now make sure that read or write was asked for.
161 
162  assert(name && *name);
164 
165  // If I am already open, close the old file first.
166  if (fd() != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
167  close();
168 
169  IOFD newfd = EDM_IOFD_INVALID;
170  unsigned newflags = InternalAutoClose;
171 
172  sysopen(name, flags, perms, newfd, newflags);
173 
174  fd(newfd);
175  m_flags = newflags;
176 }
177 
179  this->fd(fd);
180  m_flags = 0;
181 }
182 
184 
185 bool File::prefetch(const IOPosBuffer *what, IOSize n) {
186  IOFD fd = this->fd();
187  for (IOSize i = 0; i < n; ++i) {
188 #if F_RDADVISE
189  radvisory info;
190  info.ra_offset = what[i].offset();
191  info.ra_count = what[i].size();
192  fcntl(fd, F_RDADVISE, &info);
193 #elif _POSIX_ADVISORY_INFO > 0
194  posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED);
195 #else
196 #error advisory read ahead not available on this platform
197 #endif
198  }
199  return true;
200 }
201 
203 IOSize File::read(void *into, IOSize n) { return IOChannel::read(into, n); }
204 
206 IOSize File::readv(IOBuffer *into, IOSize length) { return IOChannel::readv(into, length); }
207 
209 IOSize File::write(const void *from, IOSize n) {
210  // FIXME: This may create a race condition or cause trouble on
211  // remote files. Should be currently needed only on WIN32.
212  if (m_flags & OpenAppend)
213  position(0, END);
214 
215  IOSize s = IOChannel::write(from, n);
216 
217  if (m_flags & OpenUnbuffered)
218  // FIXME: Exception handling?
219  flush();
220 
221  return s;
222 }
223 
225 IOSize File::writev(const IOBuffer *from, IOSize length) {
226  // FIXME: This may create a race condition or cause trouble on
227  // remote files. Should be currently needed only on WIN32.
228  if (m_flags & OpenAppend)
229  position(0, END);
230 
231  IOSize s = IOChannel::writev(from, length);
232 
233  if (m_flags & OpenUnbuffered)
234  // FIXME: Exception handling?
235  flush();
236 
237  return s;
238 }
239 
241 void File::close(void) {
242  IOFD fd = this->fd();
243  assert(fd != EDM_IOFD_INVALID);
244 
245  int error;
246  if (!sysclose(fd, &error))
247  throwStorageError("FileCloseError", "Calling File::close()", "sysclose", error);
248 
249  m_flags &= ~InternalAutoClose;
250  this->fd(EDM_IOFD_INVALID);
251 }
252 
254 void File::abort(void) {
255  IOFD fd = this->fd();
256  if (fd != EDM_IOFD_INVALID) {
257  sysclose(fd);
258  m_flags &= ~InternalAutoClose;
259  this->fd(EDM_IOFD_INVALID);
260  }
261 }
File::open
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: File.cc:156
IOChannel::readv
IOSize readv(IOBuffer *into, IOSize buffers) override
Definition: UnixIOChannel.cc:21
mps_fire.i
i
Definition: mps_fire.py:355
File::~File
~File(void) override
Definition: File.cc:87
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
File::duplicate
File * duplicate(bool copy) const
Definition: File.cc:110
File::close
void close(void) override
Definition: File.cc:241
cms::cuda::assert
assert(be >=bs)
info
static const TGPicture * info(bool iBackgroundIsBlack)
Definition: FWCollectionSummaryWidget.cc:152
IOFlags::OpenTruncate
Definition: IOFlags.h:28
IOFlags::OpenWrite
Definition: IOFlags.h:8
relativeConstraints.error
error
Definition: relativeConstraints.py:53
ztee.fd
fd
Definition: ztee.py:136
alignCSCRings.s
s
Definition: alignCSCRings.py:92
IOChannel::writev
IOSize writev(const IOBuffer *from, IOSize buffers) override
Definition: UnixIOChannel.cc:65
File
Definition: File.h:11
File::prefetch
bool prefetch(const IOPosBuffer *what, IOSize n) override
Definition: File.cc:185
IOFlags::OpenRead
Definition: IOFlags.h:7
SysFile.h
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
trackingPlots.dup
dup
Definition: trackingPlots.py:183
File::writev
IOSize writev(const IOBuffer *from, IOSize length) override
Definition: File.cc:225
IOFlags
Definition: IOFlags.h:4
IOBuffer
Definition: IOBuffer.h:7
File::setAutoClose
virtual void setAutoClose(bool closeit)
Definition: File.cc:98
position
static int position[264][3]
Definition: ReadPGInfo.cc:289
File::attach
virtual void attach(IOFD fd)
Definition: File.cc:178
throwStorageError
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
IOFlags::OpenExclusive
Definition: IOFlags.h:25
IOFlags::OpenCreate
Definition: IOFlags.h:24
File::write
IOSize write(const void *from, IOSize n) override
Definition: File.cc:209
IOFD
int IOFD
Definition: IOTypes.h:22
IOFlags::OpenAppend
Definition: IOFlags.h:18
VtxSmearedBeamProfile_cfi.File
File
Definition: VtxSmearedBeamProfile_cfi.py:30
IOPosBuffer::size
IOSize size(void) const
Definition: IOPosBuffer.h:45
generator_cfi.exclusive
exclusive
Definition: generator_cfi.py:24
EDM_IOFD_INVALID
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
IOPosBuffer::offset
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
IOChannel::write
IOSize write(const void *from, IOSize n) override
Definition: UnixIOChannel.cc:53
IOInput::read
int read(void)
Definition: IOInput.cc:52
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
File::create
virtual void create(const char *name, bool exclusive=false, int perms=0666)
Definition: File.cc:131
IOPosBuffer
Definition: IOPosBuffer.h:7
IOFlags::OpenUnbuffered
Definition: IOFlags.h:20
class-composition.child
child
Definition: class-composition.py:91
Throw.h
File::readv
IOSize readv(IOBuffer *into, IOSize length) override
Definition: File.cc:206
File::abort
virtual void abort(void)
Definition: File.cc:254
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
IOSize
size_t IOSize
Definition: IOTypes.h:14
child
Definition: simpleInheritance.h:11
File.h
HLT_2018_cff.flags
flags
Definition: HLT_2018_cff.py:11758
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
File::File
File(void)
Definition: File.cc:57