From 196ce8eb98aa49b7661933f2fad91b2294c83e75 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 3 Sep 2008 14:55:19 +0000 Subject: POSIX FSNode: got rid of Double-slashes in paths for childs of the root; simplified code svn-id: r34307 --- backends/fs/posix/posix-fs-factory.cpp | 6 ++--- backends/fs/posix/posix-fs.cpp | 49 +++++++++++++++++----------------- backends/fs/posix/posix-fs.h | 14 +++++----- 3 files changed, 33 insertions(+), 36 deletions(-) (limited to 'backends/fs/posix') diff --git a/backends/fs/posix/posix-fs-factory.cpp b/backends/fs/posix/posix-fs-factory.cpp index 3d6e9a65d9..cbfb69b76a 100644 --- a/backends/fs/posix/posix-fs-factory.cpp +++ b/backends/fs/posix/posix-fs-factory.cpp @@ -27,17 +27,17 @@ #include "backends/fs/posix/posix-fs.cpp" AbstractFilesystemNode *POSIXFilesystemFactory::makeRootFileNode() const { - return new POSIXFilesystemNode(); + return new POSIXFilesystemNode("/"); } AbstractFilesystemNode *POSIXFilesystemFactory::makeCurrentDirectoryFileNode() const { char buf[MAXPATHLEN]; getcwd(buf, MAXPATHLEN); - return new POSIXFilesystemNode(buf, true); + return new POSIXFilesystemNode(buf); } AbstractFilesystemNode *POSIXFilesystemFactory::makeFileNodePath(const Common::String &path) const { assert(!path.empty()); - return new POSIXFilesystemNode(path, true); + return new POSIXFilesystemNode(path); } #endif diff --git a/backends/fs/posix/posix-fs.cpp b/backends/fs/posix/posix-fs.cpp index 72f45a5b27..688a7559b8 100644 --- a/backends/fs/posix/posix-fs.cpp +++ b/backends/fs/posix/posix-fs.cpp @@ -46,14 +46,7 @@ void POSIXFilesystemNode::setFlags() { _isDirectory = _isValid ? S_ISDIR(st.st_mode) : false; } -POSIXFilesystemNode::POSIXFilesystemNode() { - // The root dir. - _displayName = _path = "/"; - _isValid = true; - _isDirectory = true; -} - -POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) { +POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p) { assert(p.size() > 0); // Expand "~/" to the value of the HOME env variable @@ -73,7 +66,7 @@ POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) { _path = Common::normalizePath(_path, '/'); _displayName = Common::lastPathComponent(_path, '/'); - // TODO: should we turn relative paths into absolut ones? + // TODO: should we turn relative paths into absolute ones? // Pro: Ensures the "getParent" works correctly even for relative dirs. // Contra: The user may wish to use (and keep!) relative paths in his // config file, and converting relative to absolute paths may hurt him... @@ -91,24 +84,24 @@ POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) { // TODO: Should we enforce that the path is absolute at this point? //assert(_path.hasPrefix("/")); - if (verify) { - setFlags(); - } + setFlags(); } AbstractFilesystemNode *POSIXFilesystemNode::getChild(const Common::String &n) const { + assert(!_path.empty()); assert(_isDirectory); // Make sure the string contains no slashes assert(Common::find(n.begin(), n.end(), '/') == n.end()); // We assume here that _path is already normalized (hence don't bother to call - // Common::normalizePath on the final path + // Common::normalizePath on the final path). Common::String newPath(_path); - newPath += '/'; + if (_path != "/") + newPath += '/'; newPath += n; - return new POSIXFilesystemNode(newPath, true); + return makeNode(newPath); } bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const { @@ -164,11 +157,12 @@ bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, boo continue; } - Common::String newPath(_path); - newPath += '/'; - newPath += dp->d_name; - - POSIXFilesystemNode entry(newPath, false); + // Start with a clone of this node, with the correct path set + POSIXFilesystemNode entry(*this); + entry._displayName = dp->d_name; + if (_path != "/") + entry._path += '/'; + entry._path += entry._displayName; #if defined(SYSTEM_NOT_SUPPORTING_D_TYPE) /* TODO: d_type is not part of POSIX, so it might not be supported @@ -223,13 +217,18 @@ AbstractFilesystemNode *POSIXFilesystemNode::getParent() const { // Strip of the last component. We make use of the fact that at this // point, _path is guaranteed to be normalized - while (end > start && *end != '/') + while (end > start && *(end-1) != '/') end--; - if (end == start) - return new POSIXFilesystemNode(); - else - return new POSIXFilesystemNode(Common::String(start, end), true); + if (end == start) { + // This only happens if we were called with a relative path, for which + // there simply is no parent. + // TODO: We could also resolve this by assuming that the parent is the + // current working directory, and returning a node referring to that. + return 0; + } + + return makeNode(Common::String(start, end)); } Common::SeekableReadStream *POSIXFilesystemNode::openForReading() { diff --git a/backends/fs/posix/posix-fs.h b/backends/fs/posix/posix-fs.h index 9459b38010..7d633a7fdf 100644 --- a/backends/fs/posix/posix-fs.h +++ b/backends/fs/posix/posix-fs.h @@ -43,20 +43,18 @@ protected: Common::String _path; bool _isDirectory; bool _isValid; + + virtual AbstractFilesystemNode *makeNode(const Common::String &path) const { + return new POSIXFilesystemNode(path); + } public: - /** - * Creates a POSIXFilesystemNode with the root node as path. - */ - POSIXFilesystemNode(); - /** * Creates a POSIXFilesystemNode for a given path. * - * @param path String with the path the new node should point to. - * @param verify true if the isValid and isDirectory flags should be verified during the construction. + * @param path the path the new node should point to. */ - POSIXFilesystemNode(const Common::String &path, bool verify); + POSIXFilesystemNode(const Common::String &path); virtual bool exists() const { return access(_path.c_str(), F_OK) == 0; } virtual Common::String getDisplayName() const { return _displayName; } -- cgit v1.2.3