Imported Upstream version 17.12.0
[platform/upstream/libzypp.git] / zypp / base / Fd.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Fd.cc
10  *
11 */
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
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27   ///////////////////////////////////////////////////////////////////
28   namespace base
29   { /////////////////////////////////////////////////////////////////
30
31     ///////////////////////////////////////////////////////////////////
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
44     ///////////////////////////////////////////////////////////////////
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
58     /////////////////////////////////////////////////////////////////
59   } // namespace base
60   ///////////////////////////////////////////////////////////////////
61   /////////////////////////////////////////////////////////////////
62 } // namespace zypp
63 ///////////////////////////////////////////////////////////////////