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 #if defined(__ANDROID__) && !defined(ANDROIDSDL)
00061 #include "backends/platform/android/jni-android.h"
00062 #endif
00063 
00064 bool POSIXFilesystemNode::exists() const {
00065     return access(_path.c_str(), F_OK) == 0;
00066 }
00067 
00068 bool POSIXFilesystemNode::isReadable() const {
00069     return access(_path.c_str(), R_OK) == 0;
00070 }
00071 
00072 bool POSIXFilesystemNode::isWritable() const {
00073     return access(_path.c_str(), W_OK) == 0;
00074 }
00075 
00076 void POSIXFilesystemNode::setFlags() {
00077     struct stat st;
00078 
00079     _isValid = (0 == stat(_path.c_str(), &st));
00080     _isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
00081 }
00082 
00083 POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p) {
00084     assert(p.size() > 0);
00085 
00086 #ifdef PSP2
00087     if (p == "/") {
00088         _isDirectory = true;
00089         _isValid = false;
00090         _path = p;
00091         _displayName = p;
00092         return;
00093     }
00094 #endif
00095 
00096     // Expand "~/" to the value of the HOME env variable
00097     if (p.hasPrefix("~/") || p == "~") {
00098         const char *home = getenv("HOME");
00099         if (home != NULL && strlen(home) < MAXPATHLEN) {
00100             _path = home;
00101             // Skip over the tilda.
00102             if (p.size() > 1)
00103                 _path += p.c_str() + 1;
00104         }
00105     } else {
00106         _path = p;
00107     }
00108 
00109 #ifdef __OS2__
00110     // On OS/2, 'X:/' is a root of drive X, so we should not remove that last
00111     // slash.
00112     if (!(_path.size() == 3 && _path.hasSuffix(":/")))
00113 #endif
00114     // Normalize the path (that is, remove unneeded slashes etc.)
00115     _path = Common::normalizePath(_path, '/');
00116     _displayName = Common::lastPathComponent(_path, '/');
00117 
00118     // TODO: should we turn relative paths into absolute ones?
00119     // Pro: Ensures the "getParent" works correctly even for relative dirs.
00120     // Contra: The user may wish to use (and keep!) relative paths in his
00121     //   config file, and converting relative to absolute paths may hurt him...
00122     //
00123     // An alternative approach would be to change getParent() to work correctly
00124     // if "_path" is the empty string.
00125 #if 0
00126     if (!_path.hasPrefix("/")) {
00127         char buf[MAXPATHLEN+1];
00128         getcwd(buf, MAXPATHLEN);
00129         strcat(buf, "/");
00130         _path = buf + _path;
00131     }
00132 #endif
00133     // TODO: Should we enforce that the path is absolute at this point?
00134     //assert(_path.hasPrefix("/"));
00135 
00136     setFlags();
00137 }
00138 
00139 AbstractFSNode *POSIXFilesystemNode::getChild(const Common::String &n) const {
00140     assert(!_path.empty());
00141     assert(_isDirectory);
00142 
00143     // Make sure the string contains no slashes
00144     assert(!n.contains('/'));
00145 
00146     // We assume here that _path is already normalized (hence don't bother to call
00147     //  Common::normalizePath on the final path).
00148     Common::String newPath(_path);
00149     if (_path.lastChar() != '/')
00150         newPath += '/';
00151     newPath += n;
00152 
00153     return makeNode(newPath);
00154 }
00155 
00156 bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
00157     assert(_isDirectory);
00158 
00159 #ifdef __OS2__
00160     if (_path == "/") {
00161         // Special case for the root dir: List all DOS drives
00162         ULONG ulDrvNum;
00163         ULONG ulDrvMap;
00164 
00165         DosQueryCurrentDisk(&ulDrvNum, &ulDrvMap);
00166 
00167         for (int i = 0; i < 26; i++) {
00168             if (ulDrvMap & 1) {
00169                 char drive_root[] = "A:/";
00170                 drive_root[0] += i;
00171 
00172                 POSIXFilesystemNode *entry = new POSIXFilesystemNode();
00173                 entry->_isDirectory = true;
00174                 entry->_isValid = true;
00175                 entry->_path = drive_root;
00176                 entry->_displayName = "[" + Common::String(drive_root, 2) + "]";
00177                 myList.push_back(entry);
00178             }
00179 
00180             ulDrvMap >>= 1;
00181         }
00182 
00183         return true;
00184     }
00185 #endif
00186 #ifdef PSP2
00187     if (_path == "/") {
00188         POSIXFilesystemNode *entry1 = new POSIXFilesystemNode("ux0:");
00189         myList.push_back(entry1);
00190         POSIXFilesystemNode *entry2 = new POSIXFilesystemNode("uma0:");
00191         myList.push_back(entry2);
00192         return true;
00193     }
00194 #endif
00195 
00196 #if defined(__ANDROID__) && !defined(ANDROIDSDL)
00197     if (_path == "/") {
00198         Common::Array<Common::String> list = JNI::getAllStorageLocations();
00199         for (Common::Array<Common::String>::const_iterator it = list.begin(), end = list.end(); it != end; ++it) {
00200             POSIXFilesystemNode *entry = new POSIXFilesystemNode();
00201 
00202             entry->_isDirectory = true;
00203             entry->_isValid = true;
00204             entry->_displayName = *it;
00205             ++it;
00206             entry->_path = *it;
00207             myList.push_back(entry);
00208         }
00209         return true;
00210     }
00211 #endif
00212 
00213     DIR *dirp = opendir(_path.c_str());
00214     struct dirent *dp;
00215 
00216     if (dirp == NULL)
00217         return false;
00218 
00219     // loop over dir entries using readdir
00220     while ((dp = readdir(dirp)) != NULL) {
00221         // Skip 'invisible' files if necessary
00222         if (dp->d_name[0] == '.' && !hidden) {
00223             continue;
00224         }
00225         // Skip '.' and '..' to avoid cycles
00226         if ((dp->d_name[0] == '.' && dp->d_name[1] == 0) || (dp->d_name[0] == '.' && dp->d_name[1] == '.')) {
00227             continue;
00228         }
00229 
00230         // Start with a clone of this node, with the correct path set
00231         POSIXFilesystemNode entry(*this);
00232         entry._displayName = dp->d_name;
00233         if (_path.lastChar() != '/')
00234             entry._path += '/';
00235         entry._path += entry._displayName;
00236 
00237 #if defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
00238         /* TODO: d_type is not part of POSIX, so it might not be supported
00239          * on some of our targets. For those systems where it isn't supported,
00240          * add this #elif case, which tries to use stat() instead.
00241          *
00242          * The d_type method is used to avoid costly recurrent stat() calls in big
00243          * directories.
00244          */
00245         entry.setFlags();
00246 #else
00247         if (dp->d_type == DT_UNKNOWN) {
00248             // Fall back to stat()
00249             entry.setFlags();
00250         } else {
00251             entry._isValid = (dp->d_type == DT_DIR) || (dp->d_type == DT_REG) || (dp->d_type == DT_LNK);
00252             if (dp->d_type == DT_LNK) {
00253                 struct stat st;
00254                 if (stat(entry._path.c_str(), &st) == 0)
00255                     entry._isDirectory = S_ISDIR(st.st_mode);
00256                 else
00257                     entry._isDirectory = false;
00258             } else {
00259                 entry._isDirectory = (dp->d_type == DT_DIR);
00260             }
00261         }
00262 #endif
00263 
00264         // Skip files that are invalid for some reason (e.g. because we couldn't
00265         // properly stat them).
00266         if (!entry._isValid)
00267             continue;
00268 
00269         // Honor the chosen mode
00270         if ((mode == Common::FSNode::kListFilesOnly && entry._isDirectory) ||
00271             (mode == Common::FSNode::kListDirectoriesOnly && !entry._isDirectory))
00272             continue;
00273 
00274         myList.push_back(new POSIXFilesystemNode(entry));
00275     }
00276     closedir(dirp);
00277 
00278     return true;
00279 }
00280 
00281 AbstractFSNode *POSIXFilesystemNode::getParent() const {
00282     if (_path == "/")
00283         return 0;   // The filesystem root has no parent
00284 
00285 #ifdef __OS2__
00286     if (_path.size() == 3 && _path.hasSuffix(":/"))
00287         // This is a root directory of a drive
00288         return makeNode("/");   // return a virtual root for a list of drives
00289 #endif
00290 #ifdef PSP2
00291     if (_path.hasSuffix(":"))
00292         return makeNode("/");
00293 #endif
00294 
00295     const char *start = _path.c_str();
00296     const char *end = start + _path.size();
00297 
00298     // Strip of the last component. We make use of the fact that at this
00299     // point, _path is guaranteed to be normalized
00300     while (end > start && *(end-1) != '/')
00301         end--;
00302 
00303     if (end == start) {
00304         // This only happens if we were called with a relative path, for which
00305         // there simply is no parent.
00306         // TODO: We could also resolve this by assuming that the parent is the
00307         //       current working directory, and returning a node referring to that.
00308         return 0;
00309     }
00310 
00311     return makeNode(Common::String(start, end));
00312 }
00313 
00314 Common::SeekableReadStream *POSIXFilesystemNode::createReadStream() {
00315     return PosixIoStream::makeFromPath(getPath(), false);
00316 }
00317 
00318 Common::WriteStream *POSIXFilesystemNode::createWriteStream() {
00319     return PosixIoStream::makeFromPath(getPath(), true);
00320 }
00321 
00322 bool POSIXFilesystemNode::createDirectory() {
00323     if (mkdir(_path.c_str(), 0755) == 0)
00324         setFlags();
00325 
00326     return _isValid && _isDirectory;
00327 }
00328 
00329 namespace Posix {
00330 
00331 bool assureDirectoryExists(const Common::String &dir, const char *prefix) {
00332     struct stat sb;
00333 
00334     // Check whether the prefix exists if one is supplied.
00335     if (prefix) {
00336         if (stat(prefix, &sb) != 0) {
00337             return false;
00338         } else if (!S_ISDIR(sb.st_mode)) {
00339             return false;
00340         }
00341     }
00342 
00343     // Obtain absolute path.
00344     Common::String path;
00345     if (prefix) {
00346         path = prefix;
00347         path += '/';
00348         path += dir;
00349     } else {
00350         path = dir;
00351     }
00352 
00353     path = Common::normalizePath(path, '/');
00354 
00355     const Common::String::iterator end = path.end();
00356     Common::String::iterator cur = path.begin();
00357     if (*cur == '/')
00358         ++cur;
00359 
00360     do {
00361         if (cur + 1 != end) {
00362             if (*cur != '/') {
00363                 continue;
00364             }
00365 
00366             // It is kind of ugly and against the purpose of Common::String to
00367             // insert 0s inside, but this is just for a local string and
00368             // simplifies the code a lot.
00369             *cur = '\0';
00370         }
00371 
00372         if (mkdir(path.c_str(), 0755) != 0) {
00373             if (errno == EEXIST) {
00374                 if (stat(path.c_str(), &sb) != 0) {
00375                     return false;
00376                 } else if (!S_ISDIR(sb.st_mode)) {
00377                     return false;
00378                 }
00379             } else {
00380                 return false;
00381             }
00382         }
00383 
00384         *cur = '/';
00385     } while (cur++ != end);
00386 
00387     return true;
00388 }
00389 
00390 } // End of namespace Posix
00391 
00392 #endif //#if defined(POSIX)


Generated on Sat May 23 2020 05:00:27 for ResidualVM by doxygen 1.7.1
curved edge   curved edge