irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

default_transport.hpp
Go to the documentation of this file.
1 #ifndef IRODS_IO_DEFAULT_TRANSPORT_HPP
2 #define IRODS_IO_DEFAULT_TRANSPORT_HPP
3 
4 #undef NAMESPACE_IMPL
5 #undef rxComm
6 #undef rxDataObjOpen
7 #undef rxDataObjRead
8 #undef rxDataObjWrite
9 #undef rxDataObjClose
10 #undef rxDataObjLseek
11 
12 // clang-format off
13 #ifdef IRODS_IO_TRANSPORT_ENABLE_SERVER_SIDE_API
14  #include "rsDataObjOpen.hpp"
15  #include "rsDataObjRead.hpp"
16  #include "rsDataObjWrite.hpp"
17  #include "rsDataObjClose.hpp"
18  #include "rsDataObjLseek.hpp"
19 
20  #define NAMESPACE_IMPL server
21 
22  #define rxComm rsComm_t
23 
24  #define rxDataObjOpen rsDataObjOpen
25  #define rxDataObjRead rsDataObjRead
26  #define rxDataObjWrite rsDataObjWrite
27  #define rxDataObjClose rsDataObjClose
28  #define rxDataObjLseek rsDataObjLseek
29 #else
30  #include "dataObjOpen.h"
31  #include "dataObjRead.h"
32  #include "dataObjWrite.h"
33  #include "dataObjClose.h"
34  #include "dataObjLseek.h"
35 
36  #define NAMESPACE_IMPL client
37 
38  #define rxComm rcComm_t
39 
40  #define rxDataObjOpen rcDataObjOpen
41  #define rxDataObjRead rcDataObjRead
42  #define rxDataObjWrite rcDataObjWrite
43  #define rxDataObjClose rcDataObjClose
44  #define rxDataObjLseek rcDataObjLseek
45 #endif // IRODS_IO_TRANSPORT_ENABLE_SERVER_SIDE_API
46 // clang-format on
47 
48 #include "rcMisc.h"
49 #include "transport/transport.hpp"
50 
51 #include <string>
52 
53 namespace irods {
54 namespace experimental {
55 namespace io {
56 namespace NAMESPACE_IMPL {
57 
58  template <typename CharT>
59  class basic_transport : public transport<CharT>
60  {
61  public:
62  // clang-format off
65  using int_type = typename traits_type::int_type;
66  using pos_type = typename traits_type::pos_type;
67  using off_type = typename traits_type::off_type;
68  // clang-format on
69 
70  private:
71  // clang-format off
72  static constexpr auto uninitialized_file_descriptor = -1;
73  static constexpr auto minimum_valid_file_descriptor = 3;
74 
75  // Errors
76  static constexpr auto translation_error = -1;
77  static const pos_type seek_error; // Initialized outside of class.
78  // clang-format on
79 
80  public:
81  explicit basic_transport(rxComm& _comm)
82  : transport<CharT>{}
83  , comm_{&_comm}
84  , fd_{uninitialized_file_descriptor}
85  {
86  }
87 
89  std::ios_base::openmode _mode) override
90  {
91  return !is_open()
92  ? open_impl(_p, _mode, [](auto&) {})
93  : false;
94  }
95 
97  int _replica_number,
98  std::ios_base::openmode _mode) override
99  {
100  if (is_open()) {
101  return false;
102  }
103 
104  return open_impl(_p, _mode, [_replica_number](auto& _input) {
105  const auto replica = std::to_string(_replica_number);
106  addKeyVal(&_input.condInput, REPL_NUM_KW, replica.c_str());
107  });
108  }
109 
111  const std::string& _resource_name,
112  std::ios_base::openmode _mode) override
113  {
114  if (is_open()) {
115  return false;
116  }
117 
118  return open_impl(_p, _mode, [&_resource_name](auto& _input) {
119  addKeyVal(&_input.condInput, RESC_NAME_KW, _resource_name.c_str());
120  });
121  }
122 
123  bool close() override
124  {
125  openedDataObjInp_t input{};
126  input.l1descInx = fd_;
127 
128  if (rxDataObjClose(comm_, &input) < 0) {
129  return false;
130  }
131 
132  fd_ = uninitialized_file_descriptor;
133 
134  return true;
135  }
136 
137  std::streamsize receive(char_type* _buffer, std::streamsize _buffer_size) override
138  {
139  openedDataObjInp_t input{};
140 
141  input.l1descInx = fd_;
142  input.len = _buffer_size;
143 
144  bytesBuf_t output{};
145 
146  output.len = input.len;
147  output.buf = _buffer;
148 
149  return rxDataObjRead(comm_, &input, &output);
150  }
151 
152  std::streamsize send(const char_type* _buffer, std::streamsize _buffer_size) override
153  {
154  openedDataObjInp_t input{};
155 
156  input.l1descInx = fd_;
157  input.len = _buffer_size;
158 
159  bytesBuf_t input_buffer{};
160 
161  input_buffer.len = input.len;
162  input_buffer.buf = const_cast<char_type*>(_buffer);
163 
164  return rxDataObjWrite(comm_, &input, &input_buffer);
165  }
166 
167  pos_type seekpos(off_type _offset, std::ios_base::seekdir _dir) override
168  {
169  if (!is_open()) {
170  return seek_error;
171  }
172 
173  openedDataObjInp_t input{};
174 
175  input.l1descInx = fd_;
176  input.offset = _offset;
177 
178  switch (_dir) {
179  case std::ios_base::beg:
180  input.whence = SEEK_SET;
181  break;
182 
183  case std::ios_base::cur:
184  input.whence = SEEK_CUR;
185  break;
186 
187  case std::ios_base::end:
188  input.whence = SEEK_END;
189  break;
190 
191  default:
192  return seek_error;
193  }
194 
196 
197  if (rxDataObjLseek(comm_, &input, &output) < 0) {
198  return seek_error;
199  }
200 
201  return output->offset;
202  }
203 
204  bool is_open() const noexcept override
205  {
206  return fd_ >= minimum_valid_file_descriptor;
207  }
208 
209  int file_descriptor() const noexcept override
210  {
211  return fd_;
212  }
213 
214  private:
215  int make_open_flags(std::ios_base::openmode _mode) noexcept
216  {
217  using std::ios_base;
218 
219  const auto m = _mode & ~(ios_base::ate | ios_base::binary);
220 
221  if (ios_base::in == m) {
222  return O_RDONLY;
223  }
224  else if (ios_base::out == m || (ios_base::out | ios_base::trunc) == m) {
225  return O_CREAT | O_WRONLY | O_TRUNC;
226  }
227  else if (ios_base::app == m || (ios_base::out | ios_base::app) == m) {
228  return O_CREAT | O_WRONLY | O_APPEND;
229  }
230  else if ((ios_base::out | ios_base::in) == m) {
231  return O_RDWR;
232  }
233  else if ((ios_base::out | ios_base::in | ios_base::trunc) == m) {
234  return O_CREAT | O_RDWR | O_TRUNC;
235  }
236  else if ((ios_base::out | ios_base::in | ios_base::app) == m ||
237  (ios_base::in | ios_base::app) == m)
238  {
239  return O_CREAT | O_RDWR | O_APPEND;
240  }
241 
242  return translation_error;
243  }
244 
245  bool seek_to_end_if_required(std::ios_base::openmode _mode)
246  {
247  if (std::ios_base::ate & _mode) {
248  if (seek_error == seekpos(0, std::ios_base::end)) {
249  return false;
250  }
251  }
252 
253  return true;
254  }
255 
256  template <typename Function>
257  bool open_impl(const filesystem::path& _p, std::ios_base::openmode _mode, Function _func)
258  {
259  const auto flags = make_open_flags(_mode);
260 
261  if (flags == translation_error) {
262  return false;
263  }
264 
265  dataObjInp_t input{};
266 
267  input.createMode = 0600;
268  input.openFlags = flags;
269  rstrcpy(input.objPath, _p.c_str(), sizeof(input.objPath));
270 
271  _func(input);
272 
273  // TODO Modularize the block of code below.
274 
275  const auto fd = rxDataObjOpen(comm_, &input);
276 
277  if (fd < minimum_valid_file_descriptor) {
278  return false;
279  }
280 
281  fd_ = fd;
282 
283  if (!seek_to_end_if_required(_mode)) {
284  close();
285  return false;
286  }
287 
288  return true;
289  }
290 
292  int fd_;
293  }; // basic_transport
294 
295  template <typename CharT>
296  const typename basic_transport<CharT>::pos_type
298 
300 
301 } // NAMESPACE_IMPL
302 } // io
303 } // experimental
304 } // irods
305 
306 #endif // IRODS_IO_DEFAULT_TRANSPORT_HPP
307 
OpenedDataObjInp
Definition: dataObjInpOut.h:104
dataObjRead.h
RESC_NAME_KW
#define RESC_NAME_KW
Definition: rodsKeyWdDef.h:19
irods::experimental::io::transport
Definition: transport.hpp:16
addKeyVal
int addKeyVal(keyValPair_t *condInput, const char *keyWord, const char *value)
Definition: rcMisc.cpp:789
rsDataObjWrite.hpp
rsDataObjOpen.hpp
irods::experimental::io::client::basic_transport::file_descriptor
int file_descriptor() const noexcept override
Definition: default_transport.hpp:209
irods::experimental::filesystem::path
Definition: path.hpp:15
OpenedDataObjInp::l1descInx
int l1descInx
Definition: dataObjInpOut.h:105
generate_iadmin_commands_for_41_to_42_upgrade.output
output
Definition: generate_iadmin_commands_for_41_to_42_upgrade.py:21
irods::experimental::io::client::basic_transport::send
std::streamsize send(const char_type *_buffer, std::streamsize _buffer_size) override
Definition: default_transport.hpp:152
rcMisc.h
NAMESPACE_IMPL
#define NAMESPACE_IMPL
Definition: default_transport.hpp:36
irods::experimental::io::client::basic_transport::fd_
int fd_
Definition: default_transport.hpp:292
dataObjLseek.h
rxComm
#define rxComm
Definition: default_transport.hpp:38
irods::experimental::io::client::basic_transport::open
bool open(const irods::experimental::filesystem::path &_p, int _replica_number, std::ios_base::openmode _mode) override
Definition: default_transport.hpp:96
REPL_NUM_KW
#define REPL_NUM_KW
Definition: rodsKeyWdDef.h:30
irods::experimental::io::client::basic_transport::traits_type
typename transport< CharT >::traits_type traits_type
Definition: default_transport.hpp:64
BytesBuf::len
int len
Definition: rodsDef.h:198
irods::experimental::filesystem::client::end
auto end(const collection_iterator &) noexcept -> const collection_iterator
Definition: collection_iterator.hpp:88
irods::experimental::io::client::basic_transport::int_type
typename traits_type::int_type int_type
Definition: default_transport.hpp:65
rxDataObjLseek
#define rxDataObjLseek
Definition: default_transport.hpp:44
irods
Definition: apiHandler.hpp:35
irods::experimental::io::client::basic_transport::off_type
typename traits_type::off_type off_type
Definition: default_transport.hpp:67
irods::experimental::io::client::basic_transport::open
bool open(const irods::experimental::filesystem::path &_p, const std::string &_resource_name, std::ios_base::openmode _mode) override
Definition: default_transport.hpp:110
irods::experimental::io::client::basic_transport::comm_
rcComm_t * comm_
Definition: default_transport.hpp:291
DataObjInp
Definition: dataObjInpOut.h:65
rsDataObjClose.hpp
irods::experimental::io::client::basic_transport::open
bool open(const irods::experimental::filesystem::path &_p, std::ios_base::openmode _mode) override
Definition: default_transport.hpp:88
irods::experimental::io::client::basic_transport::close
bool close() override
Definition: default_transport.hpp:123
irods::experimental::io::client::basic_transport::basic_transport
basic_transport(rcComm_t &_comm)
Definition: default_transport.hpp:81
irods::experimental::io::client::basic_transport::seek_error
static const pos_type seek_error
Definition: default_transport.hpp:77
dataObjWrite.h
dataObjClose.h
irods::experimental::io::client::basic_transport::receive
std::streamsize receive(char_type *_buffer, std::streamsize _buffer_size) override
Definition: default_transport.hpp:137
BytesBuf
Definition: rodsDef.h:197
FileLseekOut
Definition: fileLseek.h:53
rxDataObjOpen
#define rxDataObjOpen
Definition: default_transport.hpp:40
rxDataObjRead
#define rxDataObjRead
Definition: default_transport.hpp:41
DataObjInp::createMode
int createMode
Definition: dataObjInpOut.h:67
irods::experimental::io::client::basic_transport::seek_to_end_if_required
bool seek_to_end_if_required(std::ios_base::openmode _mode)
Definition: default_transport.hpp:245
rxDataObjWrite
#define rxDataObjWrite
Definition: default_transport.hpp:42
rsDataObjRead.hpp
rsDataObjLseek.hpp
irods::experimental::io::client::basic_transport
Definition: default_transport.hpp:60
irods::experimental::io::client::basic_transport::char_type
typename transport< CharT >::char_type char_type
Definition: default_transport.hpp:63
transport.hpp
irods::experimental::io::client::basic_transport::seekpos
pos_type seekpos(off_type _offset, std::ios_base::seekdir _dir) override
Definition: default_transport.hpp:167
irods::experimental::io::client::basic_transport::is_open
bool is_open() const noexcept override
Definition: default_transport.hpp:204
irods::experimental::io::client::basic_transport::open_impl
bool open_impl(const filesystem::path &_p, std::ios_base::openmode _mode, Function _func)
Definition: default_transport.hpp:257
irods::experimental::filesystem::path::c_str
auto c_str() const noexcept -> const value_type *
Definition: path.hpp:200
dataObjOpen.h
rstrcpy
char * rstrcpy(char *dest, const char *src, int maxLen)
Definition: stringOpr.cpp:51
irods::experimental::io::client::basic_transport::make_open_flags
int make_open_flags(std::ios_base::openmode _mode) noexcept
Definition: default_transport.hpp:215
manual_cleanup.out
out
Definition: manual_cleanup.py:29
irods::experimental::io::client::basic_transport::pos_type
typename traits_type::pos_type pos_type
Definition: default_transport.hpp:66
rxDataObjClose
#define rxDataObjClose
Definition: default_transport.hpp:43