PERFORCE change 164241 for review
David Forsythe
dforsyth at FreeBSD.org
Sat Jun 13 07:24:52 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=164241
Change 164241 by dforsyth at squirrel on 2009/06/13 07:24:24
More changes to db access scheme.
Affected files ...
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.c#15 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.h#15 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb.c#3 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb.h#3 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb_pkgdb_sub.c#4 edit
.. //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb_pkgdb_sub.h#2 edit
.. //depot/projects/soc2009/dforsyth_libpkg/pkg_info/main.c#9 edit
Differences ...
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.c#15 (text+ko) ====
@@ -12,16 +12,11 @@
struct pkg {
char *ident; /* User given name for this pkg. */
- char *comment_text;
- char *contents_text;
- char *desc_text;
- char *display_text;
- char *mtree_dirs_text;
- char *required_by_text;
-
+
struct pkg_plist plist;
+ struct pkgdb_sub sub;
- TAILQ_ENTRY(pkg) next;
+ /* TAILQ_ENTRY(pkg) next; */
short dirty;
/* Add an owner field? */
};
@@ -37,31 +32,6 @@
return (p);
}
-#if 0
-struct pkg *
-pkg_new(const char *ident)
-{
- struct pkg *p;
- char *new_ident;
-
- if (ident == NULL)
- return (NULL);
-
- p = calloc(1, sizeof(*p));
- new_ident = strdup(ident);
- if (p == NULL || new_ident == NULL) {
- free(new_ident);
- free(p);
- return (NULL);
- }
- p->ident = new_ident;
- p->comment = NULL;
- p->plist = NULL;
-
- return (p);
-}
-#endif
-
/* TODO: Can't copy string for some _set_ functions and then just point to
* the passed pkg_plist for _set_pkg_plist. */
@@ -143,6 +113,23 @@
return (pf);
}
+void
+pkg_reset(struct pkg *p)
+{
+ if (p == NULL)
+ arg_rage_quit(__func__, "Not a valid package.", RAGE_AT_CLIENT);
+
+ pkg_plist_free(p->plist);
+ p->ident = NULL;
+ p->comment_text = NULL;
+ p->contents_text = NULL;
+ p->desc_text = NULL;
+ p->display_text = NULL;
+ p->mtree_text = NULL;
+ p->required_by = NULL;
+ p->plist = NULL;
+}
+
/* TODO: Make an note in the manual for libpkg that pkg_free should not be
* called on pkgs that are not explicitly created by the client. */
@@ -150,8 +137,8 @@
pkg_free(struct pkg *p)
{
if (p == NULL)
- return;
+ arg_rage_quit(__func__, "Not a valid package.", RAGE_AT_CLIENT);
- free(p->ident);
+ pkg_reset(p);
free(p);
}
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkg.h#15 (text+ko) ====
@@ -2,15 +2,18 @@
#define __PKG_H__
/* When I get more than one, I'll plop it into an enum type. */
-#define HIERDB 0
+#define HIER_DB 0
/* TODO: Error codes. */
+#define RAGE_AT_CLIENT 0x00000001
+#define RAGE_AT_LIBPKG 0x00000002
+
/* pkg_file */
struct pkg_file;
-struct pkg_file *pkg_file_new();
+struct pkg_file *pkg_file_new(void);
const char *pkg_file_path(struct pkg_file *pf);
@@ -80,15 +83,15 @@
struct pkgdb;
-struct pkgdb *pkgdb_new(); /* Should eventually take a type argument. */
+struct pkgdb *pkgdb_new(int type);
-int pkgdb *pkgdb_db_open(struct pkgdb *db, const char *db_root);
+int pkgdb_db_open(struct pkgdb *db, const char *db_root);
-int pkgdb *pkgdb_db_init(struct pkgdb *db);
+int pkgdb_db_init(struct pkgdb *db);
-int pkgdb *pkgdb_db_close(struct pkgdb *db);
+int pkgdb_db_close(struct pkgdb *db);
-int pkgdb_pkg_next(struct pkgdb *db, struct pkg *p);
+struct pkg *pkgdb_pkg_next(struct pkgdb *db);
int pkgdb_pkg_get(struct pkgdb *db, struct pkg *p, const char *ident);
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb.c#3 (text+ko) ====
@@ -4,51 +4,53 @@
#include "pkg_util.h"
#include "pkg.h"
-#include "pkgdb_hierdb_sub.h"
+#include "pkgdb.h"
#include "pkgdb_hierdb.h"
-#include "pkgdb.h"
+#include "pkgdb_hierdb_pkgdb_sub.h"
int
pkgdb_hierdb_db_open(struct pkgdb *db, const char *db_root)
{
int s;
struct stat sb;
- char *new_db_root;
#if 0
if (!VALID_DB(db))a
#endif
+
if (db == NULL)
arg_rage_quit(__func__, "Not a valid database.", RAGE_AT_CLIENT);
if (db_root == NULL)
arg_rage_quit(__func__, "Not a valid root directory for
database.", RAGE_AT_CLIENT);
+
+ if (db->open)
+ return (DB_IS_OPEN);
s = lstat(&sb);
if (s < 0 || !S_ISDIR(sb.st_mode) || S_ISLINK(sb.st_mode))
- return (DB_BAD_ROOT);
+ return (DB_INVALID_ROOT);
- new_db_root = path_strdup(db_root);
- if (new_db_root == NULL)
+ db->db_root = strdup(db_root);
+ if (db->db_root == NULL)
return (DB_MEMORY_ERR);
- db->db_root = new_db_root;
db->dirty = 1;
- db->dbs_curr = NULL;
- db->ent_count = 0;
+ db->open = 1;
+ db->sub_curr = NULL;
+ db->sub_count = 0;
+
- db->pkgdb_db_init = &pkgdb_hierdb_db_init;
-
return (DB_OK);
}
int
-pkgdb_hier_db_init(struct pkgdb *db)
+pkgdb_hierdb_db_init(struct pkgdb *db)
{
int i;
int c;
int cnt;
int status;
- struct pkgdb_sub *dbs;
+ struct pkgdb_sub *sub;
struct dirent **ents;
#if 0
if (!VALID_DB(db))
@@ -63,8 +65,8 @@
return (DB_NO_READ | DB_MEMORY_ERR);
for (i = 0, cnt = 0; i < c; ++i) {
- dbs = pkgdb_sub_new();
- status = pkgdb_read_pkgdb_sub(db, dbs, ents[i]->d_name);
+ sub = pkgdb_sub_new();
+ status = pkgdb_hierdb_read_pkgdb_sub(db, sub, ents[i]->d_name);
if (SUB_MEMORY_ERR & status) {
/* Ruh roh Shaggy! */
pkgdb_pkgdb_sub_list_free(db);
@@ -79,18 +81,18 @@
* there is no plist, or the plist is empty, this is not a
* package, and we don't care about it.
*/
- pkgdb_sub_free(dbs);
+ pkg_delete(p);
else {
- pkgdb_pkgdb_sub_list_append(db, dbs);
+ pkgdb_pkgdb_sub_list_append(db, sub);
cnt++;
}
free(ents[i]);
}
free(ents);
- db->dbs_curr = NULL;
- db->ent_count = cnt;
- db->dirty = 1;
+ db->sub_curr = NULL;
+ db->sub_count = cnt;
+ db->dirty = 0;
return (DB_OK);
}
@@ -98,13 +100,12 @@
/* This function will properly initialize a pkgdb_sub for a pkgdb to use.
* This includes setting db as the owner of dbs. */
int
-pkgdb_hierdb_read_pkgdb_sub(struct pkgdb *db, struct pkgdb_sur *dbs,
+pkgdb_hierdb_read_pkgdb_sub(struct pkgdb *db, struct pkgdb_sub *sub,
const char *ident)
{
int s;
int status;
struct stat sb;
- const char *text;
#if 0 /* The client doesn't know about the functions in this file. */
if (!VALID_DB(x))
arg_rage_quit(__func__, "Not a valid database.", RAGE_AT_CLIENT);
@@ -112,27 +113,27 @@
arg_rage_quit(__func__, "Not a valid identification string.",
RAGE_AT_CLIENT);
#endif
- pkgdb_sub_reset(dbs);
+ pkg_reset(sub);
status = SUB_OK;
status |= pkgdb_sub_set_ident(ident);
- status |= pkgdb_sub_set_assign_db(db, dbs);
+ status |= pkgdb_sub_set_assign_db(db, sub);
if ((SUB_MEMORY_ERR & status) || (SUB_BAD_IDENT & status))
return (status);
/* Hope you have your reading glasses on... */
status = SUB_OK;
- status |= pkgdb_sub_reat_file_to_text(db, dbs, COMMENT_FILE) &
+ status |= pkgdb_sub_reat_file_to_text(db, sub, COMMENT_FILE) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_COMMENT : SUB_OK;
- status |= pkgdb_sub_read_file_to_text(db, dbs, CONTENTS_FILE) &
+ status |= pkgdb_sub_read_file_to_text(db, sub, CONTENTS_FILE) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_CONTENTS : SUB_OK;
- status |= pkgdb_sub_read_file_to_text(db, dbs, DESC_FILE) &
+ status |= pkgdb_sub_read_file_to_text(db, sub, DESC_FILE) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_DESC : SUB_OK;
- status |= pkgdb_sub_read_file_to_text(db, dbs, DISPLAY_FILE) &
+ status |= pkgdb_sub_read_file_to_text(db, sub, DISPLAY_FILE) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_DISPLAY : SUB_OK;
- status |= pkgdb_sub_read_file_to_text(db, dbs, MTREE_DIRS_FILE) &
+ status |= pkgdb_sub_read_file_to_text(db, sub, MTREE_DIRS_FILE) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_MTREE_DIRS : SUB_OK;
- status |= pkgdb_sub_read_file_to_text(db, dbs, REQUIRED_BY_FILE:) &
+ status |= pkgdb_sub_read_file_to_text(db, sub, REQUIRED_BY_FILE:) &
(SUB_EMPTY_FILE | SUB_NO_FILE) ? SUB_NO_REQUIRED_BY : SUB_OK;
return (status);
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb.h#3 (text+ko) ====
@@ -1,12 +1,13 @@
#ifndef __PKGDB_HIERDB_H__
#define __PKGDB_HIERDB_H__
+#include "pkg.h"
+
int pkgdb_hierdb_db_open(struct pkgdb *db, const char *db_root);
-int pkgdb_hier_db_init(struct pkgdb *db);
+int pkgdb_hierdb_db_init(struct pkgdb *db);
-
-int pkgdb_hierdb_read_pkgdb_sub(struct pkgdb *db, struct pkgdb_sur *dbs,
+int pkgdb_hierdb_read_pkgdb_sub(struct pkgdb *db, struct pkgdb_sub *dbs,
const char *ident);
int pkgdb_hierdb_db_close(struct pkgdb *db);
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb_pkgdb_sub.c#4 (text+ko) ====
@@ -1,11 +1,13 @@
#include <sys/types.h>
#include <sys/stat.h>
-#include "pkgdb_sub.h"
+#include "pkgdb_hierdb_pkgdb_sub.h"
#include "pkg.h"
+/* TODO: Create pkgdb_sub.c */
+
struct pkgdb_sub {
- struct pkgdb_sub *owner_db;
+ struct pkgdb *owner_db;
char *ident;
char *path;
@@ -16,6 +18,8 @@
char *display_text;
char *mtree_dirs_text;
char *required_by_text;
+
+ TAILQ_ENTRY(pkgdb_sub) next;
};
struct pkgdb_sub *
==== //depot/projects/soc2009/dforsyth_libpkg/libpkg/pkgdb_hierdb_pkgdb_sub.h#2 (text+ko) ====
@@ -1,3 +1,6 @@
+#ifndef __PKGDB_SUB_H__
+#define __PKGDB_SUB_H__
+
#define SUB_NO_COMMENT 0x00000001
#define SUB_NO_CONTENTS 0x00000002
#define SUB_NO_DESC 0x00000004
@@ -10,3 +13,7 @@
#define SUB_BAD_IDENT 0x00000040
#define SUB_MEMORY_ERR 0x00000080
+
+struct pkgdb_sub;
+
+#endif
==== //depot/projects/soc2009/dforsyth_libpkg/pkg_info/main.c#9 (text+ko) ====
@@ -12,6 +12,8 @@
short opt_show_comment = 0;
char *info_targets;
+/* TODO: Write common error handling functions for pkg_tools. */
+
#if 0
static char opts[] = "a";
static struct option lopts[] = {
@@ -25,8 +27,9 @@
int
main (int argc, char **argv)
{
+ int status;
const char *db_root;
- struct pkgdb *db;
+ struct pkgdb db;
if (argc == 1 && argv != NULL /* Giving argv something to do */) {
opt_all = 1;
@@ -38,16 +41,21 @@
db_root = getenv("PKG_DBDIR"); /* User set it */
if (db_root == NULL)
db_root = PKG_DBDIR_DEFAULT; /* Default */
-
- db = pkgdb_read_db_hierdb(db_root);
+#if 0
+ db = pkgdb_new(HIER_DB);
if (db == NULL) {
fprintf(stderr, "Could not attach to database \"%s\"\n", db_root);
}
+#endif
+ status = pkgdb_db_open(&db, db_root);
+ if (PKGDB_FAIL(status)) {
+ fprintf(stderr, "Could not open database \"%s\"\n", db_root);
+ exit(EXIT_FAILURE);
+ }
+
+ perform_on_db(&db);
- perform_on_db(db);
-
- pkgdb_free_hierdb(db);
- return (0);
+ return (EXIT_SUCCESS);
}
void
@@ -74,16 +82,18 @@
void
perform_on_db(struct pkgdb *db)
{
- int count;
+ int status;
/* char *target; */
- struct pkg *p;
+ struct pkg p;
/* There will be cases where an init is useless, but since I haven't
* written that much yet, init regardless. */
- count = pkgdb_init_db_hierdb(db);
- if (count < 0)
- exit(1);
+ status = pkgdb_db_init(&db);
+ if (PKGDB_FAIL(status)) {
+ fprint(stderr, "Could not initialize database \"%s\"\n", db_root);
+ exit(EXIT_FAILURE);
+ }
while ((p = pkgdb_next_pkg(db)) != NULL) {
if (!opt_all) { /* Wont happen at this point. */
More information about the p4-projects
mailing list