Merge lp:~nha/widelands/filesystem into lp:widelands

Proposed by Nicolai Hähnle
Status: Merged
Merged at revision: 5358
Proposed branch: lp:~nha/widelands/filesystem
Merge into: lp:widelands
Diff against target: 166 lines (+45/-32)
4 files modified
src/io/filesystem/disk_filesystem.cc (+23/-21)
src/io/filesystem/filesystem.h (+2/-2)
src/io/filesystem/layered_filesystem.cc (+14/-3)
src/wlapplication.cc (+6/-6)
To merge this branch: bzr merge lp:~nha/widelands/filesystem
Reviewer Review Type Date Requested Status
Tino Approve
Widelands Developers crossplatform Pending
Review via email: mp+25802@code.launchpad.net

Description of the change

Clean up the recursiveness of EnsureDirectoryExists(), and ensure that the home directory exists.

I'm a bit surprised nobody else has run into the non-creation of ~/.widelands issue, so maybe it is done somewhere I have missed, but in a way that makes it fail given the changes to EnsureDirectoryExists()? I'd appreciate if somebody who has messed with that stuff before could look over it, especially whether it affects OSX or Windows.

To post a comment you must log in.
Revision history for this message
Tino (tino79) wrote :

test on win32:
- code compiles
- wl finds existing .widelands dir in %Userhome%
- wl creates .widelands if it does not exist

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/io/filesystem/disk_filesystem.cc'
2--- src/io/filesystem/disk_filesystem.cc 2010-03-16 20:18:53 +0000
3+++ src/io/filesystem/disk_filesystem.cc 2010-05-21 20:53:27 +0000
4@@ -76,7 +76,6 @@
5 #ifdef WIN32
6 m_root = Directory;
7 #else
8- m_root = "";
9 m_root = FS_CanonicalizeName(Directory);
10 #endif
11 }
12@@ -86,10 +85,8 @@
13 * Return true if this directory is writable.
14 */
15 bool RealFSImpl::IsWritable() const {
16- return true; // should be checked in constructor
17-
18- //fweber: no, should be checked here, because the ondisk state might have
19- //changed since then
20+ // Should be checked here (ondisk state can change)
21+ return true;
22 }
23
24 /**
25@@ -294,23 +291,27 @@
26 * Create this directory if it doesn't exist, throws an error
27 * if the dir can't be created or if a file with this name exists
28 */
29-void RealFSImpl::EnsureDirectoryExists(std::string const & dirname) {
30- FileSystemPath fspath(FS_CanonicalizeName(dirname));
31- if (fspath.m_exists and fspath.m_isDirectory)
32- return; // ok, dir is already there
33+void RealFSImpl::EnsureDirectoryExists(std::string const & dirname)
34+{
35 try {
36- MakeDirectory(dirname);
37- } catch (DirectoryCannotCreate_error const & e) {
38- // need more work to do it right
39- // iterate through all possible directories
40- size_t it = 0;
41- while (it != dirname.size() && it != std::string::npos) {
42+ std::string::size_type it = 0;
43+ while (it < dirname.size()) {
44 it = dirname.find('/', it);
45- EnsureDirectoryExists(dirname.substr(0, it));
46- ++it; //make sure we don't keep finding the same directories
47+
48+ FileSystemPath fspath(FS_CanonicalizeName(dirname.substr(0, it)));
49+ if (fspath.m_exists and !fspath.m_isDirectory)
50+ throw wexception
51+ ("%s exists and is not a directory",
52+ dirname.substr(0, it).c_str());
53+ if (!fspath.m_exists)
54+ MakeDirectory(dirname.substr(0, it));
55+
56+ if (it == std::string::npos)
57+ break;
58+ ++it;
59 }
60- } catch (_wexception const & e) {
61- throw wexception ("RealFSImpl::EnsureDirectory"); //, e.what());
62+ } catch (const std::exception & e) {
63+ throw wexception("RealFSImpl::EnsureDirectoryExists(%s): %s", dirname.c_str(), e.what());
64 }
65 }
66
67@@ -338,8 +339,9 @@
68 ==
69 -1)
70 throw DirectoryCannotCreate_error
71- ("could not create directory %s: %s",
72- dirname.c_str(), strerror(errno));
73+ ("RealFSImpl::MakeDirectory",
74+ dirname,
75+ strerror(errno));
76 }
77
78 /**
79
80=== modified file 'src/io/filesystem/filesystem.h'
81--- src/io/filesystem/filesystem.h 2010-03-16 20:18:53 +0000
82+++ src/io/filesystem/filesystem.h 2010-05-21 20:53:27 +0000
83@@ -94,7 +94,7 @@
84 */
85 virtual StreamWrite * OpenStreamWrite(std::string const & fname) = 0;
86
87- virtual FileSystem & MakeSubFileSystem(std::string const & dirname) = 0;
88+ virtual FileSystem & MakeSubFileSystem(std::string const & dirname) = 0;
89 virtual FileSystem & CreateSubFileSystem
90 (std::string const & dirname, Type) = 0;
91 virtual void Unlink(std::string const &) = 0;
92@@ -114,7 +114,7 @@
93 bool pathIsAbsolute(std::string const & path) const;
94 static char const * FS_Filename(char const *);
95 static char const * FS_Filename(char const *, char const * & extension);
96- static std::string FS_FilenameWoExt(char const *);
97+ static std::string FS_FilenameWoExt(char const *);
98 static std::string GetHomedir();
99
100 virtual unsigned long long DiskSpace() = 0;
101
102=== modified file 'src/io/filesystem/layered_filesystem.cc'
103--- src/io/filesystem/layered_filesystem.cc 2010-03-16 20:18:53 +0000
104+++ src/io/filesystem/layered_filesystem.cc 2010-05-21 20:53:27 +0000
105@@ -56,24 +56,35 @@
106 }
107
108 /**
109- * Add a new filesystem to the top of the stack
110+ * Add a new filesystem to the top of the stack.
111+ * Take ownership of the given filesystem.
112 * \todo throw on failure
113 */
114 void LayeredFileSystem::AddFileSystem(FileSystem & fs)
115 {
116 //only add it to the stack if there isn't a conflicting version file in
117 //the directory
118- if (! FindConflictingVersionFile(fs)) {
119+ if (!FindConflictingVersionFile(fs)) {
120 m_filesystems.push_back(&fs);
121 } else {
122+ delete &fs;
123 log("File system NOT added\n");
124 }
125 }
126+
127+/**
128+ * Set the home filesystem (which is the preferred filesystem for writing files).
129+ * Take ownership of the given filesystem.
130+ */
131 void LayeredFileSystem::SetHomeFileSystem(FileSystem & fs)
132 {
133- if (! FindConflictingVersionFile(fs)) {
134+ delete m_home;
135+ m_home = 0;
136+
137+ if (!FindConflictingVersionFile(fs)) {
138 m_home = &fs;
139 } else {
140+ delete &fs;
141 log("File system NOT added\n");
142 }
143 }
144
145=== modified file 'src/wlapplication.cc'
146--- src/wlapplication.cc 2010-04-28 16:12:03 +0000
147+++ src/wlapplication.cc 2010-05-21 20:53:27 +0000
148@@ -200,12 +200,12 @@
149 //assume some dir exists
150 try {
151 log ("Set home directory: %s\n", m_homedir.c_str());
152- g_fs->SetHomeFileSystem(FileSystem::Create(m_homedir.c_str()));
153- } catch (FileNotFound_error const & e) {
154- } catch (FileAccessDenied_error const & e) {
155- log("Access denied on %s. Continuing.\n", e.m_filename.c_str());
156- } catch (FileType_error const & e) {
157- //TODO: handle me
158+
159+ std::auto_ptr<FileSystem> home(new RealFSImpl(m_homedir));
160+ home->EnsureDirectoryExists(".");
161+ g_fs->SetHomeFileSystem(*home.release());
162+ } catch (const std::exception & e) {
163+ log("Failed to add home directory: %s\n", e.what());
164 }
165 } else {
166 //TODO: complain

Subscribers

People subscribed via source and target branches

to status/vote changes: