PERFORCE change 185587 for review

David Forsythe dforsyth at FreeBSD.org
Wed Nov 10 04:25:44 UTC 2010


http://p4web.freebsd.org/@@185587?ac=10

Change 185587 by dforsyth at skunk on 2010/11/10 04:25:19

	Add release functions and clean up leaks.  Get rid of some old junk.

Affected files ...

.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/conflict.c#2 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/conflict.h#2 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/database.c#9 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/database.h#7 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/depend.c#4 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/depend.h#4 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.c#10 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.h#9 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg.c#12 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg.h#12 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg_types.h#5 edit
.. //depot/projects/soc2010/dforsyth_libpkg/pkg_install/pkg_info/pkg_info.c#8 edit

Differences ...

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/conflict.c#2 (text+ko) ====

@@ -12,6 +12,12 @@
 }
 
 void
+pkg_conflict_release(struct pkg_conflict *c)
+{
+	free(c);
+}
+
+void
 _pkg_conflict_set_expr(struct pkg_conflict *conflict, const char *expr)
 {
 	strncpy(conflict->expr, expr, PATH_MAX);

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/conflict.h#2 (text+ko) ====

@@ -3,6 +3,8 @@
 
 struct pkg_conflict	*pkg_conflict_alloc(void);
 
+void		 pkg_conflict_release(struct pkg_conflict *);
+
 void			 _pkg_conflict_set_expr(struct pkg_conflict *,
 				const char *);
 

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/database.c#9 (text+ko) ====

@@ -48,6 +48,7 @@
 
 	/* TODO: NULL out callback pointers. */
 	db->open = fbsd_directorydb_open;
+	db->close = fbsd_directorydb_close;
 	db->all = fbsd_directorydb_all;
 	db->get = fbsd_directorydb_get;
 
@@ -57,6 +58,7 @@
 int
 pkg_db_finish(struct pkg_db *db)
 {
+	/* Close if the db is open. */
 	if (db->internal != NULL) {
 		if (pkg_db_close(db) != PKG_OK) {
 			warnx("Could not close pkg_db (%s)\n", db->path);
@@ -117,20 +119,7 @@
 int
 pkg_db_close(struct pkg_db *db)
 {
-	int r;
-
-	if (db->internal != NULL) {
-		if (db->close) {
-			r = db->close(db);
-		} else {
-			r = PKG_OK;
-		}
-	} else {
-		/* kaboom? */
-		return (PKG_NOT_OK);
-	}
-
-	return (r);
+	return (db->close(db));
 }
 
 struct pkg *

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/database.h#7 (text+ko) ====


==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/depend.c#4 (text+ko) ====

@@ -11,6 +11,12 @@
 	return (calloc(1, sizeof(struct pkg_depend)));
 }
 
+void
+pkg_depend_release(struct pkg_depend *d)
+{
+	free(d);
+}
+
 const char *
 pkg_depend_name(struct pkg_depend *dep)
 {

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/depend.h#4 (text+ko) ====

@@ -19,6 +19,9 @@
 #include "pkg_types.h"
 
 struct pkg_depend	       *pkg_depend_alloc(void);
+
+void			 pkg_depend_release(struct pkg_depend *);
+
 #if 0
 const char		      *pkg_depend_name(struct pkg_depend *);
 const char		      *pkg_depend_origin(struct pkg_depend *);

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.c#10 (text+ko) ====

@@ -32,31 +32,13 @@
  * setup method is in place. */
 #include "pkg_internal.h"
 
-struct _read_plist {
-	struct pkg_property *plist;
-	char key[PATH_MAX];
-	uint32_t parsed;
-	RB_ENTRY(_read_plist) entry;
-};
-
 struct _directorydb {
 	/* Location of the database. */
 	char path[PATH_MAX];
 	
-	/* Journal handle. */
-	int journal;
-	char journal_path[PATH_MAX];
-	char journal_dir_path[PATH_MAX];
-
 	/* A NULL terminated list of keys the db currently has. */
 	char **keylist;
 
-	/* 
-	 * The time of our last access.  If this has changed, another
-	 * directorydb instance has changes something, so we need to refresh.
-	 */
-	time_t last;
-
 	RB_HEAD(plist_head, _read_plist) plist_head;
 };
 
@@ -81,11 +63,8 @@
 
 static int		 fbsd_directorydb_read_pkg(struct _directorydb *,
 				struct pkg *, uint32_t);
-int			 fbsd_directorydb_finish(struct pkg_db *,
-				struct pkg *);
+static int		 fbsd_directorydb_finish(struct pkg *);
 static char		*read_file(const char *);
-static int		 _read_plist_cmp(struct _read_plist *,
-				struct _read_plist *);
 static void		 fbsd_directorydb_pkg_setup(struct pkg_db *,
 				struct pkg *, const char *);
 
@@ -103,16 +82,17 @@
 
 static int		 fbsd_directorydb_read_description(struct pkg *);
 
-RB_GENERATE_STATIC(plist_head, _read_plist, entry, _read_plist_cmp);
+static int		 fbsd_directorydb_has_backup(struct _directorydb *,
+				const char *);
+static int		 _fbsd_directorydb_has_backup(const char *);
+
+static int 		 fbsd_directorydb_resolve(struct _directorydb *,
+				const char *, int);
+static int		 _fbsd_directorydb_resolve(const char *, int);
 
 #define PKG_DIRDB_MAGIC	 0x11111111
-#define DB_DIRDB_MAGIC	  0x11111111
+#define DB_DIRDB_MAGIC	 0x11111111
 
-/* The journal file. */
-#define JOURNAL ".journal"
-/* Where the journaler can store files for changes before a sync. */
-#define JOURNAL_DIR ".journal_sav"
-
 #define journal_open(path) open(path, O_CREAT | O_APPEND | O_EXLOCK)
 
 #define _pkg_check_magic(p) assert(p->magic == PKG_DIRDB_MAGIC)
@@ -129,18 +109,12 @@
 	return (fbsd_directorydb_read_pkg(d, pkg, src_mask));	\
 }
 
-static int
-_read_plist_cmp(struct _read_plist *a, struct _read_plist *b)
-{
-	return (strcmp(a->key, b->key));
-}
-
 /* Close a "connection" to a directorydb. */
 int
 fbsd_directorydb_close(struct pkg_db *db)
 {
 	struct _directorydb *d = db->internal;
-	close(d->journal);
+	free(d);
 	return (0);
 }
 
@@ -150,57 +124,18 @@
 {
 	struct _directorydb *d;
 	struct stat sb;
-	int jfd;
 
-	printf("ddb open\n");
-
+	stat(path, &sb);
+	assert(S_ISDIR(sb.st_mode));
 
 	d = calloc(1, sizeof(*d));
-	if (d == NULL) {
-		return (PKG_NOT_OK);
-	}
+	if (d == NULL) return (PKG_NOT_OK);
 	
 	strcpy(d->path, path);
 	printf("path: %s\n", d->path);
 
 	db->magic = DB_DIRDB_MAGIC;
-	RB_INIT(&d->plist_head);
 
-	(void)jfd;
-	(void)sb;
-#if 0
-
-	/* Open the journal. */
-
-	strcpy(d->journal_path, d->path);
-	strcat(d->journal_path, JOURNAL);
-	
-	/* Grab the lock on the journal file, creating it if it doesn't exist.
-	 * */
-	if ((jfd = journal_open(d->journal_path)) < 0) {
-		return (PKG_NOT_OK);
-	}
-
-	/* Make sure that the journal dir exists (create it if it doesn't). */
-	strcpy(d->journal_dir_path, d->path);
-	strcpy(d->journal_dir_path, "/");
-	strcpy(d->journal_dir_path, JOURNAL_DIR);
-
-	if (stat(d->journal_dir_path, &sb) < 0) {
-		/* not there. */
-		mkdir(d->journal_dir_path, 0755);
-	} else if (!S_ISDIR(sb.st_mode)) {
-		close(jfd);
-		return (PKG_NOT_OK); /* This could probably be FATAL */
-	}
-
-	/* Let the journal file go. */
-	if (close(jfd) < 0) {
-		return (PKG_FATAL);
-	}
-#endif
-
-	/* Annnnd... go. */
 	db->internal = d;
 
 	return (PKG_OK);
@@ -237,6 +172,8 @@
 	
 	pkg->add_conflict = fbsd_directorydb_add_conflict;
 	pkg->add_file = fbsd_directorydb_add_file;
+
+	pkg->finish = fbsd_directorydb_finish;
 }
 
 static int
@@ -285,6 +222,7 @@
 		TAILQ_INSERT_TAIL(list, pkg, next);
 	}
 	fts_close(ftsp);
+	free(paths[0]);
 
 	return (list);
 }
@@ -317,16 +255,63 @@
 	char path[PATH_MAX];
 
 	d = db->internal;
+	
+	sprintf(path, "%s/%s", d->path, key);
+
+	if (stat(path, &sb) < 0) return (0);
+
+	return (S_ISDIR(sb.st_mode));
+}
+
+static int
+fbsd_directorydb_has_backup(struct _directorydb *d, const char *name)
+{
+	char directory[PATH_MAX];
+
+	sprintf(directory, "%s/%s", d->path, name);
+	return (_fbsd_directorydb_has_backup(directory));
+}
+
+static int
+fbsd_directorydb_resolve(struct _directorydb *d, const char *name,
+	int use_bak)
+{
+	char directory[PATH_MAX];
+
+	sprintf(directory, "%s/%s", d->path, name);
+	return (_fbsd_directorydb_resolve(directory, use_bak));
+}
 
-	strcpy(path, d->path);
-	strcat(path, "/");
-	strcat(path, key);
+/* XXX: Pull these out of the top level has_backup and resolve functions in case
+ * they change (for now at least). */
+static int
+_fbsd_directorydb_has_backup(const char *directory)
+{
+	char bak_path[PATH_MAX];
+
+	assert(directory != NULL);
+
+	sprintf(bak_path, "%s/%s", directory, "+CONTENTS.bak");
+	return (access(bak_path, F_OK) == 0);
+}
+
+static int
+_fbsd_directorydb_resolve(const char *directory, int use_bak)
+{
+	char bak_path[PATH_MAX];
+	char cur_path[PATH_MAX];
+
+	assert(directory != NULL);
+
+	sprintf(bak_path, "%s/%s", directory, "+CONTENTS.bak");
+	sprintf(cur_path, "%s/%s", directory, "+CONTENTS");
 
-	if (stat(path, &sb) < 0) {
-		return (0);
-	}
+	if (use_bak) {
+		/* do replacement. */
+	} else return (unlink(bak_path));
 
-	return (S_ISDIR(sb.st_mode));
+	/* NOT REACHED */
+	return (-1);
 }
 
 int
@@ -366,13 +351,9 @@
 	
 	d = db->internal;
 
-	strcpy(path, d->path);
-	strcat(path, "/");
-	strcat(path, key);
+	sprintf(path, "%s/%s", d->path, key);
 	
-	if (stat(path, &sb) < 0) {
-		return (PKG_NOT_OK);
-	}
+	if (stat(path, &sb) < 0) return (PKG_NOT_OK);
 
 	fbsd_directorydb_pkg_setup(db, pkg, key);
 
@@ -380,10 +361,20 @@
 }
 
 int
-fbsd_directorydb_finish(struct pkg_db *db, struct pkg *pkg)
+fbsd_directorydb_finish(struct pkg *pkg)
 {
+	const char *key;
+	struct pkg_db *db;
+	struct _directorydb *d;
+
 	_pkg_check_magic(pkg);
-	assert(pkg->source == db);
+	
+	key = pkg->key;
+	db = pkg->source;
+	d = db->internal;
+
+	if (fbsd_directorydb_has_backup(d, key))
+		fbsd_directorydb_resolve(d, key, 1);
 
 	pkg_release(pkg);
 
@@ -414,12 +405,13 @@
 
 	for (pi = pkg_entries; pi->info_name != NULL; pi++) {
 		if (mask & pi->info_mask) {
-			_pkg_util_path_join(path, d->path, pkg->key);
-			_pkg_util_path_join(path, path, pi->file_name);
+			sprintf(path, "%s/%s/%s", d->path, pkg->key,
+				pi->file_name);
 
 			if ((data = read_file(path)) == NULL) continue;
 
 			r = fbsd_plist_parse(pkg, data, pi->info_name);
+			free(data);
 			if (r != PKG_OK) return (r);
 		}
 	}

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.h#9 (text+ko) ====


==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg.c#12 (text+ko) ====

@@ -12,6 +12,7 @@
 #include "pkg.h"
 #include "pkg_internal.h"
 
+#include "conflict.h"
 #include "file.h"
 #include "depend.h"
 
@@ -46,6 +47,41 @@
 void
 pkg_release(struct pkg *pkg)
 {
+	struct pkg_conflict *c, *nc;
+	struct pkg_depend *d, *nd;
+	struct pkg_file *f, *nf;
+
+	if (pkg->conflicts != NULL) {
+		/* Do tailq deletion. */
+		c = TAILQ_FIRST(pkg->conflicts);
+		while (c != NULL) {
+			nc = TAILQ_NEXT(c, next);
+			pkg_conflict_release(c);
+			c = nc;
+		}
+		free(pkg->conflicts);
+	}
+
+	if (pkg->depends != NULL) {
+		d = TAILQ_FIRST(pkg->depends);
+		while (d != NULL) {
+			nd = TAILQ_NEXT(d, next);
+			pkg_depend_release(d);
+			d = nd;
+		}
+		free(pkg->depends);
+	}
+
+	if (pkg->files != NULL) {
+		f = TAILQ_FIRST(pkg->files);
+		while (f != NULL) {
+			nf = TAILQ_NEXT(f, next);
+			pkg_file_release(f);
+			f = nf;
+		}
+		free(pkg->files);
+	}
+
 	free(pkg);
 }
 
@@ -221,6 +257,34 @@
 }
 
 int
+pkg_finish(struct pkg *pkg)
+{
+	return (pkg->finish(pkg));
+}
+
+int
+pkg_list_finish(struct pkg_list *pkgs)
+{
+	struct pkg *pkg;
+
+	/* delete this way so that we can 'recover' on a bad pkg_finish. */
+
+	while (!TAILQ_EMPTY(pkgs)) {
+		pkg = TAILQ_FIRST(pkgs);
+		TAILQ_REMOVE(pkgs, pkg, next);
+		if (pkg->finish(pkg) != PKG_OK) {
+			/* finish failed, put pkg back at the head of the list
+			 * and return not_ok. */
+			TAILQ_INSERT_HEAD(pkgs, pkg, next);
+			return (PKG_NOT_OK);
+		}
+	}
+	free(pkgs);
+
+	return (PKG_OK);
+}
+
+int
 pkg_parse(struct pkg *p, char *data, const char *metaname)
 {
 	return (p->parse(p, data, metaname));

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg.h#12 (text+ko) ====

@@ -70,6 +70,8 @@
  */
 int			 pkg_finish(struct pkg *);
 
+int			 pkg_list_finish(struct pkg_list *);
+
 /*
  * Retrieve the name of a package.
  */

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg_types.h#5 (text+ko) ====

@@ -70,6 +70,8 @@
 	int		      (*set_comment) (struct pkg *, const char *);
 	int		      (*set_description) (struct pkg *, 
 				const char *);
+	
+	int		 (*finish) (struct pkg *);
 
 	int (*parse) (struct pkg *, char *, const char *);
 	

==== //depot/projects/soc2010/dforsyth_libpkg/pkg_install/pkg_info/pkg_info.c#8 (text+ko) ====

@@ -467,7 +467,8 @@
 			}
 		}
 	}
-	
+
+	pkg_list_finish(pkgs);
 	pkg_db_finish(db);
 	return (r);
 }


More information about the p4-projects mailing list