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