libzypp  13.10.6
Fd.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 extern "C"
13 {
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 }
18 
19 #include <iostream>
20 
21 #include "zypp/base/Exception.h"
22 #include "zypp/base/Fd.h"
23 
25 namespace zypp
26 {
27  namespace base
29  {
30 
32  //
33  // METHOD NAME : Fd::Fd
34  // METHOD TYPE : Ctor
35  //
36  Fd::Fd( const Pathname & file_r, int open_flags, mode_t mode )
37  : m_fd( -1 )
38  {
39  m_fd = open( file_r.asString().c_str(), open_flags, mode );
40  if ( m_fd == -1 )
41  ZYPP_THROW_ERRNO_MSG( Exception, std::string("open ")+file_r.asString() );
42  }
43 
45  //
46  // METHOD NAME : Fd::close
47  // METHOD TYPE : void
48  //
49  void Fd::close()
50  {
51  if ( m_fd != -1 )
52  {
53  ::close( m_fd );
54  m_fd = -1;
55  }
56  }
57 
59  } // namespace base
62 } // namespace zypp
#define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)
Throw Exception built from errno and a message string.
Definition: Exception.h:345
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:59
void close()
Explicitly close the file.
Definition: Fd.cc:49
int m_fd
The filedescriptor.
Definition: Fd.h:69
Base class for Exception.
Definition: Exception.h:143
Fd(const Pathname &file_r, int open_flags, mode_t mode=0)
Ctor opens file.
Definition: Fd.cc:36