[darwinbuild-changes] [813] branches/PR-6973110/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Mon Apr 19 16:32:01 PDT 2010


Revision: 813
          http://trac.macosforge.org/projects/darwinbuild/changeset/813
Author:   wsiegrist at apple.com
Date:     2010-04-19 16:32:00 -0700 (Mon, 19 Apr 2010)
Log Message:
-----------
Pre-flight some access checks so we can exit with meaningful errors instead of assertions.

Modified Paths:
--------------
    branches/PR-6973110/darwinup/Database.cpp
    branches/PR-6973110/darwinup/Database.h
    branches/PR-6973110/darwinup/Depot.cpp

Modified: branches/PR-6973110/darwinup/Database.cpp
===================================================================
--- branches/PR-6973110/darwinup/Database.cpp	2010-04-19 21:25:32 UTC (rev 812)
+++ branches/PR-6973110/darwinup/Database.cpp	2010-04-19 23:32:00 UTC (rev 813)
@@ -121,8 +121,22 @@
 		fprintf(stderr, "Error: pre-connection failed.\n");
 		return res;
 	}
-	
+
+	// test our access level
 	int exists = is_regular_file(m_path);
+	bool readonly = false;
+	if (!exists && access(dirname(m_path), W_OK | X_OK)) {
+		// does not exist and we cannot write to the directory
+		fprintf(stderr, 
+				"Error: Unable to create new darwinup database. "
+				"Try running as root.");
+		return DB_ERROR;					
+	}
+	if (exists && access(m_path, W_OK)) {
+		// db exists already but we cannot write to it
+		readonly = true;
+	}
+
 	res = sqlite3_open(m_path, &m_db);
 	if (res) {
 		sqlite3_close(m_db);
@@ -137,7 +151,7 @@
 		fprintf(stderr, "Error: post-connection failed.\n");
 		return res;
 	}
-	
+
 	if (!exists) {
 		// create schema since it is empty
 		assert(this->create_tables() == 0);
@@ -150,12 +164,23 @@
 		}
 
 		if (version < this->m_schema_version) {
+			if (readonly) {
+				fprintf(stderr, 
+						"Error: the darwinup database needs to be upgraded "
+						"but darwinup cannot write to database. "
+						"Try running as root.\n");
+				sqlite3_close(m_db);
+				m_db = NULL;
+				return DB_ERROR;
+			}
 			IF_DEBUG("Upgrading schema from %u to %u \n", version, this->m_schema_version);
 			assert(this->upgrade_schema(version) == 0);
 			assert(this->set_schema_version(this->m_schema_version) == 0);
 		}
 		if (version > this->m_schema_version) {
 			fprintf(stderr, "Error: this client is too old!\n");
+			sqlite3_close(m_db);
+			m_db = NULL;
 			return DB_ERROR;
 		}
 	}
@@ -203,6 +228,10 @@
 	return this->connect();
 }
 
+bool Database::is_connected() {
+	return m_db != NULL;
+}
+
 int Database::begin_transaction() {
 	return this->execute(m_begin_transaction);
 }

Modified: branches/PR-6973110/darwinup/Database.h
===================================================================
--- branches/PR-6973110/darwinup/Database.h	2010-04-19 21:25:32 UTC (rev 812)
+++ branches/PR-6973110/darwinup/Database.h	2010-04-19 23:32:00 UTC (rev 813)
@@ -136,6 +136,7 @@
 	const char*  error();
 	int          connect();
 	int          connect(const char* path);
+	bool         is_connected();
 	
 	int          begin_transaction();
 	int          rollback_transaction();

Modified: branches/PR-6973110/darwinup/Depot.cpp
===================================================================
--- branches/PR-6973110/darwinup/Depot.cpp	2010-04-19 21:25:32 UTC (rev 812)
+++ branches/PR-6973110/darwinup/Depot.cpp	2010-04-19 23:32:00 UTC (rev 813)
@@ -98,8 +98,8 @@
 
 int Depot::connect() {
 	m_db = new DarwinupDatabase(m_database_path);
-	if (!m_db) {
-		fprintf(stderr, "Error: unable to connect to database in Depot::connect().\n");
+	if (!m_db || !m_db->is_connected()) {
+		fprintf(stderr, "Error: unable to connect to database.\n");
 		return 1;
 	}
 	return 0;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100419/1f7f5e17/attachment.html>


More information about the darwinbuild-changes mailing list