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


Generated on Sat Jul 20 2019 05:00:44 for ResidualVM by doxygen 1.7.1
curved edge   curved edge