dmlite  0.6
DomeUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 CERN
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /** @file DomeUtils.h
19  * @brief Small utilities used throughout dome
20  * @author Georgios Bitzes
21  * @date Feb 2016
22  */
23 
24 #ifndef DOMEUTILS_H
25 #define DOMEUTILS_H
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 
31 #include <string>
32 #include <vector>
33 
34 #include <dmlite/cpp/exceptions.h>
35 
36 namespace DomeUtils {
37 
38 using namespace dmlite;
39 
40 inline std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix) {
41  if(prefix.size() > str.size()) return str;
42 
43  if(std::equal(prefix.begin(), prefix.end(), str.begin())) {
44  return str.substr(prefix.size(), str.size()-prefix.size());
45  }
46 
47  return str;
48 }
49 
50 inline std::string trim_trailing_slashes(std::string str) {
51  while(str.size() > 0 && str[str.size()-1] == '/') {
52  str.erase(str.size()-1);
53  }
54  return str;
55 }
56 
57 inline std::string join(const std::string &separator, const std::vector<std::string> &arr) {
58  if(arr.empty()) return std::string();
59 
60  std::stringstream ss;
61  for(size_t i = 0; i < arr.size()-1; i++) {
62  ss << arr[i];
63  ss << separator;
64  }
65  ss << arr[arr.size()-1];
66  return ss.str();
67 }
68 
69 inline std::vector<std::string> split(std::string data, std::string token) {
70  std::vector<std::string> output;
71  size_t pos = std::string::npos;
72  do {
73  pos = data.find(token);
74  output.push_back(data.substr(0, pos));
75  if(std::string::npos != pos)
76  data = data.substr(pos + token.size());
77  } while (std::string::npos != pos);
78  return output;
79 }
80 
81 inline void mkdirp(const std::string& path) {
82  std::vector<std::string> parts = split(path, "/");
83  std::ostringstream tocreate(parts[0]);
84 
85  // rudimentary sanity protection: never try to create the top-level directory
86  for(std::vector<std::string>::iterator it = parts.begin()+1; it+1 != parts.end(); it++) {
87  tocreate << "/" + *it;
88 
89  struct stat info;
90  if(::stat(tocreate.str().c_str(), &info) != 0) {
91  Log(Logger::Lvl1, Logger::unregistered, Logger::unregisteredname, " Creating directory: " << tocreate.str());
92 
93  mode_t prev = umask(0);
94  int ret = ::mkdir(tocreate.str().c_str(), 0770);
95  umask(prev);
96 
97  if(ret != 0) {
98  char errbuffer[256];
99  dpm_strerror_r(errno, errbuffer, sizeof(errbuffer));
100  throw DmException(errno, "Could not create directory: '%s' err: %d:'%s'", tocreate.str().c_str(), errno, errbuffer);
101  }
102  }
103  }
104 }
105 
106 inline std::string bool_to_str(bool b) {
107  if(b) return "true";
108  else return "false";
109 }
110 
111 inline bool str_to_bool(const std::string &str) {
112  bool value = false;
113 
114  if(str == "false" || str == "0" || str == "no") {
115  value = false;
116  } else if(str == "true" || str == "1" || str == "yes") {
117  value = true;
118  }
119  return value;
120 }
121 
122 inline std::string pfn_from_rfio_syntax(const std::string &rfn) {
123  size_t pos = rfn.find(":");
124  if(pos == std::string::npos)
125  return rfn;
126  return rfn.substr(pos+1, rfn.size());
127 }
128 
129 inline std::string server_from_rfio_syntax(const std::string &rfn) {
130  size_t pos = rfn.find(":");
131  if(pos == std::string::npos)
132  return rfn;
133  return rfn.substr(0, pos);
134 }
135 
136 inline std::string unescape_forward_slashes(const std::string &str) {
137  std::ostringstream ss;
138  for(size_t i = 0; i < str.size(); i++) {
139  if(i != str.size()-1 && str[i] == '\\' && str[i+1] == '/') {
140  ss << "/";
141  i++;
142  }
143  else {
144  ss << str[i];
145  }
146  }
147  return ss.str();
148 }
149 
150 }
151 
152 
153 
154 
155 #endif
exceptions.h
Exceptions used by the API.
Logger::unregistered
static bitmask unregistered
Definition: logger.h:83
DomeUtils::unescape_forward_slashes
std::string unescape_forward_slashes(const std::string &str)
Definition: DomeUtils.h:136
Logger::Lvl1
@ Lvl1
Definition: logger.h:91
DomeUtils::split
std::vector< std::string > split(std::string data, std::string token)
Definition: DomeUtils.h:69
DomeUtils::mkdirp
void mkdirp(const std::string &path)
Definition: DomeUtils.h:81
Logger::unregisteredname
static char * unregisteredname
Definition: logger.h:84
DomeUtils::bool_to_str
std::string bool_to_str(bool b)
Definition: DomeUtils.h:106
DomeUtils::server_from_rfio_syntax
std::string server_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:129
DomeUtils::trim_trailing_slashes
std::string trim_trailing_slashes(std::string str)
Definition: DomeUtils.h:50
DomeUtils::pfn_from_rfio_syntax
std::string pfn_from_rfio_syntax(const std::string &rfn)
Definition: DomeUtils.h:122
dmlite::DmException
Base exception class.
Definition: exceptions.h:17
DomeUtils::join
std::string join(const std::string &separator, const std::vector< std::string > &arr)
Definition: DomeUtils.h:57
dpm_strerror_r
#define dpm_strerror_r(errnum, buf, buflen)
Definition: logger.h:32
Log
#define Log(lvl, mymask, where, what)
Definition: logger.h:53
DomeUtils
Definition: DomeUtils.h:36
DomeUtils::str_to_bool
bool str_to_bool(const std::string &str)
Definition: DomeUtils.h:111
dmlite
Namespace for the dmlite C++ API.
Definition: authn.h:15
DomeUtils::remove_prefix_if_exists
std::string remove_prefix_if_exists(const std::string &str, const std::string &prefix)
Definition: DomeUtils.h:40