libcsdbg  1.28
C++ exception (and generic) stack trace debug library
filebuf & csdbg::filebuf::open ( u32  flags,
u32  umask 
)
virtual

Open the file.

Parameters
[in]flagsthe flags used to open the file
[in]umaskthe file mode (ignored if the file exists)
Returns
*this
Exceptions
csdbg::exception
Note
If the file is already open, it is re-opened with the new flags

Definition at line 162 of file filebuf.cpp.

References csdbg::streambuf::close(), csdbg::util::is_regular(), csdbg::util::is_writable(), csdbg::streambuf::m_handle, m_path, open(), and unlikely.

163 {
164  if ( unlikely(m_handle >= 0) )
165  close();
166 
167  do {
168  m_handle = ::open(m_path, flags, umask);
169  }
170  while ( unlikely(m_handle < 0 && errno == EINTR) );
171 
172  if ( unlikely(m_handle < 0) )
173  throw exception(
174  "failed to open file '%s' (errno %d - %s)",
175  m_path,
176  errno,
177  strerror(errno)
178  );
179 
180  /* Sanity checks, if a test fails an appropriate exception is thrown */
181  try {
182  fileinfo_t inf;
183  i32 retval = fstat(m_handle, &inf);
184  if ( unlikely(retval < 0) )
185  throw exception(
186  "failed to stat path '%s' (errno %d - %s)",
187  m_path,
188  errno,
189  strerror(errno)
190  );
191 
192  else if ( unlikely(!util::is_regular(inf)) )
193  throw exception("'%s' is not a regular file", m_path);
194 
195  else if ( unlikely(!util::is_writable(inf)) )
196  throw exception("file '%s' is not writable", m_path);
197 
198  return *this;
199  }
200 
201  catch (...) {
202  /* If any of the checks failed, the descriptor is unusable */
203  close();
204  throw;
205  }
206 }
static bool is_writable(const fileinfo_t &)
Check if the process has write access to a file.
Definition: util.cpp:464
struct stat fileinfo_t
File metadata.
Definition: config.hpp:112
i32 m_handle
Stream handle (descriptor)
Definition: streambuf.hpp:43
virtual filebuf & open()
Open the file for output.
Definition: filebuf.cpp:143
int i32
32-bit signed integer
Definition: config.hpp:82
static bool is_regular(const fileinfo_t &)
Check if a file is a regular one.
Definition: util.cpp:419
i8 * m_path
Output file path.
Definition: filebuf.hpp:37
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
virtual streambuf & close()
Close the stream.
Definition: streambuf.cpp:130

+ Here is the call graph for this function: