bes  Updated for version 3.20.5
BESFSFile.cc
1 // BESFSFile.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <cerrno>
39 #include <cstring>
40 
41 #include "BESFSFile.h"
42 
43 BESFSFile::BESFSFile(const string &fullPath) :
44  _dirName(""), _fileName(""), _baseName(""), _extension("")
45 {
46  breakApart(fullPath);
47 }
48 
49 BESFSFile::BESFSFile(const string &dirName, const string &fileName) :
50  _dirName(dirName), _fileName(fileName), _baseName(""), _extension("")
51 {
52  breakExtension();
53 }
54 
55 BESFSFile::BESFSFile(const BESFSFile &copyFrom) :
56  _dirName(copyFrom._dirName), _fileName(copyFrom._fileName), _baseName(copyFrom._baseName), _extension(
57  copyFrom._extension)
58 {
59 }
60 
61 BESFSFile::~BESFSFile()
62 {
63 }
64 
65 string BESFSFile::getDirName()
66 {
67  return _dirName;
68 }
69 
70 string BESFSFile::getFileName()
71 {
72  return _fileName;
73 }
74 
75 string BESFSFile::getBaseName()
76 {
77  return _baseName;
78 }
79 
80 string BESFSFile::getExtension()
81 {
82  return _extension;
83 }
84 
85 string BESFSFile::getFullPath()
86 {
87  return _dirName + "/" + _fileName;
88 }
89 
90 void BESFSFile::breakApart(const string &fullPath)
91 {
92  string::size_type pos = fullPath.rfind("/");
93  if (pos != string::npos) {
94  _dirName = fullPath.substr(0, pos);
95  _fileName = fullPath.substr(pos + 1, fullPath.length() - pos);
96  }
97  else {
98  _dirName = "./";
99  _fileName = fullPath;
100  }
101 
102  breakExtension();
103 }
104 
105 void BESFSFile::breakExtension()
106 {
107  string::size_type pos = _fileName.rfind(".");
108  if (pos != string::npos) {
109  _baseName = _fileName.substr(0, pos);
110  _extension = _fileName.substr(pos + 1, _fileName.length() - pos);
111  }
112  else {
113  _baseName = _fileName;
114  }
115 }
116 
117 bool BESFSFile::exists(string &reason)
118 {
119  bool ret = false;
120  if (!access(getFullPath().c_str(), F_OK)) {
121  ret = true;
122  }
123  else {
124  char *err = strerror(errno);
125  if (err) {
126  reason += err;
127  }
128  else {
129  reason += "Unknown error";
130  }
131  }
132  return ret;
133 }
134 
135 bool BESFSFile::isReadable(string &reason)
136 {
137  bool ret = false;
138  if (!access(getFullPath().c_str(), R_OK)) {
139  ret = true;
140  }
141  else {
142  char *err = strerror(errno);
143  if (err) {
144  reason += err;
145  }
146  else {
147  reason += "Unknown error";
148  }
149  }
150  return ret;
151 }
152 
153 bool BESFSFile::isWritable(string &reason)
154 {
155  bool ret = false;
156  if (!access(getFullPath().c_str(), W_OK)) {
157  ret = true;
158  }
159  else {
160  char *err = strerror(errno);
161  if (err) {
162  reason += err;
163  }
164  else {
165  reason += "Unknown error";
166  }
167  }
168  return ret;
169 }
170 
171 bool BESFSFile::isExecutable(string &reason)
172 {
173  bool ret = false;
174  if (!access(getFullPath().c_str(), X_OK)) {
175  ret = true;
176  }
177  else {
178  char *err = strerror(errno);
179  if (err) {
180  reason += err;
181  }
182  else {
183  reason += "Unknown error";
184  }
185  }
186  return ret;
187 }
188 
189 bool BESFSFile::hasDotDot()
190 {
191  bool ret = false;
192  string fp = getFullPath();
193  if (fp.find("..") != string::npos) {
194  ret = true;
195  }
196  return ret;
197 }
198 
BESFSFile
Definition: BESFSFile.h:42