From 63aec29edb3e6065e5b167058247fa4cc39adef7 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 22 Jul 2006 17:01:50 +0000 Subject: Added isValid to FilesystemNode and AbstractFilesystemNode. See my mail to -devel for more information. svn-id: r23567 --- backends/fs/abstract-fs.h | 2 ++ backends/fs/gp32/gp32-fs.cpp | 2 ++ common/file.cpp | 14 ++++++++++++-- common/fs.cpp | 6 ++++++ common/fs.h | 5 +++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/backends/fs/abstract-fs.h b/backends/fs/abstract-fs.h index 7b5b7cbd1d..844d3115e1 100644 --- a/backends/fs/abstract-fs.h +++ b/backends/fs/abstract-fs.h @@ -104,6 +104,8 @@ public: // By default, we use the actual file name as 'display name'. virtual String displayName() const { return name(); } + virtual bool isValid() const = 0; + virtual bool isDirectory() const = 0; /** diff --git a/backends/fs/gp32/gp32-fs.cpp b/backends/fs/gp32/gp32-fs.cpp index 4d242d339f..c94460d727 100644 --- a/backends/fs/gp32/gp32-fs.cpp +++ b/backends/fs/gp32/gp32-fs.cpp @@ -40,6 +40,8 @@ public: virtual String displayName() const { return _displayName; } virtual String name() const { return _displayName; } + // FIXME: isValid should return false if this Node can't be used! + // client code can rely on the return value. virtual bool isValid() const { return true; } virtual bool isDirectory() const { return _isDirectory; } virtual String path() const { return _path; } diff --git a/common/file.cpp b/common/file.cpp index f8e131a80e..da682037e8 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -277,6 +277,15 @@ bool File::open(const String &filename, AccessMode mode) { bool File::open(const FilesystemNode &node, AccessMode mode) { assert(mode == kFileReadMode || mode == kFileWriteMode); + + if (!node.isValid()) { + warning("File::open: Trying to open an invalid FilesystemNode object"); + return false; + } else if (node.isDirectory()) { + warning("File::open: Trying to open a FilesystemNode which is a directory"); + return false; + } + String filename(node.name()); if (_handle) { @@ -288,10 +297,8 @@ bool File::open(const FilesystemNode &node, AccessMode mode) { const char *modeStr = (mode == kFileReadMode) ? "rb" : "wb"; - _handle = fopen(node.path().c_str(), modeStr); - if (_handle == NULL) { if (mode == kFileReadMode) debug(2, "File %s not found", filename.c_str()); @@ -313,6 +320,9 @@ bool File::exists(const String &filename) { // First try to find the file it via a FilesystemNode (in case an absolute // path was passed). But we only use this to filter out directories. FilesystemNode file(filename); + // FIXME: can't use isValid() here since at the time of writing + // FilesystemNode is to be unable to find for example files + // added in extrapath if (file.isDirectory()) return false; diff --git a/common/fs.cpp b/common/fs.cpp index 56d7eab49b..11d580436f 100644 --- a/common/fs.cpp +++ b/common/fs.cpp @@ -77,6 +77,12 @@ FilesystemNode &FilesystemNode::operator =(const FilesystemNode &node) { return *this; } +bool FilesystemNode::isValid() const { + if (_realNode == 0) + return false; + return _realNode->isValid(); +} + FilesystemNode FilesystemNode::getParent() const { if (_realNode == 0) return *this; diff --git a/common/fs.h b/common/fs.h index d18793f015..5d35c2581b 100644 --- a/common/fs.h +++ b/common/fs.h @@ -113,6 +113,11 @@ public: */ FilesystemNode &operator =(const FilesystemNode &node); + /** + * Checks if the FilesystemNode is valid for any usage + */ + bool isValid() const; + /** * Get the parent node of this node. If this node has no parent node, * then it returns a duplicate of this node. -- cgit v1.2.3