ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
FileDescriptor.cpp
Go to the documentation of this file.
1 /*
2  * FileDescriptor.cpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdexcept>
22 
23 #include "FileDescriptor.hpp"
24 
25 namespace onposix {
26 
27 /**
28  * \brief Constructor for descriptors of files.
29  *
30  * It calls open() with the given flags.
31  * @param name of the file
32  * @param flags that can be
33  * <ul>
34  * <li> O_RDONLY
35  * <li> O_WRONLY
36  * <li> or O_RDWR
37  * </ul>
38  * or-ed with zero or more flags:
39  * <ul>
40  * <li> O_APPEND
41  * <li> O_ASYNC
42  * <li> O_CREAT
43  * <li> O_NONBLOCK
44  * <li> O_SYNC
45  * </ul>
46  * @exception runtime_error if the ::open() returns an error
47  */
48 FileDescriptor::FileDescriptor(const std::string& name,const int flags)
49 {
50  fd_ = open(name.c_str(), flags);
51  if (fd_ <= 0) {
52  ERROR("Opening file " << name);
53  throw std::runtime_error ("Open file error");
54  }
55 }
56 
57 
58 /**
59  * \brief Constructor for descriptors of files.
60  *
61  * It calls open() with the given flags.
62  * @param name of the file
63  * @param flags that can be
64  * <ul>
65  * <li> O_RDONLY
66  * <li> O_WRONLY
67  * <li> or O_RDWR
68  * </ul>
69  * or-ed with zero or more flags:
70  * <ul>
71  * <li> O_APPEND
72  * <li> O_ASYNC
73  * <li> O_CREAT
74  * <li> O_NONBLOCK
75  * <li> O_SYNC
76  * </ul>
77  * @param mode can be
78  * <ul>
79  * <li> S_IRWXU 00700 user (file owner) has read, write and execute permission
80  * <li> S_IRUSR 00400 user has read permission
81  * <li> S_IWUSR 00200 user has write permission
82  * <li> S_IXUSR 00100 user has execute permission
83  * <li> S_IRWXG 00070 group has read, write and execute permission
84  * <li> S_IRGRP 00040 group has read permission
85  * <li> S_IWGRP 00020 group has write permission
86  * <li> S_IXGRP 00010 group has execute permission
87  * <li> S_IRWXO 00007 others have read, write and execute permission
88  * <li> S_IROTH 00004 others have read permission
89  * <li> S_IWOTH 00002 others have write permission
90  * <li> S_IXOTH 00001 others have execute permission
91  * </ul>
92  * @exception runtime_error if the ::open() returns an error
93  */
94 FileDescriptor::FileDescriptor(const std::string& name, const int flags,
95  const mode_t mode)
96 {
97  fd_ = open(name.c_str(), flags, mode);
98  if (fd_ <= 0) {
99  ERROR("Opening file " << name);
100  throw std::runtime_error ("Open file error");
101  }
102 }
103 
104 
105 
106 /**
107  * \brief Method to get the length of current available data.
108  *
109  * @return length of data available on the file
110  */
112 {
113  int currentPosition = ::lseek(fd_, 0, SEEK_CUR);
114  int end = ::lseek(fd_, 0, SEEK_END);
115  ::lseek(fd_, currentPosition, SEEK_SET);
116  return end;
117 }
118 
119 } /* onposix */