aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorCameron Cawley2019-07-02 18:08:33 +0100
committerFilippos Karapetis2019-08-11 22:15:54 +0300
commitaca627bec7b407790d78a64df984344ff454c15b (patch)
tree8aa18208291f43525b79606fa891cb564fa84892 /common
parent04c57babbc06dfba33a5597a83e80c27c1ba6be9 (diff)
downloadscummvm-rg350-aca627bec7b407790d78a64df984344ff454c15b.tar.gz
scummvm-rg350-aca627bec7b407790d78a64df984344ff454c15b.tar.bz2
scummvm-rg350-aca627bec7b407790d78a64df984344ff454c15b.zip
COMMON: Implement FSNode::createDirectoryRecursive()
Diffstat (limited to 'common')
-rw-r--r--common/file.cpp20
-rw-r--r--common/fs.cpp21
-rw-r--r--common/fs.h8
3 files changed, 32 insertions, 17 deletions
diff --git a/common/file.cpp b/common/file.cpp
index 6228c6640b..12461b7382 100644
--- a/common/file.cpp
+++ b/common/file.cpp
@@ -155,24 +155,10 @@ bool DumpFile::open(const String &filename, bool createPath) {
assert(!filename.empty());
assert(!_handle);
- if (createPath) {
- for (uint32 i = 0; i < filename.size(); ++i) {
- if (filename[i] == '/' || filename[i] == '\\') {
- Common::String subpath = filename;
- subpath.erase(i);
- if (subpath.empty()) continue;
- AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(subpath);
- if (node->exists()) {
- delete node;
- continue;
- }
- if (!node->createDirectory()) warning("DumpFile: unable to create directories from path prefix");
- delete node;
- }
- }
- }
-
FSNode node(filename);
+ if (createPath)
+ node.getParent().createDirectoryRecursive();
+
return open(node);
}
diff --git a/common/fs.cpp b/common/fs.cpp
index d27de48370..2ba64099b4 100644
--- a/common/fs.cpp
+++ b/common/fs.cpp
@@ -168,6 +168,27 @@ bool FSNode::createDirectory() const {
return _realNode->createDirectory();
}
+bool FSNode::createDirectoryRecursive() const {
+ if (_realNode == nullptr)
+ return false;
+
+ if (_realNode->exists()) {
+ if (!_realNode->isDirectory()) {
+ warning("FSNode::createDirectoryRecursive: '%s' is a file", _realNode->getName().c_str());
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ FSNode parent = getParent();
+ assert(parent.getPath() != _realNode->getPath());
+ if (!parent.createDirectoryRecursive())
+ return false;
+
+ return _realNode->createDirectory();
+}
+
FSDirectory::FSDirectory(const FSNode &node, int depth, bool flat)
: _node(node), _cached(false), _depth(depth), _flat(flat) {
}
diff --git a/common/fs.h b/common/fs.h
index 7041428fc8..e8c23f8486 100644
--- a/common/fs.h
+++ b/common/fs.h
@@ -239,6 +239,14 @@ public:
* @return true if the directory was created, false otherwise.
*/
bool createDirectory() const;
+
+ /**
+ * Creates a directory referred by this node. The parent directory
+ * will also be created if it doesn't exist.
+ *
+ * @return true if the directory was created, false otherwise.
+ */
+ bool createDirectoryRecursive() const;
};
/**