2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2006-06-24 10:08:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-03-16 23:07:08 +00:00
|
|
|
#if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
|
|
|
|
|
2006-07-09 13:10:45 +00:00
|
|
|
#include "common/savefile.h"
|
2007-01-21 15:17:28 +00:00
|
|
|
#include "common/util.h"
|
2007-07-08 07:19:50 +00:00
|
|
|
#include "common/fs.h"
|
2007-12-28 07:37:04 +00:00
|
|
|
#include "common/config-manager.h"
|
2006-06-24 10:08:43 +00:00
|
|
|
#include "backends/saves/default/default-saves.h"
|
2007-02-18 02:25:39 +00:00
|
|
|
#include "backends/saves/compressed/compressed-saves.h"
|
2006-06-24 10:08:43 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-10-31 18:51:11 +00:00
|
|
|
#include <errno.h>
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2008-09-07 12:54:26 +00:00
|
|
|
#if defined(UNIX)
|
2007-01-21 15:17:28 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-04 17:17:37 +00:00
|
|
|
#ifdef UNIX
|
|
|
|
#ifdef MACOSX
|
|
|
|
#define DEFAULT_SAVE_PATH "Documents/ScummVM Savegames"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_SAVE_PATH ".scummvm"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DefaultSaveFileManager::DefaultSaveFileManager() {
|
|
|
|
// Register default savepath
|
|
|
|
// TODO: Remove this code here, and instead leave setting the
|
|
|
|
// default savepath to the ports using this class.
|
|
|
|
#ifdef DEFAULT_SAVE_PATH
|
2008-08-04 19:38:28 +00:00
|
|
|
Common::String savePath;
|
2008-08-04 17:17:37 +00:00
|
|
|
#if defined(UNIX) && !defined(IPHONE)
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home && *home && strlen(home) < MAXPATHLEN) {
|
2008-08-04 19:38:28 +00:00
|
|
|
savePath = home;
|
|
|
|
savePath += "/" DEFAULT_SAVE_PATH;
|
2008-08-04 17:17:37 +00:00
|
|
|
ConfMan.registerDefault("savepath", savePath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif // #ifdef DEFAULT_SAVE_PATH
|
|
|
|
}
|
|
|
|
|
2008-08-04 19:38:28 +00:00
|
|
|
DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
|
|
|
|
ConfMan.registerDefault("savepath", defaultSavepath);
|
|
|
|
}
|
2008-08-04 17:17:37 +00:00
|
|
|
|
|
|
|
|
2007-12-30 13:05:38 +00:00
|
|
|
Common::StringList DefaultSaveFileManager::listSavefiles(const char *pattern) {
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode savePath(getSavePath());
|
2008-09-03 11:22:51 +00:00
|
|
|
Common::FSList savefiles;
|
2007-09-18 20:02:04 +00:00
|
|
|
Common::StringList results;
|
2007-12-30 13:05:38 +00:00
|
|
|
Common::String search(pattern);
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2008-07-07 13:01:54 +00:00
|
|
|
if (savePath.lookupFile(savefiles, search, false, true, 0)) {
|
2008-09-03 11:22:51 +00:00
|
|
|
for (Common::FSList::const_iterator file = savefiles.begin(); file != savefiles.end(); ++file) {
|
2008-01-10 11:54:31 +00:00
|
|
|
results.push_back(file->getName());
|
2007-09-18 20:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-09-18 20:02:04 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
|
2008-08-04 17:41:07 +00:00
|
|
|
const Common::String path = dir.getPath();
|
2007-12-28 07:37:04 +00:00
|
|
|
clearError();
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2008-09-07 12:54:26 +00:00
|
|
|
#if defined(UNIX)
|
2007-01-21 15:17:28 +00:00
|
|
|
struct stat sb;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-01-21 15:17:28 +00:00
|
|
|
// Check whether the dir exists
|
2007-12-28 16:47:28 +00:00
|
|
|
if (stat(path.c_str(), &sb) == -1) {
|
2007-01-21 15:17:28 +00:00
|
|
|
// The dir does not exist, or stat failed for some other reason.
|
|
|
|
// If the problem was that the path pointed to nothing, try
|
2007-07-18 20:51:26 +00:00
|
|
|
// to create the dir (ENOENT case).
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_ACCESS, "Search or write permission denied: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ELOOP:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_LOOP, "Too many symbolic links encountered while traversing the path: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENAMETOOLONG:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NAMETOOLONG, "The path name is too long: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENOENT:
|
2007-12-28 16:47:28 +00:00
|
|
|
if (mkdir(path.c_str(), 0755) != 0) {
|
2007-01-21 15:17:28 +00:00
|
|
|
// mkdir could fail for various reasons: The parent dir doesn't exist,
|
|
|
|
// or is not writeable, the path could be completly bogus, etc.
|
2007-12-28 16:47:28 +00:00
|
|
|
warning("mkdir for '%s' failed!", path.c_str());
|
2007-01-21 15:17:28 +00:00
|
|
|
perror("mkdir");
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-07-18 20:51:26 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_ACCESS, "Search or write permission denied: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case EMLINK:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_LINKMAX, "The link count of the parent directory would exceed {LINK_MAX}: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ELOOP:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_LOOP, "Too many symbolic links encountered while traversing the path: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENAMETOOLONG:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NAMETOOLONG, "The path name is too long: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENOENT:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NOENT, "A component of the path does not exist, or the path is an empty string: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENOTDIR:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NOTDIR, "A component of the path prefix is not a directory: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case EROFS:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_ROFS, "The parent directory resides on a read-only file system:"+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-01-21 15:17:28 +00:00
|
|
|
}
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
|
|
|
case ENOTDIR:
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NOTDIR, "A component of the path prefix is not a directory: "+path);
|
2007-07-18 20:51:26 +00:00
|
|
|
break;
|
2007-09-19 08:40:12 +00:00
|
|
|
}
|
2007-01-21 15:17:28 +00:00
|
|
|
} else {
|
2007-07-29 01:36:59 +00:00
|
|
|
// So stat() succeeded. But is the path actually pointing to a directory?
|
2007-01-21 15:17:28 +00:00
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
2007-12-28 16:47:28 +00:00
|
|
|
setError(SFM_DIR_NOTDIR, "The given savepath is not a directory: "+path);
|
2007-01-21 15:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-04 17:41:07 +00:00
|
|
|
#else
|
|
|
|
if (!dir.exists()) {
|
|
|
|
// TODO: We could try to mkdir the directory here; or rather, we could
|
2008-10-02 16:58:59 +00:00
|
|
|
// add a mkdir method to FSNode and invoke that here.
|
2008-08-04 17:41:07 +00:00
|
|
|
setError(SFM_DIR_NOENT, "A component of the path does not exist, or the path is an empty string: "+path);
|
|
|
|
} else if (!dir.isDirectory()) {
|
|
|
|
setError(SFM_DIR_NOTDIR, "The given savepath is not a directory: "+path);
|
|
|
|
}
|
2007-01-21 15:17:28 +00:00
|
|
|
#endif
|
2007-12-28 07:37:04 +00:00
|
|
|
}
|
2007-01-21 15:17:28 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
Common::InSaveFile *DefaultSaveFileManager::openForLoading(const char *filename) {
|
|
|
|
// Ensure that the savepath is valid. If not, generate an appropriate error.
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode savePath(getSavePath());
|
2007-12-28 07:37:04 +00:00
|
|
|
checkPath(savePath);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
if (getError() == SFM_NO_ERROR) {
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode file = savePath.getChild(filename);
|
2008-08-04 11:32:42 +00:00
|
|
|
|
2008-08-04 11:48:33 +00:00
|
|
|
// Open the file for reading
|
|
|
|
Common::SeekableReadStream *sf = file.openForReading();
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
return wrapInSaveFile(sf);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const char *filename) {
|
|
|
|
// Ensure that the savepath is valid. If not, generate an appropriate error.
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode savePath(getSavePath());
|
2007-12-28 07:37:04 +00:00
|
|
|
checkPath(savePath);
|
2006-06-24 10:08:43 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
if (getError() == SFM_NO_ERROR) {
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode file = savePath.getChild(filename);
|
2008-08-04 11:32:42 +00:00
|
|
|
|
2008-08-04 11:48:33 +00:00
|
|
|
// Open the file for saving
|
|
|
|
Common::WriteStream *sf = file.openForWriting();
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-12-28 07:37:04 +00:00
|
|
|
return wrapOutSaveFile(sf);
|
|
|
|
} else {
|
|
|
|
return 0;
|
2006-06-24 10:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-29 01:36:59 +00:00
|
|
|
bool DefaultSaveFileManager::removeSavefile(const char *filename) {
|
2007-12-28 07:37:04 +00:00
|
|
|
clearError();
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
Common::FSNode savePath(getSavePath());
|
|
|
|
Common::FSNode file = savePath.getChild(filename);
|
2008-08-04 11:32:42 +00:00
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
// TODO: Add new method FSNode::remove()
|
2008-08-04 11:32:42 +00:00
|
|
|
if (remove(file.getPath().c_str()) != 0) {
|
2007-11-03 21:17:40 +00:00
|
|
|
#ifndef _WIN32_WCE
|
2007-10-31 13:59:59 +00:00
|
|
|
if (errno == EACCES)
|
2008-08-04 11:32:42 +00:00
|
|
|
setError(SFM_DIR_ACCESS, "Search or write permission denied: "+file.getName());
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-10-31 13:59:59 +00:00
|
|
|
if (errno == ENOENT)
|
2008-08-04 11:32:42 +00:00
|
|
|
setError(SFM_DIR_NOENT, "A component of the path does not exist, or the path is an empty string: "+file.getName());
|
2007-11-03 21:17:40 +00:00
|
|
|
#endif
|
2007-10-31 13:59:59 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2007-10-31 19:09:23 +00:00
|
|
|
}
|
2006-06-24 10:08:43 +00:00
|
|
|
}
|
2007-03-16 23:07:08 +00:00
|
|
|
|
2007-12-28 21:12:30 +00:00
|
|
|
Common::String DefaultSaveFileManager::getSavePath() const {
|
2007-12-28 07:37:04 +00:00
|
|
|
|
2007-12-28 21:12:30 +00:00
|
|
|
Common::String dir;
|
2007-12-28 07:37:04 +00:00
|
|
|
|
|
|
|
// Try to use game specific savepath from config
|
2007-12-28 21:12:30 +00:00
|
|
|
dir = ConfMan.get("savepath");
|
2007-12-28 07:37:04 +00:00
|
|
|
|
|
|
|
// Work around a bug (#999122) in the original 0.6.1 release of
|
|
|
|
// ScummVM, which would insert a bad savepath value into config files.
|
2007-12-28 21:12:30 +00:00
|
|
|
if (dir == "None") {
|
2007-12-28 07:37:04 +00:00
|
|
|
ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
|
|
|
|
ConfMan.flushToDisk();
|
2007-12-28 21:12:30 +00:00
|
|
|
dir = ConfMan.get("savepath");
|
2007-12-28 07:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
2007-12-28 21:12:30 +00:00
|
|
|
if (dir.empty())
|
|
|
|
dir = ConfMan.get("path");
|
2007-12-28 07:37:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2007-03-16 23:07:08 +00:00
|
|
|
#endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)
|