ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

posix-fs.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #if defined(POSIX) || defined(PLAYSTATION3) || defined(PSP2)
00024 
00025 // Re-enable some forbidden symbols to avoid clashes with stat.h and unistd.h.
00026 // Also with clock() in sys/time.h in some Mac OS X SDKs.
00027 #define FORBIDDEN_SYMBOL_EXCEPTION_time_h
00028 #define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
00029 #define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
00030 #define FORBIDDEN_SYMBOL_EXCEPTION_getenv
00031 #define FORBIDDEN_SYMBOL_EXCEPTION_exit     //Needed for IRIX's unistd.h
00032 #define FORBIDDEN_SYMBOL_EXCEPTION_random
00033 #define FORBIDDEN_SYMBOL_EXCEPTION_srandom
00034 
00035 #include "backends/fs/posix/posix-fs.h"
00036 #include "backends/fs/posix/posix-iostream.h"
00037 #include "common/algorithm.h"
00038 
00039 #include <sys/param.h>
00040 #include <sys/stat.h>
00041 #ifdef MACOSX
00042 #include <sys/types.h>
00043 #endif
00044 #ifdef PSP2
00045 #include "backends/fs/psp2/psp2-dirent.h"
00046 #define mkdir sceIoMkdir
00047 #else
00048 #include <dirent.h>
00049 #endif
00050 #include <stdio.h>
00051 #include <errno.h>
00052 #include <fcntl.h>
00053 #include <unistd.h>
00054 
00055 #ifdef __OS2__
00056 #define INCL_DOS
00057 #include <os2.h>
00058 #endif
00059 
00060 
00061 bool POSIXFilesystemNode::exists() const {
00062     return access(_path.c_str(), F_OK) == 0;
00063 }
00064 
00065 bool POSIXFilesystemNode::isReadable() const {
00066     return access(_path.c_str(), R_OK) == 0;
00067 }
00068 
00069 bool POSIXFilesystemNode::isWritable() const {
00070     return access(_path.c_str(), W_OK) == 0;
00071 }
00072 
00073 void POSIXFilesystemNode::setFlags() {
00074     struct stat st;
00075 
00076     _isValid = (0 == stat(_path.c_str(), &st));
00077     _isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
00078 }
00079 
00080 POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p) {
00081     assert(p.size() > 0);
00082 
00083 #ifdef PSP2
00084     if (p == "/") {
00085         _isDirectory = true;
00086         _isValid = false;
00087         _path = p;
00088         _displayName = p;
00089         return;
00090     }
00091 #endif
00092 
00093     // Expand "~/" to the value of the HOME env variable
00094     if (p.hasPrefix("~/") || p == "~") {
00095         const char *home = getenv("HOME");
00096         if (home != NULL && strlen(home) < MAXPATHLEN) {
00097             _path = home;
00098             // Skip over the tilda.
00099             if (p.size() > 1)
00100                 _path += p.c_str() + 1;
00101         }
00102     } else {
00103         _path = p;
00104     }
00105 
00106 #ifdef __OS2__
00107     // On OS/2, 'X:/' is a root of drive X, so we should not remove that last
00108     // slash.
00109     if (!(_path.size() == 3 && _path.hasSuffix(":/")))
00110 #endif
00111     // Normalize the path (that is, remove unneeded slashes etc.)
00112     _path = Common::normalizePath(_path, '/');
00113     _displayName = Common::lastPathComponent(_path, '/');
00114 
00115     // TODO: should we turn relative paths into absolute ones?
00116     // Pro: Ensures the "getParent" works correctly even for relative dirs.
00117     // Contra: The user may wish to use (and keep!) relative paths in his
00118     //   config file, and converting relative to absolute paths may hurt him...
00119     //
00120     // An alternative approach would be to change getParent() to work correctly
00121     // if "_path" is the empty string.
00122 #if 0
00123     if (!_path.hasPrefix("/")) {
00124         char buf[MAXPATHLEN+1];
00125         getcwd(buf, MAXPATHLEN);
00126         strcat(buf, "/");
00127         _path = buf + _path;
00128     }
00129 #endif
00130     // TODO: Should we enforce that the path is absolute at this point?
00131     //assert(_path.hasPrefix("/"));
00132 
00133     setFlags();
00134 }
00135 
00136 AbstractFSNode *POSIXFilesystemNode::getChild(const Common::String &n) const {
00137     assert(!_path.empty());
00138     assert(_isDirectory);
00139 
00140     // Make sure the string contains no slashes
00141     assert(!n.contains('/'));
00142 
00143     // We assume here that _path is already normalized (hence don't bother to call
00144     //  Common::normalizePath on the final path).
00145     Common::String newPath(_path);
00146     if (_path.lastChar() != '/')
00147         newPath += '/';
00148     newPath += n;
00149 
00150     return makeNode(newPath);
00151 }
00152 
00153 bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
00154     assert(_isDirectory);
00155 
00156 #ifdef __OS2__
00157     if (_path == "/") {
00158         // Special case for the root dir: List all DOS drives
00159         ULONG ulDrvNum;
00160         ULONG ulDrvMap;
00161 
00162         DosQueryCurrentDisk(&ulDrvNum, &ulDrvMap);
00163 
00164         for (int i = 0; i < 26; i++) {
00165             if (ulDrvMap & 1) {
00166                 char drive_root[] = "A:/";
00167                 drive_root[0] += i;
00168 
00169                 POSIXFilesystemNode *entry = new POSIXFilesystemNode();
00170                 entry->_isDirectory = true;
00171                 entry->_isValid = true;
00172                 entry->_path = drive_root;
00173                 entry->_displayName = "[" + Common::String(drive_root, 2) + "]";
00174                 myList.push_back(entry);
00175             }
00176 
00177             ulDrvMap >>= 1;
00178         }
00179 
00180         return true;
00181     }
00182 #endif
00183 #ifdef PSP2
00184     if (_path == "/") {
00185         POSIXFilesystemNode *entry1 = new POSIXFilesystemNode("ux0:");
00186         myList.push_back(entry1);
00187         POSIXFilesystemNode *entry2 = new POSIXFilesystemNode("uma0:");
00188         myList.push_back(entry2);
00189         return true;
00190     }
00191 #endif
00192 
00193     DIR *dirp = opendir(_path.c_str());
00194     struct dirent *dp;
00195 
00196     if (dirp == NULL)
00197         return false;
00198 
00199     // loop over dir entries using readdir
00200     while ((dp = readdir(dirp)) != NULL) {
00201         // Skip 'invisible' files if necessary
00202         if (dp->d_name[0] == '.' && !hidden) {
00203             continue;
00204         }
00205         // Skip '.' and '..' to avoid cycles
00206         if ((dp->d_name[0] == '.' && dp->d_name[1] == 0) || (dp->d_name[0] == '.' && dp->d_name[1] == '.')) {
00207             continue;
00208         }
00209 
00210         // Start with a clone of this node, with the correct path set
00211         POSIXFilesystemNode entry(*this);
00212         entry._displayName = dp->d_name;
00213         if (_path.lastChar() != '/')
00214             entry._path += '/';
00215         entry._path += entry._displayName;
00216 
00217 #if defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
00218         /* TODO: d_type is not part of POSIX, so it might not be supported
00219          * on some of our targets. For those systems where it isn't supported,
00220          * add this #elif case, which tries to use stat() instead.
00221          *
00222          * The d_type method is used to avoid costly recurrent stat() calls in big
00223          * directories.
00224          */
00225         entry.setFlags();
00226 #else
00227         if (dp->d_type == DT_UNKNOWN) {
00228             // Fall back to stat()
00229             entry.setFlags();
00230         } else {
00231             entry._isValid = (dp->d_type == DT_DIR) || (dp->d_type == DT_REG) || (dp->d_type == DT_LNK);
00232             if (dp->d_type == DT_LNK) {
00233                 struct stat st;
00234                 if (stat(entry._path.c_str(), &st) == 0)
00235                     entry._isDirectory = S_ISDIR(st.st_mode);
00236                 else
00237                     entry._isDirectory = false;
00238             } else {
00239                 entry._isDirectory = (dp->d_type == DT_DIR);
00240             }
00241         }
00242 #endif
00243 
00244         // Skip files that are invalid for some reason (e.g. because we couldn't
00245         // properly stat them).
00246         if (!entry._isValid)
00247             continue;
00248 
00249         // Honor the chosen mode
00250         if ((mode == Common::FSNode::kListFilesOnly && entry._isDirectory) ||
00251             (mode == Common::FSNode::kListDirectoriesOnly && !entry._isDirectory))
00252             continue;
00253 
00254         myList.push_back(new POSIXFilesystemNode(entry));
00255     }
00256     closedir(dirp);
00257 
00258     return true;
00259 }
00260 
00261 AbstractFSNode *POSIXFilesystemNode::getParent() const {
00262     if (_path == "/")
00263         return 0;   // The filesystem root has no parent
00264 
00265 #ifdef __OS2__
00266     if (_path.size() == 3 && _path.hasSuffix(":/"))
00267         // This is a root directory of a drive
00268         return makeNode("/");   // return a virtual root for a list of drives
00269 #endif
00270 #ifdef PSP2
00271     if (_path.hasSuffix(":"))
00272         return makeNode("/");
00273 #endif
00274 
00275     const char *start = _path.c_str();
00276     const char *end = start + _path.size();
00277 
00278     // Strip of the last component. We make use of the fact that at this
00279     // point, _path is guaranteed to be normalized
00280     while (end > start && *(end-1) != '/')
00281         end--;
00282 
00283     if (end == start) {
00284         // This only happens if we were called with a relative path, for which
00285         // there simply is no parent.
00286         // TODO: We could also resolve this by assuming that the parent is the
00287         //       current working directory, and returning a node referring to that.
00288         return 0;
00289     }
00290 
00291     return makeNode(Common::String(start, end));
00292 }
00293 
00294 Common::SeekableReadStream *POSIXFilesystemNode::createReadStream() {
00295     return PosixIoStream::makeFromPath(getPath(), false);
00296 }
00297 
00298 Common::WriteStream *POSIXFilesystemNode::createWriteStream() {
00299     return PosixIoStream::makeFromPath(getPath(), true);
00300 }
00301 
00302 bool POSIXFilesystemNode::createDirectory() {
00303     if (mkdir(_path.c_str(), 0755) == 0)
00304         setFlags();
00305 
00306     return _isValid && _isDirectory;
00307 }
00308 
00309 namespace Posix {
00310 
00311 bool assureDirectoryExists(const Common::String &dir, const char *prefix) {
00312     struct stat sb;
00313 
00314     // Check whether the prefix exists if one is supplied.
00315     if (prefix) {
00316         if (stat(prefix, &sb) != 0) {
00317             return false;
00318         } else if (!S_ISDIR(sb.st_mode)) {
00319             return false;
00320         }
00321     }
00322 
00323     // Obtain absolute path.
00324     Common::String path;
00325     if (prefix) {
00326         path = prefix;
00327         path += '/';
00328         path += dir;
00329     } else {
00330         path = dir;
00331     }
00332 
00333     path = Common::normalizePath(path, '/');
00334 
00335     const Common::String::iterator end = path.end();
00336     Common::String::iterator cur = path.begin();
00337     if (*cur == '/')
00338         ++cur;
00339 
00340     do {
00341         if (cur + 1 != end) {
00342             if (*cur != '/') {
00343                 continue;
00344             }
00345 
00346             // It is kind of ugly and against the purpose of Common::String to
00347             // insert 0s inside, but this is just for a local string and
00348             // simplifies the code a lot.
00349             *cur = '\0';
00350         }
00351 
00352         if (mkdir(path.c_str(), 0755) != 0) {
00353             if (errno == EEXIST) {
00354                 if (stat(path.c_str(), &sb) != 0) {
00355                     return false;
00356                 } else if (!S_ISDIR(sb.st_mode)) {
00357                     return false;
00358                 }
00359             } else {
00360                 return false;
00361             }
00362         }
00363 
00364         *cur = '/';
00365     } while (cur++ != end);
00366 
00367     return true;
00368 }
00369 
00370 } // End of namespace Posix
00371 
00372 #endif //#if defined(POSIX)


Generated on Sat Dec 14 2019 05:00:26 for ResidualVM by doxygen 1.7.1
curved edge   curved edge