/* * Check to see if file is a dir, and is empty */ Boolean isemptydir(const char *fname) { if (isdir(fname) || islinktodir(fname)) { DIR *dirp; struct dirent *dp; dirp = opendir(fname); if (!dirp) return FALSE; /* no perms, leave it alone */ for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) { closedir(dirp); return FALSE; } } (void) closedir(dirp); return TRUE; } return FALSE; }
/* * add1pkg(<pkg>) * adds the files listed in the +CONTENTS of <pkg> into the * pkgdb.byfile.db database file in the current package dbdir. It * returns the number of files added to the database file. */ static int add_pkg(const char *pkgdir, void *vp) { FILE *f; plist_t *p; package_t Plist; char *contents; char *PkgName, *dirp; char file[MaxPathSize]; struct pkgdb_count *count; if (!pkgdb_open(ReadWrite)) err(EXIT_FAILURE, "cannot open pkgdb"); count = vp; ++count->packages; contents = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME); if ((f = fopen(contents, "r")) == NULL) errx(EXIT_FAILURE, "%s: can't open `%s'", pkgdir, CONTENTS_FNAME); free(contents); read_plist(&Plist, f); if ((p = find_plist(&Plist, PLIST_NAME)) == NULL) { errx(EXIT_FAILURE, "Package `%s' has no @name, aborting.", pkgdir); } PkgName = p->name; dirp = NULL; for (p = Plist.head; p; p = p->next) { switch(p->type) { case PLIST_FILE: if (dirp == NULL) { errx(EXIT_FAILURE, "@cwd not yet found, please send-pr!"); } (void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name); if (!(isfile(file) || islinktodir(file))) { if (isbrokenlink(file)) { warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME); } else { warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME); } } else { pkgdb_store(file, PkgName); ++count->files; } break; case PLIST_PKGDIR: add_pkgdir(PkgName, dirp, p->name); ++count->directories; break; case PLIST_CWD: if (strcmp(p->name, ".") != 0) dirp = p->name; else dirp = pkgdb_pkg_dir(pkgdir); break; case PLIST_IGNORE: p = p->next; break; case PLIST_SHOW_ALL: case PLIST_SRC: case PLIST_CMD: case PLIST_CHMOD: case PLIST_CHOWN: case PLIST_CHGRP: case PLIST_COMMENT: case PLIST_NAME: case PLIST_UNEXEC: case PLIST_DISPLAY: case PLIST_PKGDEP: case PLIST_DIR_RM: case PLIST_OPTION: case PLIST_PKGCFL: case PLIST_BLDDEP: break; } } free_plist(&Plist); fclose(f); pkgdb_close(); return 0; }
/* * Assumes CWD is in /var/db/pkg/<pkg>! */ static void check1pkg(const char *pkgdir, int *filecnt, int *pkgcnt) { FILE *f; plist_t *p; package_t Plist; char *PkgName, *dirp = NULL, *md5file; char file[MaxPathSize]; char *content; content = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME); f = fopen(content, "r"); if (f == NULL) err(EXIT_FAILURE, "can't open %s", content); free(content); read_plist(&Plist, f); p = find_plist(&Plist, PLIST_NAME); if (p == NULL) errx(EXIT_FAILURE, "Package %s has no @name, aborting.", pkgdir); PkgName = p->name; for (p = Plist.head; p; p = p->next) { switch (p->type) { case PLIST_FILE: if (dirp == NULL) { warnx("dirp not initialized, please send-pr!"); abort(); } (void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name); if (isfile(file) || islinktodir(file)) { if (p->next && p->next->type == PLIST_COMMENT) { if (strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) { if ((md5file = MD5File(file, NULL)) != NULL) { /* Mismatch? */ if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0) printf("%s fails MD5 checksum\n", file); free(md5file); } } else if (strncmp(p->next->name, SYMLINK_HEADER, SymlinkHeaderLen) == 0) { char buf[MaxPathSize + SymlinkHeaderLen]; int cc; (void) strlcpy(buf, SYMLINK_HEADER, sizeof(buf)); if ((cc = readlink(file, &buf[SymlinkHeaderLen], sizeof(buf) - SymlinkHeaderLen - 1)) < 0) { warnx("can't readlink `%s'", file); } else { buf[SymlinkHeaderLen + cc] = 0x0; if (strcmp(buf, p->next->name) != 0) { printf("symlink (%s) is not same as recorded value, %s: %s\n", file, buf, p->next->name); } } } } (*filecnt)++; } else if (isbrokenlink(file)) { warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME); } else { warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME); } break; case PLIST_CWD: if (strcmp(p->name, ".") != 0) dirp = p->name; else dirp = pkgdb_pkg_dir(pkgdir); break; case PLIST_IGNORE: p = p->next; break; case PLIST_SHOW_ALL: case PLIST_SRC: case PLIST_CMD: case PLIST_CHMOD: case PLIST_CHOWN: case PLIST_CHGRP: case PLIST_COMMENT: case PLIST_NAME: case PLIST_UNEXEC: case PLIST_DISPLAY: case PLIST_PKGDEP: case PLIST_DIR_RM: case PLIST_OPTION: case PLIST_PKGCFL: case PLIST_BLDDEP: case PLIST_PKGDIR: break; } } free_plist(&Plist); fclose(f); (*pkgcnt)++; }