PERFORCE change 162982 for review
David Forsythe
dforsyth at FreeBSD.org
Fri May 29 00:00:23 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=162982
Change 162982 by dforsyth at squirrel on 2009/05/28 23:59:32
Modified read_subdir routine.
Affected files ...
.. //depot/projects/soc2009/dforsyth_libpkg/pkg.c#3 edit
.. //depot/projects/soc2009/dforsyth_libpkg/pkg.h#2 edit
.. //depot/projects/soc2009/dforsyth_libpkg/pkgdb.c#3 edit
Differences ...
==== //depot/projects/soc2009/dforsyth_libpkg/pkg.c#3 (text+ko) ====
@@ -28,12 +28,11 @@
return (p);
memset(p, 0, sizeof(*p));
- p->ident = malloc(strlen(ident) + 1);
+ p->ident = strdup(ident);
if (p->ident == NULL) {
pkg_free(p);
return (NULL);
}
- strcpy(p->ident, ident);
return (p);
}
==== //depot/projects/soc2009/dforsyth_libpkg/pkg.h#2 (text+ko) ====
@@ -2,7 +2,7 @@
/* pkgdb. */
struct pkgdb;
struct pkgdb_subdir;
-struct sd_head;
+struct subdir_head;
struct dirent;
struct pkgdb *pkgdb_new_hierdb(const char *db_root);
==== //depot/projects/soc2009/dforsyth_libpkg/pkgdb.c#3 (text+ko) ====
@@ -16,7 +16,7 @@
int sd_count;
struct pkgdb_subdir *sd_curr;
- TAILQ_HEAD(sd_head, pkgdb_subdir) *sd_headp;
+ TAILQ_HEAD(subdir_head, pkgdb_subdir) sd_head;
/* Callbacks */
/* tuuuummmmbbbllleeewwwweeeedddddd*/
@@ -26,8 +26,9 @@
TAILQ_ENTRY(pkgdb_subdir) next;
char *name;
+ char *path;
+ int file_count;
char **file_list;
- int num_files;
int dirty;
};
@@ -42,36 +43,29 @@
int s;
struct stat sb;
struct pkgdb *db;
+ char *new_db_root;
+ struct sd_head *new_sd_headp;
if (db_root == NULL)
return (NULL);
-
+
s = stat(db_root, &sb);
- if (s != 0)
+ if (s != 0 || !S_ISDIR(sb.st_mode))
return (NULL);
- if (!S_ISDIR(sb.st_mode))
+
+ db = calloc(1, sizeof(*db));
+ new_db_root = strdup(db_root);
+ // new_sd_headp = calloc(1, sizeof(*db->sd_headp));
+ if (db == NULL || new_db_root == NULL/* || new_sd_headp == NULL*/) {
+ free(db);
+ free(new_db_root);
+ // free(new_sd_headp);
return (NULL);
-
- db = malloc(sizeof(*db));
- if (db == NULL)
- return (db);
- memset(db, 0, sizeof(*db));
-
- db->db_root = malloc(strlen(db_root) + 1);
- if (db->db_root == NULL) {
- pkgdb_free_hierdb(db);
- return (NULL);
}
- strcpy(db->db_root, db_root);
+ db->db_root = new_db_root;
+ // db->sd_headp = new_sd_headp;
+ TAILQ_INIT(&db->sd_head);
- db->sd_headp = malloc(sizeof(*db->sd_headp));
- if (db->sd_headp == NULL) {
- pkgdb_free_hierdb(db);
- return (NULL);
- }
- memset(db->sd_headp, 0, sizeof(*db->sd_headp));
- TAILQ_INIT(db->sd_headp);
-
db->sd_count = 0;
db->dirty = 1;
@@ -125,7 +119,7 @@
if (db->dirty == 0) {
/* No changes since the last reset, don't bother walking the
* database again. */
- db->sd_curr = TAILQ_FIRST(db->sd_headp);
+ db->sd_curr = TAILQ_FIRST(&db->sd_head);
return (db->sd_count);
}
@@ -133,22 +127,24 @@
/* Clear out old list. */
pkgdb_free_pkgdb_subdir_list(db);
-
- TAILQ_INIT(db->sd_headp);
+ /* Later on I should look into inserting changes into the existing
+ * list, rather than just bombing the whole thing. */
+
+ TAILQ_INIT(&db->sd_head);
for (i = 0; i < db->sd_count; ++i) {
sd = pkgdb_read_subdir(db, ents[i]->d_name);
if (sd == NULL) {
pkgdb_free_pkgdb_subdir_list(db);
+ free(ents);
return (-1);
}
- TAILQ_INSERT_TAIL(db->sd_headp, sd, next);
+ TAILQ_INSERT_TAIL(&db->sd_head, sd, next);
free(ents[i]);
}
-
+ free(ents);
+ db->sd_curr = TAILQ_FIRST(&db->sd_head);
db->dirty = 0;
- db->sd_curr = TAILQ_FIRST(db->sd_headp);
-
return (db->sd_count);
}
@@ -163,18 +159,56 @@
struct pkgdb_subdir *
pkgdb_read_subdir(struct pkgdb *db, const char *name)
{
+ int i;
+ int s;
+ int slash;
+ struct stat sb;
struct pkgdb_subdir *sd;
+ char *new_name;
+ char *new_path;
+ DIR *sd_dirp;
+ struct dirent **ents;
+
+ slash = (db->db_root[strlen(db->db_root) - 1] == '/' ? 0 : 1);
+
+ sd = calloc(1, sizeof(*sd));
+ new_name = strdup(name);
+ new_path = malloc(strlen(db->db_root) + slash + strlen(name) + 1);
+ if (sd == NULL || new_name == NULL || new_path == NULL) {
+ free(sd);
+ free(new_name);
+ free(new_path);
+ }
+ strcpy(new_path, db->db_root);
+ strcat(new_path, "/");
+ strcat(new_path, name);
- sd = malloc(sizeof(*sd));
- if (sd == NULL)
+ sd->name = new_name;
+ sd->path = new_path;
+
+ s = lstat(sd->path, &sb);
+ if (s < 0 || S_ISLNK(sb.st_mode) || !S_ISDIR(sb.st_mode)) {
+ pkgdb_free_pkgdb_subdir(sd);
return (NULL);
+ }
- sd->name = malloc(strlen(name) + 1);
- if (sd->name == NULL) {
- free(sd);
+ sd->file_count = scandir(sd->path, &ents, subdir_sel, alphasort);
+ sd->file_list = calloc(sd->file_count, sizeof(char *));
+ if (sd->file_list == NULL) {
+ pkgdb_free_pkgdb_subdir(sd);
return (NULL);
}
- strcpy(sd->name, name);
+ for (i = 0; i < sd->file_count; ++i) {
+ sd->file_list[i] = strdup(ents[i]->d_name);
+ if (sd->file_list[i] == NULL) {
+ sd->file_count = i; /* free up to */
+ pkgdb_free_pkgdb_subdir(sd);
+ free(ents);
+ return (NULL);
+ }
+ free(ents[i]);
+ }
+ free(ents);
return (sd);
}
@@ -182,25 +216,42 @@
/* Move the current subdir pointer to the next one in the list. Return
* the previous subdir. Return NULL if there are no more left. */
-struct pkg *
-pkgdb_next_pkg(struct pkgdb *db)
+struct pkgdb_subdir *
+pkgdb_next_pkgdb_subdir(struct pkgdb *db)
{
- struct pkg *p;
struct pkgdb_subdir *sd;
if (db == NULL)
return (NULL);
sd = db->sd_curr;
- if (db->sd_curr == NULL)
+ if (sd == NULL)
return (NULL);
db->sd_curr = TAILQ_NEXT(db->sd_curr, next);
+ return (sd);
+}
+
+struct pkg *
+pkgdb_next_pkg(struct pkgdb *db)
+{
+ struct pkgdb_subdir *sd;
+ struct pkg *p;
+
+
+ if (db == NULL)
+ return (NULL);
+
+ sd = pkgdb_next_pkgdb_subdir(db);
+ if (sd == NULL)
+ return (NULL);
p = pkg_new(sd->name);
return (p);
}
+
+
/* Free a hierdb. */
void
@@ -220,35 +271,27 @@
struct pkgdb_subdir *sd;
struct pkgdb_subdir *sdn;
- sd = TAILQ_FIRST(db->sd_headp);
+ sd = TAILQ_FIRST(&db->sd_head);
while (sd != NULL) {
sdn = TAILQ_NEXT(sd, next);
pkgdb_free_pkgdb_subdir(sd);
sd = sdn;
}
- TAILQ_INIT(db->sd_headp);
+ TAILQ_INIT(&db->sd_head);
}
void
pkgdb_free_pkgdb_subdir(struct pkgdb_subdir *sd)
{
+ int i;
+
if (sd == NULL)
return;
free(sd->name);
+ free(sd->path);
+ for (i = 0; i < sd->file_count; ++i)
+ free(sd->file_list[i]);
+ free(sd->file_list);
free(sd);
}
-
-void
-free_ptr_array(void **arr, int c)
-{
- int i;
-
- if (arr == NULL)
- return;
-
- for (i = 0; i < c; ++i)
- free(arr[i]);
-
- free(arr);
-}
More information about the p4-projects
mailing list