bes  Updated for version 3.20.6
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 using std::string;
44 
45 BESFSFile::BESFSFile(const string &fullPath) :
46  _dirName(""), _fileName(""), _baseName(""), _extension("")
47 {
48  breakApart(fullPath);
49 }
50 
51 BESFSFile::BESFSFile(const string &dirName, const string &fileName) :
52  _dirName(dirName), _fileName(fileName), _baseName(""), _extension("")
53 {
54  breakExtension();
55 }
56 
57 BESFSFile::BESFSFile(const BESFSFile &copyFrom) :
58  _dirName(copyFrom._dirName), _fileName(copyFrom._fileName), _baseName(copyFrom._baseName), _extension(
59  copyFrom._extension)
60 {
61 }
62 
63 BESFSFile::~BESFSFile()
64 {
65 }
66 
67 string BESFSFile::getDirName()
68 {
69  return _dirName;
70 }
71 
72 string BESFSFile::getFileName()
73 {
74  return _fileName;
75 }
76 
77 string BESFSFile::getBaseName()
78 {
79  return _baseName;
80 }
81 
82 string BESFSFile::getExtension()
83 {
84  return _extension;
85 }
86 
87 string BESFSFile::getFullPath()
88 {
89  return _dirName + "/" + _fileName;
90 }
91 
92 void BESFSFile::breakApart(const string &fullPath)
93 {
94  string::size_type pos = fullPath.rfind("/");
95  if (pos != string::npos) {
96  _dirName = fullPath.substr(0, pos);
97  _fileName = fullPath.substr(pos + 1, fullPath.length() - pos);
98  }
99  else {
100  _dirName = "./";
101  _fileName = fullPath;
102  }
103 
104  breakExtension();
105 }
106 
107 void BESFSFile::breakExtension()
108 {
109  string::size_type pos = _fileName.rfind(".");
110  if (pos != string::npos) {
111  _baseName = _fileName.substr(0, pos);
112  _extension = _fileName.substr(pos + 1, _fileName.length() - pos);
113  }
114  else {
115  _baseName = _fileName;
116  }
117 }
118 
119 bool BESFSFile::exists(string &reason)
120 {
121  bool ret = false;
122  if (!access(getFullPath().c_str(), F_OK)) {
123  ret = true;
124  }
125  else {
126  char *err = strerror(errno);
127  if (err) {
128  reason += err;
129  }
130  else {
131  reason += "Unknown error";
132  }
133  }
134  return ret;
135 }
136 
137 bool BESFSFile::isReadable(string &reason)
138 {
139  bool ret = false;
140  if (!access(getFullPath().c_str(), R_OK)) {
141  ret = true;
142  }
143  else {
144  char *err = strerror(errno);
145  if (err) {
146  reason += err;
147  }
148  else {
149  reason += "Unknown error";
150  }
151  }
152  return ret;
153 }
154 
155 bool BESFSFile::isWritable(string &reason)
156 {
157  bool ret = false;
158  if (!access(getFullPath().c_str(), W_OK)) {
159  ret = true;
160  }
161  else {
162  char *err = strerror(errno);
163  if (err) {
164  reason += err;
165  }
166  else {
167  reason += "Unknown error";
168  }
169  }
170  return ret;
171 }
172 
173 bool BESFSFile::isExecutable(string &reason)
174 {
175  bool ret = false;
176  if (!access(getFullPath().c_str(), X_OK)) {
177  ret = true;
178  }
179  else {
180  char *err = strerror(errno);
181  if (err) {
182  reason += err;
183  }
184  else {
185  reason += "Unknown error";
186  }
187  }
188  return ret;
189 }
190 
191 bool BESFSFile::hasDotDot()
192 {
193  bool ret = false;
194  string fp = getFullPath();
195  if (fp.find("..") != string::npos) {
196  ret = true;
197  }
198  return ret;
199 }
200 
BESFSFile
Definition: BESFSFile.h:40