Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Create the directory if not exists prior to open the database
Baptiste Daroussin committed 13 years ago
commit af94c44ea932c9e49e61d346a4e08c946dd43612
parent e5742f0
1 file changed +17 -13
modified libpkg/pkgdb.c
@@ -706,6 +706,7 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
	const char	*dbdir = NULL;
	bool		 multirepos_enabled = false;
	bool		 create = false;
+
	bool		 createdir = false;
	int		 ret;

	if (*db_p != NULL) {
@@ -736,19 +737,28 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
				pkg_emit_nolocaldb();
				pkgdb_close(db);
				return (EPKG_ENODB);
-
			} else if (eaccess(dbdir, W_OK) != 0) {
+
			} else if ((eaccess(dbdir, W_OK) != 0)) {
				/*
				 * If we need to create the db but cannot
				 * write to it, fail early
				 */
-
				pkg_emit_nolocaldb();
-
				pkgdb_close(db);
-
				return (EPKG_ENODB);
+
				if (errno == ENOENT) {
+
					createdir = true;
+
					create = true;
+
				} else {
+
					pkg_emit_nolocaldb();
+
					pkgdb_close(db);
+
					return (EPKG_ENODB);
+
				}
			} else {
				create = true;
			}
		}

+
		/* Create the diretory if it doesn't exists */
+
		if (createdir && mkdirs(dbdir) != EPKG_OK)
+
				return (EPKG_FATAL);
+

		sqlite3_initialize();
		if (sqlite3_open(localpath, &db->sqlite) != SQLITE_OK) {
			ERROR_SQLITE(db->sqlite);
@@ -760,15 +770,9 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
		sqlite3_busy_timeout(db->sqlite, 5000);

		/* If the database is missing we have to initialize it */
-
		if (create == true) {
-
			if (mkdirs(dbdir) != EPKG_OK) {
-
				pkgdb_close(db);
-
				return (EPKG_FATAL);
-
			}
-
			if (pkgdb_init(db->sqlite) != EPKG_OK) {
-
				pkgdb_close(db);
-
				return (EPKG_FATAL);
-
			}
+
		if (create && pkgdb_init(db->sqlite) != EPKG_OK) {
+
			pkgdb_close(db);
+
			return (EPKG_FATAL);
		}

		/* Create our functions */