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

riscos-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(RISCOS)
00024 
00025 // Re-enable some forbidden symbols to avoid clashes with stat.h and unistd.h.
00026 #define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
00027 #define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
00028 
00029 #include "backends/fs/riscos/riscos-fs.h"
00030 #include "backends/fs/stdiostream.h"
00031 #include "common/algorithm.h"
00032 
00033 #include <limits.h>
00034 #include <stdio.h>
00035 #include <unistd.h>
00036 #include <fcntl.h>
00037 
00038 #include <unixlib/local.h>
00039 #include <kernel.h>
00040 #include <swis.h>
00041 
00042 bool RISCOSFilesystemNode::exists() const {
00043     return access(_path.c_str(), F_OK) == 0;
00044 }
00045 
00046 bool RISCOSFilesystemNode::isReadable() const {
00047     return access(_path.c_str(), R_OK) == 0;
00048 }
00049 
00050 bool RISCOSFilesystemNode::isWritable() const {
00051     return access(_path.c_str(), W_OK) == 0;
00052 }
00053 
00054 RISCOSFilesystemNode::RISCOSFilesystemNode(const Common::String &p) {
00055     _path = p;
00056     if (p == "/") {
00057         _isDirectory = true;
00058         _isValid = true;
00059     } else {
00060         int type = _swi(OS_File, _INR(0,1)|_RETURN(0), 20, toRISCOS(_path).c_str());
00061         if (type == 0) {
00062             _isDirectory = false;
00063             _isValid = false;
00064         } else if (type == 2) {
00065             _isDirectory = true;
00066             _isValid = true;
00067         } else {
00068             _isDirectory = false;
00069             _isValid = true;
00070         }
00071     }
00072 }
00073 
00074 Common::String RISCOSFilesystemNode::toRISCOS(Common::String &path) {
00075     char start[PATH_MAX];
00076     char *end = __riscosify_std(path.c_str(), 0, start, PATH_MAX, 0);
00077     return Common::String(start, end);
00078 }
00079 
00080 Common::String RISCOSFilesystemNode::toUnix(Common::String &path) {
00081     Common::String out = Common::String(path);
00082     uint32 start = 0;
00083     if (out.contains("$")) {
00084         char *x = strstr(out.c_str(), "$");
00085         start = x ? x - out.c_str() : -1;
00086     } else if (out.contains(":")) {
00087         char *x = strstr(out.c_str(), ":");
00088         start = x ? x - out.c_str() : -1;
00089     }
00090 
00091     for (uint32 ptr = start; ptr < out.size(); ptr += 1) {
00092         switch (out.c_str()[ptr]) {
00093         case '.':
00094             out.setChar('/', ptr);
00095             break;
00096         case '/':
00097             out.setChar('.', ptr);
00098             break;
00099         case '\xA0':
00100             out.setChar(' ', ptr);
00101             break;
00102         default:
00103             break;
00104         }
00105     }
00106 
00107     if (out.contains("$") || out.contains(":"))
00108         out = "/" + out;
00109 
00110     return out;
00111 }
00112 
00113 AbstractFSNode *RISCOSFilesystemNode::getChild(const Common::String &n) const {
00114     assert(!_path.empty());
00115     assert(_isDirectory);
00116 
00117     // Make sure the string contains no slashes
00118     assert(!n.contains('/'));
00119 
00120     // We assume here that _path is already normalized (hence don't bother to call
00121     //  Common::normalizePath on the final path).
00122     Common::String newPath(_path);
00123     if (_path.lastChar() != '/')
00124         newPath += '/';
00125     newPath += n;
00126 
00127     return makeNode(newPath);
00128 }
00129 
00130 bool RISCOSFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
00131     assert(_isDirectory);
00132 
00133     if (_path == "/") {
00134         // Special case for the root dir: List all drives
00135         char fsname[PATH_MAX] = "";
00136         for (int fsNum = 0; fsNum < 256; fsNum += 1) {
00137             _swi(OS_FSControl, _INR(0,3), 33, fsNum, fsname, sizeof(fsname));
00138             if (strcmp(fsname, "") != 0) {
00139                 if (!(fsNum == 46 || fsNum == 53 || fsNum == 99)) {
00140                     int drives = 9;
00141                     if (fsNum == 193)
00142                         drives = 23;
00143 
00144                     for (int discnum = 0; discnum <= drives; discnum += 1) {
00145                         const Common::String path = Common::String::format("%s::%d.$", fsname, discnum);
00146                         char outpath[PATH_MAX] = "";
00147                         if(_swix(OS_FSControl, _INR(0,2)|_IN(5), 37, path.c_str(), outpath, sizeof(outpath)) == NULL) {
00148                             int exist;
00149                             if (_swix(OS_File, _INR(0,1)|_OUT(0), 23, outpath, &exist) != NULL || exist != 2)
00150                                 continue;
00151 
00152                             RISCOSFilesystemNode *entry = new RISCOSFilesystemNode();
00153                             entry->_isDirectory = true;
00154                             entry->_isValid = true;
00155                             entry->_path = Common::String::format("/%s", outpath);
00156                             entry->_displayName = outpath;
00157                             myList.push_back(entry);
00158                         }
00159                     }
00160                 }
00161             }
00162         }
00163         return true;
00164     }
00165 
00166     int count = 0;
00167     int read = 0;
00168     char file[PATH_MAX];
00169     Common::String dir = _path;
00170 
00171     while (count != -1) {
00172         _swix(OS_GBPB, _INR(0,5)|_OUTR(3,4), 9, toRISCOS(dir).c_str(), file, 1, count, sizeof(file), &read, &count);
00173 
00174         if (count == -1)
00175             continue;
00176 
00177         // Start with a clone of this node, with the correct path set
00178         RISCOSFilesystemNode entry(*this);
00179         entry._displayName = file;
00180         entry._displayName = toUnix(entry._displayName);
00181         if (_path.lastChar() != '/')
00182             entry._path += '/';
00183         entry._path += entry._displayName;
00184 
00185         int type = _swi(OS_File, _INR(0,1)|_RETURN(0), 20, toRISCOS(entry._path).c_str());
00186         if (type == 0) {
00187             continue;
00188         } else if (type == 2) {
00189             entry._isDirectory = true;
00190         } else {
00191             entry._isDirectory = false;
00192         }
00193 
00194         // Honor the chosen mode
00195         if ((mode == Common::FSNode::kListFilesOnly && entry._isDirectory) ||
00196             (mode == Common::FSNode::kListDirectoriesOnly && !entry._isDirectory))
00197             continue;
00198 
00199         myList.push_back(new RISCOSFilesystemNode(entry));
00200     }
00201 
00202     return true;
00203 }
00204 
00205 AbstractFSNode *RISCOSFilesystemNode::getParent() const {
00206     if (_path == "/")
00207         return 0;   // The filesystem root has no parent
00208 
00209     const char *start = _path.c_str();
00210     const char *end = start + _path.size();
00211 
00212     // Strip of the last component. We make use of the fact that at this
00213     // point, _path is guaranteed to be normalized
00214     while (end > start && *(end-1) != '/')
00215         end--;
00216 
00217     if (end == start) {
00218         // This only happens if we were called with a relative path, for which
00219         // there simply is no parent.
00220         // TODO: We could also resolve this by assuming that the parent is the
00221         //       current working directory, and returning a node referring to that.
00222         return 0;
00223     }
00224 
00225     if (*(end-1) == '/' && end != start + 1)
00226         end--;
00227 
00228     return makeNode(Common::String(start, end));
00229 }
00230 
00231 Common::SeekableReadStream *RISCOSFilesystemNode::createReadStream() {
00232     return StdioStream::makeFromPath(getPath(), false);
00233 }
00234 
00235 Common::WriteStream *RISCOSFilesystemNode::createWriteStream() {
00236     return StdioStream::makeFromPath(getPath(), true);
00237 }
00238 
00239 bool RISCOSFilesystemNode::create(bool isDirectoryFlag) {
00240     bool success;
00241 
00242     if (isDirectoryFlag) {
00243         success = _swix(OS_File, _INR(0,1), 8, toRISCOS(_path).c_str()) == NULL;
00244     } else {
00245         int fd = open(_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0755);
00246         success = fd >= 0;
00247 
00248         if (fd >= 0) {
00249             close(fd);
00250         }
00251     }
00252 
00253     if (success) {
00254         if (exists()) {
00255             _isDirectory = _swi(OS_File, _INR(0,1)|_RETURN(0), 20, toRISCOS(_path).c_str()) == 2;
00256             if (_isDirectory != isDirectoryFlag) warning("failed to create %s: got %s", isDirectoryFlag ? "directory" : "file", _isDirectory ? "directory" : "file");
00257             return _isDirectory == isDirectoryFlag;
00258         }
00259 
00260         warning("RISCOSFilesystemNode: Attempting to create a %s was a success, but access indicates there is no such %s",
00261             isDirectoryFlag ? "directory" : "file", isDirectoryFlag ? "directory" : "file");
00262         return false;
00263     }
00264 
00265     return false;
00266 }
00267 
00268 namespace Riscos {
00269 
00270 bool assureDirectoryExists(const Common::String &dir, const char *prefix) {
00271     AbstractFSNode *node;
00272 
00273     // Check whether the prefix exists if one is supplied.
00274     if (prefix) {
00275         node = new RISCOSFilesystemNode(prefix);
00276         if (!node->isDirectory()) {
00277             return false;
00278         }
00279     }
00280 
00281     // Obtain absolute path.
00282     Common::String path;
00283     if (prefix) {
00284         path = prefix;
00285         path += '/';
00286         path += dir;
00287     } else {
00288         path = dir;
00289     }
00290 
00291     path = Common::normalizePath(path, '/');
00292 
00293     const Common::String::iterator end = path.end();
00294     Common::String::iterator cur = path.begin();
00295     if (*cur == '/')
00296         ++cur;
00297 
00298     do {
00299         if (cur + 1 != end) {
00300             if (*cur != '/') {
00301                 continue;
00302             }
00303 
00304             // It is kind of ugly and against the purpose of Common::String to
00305             // insert 0s inside, but this is just for a local string and
00306             // simplifies the code a lot.
00307             *cur = '\0';
00308         }
00309 
00310         node = new RISCOSFilesystemNode(path);
00311         if (!node->create(true)) {
00312             if (node->exists()) {
00313                 if (!node->isDirectory()) {
00314                     return false;
00315                 }
00316             } else {
00317                 return false;
00318             }
00319         }
00320 
00321         *cur = '/';
00322     } while (cur++ != end);
00323 
00324     return true;
00325 }
00326 
00327 } // End of namespace RISCOS
00328 
00329 #endif //#if defined(RISCOS)


Generated on Sat Mar 16 2019 05:01:52 for ResidualVM by doxygen 1.7.1
curved edge   curved edge