ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
FifoDescriptor.cpp
Go to the documentation of this file.
1 /*
2  * FifoDescriptor.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 #include <cerrno>
23 
24 #include "FifoDescriptor.hpp"
25 
26 namespace onposix {
27 
28 /**
29  * \brief Constructor for descriptors of fifos.
30  *
31  * It calls open() with the given flags.
32  * @param name of the fifo
33  * @param flags that can be
34  * <ul>
35  * <li> O_RDONLY
36  * <li> O_WRONLY
37  * <li> or O_RDWR
38  * </ul>
39  * or-ed with zero or more flags:
40  * <ul>
41  * <li> O_APPEND
42  * <li> O_ASYNC
43  * <li> O_CREAT
44  * <li> O_NONBLOCK
45  * <li> O_SYNC
46  * </ul>
47  * @exception runtime_error if the ::open() returns an error
48  */
49 FifoDescriptor::FifoDescriptor(const std::string& name, const int flags)
50 {
51  DEBUG("Opening fifo...");
52  fd_ = open(name.c_str(), flags);
53  if (fd_ <= 0) {
54  ERROR("Opening fifo " << name);
55  throw std::runtime_error ("Open fifo error");
56  }
57 }
58 
59 
60 /**
61  * \brief Constructor for descriptors of fifos.
62  *
63  * It calls open() with the given flags.
64  * @param name of the file
65  * @param flags that can be
66  * <ul>
67  * <li> O_RDONLY
68  * <li> O_WRONLY
69  * <li> or O_RDWR
70  * </ul>
71  * or-ed with zero or more flags:
72  * <ul>
73  * <li> O_APPEND
74  * <li> O_ASYNC
75  * <li> O_CREAT
76  * <li> O_NONBLOCK
77  * <li> O_SYNC
78  * </ul>
79  * @param mode can be
80  * <ul>
81  * <li> S_IRWXU 00700 user (file owner) has read, write and execute permission
82  * <li> S_IRUSR 00400 user has read permission
83  * <li> S_IWUSR 00200 user has write permission
84  * <li> S_IXUSR 00100 user has execute permission
85  * <li> S_IRWXG 00070 group has read, write and execute permission
86  * <li> S_IRGRP 00040 group has read permission
87  * <li> S_IWGRP 00020 group has write permission
88  * <li> S_IXGRP 00010 group has execute permission
89  * <li> S_IRWXO 00007 others have read, write and execute permission
90  * <li> S_IROTH 00004 others have read permission
91  * <li> S_IWOTH 00002 others have write permission
92  * <li> S_IXOTH 00001 others have execute permission
93  * </ul>
94  * @exception runtime_error if the ::open() returns an error
95  */
96 FifoDescriptor::FifoDescriptor(const std::string& name, const int flags,
97  const mode_t mode)
98 {
99  fd_ = open(name.c_str(), flags, mode);
100  if (fd_ <= 0) {
101  ERROR("Opening fifo " << name);
102  throw std::runtime_error ("Open fifo error");
103  }
104 }
105 
106 
107 
108 /**
109  * \brief Method to get the capacity of the fifo
110  *
111  * @return length of data available on the file;
112  * -1 in case of error or unlimited capacity
113  * @exception runtime_error in case it's not possible to get the capacity
114  */
116 {
117  long int v;
118  errno = 0;
119  if ((v = fpathconf(fd_,_PC_PIPE_BUF)) == -1)
120  if (errno != 0) {
121  ERROR("Can't get fifo capacity");
122  throw std::runtime_error ("Fifo capacity error");
123  return -1;
124  }
125  return v;
126 }
127 
128 } /* onposix */