[darwinbuild-changes] [703] branches/PR-7598640/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 11 19:47:32 PST 2010


Revision: 703
          http://trac.macosforge.org/projects/darwinbuild/changeset/703
Author:   wsiegrist at apple.com
Date:     2010-02-11 19:47:32 -0800 (Thu, 11 Feb 2010)
Log Message:
-----------
Allow for non-root users to do list, files, and dump.

Modified Paths:
--------------
    branches/PR-7598640/darwinup/Depot.cpp
    branches/PR-7598640/darwinup/Depot.h
    branches/PR-7598640/darwinup/main.cpp

Modified: branches/PR-7598640/darwinup/Depot.cpp
===================================================================
--- branches/PR-7598640/darwinup/Depot.cpp	2010-02-12 02:03:17 UTC (rev 702)
+++ branches/PR-7598640/darwinup/Depot.cpp	2010-02-12 03:47:32 UTC (rev 703)
@@ -88,16 +88,18 @@
 const char*	Depot::downloads_path()		{ return m_downloads_path; }
 const char*     Depot::prefix()                 { return m_prefix; }
 
-// Initialize the depot storage on disk
-int Depot::initialize() {
-	int res = 0;
-	
-	// initialization requires all these paths to be set
-	if (!(m_prefix && m_depot_path && m_database_path && m_archives_path && m_downloads_path)) {
-		return -1;
+int Depot::connect() {
+	int res = sqlite3_open(m_database_path, &m_db);
+	if (res) {
+		sqlite3_close(m_db);
+		m_db = NULL;
 	}
-	
-	res = mkdir(m_depot_path, m_depot_mode);
+
+	return res;
+}
+
+int Depot::create_storage() {
+	int res = mkdir(m_depot_path, m_depot_mode);
 	if (res && errno != EEXIST) {
 		perror(m_depot_path);
 		return res;
@@ -113,26 +115,44 @@
 		perror(m_downloads_path);
 		return res;
 	}
+	return 0;
+}
 
-	res = this->lock(LOCK_SH);
-	if (res) return res;
-	m_is_locked = 1;
+// Initialize the depot
+int Depot::initialize(bool writable) {
+	int res = 0;
 	
-	int exists = is_regular_file(m_database_path);
+	// initialization requires all these paths to be set
+	if (!(m_prefix && m_depot_path && m_database_path && m_archives_path && m_downloads_path)) {
+		return -1;
+	}
 	
-	res = sqlite3_open(m_database_path, &m_db);
-	if (res) {
-		sqlite3_close(m_db);
-		m_db = NULL;
+	if (writable) {
+		uid_t uid = getuid();
+		if (uid) {
+			fprintf(stderr, "You must be root to perform that operation.\n");
+			exit(3);
+		}			
+		res = this->create_storage();
+		if (res) return res;
+		res = this->lock(LOCK_SH);
+		if (res) return res;
+		m_is_locked = 1;		
 	}
-	
+
+	int exists = is_regular_file(m_database_path);
+	if (!exists && !writable) {
+		// read-only mode requested but we have no database
+		return -2;
+	}
+	res = this->connect();
 	if (m_db && !exists) {
 		this->SQL("CREATE TABLE archives (serial INTEGER PRIMARY KEY AUTOINCREMENT, uuid BLOB UNIQUE, name TEXT, date_added INTEGER, active INTEGER, info INTEGER)");
 		this->SQL("CREATE TABLE files (serial INTEGER PRIMARY KEY AUTOINCREMENT, archive INTEGER, info INTEGER, mode INTEGER, uid INTEGER, gid INTEGER, size INTEGER, digest BLOB, path TEXT)");
 		this->SQL("CREATE INDEX archives_uuid ON archives (uuid)");
 		this->SQL("CREATE INDEX files_path ON files (path)");
 	}
-	
+
 	return res;
 }
 

Modified: branches/PR-7598640/darwinup/Depot.h
===================================================================
--- branches/PR-7598640/darwinup/Depot.h	2010-02-12 02:03:17 UTC (rev 702)
+++ branches/PR-7598640/darwinup/Depot.h	2010-02-12 03:47:32 UTC (rev 703)
@@ -53,7 +53,15 @@
 	
 	virtual ~Depot();
 
-	int initialize();
+	// establish database connection
+	int connect();
+
+	// create directories we need for storage
+	int create_storage();
+	
+	// use initialize() to connect to database 
+	//  and (optionally) create the storage directories
+	int initialize(bool writable);
 	int is_initialized();
 	
 	const char* prefix();

Modified: branches/PR-7598640/darwinup/main.cpp
===================================================================
--- branches/PR-7598640/darwinup/main.cpp	2010-02-12 02:03:17 UTC (rev 702)
+++ branches/PR-7598640/darwinup/main.cpp	2010-02-12 03:47:32 UTC (rev 703)
@@ -41,6 +41,7 @@
 #include <unistd.h>
 #include <limits.h>
 
+
 void usage(char* progname) {
 	fprintf(stderr, "usage:    %s [-v] [-p DIR] [command] [args]          \n", progname);
 	fprintf(stderr, "                                                               \n");
@@ -119,13 +120,6 @@
 	argc -= optind;
 	argv += optind;
 
-	// you must be root
-	uid_t uid = getuid();
-	if (uid) {
-		fprintf(stderr, "You must be root to run this tool.\n");
-		exit(3);
-	}
-	
 	int res = 0;
 
 	if (!path) {
@@ -133,15 +127,12 @@
 	}
 		
 	Depot* depot = new Depot(path);
-	res = depot->initialize();
-	if (res) {
-		fprintf(stderr, "Error: unable to initialize storage.\n");
-		exit(2);
-	}
-	
+		
 	if (argc == 2 && strcmp(argv[0], "install") == 0) {
+		if (depot->initialize(true)) exit(13);
 		res = depot->install(argv[1]);
 	} else if (argc == 2 && strcmp(argv[0], "upgrade") == 0) {
+		if (depot->initialize(true)) exit(14);
 		// find most recent matching archive by name
 		Archive* old = depot->get_archive(basename(argv[1]));
 		if (!old) {
@@ -153,14 +144,23 @@
 		// uninstall old archive
 		if (res == 0) res = depot->uninstall(old);
 	} else if (argc == 1 && strcmp(argv[0], "list") == 0) {
-		depot->list();
+		res = depot->initialize(false);
+		if (res == -2) {
+			fprintf(stdout, "Nothing has been installed yet.\n");
+			exit(0);
+		}
+		if (res == 0) depot->list();
 	} else if (argc == 1 && strcmp(argv[0], "dump") == 0) {
+		if (depot->initialize(false)) exit(11);
 		depot->dump();
 	} else if (argc == 2 && strcmp(argv[0], "files") == 0) {
+		if (depot->initialize(false)) exit(12);
 		res = depot->process_archive(argv[0], argv[1]);
 	} else if (argc == 2 && strcmp(argv[0], "uninstall") == 0) {
+		if (depot->initialize(true)) exit(15);
 		res = depot->process_archive(argv[0], argv[1]);
 	} else if (argc == 2 && strcmp(argv[0], "verify") == 0) {
+		if (depot->initialize(true)) exit(16);
 		res = depot->process_archive(argv[0], argv[1]);
 	} else {
 		usage(progname);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100211/18ea8b66/attachment.html>


More information about the darwinbuild-changes mailing list