PERFORCE change 185032 for review

David Forsythe dforsyth at FreeBSD.org
Sun Oct 24 07:27:11 UTC 2010


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

Change 185032 by dforsyth at skunk on 2010/10/24 07:26:29

	   Start to move pkg_delete over to new design

Affected files ...

.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.c#5 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.h#5 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg.c#7 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg_internal.h#5 edit
.. //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg_pkg.h#6 edit
.. //depot/projects/soc2010/dforsyth_libpkg/pkg_install/pkg_delete/pkg_delete.c#2 edit

Differences ...

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

@@ -275,7 +275,6 @@
 
         pkg->magic = PKG_DIRDB_MAGIC;
         pkg->source = db;
-        pkg->plist = NULL;
         
         strncpy(pkg->origin, origin, PATH_MAX);
         strncpy(pkg->name, name, PATH_MAX);

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

@@ -10,26 +10,36 @@
 
 int                      fbsd_directorydb_add(struct pkg_db *, struct pkg *,
                         const char *, const char *, const char *, const char *);
+
 /* Test if a key exists. */
 int                      fbsd_directorydb_contains(struct pkg_db *,
                                 const char *);
+
 /* Populates a pkg with the value from key. */
 int                      fbsd_directorydb_get(struct pkg_db *, struct pkg *, 
                         const char *);
+
 int                      fbsd_directorydb_delete(struct pkg_db *, struct pkg *);
+
 /* Returns the uid of the owner of a database. */
 uid_t                    fbsd_directorydb_owner(struct pkg_db *);
+
 /* Connect. */
 int                      fbsd_directorydb_open(struct pkg_db *, const char *);
+
 /* Quit (nocommit). */
 int                      fbsd_directorydb_close(struct pkg_db *);
+
 /* Returns a pkg_list of all the packages in a database. */
 struct pkg_list         *fbsd_directorydb_all(struct pkg_db *);
+
 /* Returns a newly created file in a package. */
 struct pkg_file         *fbsd_directorydb_add_file(struct pkg *, const char *,
                         const char *);
+
 /* Returns a pkg_file_list of all files in a package. */
 struct pkg_file_list    *fbsd_directorydb_files(struct pkg *);
+
 /* Returns a pkg_depend_list of all dependencies in a package. */
 struct pkg_depend_list  *fbsd_directorydb_depends(struct pkg *);
 

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


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

@@ -21,7 +21,6 @@
         unsigned int             magic;
         char                     key[PATH_MAX];
         void                    *source; /* XXX: Make this a union. */
-        struct pkg_property     *plist;
         
         char                     origin[PATH_MAX];
         char                     name[PATH_MAX];

==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/pkg_pkg.h#6 (text+ko) ====


==== //depot/projects/soc2010/dforsyth_libpkg/pkg_install/pkg_delete/pkg_delete.c#2 (text+ko) ====

@@ -7,7 +7,6 @@
 #include <sys/queue.h>
 
 #include <pkg.h>
-#include <pkg_freebsd.h>
 
 typedef enum {
         MATCH_ALL,
@@ -42,7 +41,7 @@
 static void clean_up(int);
 static void usage(void);
 static int pkg_delete(struct delete_config *);
-static int delete(struct pkg *p, struct pkg_database *, struct delete_config *);
+static int delete(struct pkg *, struct pkg_db *, struct delete_config *);
 static int pkg_info_hash_match(struct pkg_file *);
 static void parse_arguments(int, char **);
 static int pattern_match(match_t, const char *, const char *);
@@ -103,6 +102,7 @@
                         break;
                 case ('a'):
                         c.match = MATCH_ALL;
+                        c.recursive = 0;
                         break;
                 case ('G'):
                         c.match = MATCH_EXACT;
@@ -118,6 +118,7 @@
                         break;
                 case ('r'):
                         c.recursive = 1;
+                        c.match ^= MATCH_ALL;
                         break;
                 case ('h'):
                 default:
@@ -167,20 +168,20 @@
 static int
 pkg_delete(struct delete_config *_c)
 {
-        struct pkg *p;
-        struct pkg_database *db;
+        struct pkg *pkg;
+        struct pkg_list *pkgs;
+        struct pkg_db *db;
         struct pkg_target *pt;
         struct stat sb;
         int failure;
         int match_count;
 
-        p = pkg_freebsd_create();
-        db = pkg_freebsd_database_create();
+        db = pkg_db_create();
 
+        /* This is all sorts of terrible... */
         _c->database_location = getenv("PKG_DBDIR");
-        if (_c->database_location == NULL) {
+        if (_c->database_location == NULL)
                 _c->database_location = "/var/db/pkg";
-        }
 
 #if 0
         /* Make sure we can act on the database. */
@@ -194,30 +195,23 @@
 #endif
         (void)sb;
 
-        pkg_database_open(db, _c->database_location, 0);
+        if (pkg_db_open(db, _c->database_location) != PKG_OK) return (1);
+
         failure = 0;
         match_count = 0;
 
         if (_c->match == MATCH_ALL) {
-                while (pkg_database_get_next_pkg(db, p, 0) == PKG_OK) {
-                        if (delete(p, db, _c) != 0) {
-                                failure++;
-                        }
-                        pkg_database_rewind(db);
-                }
+                TAILQ_FOREACH(pkg, pkgs, next)
+                        if (delete(pkg, db, _c) != 0) failure++;
         } else {
                 STAILQ_FOREACH(pt, &_c->targets, next) {
-                        while (pkg_database_get_next_pkg(db, p, 0) == PKG_OK) {
-                                if (pattern_match(_c->match,
-                                        pkg_freebsd_get_name(p),
+                        TAILQ_FOREACH(pkg, pkgs, next) {
+                                if (pattern_match(_c->match, pkg_name(pkg),
                                         pt->target)) {
-                                        if (delete(p, db, _c) != 0) {
-                                                failure++;
-                                        }
+                                        if (delete(pkg, db, _c) != 0) failure++;
                                         break;
                                 }
                         }
-                        pkg_database_rewind(db);
                 }
         }
        
@@ -225,12 +219,18 @@
 }
 
 static int
-delete(struct pkg *p, struct pkg_database *db, struct delete_config *_c)
+delete(struct pkg *pkg, struct pkg_db *db, struct delete_config *_c)
 {
-        struct pkg_dependency d;
-        struct pkg_exec e;
-        struct pkg_file f;
-        struct pkg *pp;
+        struct pkg_depend *depend;
+        struct pkg_file *file;
+        struct pkg_reqby *reqby;
+
+        struct pkg_depend_list *depends;
+        struct pkg_file_list *files;
+        struct pkg_reqby_list *reqbys;
+
+        struct pkg *rpkg;
+
         const char *script;
         const char *rb;
         char pathname[PATH_MAX];
@@ -239,41 +239,18 @@
         int rb_count;
         const char *dep_name;
         const char *dep_origin;
-        const char *pkg_name;
-        
-        /* We're assuming p is installed. */
         
-        /* A second pkg to play with. */
-        pp = pkg_freebsd_create();
-        if (pp == NULL) {
-                exit(99);
-        }
-
         rval = 0;
-        pkg_name = pkg_freebsd_get_name(p);
 
         /* 
          * Check the dependents for this package.  If we're doing a recursive
          * removal, do it here. 
          */
-        rb_count = 0; /* This is for the second dependent check. */
-        while ((rb = pkg_freebsd_get_next_required_by(p)) != NULL) {
-                /* 
-                 * Have to rewind since delete uses the cursor, and we got here
-                 * from pkg_delete(), which is iterating through the database
-                 * itself.
-                 */
-                ++rb_count;
+        reqbys = pkg_reqbys(pkg);
+        TAILQ_FOREACH(reqby, reqbys, next) {
                 if (_c->recursive) {
-                        pkg_database_rewind(db);
-                        while (pkg_database_get_next_pkg(db, pp, 0) == PKG_OK) {
-                                if (strcmp(rb, pkg_freebsd_get_name(pp)) == 0) {
-                                        /* XXX: Apparently there's only supposed
-                                         * to be one level of recursion... */
-                                        delete(pp, db, _c);
-                                        --rb_count;
-                                }
-                        }
+                        rpkg = pkg_db_get(db, pkg_reqby_name(reqby));
+                        if (rpkg != NULL) delete(rpkg, db, _c);
                 }
         }
 
@@ -282,102 +259,62 @@
                 fflush(stderr);
                 /* XXX: */
         }
-        
-        if (rb_count > 0) {
+
+#if 0
+        if (!TAILQ_EMPTY(reqbys) && !_c->recursive) {
+                /* If we're recursive, even if this isn't empty, all the reqbys
+                 * are gone. */
                 warnx("Package '%s' is required by these other packages and "
-                        "may not be deinstalled %s:", pkg_name,
+                        "may not be deinstalled %s:", pkg_name(pkg),
                         (_c->force ? "(but it will be deleted anyway)" : ""));
-                pkg_freebsd_required_by_rewind(p);
-                while ((rb = pkg_freebsd_get_next_required_by(p)) != NULL) {
-                        pkg_database_rewind(db);
-                        /* XXX: It would be nice to verify against the database
-                         * here. */
-                        fprintf(stderr, "%s\n", rb);
-                }
+
+                TAILQ_FOREACH(reqby, reqbys, next)
+                        fprintf(stderr, "%s\n", pkg_reqby_name(reqby));
+
         }
+#endif
 
-        script = pkg_freebsd_get_require_script_path(p);
+        script = pkg_require(pkg);
         if (script != NULL) {
-                if (_c->verbose) {
-                        printf("Executing 'require' script.\n");
-                }
+                if (_c->verbose) printf("Executing 'require' script.\n");
                 warnx("RUN: %s", script);
         }
 
-        
-        /*
-         * XXX: I don't check to see if the old style post-deinstall script is
-         * here.  I should add a check for that, or figure out a way to have
-         * pkg_freebsd be smart enough to deal with it.
-         */
-        script = pkg_freebsd_get_deinstall_script_path(p);
+        script = pkg_deinstall(pkg);
         if (script != NULL) {
-                if (_c->fake) {
+                if (_c->fake)
                         printf("Would execute deinstall script at this "
-                        "point.\n");
-                } else {
-                        warnx("RUN: %s %s %s", script, pkg_name, "DEINSTALL");
-                }
+                                "point.\n");
+                else
+                        warnx("RUN: %s %s %s", script, pkg_name(pkg),
+                                "DEINSTALL");
         }
 
-        while (pkg_freebsd_get_next_dependency(p, &d) == PKG_OK) {
-                dep_name = pkg_freebsd_dependency_get_name(&d);
-                if (_c->verbose) {
-                        dep_origin = pkg_freebsd_dependency_get_origin(&d);
-                        printf("Trying to remove dependency on package '%s'",
-                                dep_name);
-                        if (dep_origin != NULL) {
-                                printf(" with '%s' origin", dep_origin);
-                        }
-                        printf(".\n");
-                }
-                if (_c->fake) {
-                        continue;
-                }
-                /* 
-                 * TODO: All these rewinds are getting tedious.  I should just
-                 * add a get function to the database API.
-                 */
-                pkg_database_rewind(db);
-                while (pkg_database_get_next_pkg(db, pp, 
-                        SKIP_ALL ^ SKIP_REQUIRED_BY) == PKG_OK) {
-                        if (strcmp(dep_name, pkg_freebsd_get_name(pp)) == 0) {
-                                pkg_freebsd_remove_required_by(pp, pkg_name);
-                                /* XXX: Uncomment this add. */
+        depends = pkg_depends(pkg);
+        TAILQ_FOREACH(depend, depends, next) {
+                if (_c->verbose)
+                        printf("Trying to remove dependency on package '%s'"
+                                " with '%s' origin.", pkg_depend_name(depend),
+                                pkg_depend_origin(depend));
+
+                if (_c->fake) continue;
+
+                rpkg = pkg_db_get(db, pkg_depend_name(depend));
 #if 0
-                                r = pkg_database_add_pkg(db, pp, 
-                                        SKIP_ALL ^ SKIP_REQUIRED_BY);
+                if (rpkg != NULL) pkg_remove_reqby(rpkg, pkg_name(pkg));
 #endif
-                                warnx("ADD: '%s' unrequired-by '%s'", dep_name,
-                                        pkg_name);
-                                r = PKG_OK;
-                                if (r != PKG_OK) {
-                                        warnx("Error removing required-by entry"
-                                        "'%s' in package '%s'", pkg_name,
-                                        pkg_freebsd_get_name(pp));
-                                }
-                        }
-                }
+                warnx("removed reqby %s from pkg %s", pkg_depend_name(depend),
+                        pkg_name(pkg));
         }
 
-        while (pkg_freebsd_get_next_file(p, &f) == PKG_OK) {
-                if (_c->prefix == NULL && 
-                        pkg_freebsd_file_get_prefix(&f) == NULL) {
-                        /* 
-                         * We have no prefix.  Because we don't check for this
-                         * ahead of time, we're sort of SOL.  Just die here and
-                         * let the user clean up the mess.
-                         */
-                        errx(1, "File %s has no prefix!\n",
-                                pkg_freebsd_file_get_pathname(&f));
-                }
-                /* Do a hash check for the file before we spend time building
-                 * the path for it. */
-                if (!pkg_info_hash_match(&f) ) {
-                        warnx("'%s' fails original MD5 checksum - %s",
-                        pkg_freebsd_file_get_pathname(&f),
-                        (_c->force ? "deleted anyway." : "not deleted."));
-                        if (!_c->force) {
+        files = pkg_files(pkg);
+        TAILQ_FOREACH(file, files, next) {
+                if (!pkg_info_hash_match(file)) {
+                        warnx("'%s' fails original MD5 checksum - Will %s.",
+                                pkg_file_pathname(file),
+                                (_c->force ? 
+                                "be deleted anyway" : "not be deleted"));
+                        if (!_c->force)
                                 /* 
                                  * The current pkg_delete tool continues in its
                                  * loop in this situation.  I don't like that,
@@ -386,25 +323,17 @@
                                  * disk.
                                  */
                                 return (1);
-                        }
                 }
-                /* Now build a pathname for the file. */
-                /* _c->prefix overrides the prefix from the packing list. */
-                strcpy(pathname, 
-                        (_c->prefix == NULL ? _c->prefix : 
-                        pkg_freebsd_file_get_prefix(&f)));
-                strcat(pathname, "/");
-                strcat(pathname, pkg_freebsd_file_get_pathname(&f));
+
+                
+                snprintf(pathname, PATH_MAX, "%s/%s", pkg_file_prefix(file),
+                        pkg_file_pathname(file));
+
+                if (_c->verbose) printf("Delete file %s\n", pathname);
 
-                if (_c->verbose) {
-                        printf("Delete file %s\n", pathname);
-                }
-                if (!_c->fake) {
-                        warnx("DELETE: %s", pathname);
-                }
-                if (pkg_freebsd_get_preserve(p)) {
-                        /* Do preserve stuff... */
-                }
+                if (!_c->fake) warnx("DELETE: %s", pathname);
+                
+                if (pkg_preserve(pkg)) {/* Do preserve stuff... */}
         }
 
         while (pkg_freebsd_get_next_unexec(p, &e) == PKG_OK) {
@@ -460,7 +389,7 @@
 }
 
 static int
-pkg_info_hash_match(struct pkg_file *f)
+pkg_info_hash_match(struct pkg_file *filw)
 {
         char path[PATH_MAX];
         char hash[33];
@@ -469,15 +398,10 @@
         char link_buf[PATH_MAX];
         struct stat sb;
         
-        snprintf(path, PATH_MAX, "%s/%s",
-                pkg_freebsd_file_get_prefix(f),
-                pkg_freebsd_file_get_pathname(f));
+        snprintf(path, PATH_MAX, "%s/%s", pkg_file_prefix(file),
+                pkg_file_pathname(file));
+        
         if (lstat(path, &sb) < 0) {
-                /* 
-                 * Technically, we're better than the current pkg_info here.
-                 * pkg_info reports that a file doesn't exist just because it
-                 * can't open it.  That's a bug.
-                 */
                 warnx("%s does not exist", path);
                 return (0);
         }
@@ -490,19 +414,13 @@
         calc_hash = NULL;
         if (S_ISLNK(sb.st_mode)) {
                 link_len = readlink(path, link_buf, PATH_MAX);
-                if (link_len > 0) {
+                if (link_len > 0)
                         calc_hash = MD5Data(link_buf, link_len, hash);
-                }
-        } else if (S_ISREG(sb.st_mode)) {
-                calc_hash = MD5File(path, hash);
-        }
-        if (calc_hash != NULL) {
-                if (strcmp(calc_hash, pkg_freebsd_file_get_hash(f)) != 0) {
-                        return (0);
-                } else {
-                        return (1);
-                }
-        }
+
+        else if (S_ISREG(sb.st_mode)) calc_hash = MD5File(path, hash);
+        
+        if (calc_hash != NULL)
+                return ((strcmp(calc_hash, pkg_file_hash(file)) == 0 ? 1 : 0));
 
         return (0);
 }


More information about the p4-projects mailing list