1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// This define lets us use the system function remove() on Symbian, which
// is disabled by default due to a macro conflict.
// See backends/platform/symbian/src/portdefs.h .
#define SYMBIAN_USE_SYSTEM_REMOVE
#include "common/scummsys.h"
#ifdef USE_LIBCURL
#include "backends/cloud/cloudmanager.h"
#include "common/file.h"
#endif
#if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
#include "backends/saves/default/default-saves.h"
#include "common/savefile.h"
#include "common/util.h"
#include "common/fs.h"
#include "common/archive.h"
#include "common/config-manager.h"
#include "common/zlib.h"
#ifndef _WIN32_WCE
#include <errno.h> // for removeSavefile()
#endif
#ifdef USE_LIBCURL
const char *DefaultSaveFileManager::TIMESTAMPS_FILENAME = "timestamps";
#endif
DefaultSaveFileManager::DefaultSaveFileManager() {
}
DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
ConfMan.registerDefault("savepath", defaultSavepath);
}
void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
clearError();
if (!dir.exists()) {
setError(Common::kPathDoesNotExist, "The savepath '"+dir.getPath()+"' does not exist");
} else if (!dir.isDirectory()) {
setError(Common::kPathNotDirectory, "The savepath '"+dir.getPath()+"' is not a directory");
}
}
void DefaultSaveFileManager::updateSavefilesList(Common::StringArray &lockedFiles) {
//make it refresh the cache next time it lists the saves
_cachedDirectory = "";
//remember the locked files list because some of these files don't exist yet
_lockedFiles = lockedFiles;
}
Common::StringArray DefaultSaveFileManager::listSavefiles(const Common::String &pattern) {
// Assure the savefile name cache is up-to-date.
assureCached(getSavePath());
if (getError().getCode() != Common::kNoError)
return Common::StringArray();
Common::HashMap<Common::String, bool> locked;
for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
locked[*i] = true;
}
Common::StringArray results;
for (SaveFileCache::const_iterator file = _saveFileCache.begin(), end = _saveFileCache.end(); file != end; ++file) {
if (!locked.contains(file->_key) && file->_key.matchString(pattern, true)) {
results.push_back(file->_key);
}
}
return results;
}
Common::InSaveFile *DefaultSaveFileManager::openRawFile(const Common::String &filename) {
// Assure the savefile name cache is up-to-date.
assureCached(getSavePath());
if (getError().getCode() != Common::kNoError)
return nullptr;
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
if (file == _saveFileCache.end()) {
return nullptr;
} else {
// Open the file for loading.
Common::SeekableReadStream *sf = file->_value.createReadStream();
return sf;
}
}
Common::InSaveFile *DefaultSaveFileManager::openForLoading(const Common::String &filename) {
// Assure the savefile name cache is up-to-date.
assureCached(getSavePath());
if (getError().getCode() != Common::kNoError)
return nullptr;
for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
if (filename == *i) {
return nullptr; //file is locked, no loading available
}
}
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
if (file == _saveFileCache.end()) {
return nullptr;
} else {
// Open the file for loading.
Common::SeekableReadStream *sf = file->_value.createReadStream();
return Common::wrapCompressedReadStream(sf);
}
}
Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String &filename, bool compress) {
// Assure the savefile name cache is up-to-date.
const Common::String savePathName = getSavePath();
assureCached(savePathName);
if (getError().getCode() != Common::kNoError)
return nullptr;
for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
if (filename == *i) {
return nullptr; //file is locked, no saving available
}
}
#ifdef USE_LIBCURL
// Update file's timestamp
Common::HashMap<Common::String, uint32> timestamps = loadTimestamps();
timestamps[filename] = INVALID_TIMESTAMP;
saveTimestamps(timestamps);
#endif
// Obtain node.
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
Common::FSNode fileNode;
// If the file did not exist before, we add it to the cache.
if (file == _saveFileCache.end()) {
const Common::FSNode savePath(savePathName);
fileNode = savePath.getChild(filename);
} else {
fileNode = file->_value;
}
// Open the file for saving.
Common::WriteStream *const sf = fileNode.createWriteStream();
Common::OutSaveFile *const result = new Common::OutSaveFile(compress ? Common::wrapCompressedWriteStream(sf) : sf);
// Add file to cache now that it exists.
_saveFileCache[filename] = Common::FSNode(fileNode.getPath());
return result;
}
bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) {
// Assure the savefile name cache is up-to-date.
assureCached(getSavePath());
if (getError().getCode() != Common::kNoError)
return false;
// Obtain node if exists.
SaveFileCache::const_iterator file = _saveFileCache.find(filename);
if (file == _saveFileCache.end()) {
return false;
} else {
const Common::FSNode fileNode = file->_value;
// Remove from cache, this invalidates the 'file' iterator.
_saveFileCache.erase(file);
file = _saveFileCache.end();
// FIXME: remove does not exist on all systems. If your port fails to
// compile because of this, please let us know (scummvm-devel).
// There is a nicely portable workaround, too: Make this method overloadable.
if (remove(fileNode.getPath().c_str()) != 0) {
#ifndef _WIN32_WCE
if (errno == EACCES)
setError(Common::kWritePermissionDenied, "Search or write permission denied: "+fileNode.getName());
if (errno == ENOENT)
setError(Common::kPathDoesNotExist, "removeSavefile: '"+fileNode.getName()+"' does not exist or path is invalid");
#endif
return false;
} else {
return true;
}
}
}
Common::String DefaultSaveFileManager::getSavePath() const {
Common::String dir;
// Try to use game specific savepath from config
dir = ConfMan.get("savepath");
// Work around a bug (#999122) in the original 0.6.1 release of
// ScummVM, which would insert a bad savepath value into config files.
if (dir == "None") {
ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
ConfMan.flushToDisk();
dir = ConfMan.get("savepath");
}
#ifdef _WIN32_WCE
if (dir.empty())
dir = ConfMan.get("path");
#endif
return dir;
}
void DefaultSaveFileManager::assureCached(const Common::String &savePathName) {
// Check that path exists and is usable.
checkPath(Common::FSNode(savePathName));
#ifdef USE_LIBCURL
Common::Array<Common::String> files = CloudMan.getSyncingFiles(); //returns empty array if not syncing
if (!files.empty()) updateSavefilesList(files); //makes this cache invalid
else _lockedFiles = files;
#endif
if (_cachedDirectory == savePathName) {
return;
}
_saveFileCache.clear();
_cachedDirectory.clear();
if (getError().getCode() != Common::kNoError) {
warning("DefaultSaveFileManager::assureCached: Can not cache path '%s': '%s'", savePathName.c_str(), getErrorDesc().c_str());
return;
}
// FSNode can cache its members, thus create it after checkPath to reflect
// actual file system state.
const Common::FSNode savePath(savePathName);
Common::FSList children;
if (!savePath.getChildren(children, Common::FSNode::kListFilesOnly)) {
return;
}
// Build the savefile name cache.
for (Common::FSList::const_iterator file = children.begin(), end = children.end(); file != end; ++file) {
if (_saveFileCache.contains(file->getName())) {
warning("DefaultSaveFileManager::assureCached: Name clash when building cache, ignoring file '%s'", file->getName().c_str());
} else {
_saveFileCache[file->getName()] = *file;
}
}
// Only now store that we cached 'savePathName' to indicate we successfully
// cached the directory.
_cachedDirectory = savePathName;
}
#ifdef USE_LIBCURL
Common::HashMap<Common::String, uint32> DefaultSaveFileManager::loadTimestamps() {
Common::HashMap<Common::String, uint32> timestamps;
//refresh the files list
Common::Array<Common::String> files;
g_system->getSavefileManager()->updateSavefilesList(files);
//start with listing all the files in saves/ directory and setting invalid timestamp to them
Common::StringArray localFiles = g_system->getSavefileManager()->listSavefiles("*");
for (uint32 i = 0; i < localFiles.size(); ++i)
timestamps[localFiles[i]] = INVALID_TIMESTAMP;
//now actually load timestamps from file
Common::InSaveFile *file = g_system->getSavefileManager()->openRawFile(TIMESTAMPS_FILENAME);
if (!file) {
warning("DefaultSaveFileManager: failed to open '%s' file to load timestamps", TIMESTAMPS_FILENAME);
return timestamps;
}
while (!file->eos()) {
//read filename into buffer (reading until the first ' ')
Common::String buffer;
while (!file->eos()) {
byte b = file->readByte();
if (b == ' ') break;
buffer += (char)b;
}
//read timestamp info buffer (reading until ' ' or some line ending char)
Common::String filename = buffer;
while (true) {
bool lineEnded = false;
buffer = "";
while (!file->eos()) {
byte b = file->readByte();
if (b == ' ' || b == '\n' || b == '\r') {
lineEnded = (b == '\n');
break;
}
buffer += (char)b;
}
if (buffer == "" && file->eos()) break;
if (!lineEnded) filename += " " + buffer;
else break;
}
//parse timestamp
uint32 timestamp = buffer.asUint64();
if (buffer == "" || timestamp == 0) break;
timestamps[filename] = timestamp;
}
delete file;
return timestamps;
}
void DefaultSaveFileManager::saveTimestamps(Common::HashMap<Common::String, uint32> ×tamps) {
Common::DumpFile f;
Common::String filename = concatWithSavesPath(TIMESTAMPS_FILENAME);
if (!f.open(filename, true)) {
warning("DefaultSaveFileManager: failed to open '%s' file to save timestamps", filename.c_str());
return;
}
for (Common::HashMap<Common::String, uint32>::iterator i = timestamps.begin(); i != timestamps.end(); ++i) {
Common::String data = i->_key + Common::String::format(" %u\n", i->_value);
if (f.write(data.c_str(), data.size()) != data.size()) {
warning("DefaultSaveFileManager: failed to write timestamps data into '%s'", filename.c_str());
return;
}
}
f.flush();
f.finalize();
f.close();
}
Common::String DefaultSaveFileManager::concatWithSavesPath(Common::String name) {
DefaultSaveFileManager *manager = dynamic_cast<DefaultSaveFileManager *>(g_system->getSavefileManager());
Common::String path = (manager ? manager->getSavePath() : ConfMan.get("savepath"));
if (path.size() > 0 && (path.lastChar() == '/' || path.lastChar() == '\\'))
return path + name;
//simple heuristic to determine which path separator to use
int backslashes = 0;
for (uint32 i = 0; i < path.size(); ++i)
if (path[i] == '/') --backslashes;
else if (path[i] == '\\') ++backslashes;
if (backslashes > 0) return path + '\\' + name;
return path + '/' + name;
}
#endif // ifdef USE_LIBCURL
#endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
|