[Scummvm-cvs-logs] SF.net SVN: scummvm: [25150] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Jan 21 16:17:29 CET 2007


Revision: 25150
          http://scummvm.svn.sourceforge.net/scummvm/?rev=25150&view=rev
Author:   fingolfin
Date:     2007-01-21 07:17:28 -0800 (Sun, 21 Jan 2007)

Log Message:
-----------
Don't mkdir the default savepath whenever we startup; rather, mkdir (on Unix/Symbian, at least) the savedir just before we need it, i.e. just before saving. Fixes bug #1504398

Modified Paths:
--------------
    scummvm/trunk/backends/saves/default/default-saves.cpp
    scummvm/trunk/base/commandLine.cpp

Modified: scummvm/trunk/backends/saves/default/default-saves.cpp
===================================================================
--- scummvm/trunk/backends/saves/default/default-saves.cpp	2007-01-21 14:23:32 UTC (rev 25149)
+++ scummvm/trunk/backends/saves/default/default-saves.cpp	2007-01-21 15:17:28 UTC (rev 25150)
@@ -22,6 +22,7 @@
 
 #include "common/stdafx.h"
 #include "common/savefile.h"
+#include "common/util.h"
 #include "backends/saves/default/default-saves.h"
 
 #include <stdio.h>
@@ -31,6 +32,12 @@
 #include <zlib.h>
 #endif
 
+#if defined(UNIX) || defined(__SYMBIAN32__)
+#include <errno.h>
+#include <sys/stat.h>
+#endif
+
+
 class StdioSaveFile : public Common::InSaveFile, public Common::OutSaveFile {
 private:
 	FILE *fh;
@@ -170,8 +177,48 @@
 
 Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const char *filename) {
 	char buf[256];
-	join_paths(filename, getSavePath(), buf, sizeof(buf));
 
+	// Ensure that the savepath exists and is writeable. If not, generate
+	// an appropriate error
+	const char *savePath = getSavePath();
+#if defined(UNIX) || defined(__SYMBIAN32__)
+	struct stat sb;
+	
+	// Check whether the dir exists
+	if (stat(savePath, &sb) == -1) {
+		// The dir does not exist, or stat failed for some other reason.
+		// If the problem was that the path pointed to nothing, try
+		// to create the dir.
+		if (errno == ENOENT) {
+			if (mkdir(savePath, 0755) != 0) {
+				// mkdir could fail for various reasons: The parent dir doesn't exist,
+				// or is not writeable, the path could be completly bogus, etc.
+				warning("mkdir for '%s' failed!", savePath);
+				perror("mkdir");
+				// TODO: Specify an error code here so that callers can 
+				// determine what exactly went wrong.
+				return 0;
+			}
+		} else {
+			// Unknown error, abort.
+			// TODO: Specify an error code here so that callers can 
+			// determine what exactly went wrong.
+			return 0;
+		}
+	} else {
+		// So stat() succeeded. But is the path actually pointing to a
+		// directory?
+		if (!S_ISDIR(sb.st_mode)) {
+			// TODO: Specify an error code here so that callers can 
+			// determine what exactly went wrong.
+			return 0;
+		}
+	}
+#endif
+
+
+	join_paths(filename, savePath, buf, sizeof(buf));
+
 #ifdef USE_ZLIB
 	GzipSaveFile *sf = new GzipSaveFile(buf, true);
 #else
@@ -203,5 +250,8 @@
 }
 
 void DefaultSaveFileManager::listSavefiles(const char * /* prefix */, bool *marks, int num) {
+	// TODO: Implement this properly, at least on systems that support
+	// opendir/readdir.
+	// Even better, replace this with a better design...
 	memset(marks, true, num * sizeof(bool));
 }

Modified: scummvm/trunk/base/commandLine.cpp
===================================================================
--- scummvm/trunk/base/commandLine.cpp	2007-01-21 14:23:32 UTC (rev 25149)
+++ scummvm/trunk/base/commandLine.cpp	2007-01-21 15:17:28 UTC (rev 25150)
@@ -35,16 +35,12 @@
 #include "sound/mixer.h"
 
 #ifdef UNIX
-#include <errno.h>
-#include <sys/stat.h>
 #ifdef MACOSX
 #define DEFAULT_SAVE_PATH "Documents/ScummVM Savegames"
 #else
 #define DEFAULT_SAVE_PATH ".scummvm"
 #endif
 #elif defined(__SYMBIAN32__)
-#include <errno.h>
-#include <sys/stat.h>
 #define DEFAULT_SAVE_PATH "Savegames"
 #endif
 
@@ -221,36 +217,19 @@
 	ConfMan.registerDefault("alsa_port", "65:0");
 #endif
 
+	// Register default savepath
 #ifdef DEFAULT_SAVE_PATH
 	char savePath[MAXPATHLEN];
 #ifdef UNIX
-	struct stat sb;
 	const char *home = getenv("HOME");
 	if (home && *home && strlen(home) < MAXPATHLEN) {
 		snprintf(savePath, MAXPATHLEN, "%s/%s", home, DEFAULT_SAVE_PATH);
-		if (stat(savePath, &sb) == -1) {
-			/* create the dir if it does not exist */
-			if (errno == ENOENT) {
-				if (mkdir(savePath, 0755) != 0) {
-					perror("mkdir");
-					exit(1);
-				}
-			}
-		}
-		/* check that the dir is there */
-		if (stat(savePath, &sb) == 0) {
-			ConfMan.registerDefault("savepath", savePath);
-		}
+		ConfMan.registerDefault("savepath", savePath);
 	}
 #elif defined(__SYMBIAN32__)
 	strcpy(savePath, Symbian::GetExecutablePath());
 	strcat(savePath, DEFAULT_SAVE_PATH);
-	struct stat sb;
-	if (stat(savePath, &sb) == -1)
-		if (errno == ENOENT)// create the dir if it does not exist
-			if (mkdir(savePath, 0755) != 0)
-				warning("mkdir for '%s' failed!", savePath);
-	ConfMan.registerDefault("savepath", savePath); // this should be enough...
+	ConfMan.registerDefault("savepath", savePath);
 #endif
 #endif // #ifdef DEFAULT_SAVE_PATH
 }


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list