ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

default-saves.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // This define lets us use the system function remove() on Symbian, which
00024 // is disabled by default due to a macro conflict.
00025 // See backends/platform/symbian/src/portdefs.h .
00026 #define SYMBIAN_USE_SYSTEM_REMOVE
00027 
00028 #include "common/scummsys.h"
00029 
00030 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00031 #include "backends/cloud/cloudmanager.h"
00032 #endif
00033 #include "common/file.h"
00034 #include "common/system.h"
00035 
00036 #if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
00037 
00038 #include "backends/saves/default/default-saves.h"
00039 
00040 #include "common/savefile.h"
00041 #include "common/util.h"
00042 #include "common/fs.h"
00043 #include "common/archive.h"
00044 #include "common/config-manager.h"
00045 #include "common/zlib.h"
00046 
00047 #ifndef _WIN32_WCE
00048 #include <errno.h>  // for removeSavefile()
00049 #endif
00050 
00051 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00052 const char *DefaultSaveFileManager::TIMESTAMPS_FILENAME = "timestamps";
00053 #endif
00054 
00055 DefaultSaveFileManager::DefaultSaveFileManager() {
00056 }
00057 
00058 DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
00059     ConfMan.registerDefault("savepath", defaultSavepath);
00060 }
00061 
00062 
00063 void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
00064     clearError();
00065     if (!dir.exists()) {
00066         setError(Common::kPathDoesNotExist, "The savepath '"+dir.getPath()+"' does not exist");
00067     } else if (!dir.isDirectory()) {
00068         setError(Common::kPathNotDirectory, "The savepath '"+dir.getPath()+"' is not a directory");
00069     }
00070 }
00071 
00072 void DefaultSaveFileManager::updateSavefilesList(Common::StringArray &lockedFiles) {
00073     //make it refresh the cache next time it lists the saves
00074     _cachedDirectory = "";
00075 
00076     //remember the locked files list because some of these files don't exist yet
00077     _lockedFiles = lockedFiles;
00078 }
00079 
00080 Common::StringArray DefaultSaveFileManager::listSavefiles(const Common::String &pattern) {
00081     // Assure the savefile name cache is up-to-date.
00082     assureCached(getSavePath());
00083     if (getError().getCode() != Common::kNoError)
00084         return Common::StringArray();
00085 
00086     Common::HashMap<Common::String, bool> locked;
00087     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00088         locked[*i] = true;
00089     }
00090 
00091     Common::StringArray results;
00092     for (SaveFileCache::const_iterator file = _saveFileCache.begin(), end = _saveFileCache.end(); file != end; ++file) {
00093         if (!locked.contains(file->_key) && file->_key.matchString(pattern, true)) {
00094             results.push_back(file->_key);
00095         }
00096     }
00097     return results;
00098 }
00099 
00100 Common::InSaveFile *DefaultSaveFileManager::openRawFile(const Common::String &filename) {
00101     // Assure the savefile name cache is up-to-date.
00102     assureCached(getSavePath());
00103     if (getError().getCode() != Common::kNoError)
00104         return nullptr;
00105 
00106     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00107     if (file == _saveFileCache.end()) {
00108         return nullptr;
00109     } else {
00110         // Open the file for loading.
00111         Common::SeekableReadStream *sf = file->_value.createReadStream();
00112         return sf;
00113     }
00114 }
00115 
00116 Common::InSaveFile *DefaultSaveFileManager::openForLoading(const Common::String &filename) {
00117     // Assure the savefile name cache is up-to-date.
00118     assureCached(getSavePath());
00119     if (getError().getCode() != Common::kNoError)
00120         return nullptr;
00121 
00122     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00123         if (filename == *i) {
00124             return nullptr; //file is locked, no loading available
00125         }
00126     }
00127 
00128     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00129     if (file == _saveFileCache.end()) {
00130         return nullptr;
00131     } else {
00132         // Open the file for loading.
00133         Common::SeekableReadStream *sf = file->_value.createReadStream();
00134         return Common::wrapCompressedReadStream(sf);
00135     }
00136 }
00137 
00138 Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String &filename, bool compress) {
00139     // Assure the savefile name cache is up-to-date.
00140     const Common::String savePathName = getSavePath();
00141     assureCached(savePathName);
00142     if (getError().getCode() != Common::kNoError)
00143         return nullptr;
00144 
00145     for (Common::StringArray::const_iterator i = _lockedFiles.begin(), end = _lockedFiles.end(); i != end; ++i) {
00146         if (filename == *i) {
00147             return nullptr; //file is locked, no saving available
00148         }
00149     }
00150 
00151 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00152     // Update file's timestamp
00153     Common::HashMap<Common::String, uint32> timestamps = loadTimestamps();
00154     timestamps[filename] = INVALID_TIMESTAMP;
00155     saveTimestamps(timestamps);
00156 #endif
00157 
00158     // Obtain node.
00159     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00160     Common::FSNode fileNode;
00161 
00162     // If the file did not exist before, we add it to the cache.
00163     if (file == _saveFileCache.end()) {
00164         const Common::FSNode savePath(savePathName);
00165         fileNode = savePath.getChild(filename);
00166     } else {
00167         fileNode = file->_value;
00168     }
00169 
00170     // Open the file for saving.
00171     Common::WriteStream *const sf = fileNode.createWriteStream();
00172     Common::OutSaveFile *const result = new Common::OutSaveFile(compress ? Common::wrapCompressedWriteStream(sf) : sf);
00173 
00174     // Add file to cache now that it exists.
00175     _saveFileCache[filename] = Common::FSNode(fileNode.getPath());
00176 
00177     return result;
00178 }
00179 
00180 bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) {
00181     // Assure the savefile name cache is up-to-date.
00182     assureCached(getSavePath());
00183     if (getError().getCode() != Common::kNoError)
00184         return false;
00185 
00186 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00187     // Update file's timestamp
00188     Common::HashMap<Common::String, uint32> timestamps = loadTimestamps();
00189     Common::HashMap<Common::String, uint32>::iterator it = timestamps.find(filename);
00190     if (it != timestamps.end()) {
00191         timestamps.erase(it);
00192         saveTimestamps(timestamps);
00193     }
00194 #endif
00195 
00196     // Obtain node if exists.
00197     SaveFileCache::const_iterator file = _saveFileCache.find(filename);
00198     if (file == _saveFileCache.end()) {
00199         return false;
00200     } else {
00201         const Common::FSNode fileNode = file->_value;
00202         // Remove from cache, this invalidates the 'file' iterator.
00203         _saveFileCache.erase(file);
00204         file = _saveFileCache.end();
00205 
00206         // FIXME: remove does not exist on all systems. If your port fails to
00207         // compile because of this, please let us know (scummvm-devel).
00208         // There is a nicely portable workaround, too: Make this method overloadable.
00209         if (remove(fileNode.getPath().c_str()) != 0) {
00210 #ifndef _WIN32_WCE
00211             if (errno == EACCES)
00212                 setError(Common::kWritePermissionDenied, "Search or write permission denied: "+fileNode.getName());
00213 
00214             if (errno == ENOENT)
00215                 setError(Common::kPathDoesNotExist, "removeSavefile: '"+fileNode.getName()+"' does not exist or path is invalid");
00216 #endif
00217             return false;
00218         } else {
00219             return true;
00220         }
00221     }
00222 }
00223 
00224 Common::String DefaultSaveFileManager::getSavePath() const {
00225 
00226     Common::String dir;
00227 
00228     // Try to use game specific savepath from config
00229     dir = ConfMan.get("savepath");
00230 
00231     // Work around a bug (#999122) in the original 0.6.1 release of
00232     // ScummVM, which would insert a bad savepath value into config files.
00233     if (dir == "None") {
00234         ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
00235         ConfMan.flushToDisk();
00236         dir = ConfMan.get("savepath");
00237     }
00238 
00239 #ifdef _WIN32_WCE
00240     if (dir.empty())
00241         dir = ConfMan.get("path");
00242 #endif
00243 
00244     return dir;
00245 }
00246 
00247 void DefaultSaveFileManager::assureCached(const Common::String &savePathName) {
00248     // Check that path exists and is usable.
00249     checkPath(Common::FSNode(savePathName));
00250 
00251 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00252     Common::Array<Common::String> files = CloudMan.getSyncingFiles(); //returns empty array if not syncing
00253     if (!files.empty()) updateSavefilesList(files); //makes this cache invalid
00254     else _lockedFiles = files;
00255 #endif
00256 
00257     if (_cachedDirectory == savePathName) {
00258         return;
00259     }
00260 
00261     _saveFileCache.clear();
00262     _cachedDirectory.clear();
00263 
00264     if (getError().getCode() != Common::kNoError) {
00265         warning("DefaultSaveFileManager::assureCached: Can not cache path '%s': '%s'", savePathName.c_str(), getErrorDesc().c_str());
00266         return;
00267     }
00268 
00269     // FSNode can cache its members, thus create it after checkPath to reflect
00270     // actual file system state.
00271     const Common::FSNode savePath(savePathName);
00272 
00273     Common::FSList children;
00274     if (!savePath.getChildren(children, Common::FSNode::kListFilesOnly)) {
00275         return;
00276     }
00277 
00278     // Build the savefile name cache.
00279     for (Common::FSList::const_iterator file = children.begin(), end = children.end(); file != end; ++file) {
00280         if (_saveFileCache.contains(file->getName())) {
00281             warning("DefaultSaveFileManager::assureCached: Name clash when building cache, ignoring file '%s'", file->getName().c_str());
00282         } else {
00283             _saveFileCache[file->getName()] = *file;
00284         }
00285     }
00286 
00287     // Only now store that we cached 'savePathName' to indicate we successfully
00288     // cached the directory.
00289     _cachedDirectory = savePathName;
00290 }
00291 
00292 #if defined(USE_CLOUD) && defined(USE_LIBCURL)
00293 
00294 Common::HashMap<Common::String, uint32> DefaultSaveFileManager::loadTimestamps() {
00295     Common::HashMap<Common::String, uint32> timestamps;
00296 
00297     //refresh the files list
00298     Common::Array<Common::String> files;
00299     g_system->getSavefileManager()->updateSavefilesList(files);
00300 
00301     //start with listing all the files in saves/ directory and setting invalid timestamp to them
00302     Common::StringArray localFiles = g_system->getSavefileManager()->listSavefiles("*");
00303     for (uint32 i = 0; i < localFiles.size(); ++i)
00304         timestamps[localFiles[i]] = INVALID_TIMESTAMP;
00305 
00306     //now actually load timestamps from file
00307     Common::InSaveFile *file = g_system->getSavefileManager()->openRawFile(TIMESTAMPS_FILENAME);
00308     if (!file) {
00309         warning("DefaultSaveFileManager: failed to open '%s' file to load timestamps", TIMESTAMPS_FILENAME);
00310         return timestamps;
00311     }
00312 
00313     while (!file->eos()) {
00314         //read filename into buffer (reading until the first ' ')
00315         Common::String buffer;
00316         while (!file->eos()) {
00317             byte b = file->readByte();
00318             if (b == ' ') break;
00319             buffer += (char)b;
00320         }
00321 
00322         //read timestamp info buffer (reading until ' ' or some line ending char)
00323         Common::String filename = buffer;
00324         while (true) {
00325             bool lineEnded = false;
00326             buffer = "";
00327             while (!file->eos()) {
00328                 byte b = file->readByte();
00329                 if (b == ' ' || b == '\n' || b == '\r') {
00330                     lineEnded = (b == '\n');
00331                     break;
00332                 }
00333                 buffer += (char)b;
00334             }
00335 
00336             if (buffer == "" && file->eos()) break;
00337             if (!lineEnded) filename += " " + buffer;
00338             else break;
00339         }
00340 
00341         //parse timestamp
00342         uint32 timestamp = buffer.asUint64();
00343         if (buffer == "" || timestamp == 0) break;
00344         if (timestamps.contains(filename))
00345             timestamps[filename] = timestamp;
00346     }
00347 
00348     delete file;
00349     return timestamps;
00350 }
00351 
00352 void DefaultSaveFileManager::saveTimestamps(Common::HashMap<Common::String, uint32> &timestamps) {
00353     Common::DumpFile f;
00354     Common::String filename = concatWithSavesPath(TIMESTAMPS_FILENAME);
00355     if (!f.open(filename, true)) {
00356         warning("DefaultSaveFileManager: failed to open '%s' file to save timestamps", filename.c_str());
00357         return;
00358     }
00359 
00360     for (Common::HashMap<Common::String, uint32>::iterator i = timestamps.begin(); i != timestamps.end(); ++i) {
00361         Common::String data = i->_key + Common::String::format(" %u\n", i->_value);
00362         if (f.write(data.c_str(), data.size()) != data.size()) {
00363             warning("DefaultSaveFileManager: failed to write timestamps data into '%s'", filename.c_str());
00364             return;
00365         }
00366     }
00367 
00368     f.flush();
00369     f.finalize();
00370     f.close();
00371 }
00372 
00373 #endif // ifdef USE_LIBCURL
00374 
00375 Common::String DefaultSaveFileManager::concatWithSavesPath(Common::String name) {
00376     DefaultSaveFileManager *manager = dynamic_cast<DefaultSaveFileManager *>(g_system->getSavefileManager());
00377     Common::String path = (manager ? manager->getSavePath() : ConfMan.get("savepath"));
00378     if (path.size() > 0 && (path.lastChar() == '/' || path.lastChar() == '\\'))
00379         return path + name;
00380 
00381     //simple heuristic to determine which path separator to use
00382     int backslashes = 0;
00383     for (uint32 i = 0; i < path.size(); ++i)
00384         if (path[i] == '/') --backslashes;
00385         else if (path[i] == '\\') ++backslashes;
00386 
00387     if (backslashes > 0) return path + '\\' + name;
00388     return path + '/' + name;
00389 }
00390 
00391 #endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)


Generated on Sat Jul 13 2019 05:00:56 for ResidualVM by doxygen 1.7.1
curved edge   curved edge