2002-08-09 18:03:48 +00:00
|
|
|
/*
|
2003-09-03 02:09:29 +00:00
|
|
|
* db.c
|
2002-08-09 18:03:48 +00:00
|
|
|
*
|
2004-03-02 04:49:15 +00:00
|
|
|
* Copyright (c) 2002-2004 by Judd Vinet <jvinet@zeroflux.org>
|
2002-08-09 18:03:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2004-07-17 01:55:28 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
2002-08-09 18:03:48 +00:00
|
|
|
#include "package.h"
|
|
|
|
#include "util.h"
|
2003-05-30 19:56:46 +00:00
|
|
|
#include "db.h"
|
2002-08-09 18:03:48 +00:00
|
|
|
|
2003-09-03 02:09:29 +00:00
|
|
|
/* Open a database and return a pacdb_t handle */
|
2003-05-30 19:56:46 +00:00
|
|
|
pacdb_t* db_open(char *root, char *pkgdir, char *treename)
|
2002-08-09 18:03:48 +00:00
|
|
|
{
|
|
|
|
pacdb_t *db = NULL;
|
|
|
|
|
|
|
|
MALLOC(db, sizeof(pacdb_t));
|
2003-05-30 19:56:46 +00:00
|
|
|
MALLOC(db->path, strlen(root)+strlen(pkgdir)+strlen(treename)+2);
|
|
|
|
sprintf(db->path, "%s%s/%s", root, pkgdir, treename);
|
|
|
|
db->dir = opendir(db->path);
|
2002-08-09 18:03:48 +00:00
|
|
|
if(db->dir == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2004-02-18 07:09:00 +00:00
|
|
|
strncpy(db->treename, treename, sizeof(db->treename));
|
2002-08-09 18:03:48 +00:00
|
|
|
|
|
|
|
return(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
void db_close(pacdb_t* db)
|
|
|
|
{
|
2003-05-30 19:56:46 +00:00
|
|
|
if(db) {
|
|
|
|
if(db->dir) {
|
|
|
|
closedir(db->dir);
|
|
|
|
}
|
|
|
|
FREE(db->path);
|
|
|
|
}
|
|
|
|
FREE(db);
|
|
|
|
|
2002-08-09 18:03:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* frees pkgcache if necessary and returns a new package
|
|
|
|
* cache from db
|
|
|
|
*/
|
2003-11-25 02:02:36 +00:00
|
|
|
PMList* db_loadpkgs(pacdb_t *db)
|
2002-08-09 18:03:48 +00:00
|
|
|
{
|
|
|
|
pkginfo_t *info;
|
|
|
|
pkginfo_t **arr = NULL;
|
|
|
|
unsigned int arrct = 0;
|
|
|
|
int i;
|
|
|
|
PMList *cache = NULL;
|
|
|
|
|
|
|
|
rewinddir(db->dir);
|
2004-04-29 19:43:18 +00:00
|
|
|
while((info = db_scan(db, NULL, INFRQ_DESC | INFRQ_DEPENDS)) != NULL) {
|
2002-08-09 18:03:48 +00:00
|
|
|
/* add to the collective */
|
|
|
|
/* we load all package names into a linear array first, so qsort can handle it */
|
|
|
|
if(arr == NULL) {
|
|
|
|
arr = (pkginfo_t**)malloc(sizeof(pkginfo_t*));
|
|
|
|
arrct++;
|
|
|
|
} else {
|
|
|
|
arr = (pkginfo_t**)realloc(arr, (++arrct)*sizeof(pkginfo_t*));
|
|
|
|
}
|
|
|
|
if(arr == NULL) {
|
|
|
|
fprintf(stderr, "error: out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
arr[arrct-1] = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort the package list */
|
|
|
|
qsort(arr, (size_t)arrct, sizeof(pkginfo_t*), pkgcmp);
|
|
|
|
|
|
|
|
/* now load them into the proper PMList */
|
|
|
|
for(i = 0; i < arrct; i++) {
|
|
|
|
cache = list_add(cache, arr[i]);
|
|
|
|
}
|
|
|
|
|
2003-09-03 02:09:29 +00:00
|
|
|
FREE(arr);
|
2003-05-30 19:56:46 +00:00
|
|
|
|
2002-08-09 18:03:48 +00:00
|
|
|
return(cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkginfo_t* db_scan(pacdb_t *db, char *target, unsigned int inforeq)
|
|
|
|
{
|
|
|
|
struct dirent *ent = NULL;
|
|
|
|
char name[256];
|
|
|
|
char *ptr = NULL;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
if(target != NULL) {
|
|
|
|
/* search for a specific package (by name only) */
|
|
|
|
rewinddir(db->dir);
|
|
|
|
ent = readdir(db->dir);
|
|
|
|
while(!found && ent != NULL) {
|
2004-04-29 19:43:18 +00:00
|
|
|
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
|
|
|
|
ent = readdir(db->dir);
|
|
|
|
continue;
|
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
strncpy(name, ent->d_name, 255);
|
|
|
|
/* truncate the string at the second-to-last hyphen, */
|
|
|
|
/* which will give us the package name */
|
|
|
|
if((ptr = rindex(name, '-'))) {
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
if((ptr = rindex(name, '-'))) {
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
if(!strcmp(name, target)) {
|
|
|
|
found = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ent = readdir(db->dir);
|
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* normal iteration */
|
|
|
|
ent = readdir(db->dir);
|
2004-04-29 19:43:18 +00:00
|
|
|
if(ent == NULL) {
|
|
|
|
return(NULL);
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
2004-04-29 19:43:18 +00:00
|
|
|
if(!strcmp(ent->d_name, ".")) {
|
2002-08-09 18:03:48 +00:00
|
|
|
ent = readdir(db->dir);
|
|
|
|
}
|
2004-04-29 19:43:18 +00:00
|
|
|
if(!strcmp(ent->d_name, "..")) {
|
|
|
|
ent = readdir(db->dir);
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(db_read(db, ent, inforeq));
|
|
|
|
}
|
|
|
|
|
|
|
|
pkginfo_t* db_read(pacdb_t *db, struct dirent *ent, unsigned int inforeq)
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
struct stat buf;
|
|
|
|
pkginfo_t *info = NULL;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char line[512];
|
|
|
|
|
|
|
|
if(ent == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we always load DESC */
|
|
|
|
inforeq |= INFRQ_DESC;
|
|
|
|
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(path, PATH_MAX, "%s/%s", db->path, ent->d_name);
|
2002-08-09 18:03:48 +00:00
|
|
|
if(stat(path, &buf)) {
|
|
|
|
/* directory doesn't exist or can't be opened */
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2003-11-25 02:02:36 +00:00
|
|
|
info = newpkg();
|
|
|
|
|
2002-08-09 18:03:48 +00:00
|
|
|
/* DESC */
|
|
|
|
if(inforeq & INFRQ_DESC) {
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(path, PATH_MAX, "%s/%s/desc", db->path, ent->d_name);
|
2002-08-09 18:03:48 +00:00
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
|
|
|
fprintf(stderr, "error: %s: %s\n", path, strerror(errno));
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
while(!feof(fp)) {
|
|
|
|
if(fgets(line, 256, fp) == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
trim(line);
|
|
|
|
if(!strcmp(line, "%NAME%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->name, sizeof(info->name), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->name);
|
|
|
|
} else if(!strcmp(line, "%VERSION%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->version, sizeof(info->version), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->version);
|
|
|
|
} else if(!strcmp(line, "%DESC%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->desc, sizeof(info->desc), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->desc);
|
2003-09-03 02:09:29 +00:00
|
|
|
} else if(!strcmp(line, "%GROUPS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->groups = list_add(info->groups, s);
|
|
|
|
}
|
2003-05-30 19:56:46 +00:00
|
|
|
} else if(!strcmp(line, "%URL%")) {
|
|
|
|
if(fgets(info->url, sizeof(info->url), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2003-05-30 19:56:46 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->url);
|
2004-04-15 05:05:54 +00:00
|
|
|
} else if(!strcmp(line, "%LICENSE%")) {
|
|
|
|
if(fgets(info->license, sizeof(info->license), fp) == NULL) {
|
|
|
|
FREEPKG(info);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->license);
|
2004-07-03 04:25:48 +00:00
|
|
|
} else if(!strcmp(line, "%ARCH%")) {
|
|
|
|
if(fgets(info->arch, sizeof(info->arch), fp) == NULL) {
|
|
|
|
FREEPKG(info);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->arch);
|
2002-08-09 18:03:48 +00:00
|
|
|
} else if(!strcmp(line, "%BUILDDATE%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->builddate, sizeof(info->builddate), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->builddate);
|
|
|
|
} else if(!strcmp(line, "%INSTALLDATE%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->installdate, sizeof(info->installdate), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->installdate);
|
|
|
|
} else if(!strcmp(line, "%PACKAGER%")) {
|
2003-05-30 19:56:46 +00:00
|
|
|
if(fgets(info->packager, sizeof(info->packager), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(info->packager);
|
|
|
|
} else if(!strcmp(line, "%SIZE%")) {
|
|
|
|
char tmp[32];
|
|
|
|
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
trim(tmp);
|
|
|
|
info->size = atol(tmp);
|
2003-09-03 02:09:29 +00:00
|
|
|
} else if(!strcmp(line, "%REPLACES%")) {
|
|
|
|
/* the REPLACES tag is special -- it only appears in sync repositories,
|
2004-04-30 03:37:13 +00:00
|
|
|
* not the local one. */
|
2003-09-03 02:09:29 +00:00
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->replaces = list_add(info->replaces, s);
|
|
|
|
}
|
2003-11-25 02:02:36 +00:00
|
|
|
} else if(!strcmp(line, "%MD5SUM%")) {
|
|
|
|
/* MD5SUM tag only appears in sync repositories,
|
2004-04-30 03:37:13 +00:00
|
|
|
* not the local one. */
|
2003-11-25 02:02:36 +00:00
|
|
|
if(fgets(info->md5sum, sizeof(info->md5sum), fp) == NULL) {
|
|
|
|
FREEPKG(info);
|
|
|
|
return(NULL);
|
|
|
|
}
|
2004-04-30 03:37:13 +00:00
|
|
|
} else if(!strcmp(line, "%FORCE%")) {
|
|
|
|
/* FORCE tag only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
info->force = 1;
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FILES */
|
|
|
|
if(inforeq & INFRQ_FILES) {
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(path, PATH_MAX, "%s/%s/files", db->path, ent->d_name);
|
2002-08-09 18:03:48 +00:00
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
|
|
|
fprintf(stderr, "error: %s: %s\n", path, strerror(errno));
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
while(fgets(line, 256, fp)) {
|
|
|
|
trim(line);
|
|
|
|
if(!strcmp(line, "%FILES%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->files = list_add(info->files, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(line, "%BACKUP%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->backup = list_add(info->backup, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DEPENDS */
|
|
|
|
if(inforeq & INFRQ_DEPENDS) {
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(path, PATH_MAX, "%s/%s/depends", db->path, ent->d_name);
|
2002-08-09 18:03:48 +00:00
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
|
|
|
fprintf(stderr, "db_read: error: %s: %s\n", path, strerror(errno));
|
2003-11-25 02:02:36 +00:00
|
|
|
FREEPKG(info);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
while(!feof(fp)) {
|
|
|
|
fgets(line, 255, fp);
|
|
|
|
trim(line);
|
|
|
|
if(!strcmp(line, "%DEPENDS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->depends = list_add(info->depends, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(line, "%REQUIREDBY%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->requiredby = list_add(info->requiredby, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(line, "%CONFLICTS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->conflicts = list_add(info->conflicts, s);
|
|
|
|
}
|
|
|
|
}
|
2003-09-03 02:09:29 +00:00
|
|
|
if(!strcmp(line, "%PROVIDES%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(trim(line))) {
|
|
|
|
char *s = strdup(line);
|
|
|
|
info->provides = list_add(info->provides, s);
|
|
|
|
}
|
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* INSTALL */
|
2004-07-03 04:25:48 +00:00
|
|
|
if(inforeq & INFRQ_ALL) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s/install", db->path, ent->d_name);
|
|
|
|
if(!stat(path, &buf)) {
|
|
|
|
info->scriptlet = 1;
|
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(info);
|
|
|
|
}
|
|
|
|
|
2004-07-03 04:25:48 +00:00
|
|
|
int db_write(pacdb_t *db, pkginfo_t *info, unsigned int inforeq)
|
2002-08-09 18:03:48 +00:00
|
|
|
{
|
|
|
|
char topdir[PATH_MAX];
|
2003-05-30 19:56:46 +00:00
|
|
|
FILE *fp = NULL;
|
2002-08-09 18:03:48 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
mode_t oldmask;
|
|
|
|
PMList *lp = NULL;
|
|
|
|
|
|
|
|
if(info == NULL) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(topdir, PATH_MAX, "%s/%s-%s", db->path,
|
|
|
|
info->name, info->version);
|
2002-08-09 18:03:48 +00:00
|
|
|
oldmask = umask(0000);
|
2003-05-30 19:56:46 +00:00
|
|
|
mkdir(topdir, 0755);
|
2004-02-18 07:09:00 +00:00
|
|
|
/* make sure we have a sane umask */
|
|
|
|
umask(0022);
|
2002-08-09 18:03:48 +00:00
|
|
|
|
|
|
|
/* DESC */
|
2004-07-03 04:25:48 +00:00
|
|
|
if(inforeq & INFRQ_DESC) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/desc", topdir);
|
|
|
|
fp = fopen(path, "w");
|
|
|
|
if(fp == NULL) {
|
|
|
|
perror("db_write");
|
|
|
|
umask(oldmask);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
fputs("%NAME%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->name);
|
|
|
|
fputs("%VERSION%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->version);
|
|
|
|
fputs("%DESC%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->desc);
|
|
|
|
fputs("%GROUPS%\n", fp);
|
|
|
|
for(lp = info->groups; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fputs("%URL%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->url);
|
|
|
|
fputs("%LICENSE%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->license);
|
|
|
|
fputs("%ARCH%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->arch);
|
|
|
|
fputs("%BUILDDATE%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->builddate);
|
|
|
|
fputs("%INSTALLDATE%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->installdate);
|
|
|
|
fputs("%PACKAGER%\n", fp);
|
|
|
|
fprintf(fp, "%s\n\n", info->packager);
|
|
|
|
fputs("%SIZE%\n", fp);
|
|
|
|
fprintf(fp, "%ld\n\n", info->size);
|
|
|
|
fclose(fp);
|
2003-09-03 02:09:29 +00:00
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
|
|
|
|
/* FILES */
|
2004-07-03 04:25:48 +00:00
|
|
|
if(inforeq & INFRQ_FILES) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/files", topdir);
|
|
|
|
fp = fopen(path, "w");
|
|
|
|
if(fp == NULL) {
|
|
|
|
perror("db_write");
|
|
|
|
umask(oldmask);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
fputs("%FILES%\n", fp);
|
|
|
|
for(lp = info->files; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fputs("%BACKUP%\n", fp);
|
|
|
|
for(lp = info->backup; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fclose(fp);
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DEPENDS */
|
2004-07-03 04:25:48 +00:00
|
|
|
if(inforeq & INFRQ_DEPENDS) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/depends", topdir);
|
|
|
|
fp = fopen(path, "w");
|
|
|
|
if(fp == NULL) {
|
|
|
|
perror("db_write");
|
|
|
|
umask(oldmask);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
fputs("%DEPENDS%\n", fp);
|
|
|
|
for(lp = info->depends; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fputs("%REQUIREDBY%\n", fp);
|
|
|
|
for(lp = info->requiredby; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fputs("%CONFLICTS%\n", fp);
|
|
|
|
for(lp = info->conflicts; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fputs("%PROVIDES%\n", fp);
|
|
|
|
for(lp = info->provides; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char*)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
|
|
|
|
/* INSTALL */
|
|
|
|
/* nothing needed here (script is automatically extracted) */
|
|
|
|
|
2004-02-18 07:09:00 +00:00
|
|
|
umask(oldmask);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2004-04-29 19:43:18 +00:00
|
|
|
void db_search(pacdb_t *db, PMList *cache, const char *treename, PMList *needles)
|
|
|
|
{
|
|
|
|
PMList *i, *j;
|
|
|
|
if(needles == NULL || needles->data == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = needles; i; i = i->next) {
|
|
|
|
char *targ = strdup(i->data);
|
|
|
|
strtoupper(targ);
|
|
|
|
for(j = cache; j; j = j->next) {
|
|
|
|
pkginfo_t *pkg = (pkginfo_t*)j->data;
|
|
|
|
char *haystack;
|
|
|
|
int match = 0;
|
|
|
|
/* check name */
|
|
|
|
haystack = strdup(pkg->name);
|
|
|
|
strtoupper(haystack);
|
|
|
|
if(strstr(haystack, targ)) {
|
|
|
|
match = 1;
|
|
|
|
}
|
|
|
|
FREE(haystack);
|
|
|
|
|
|
|
|
/* check description */
|
|
|
|
if(!match) {
|
|
|
|
haystack = strdup(pkg->desc);
|
|
|
|
strtoupper(haystack);
|
|
|
|
if(strstr(haystack, targ)) {
|
|
|
|
match = 1;
|
|
|
|
}
|
|
|
|
FREE(haystack);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check provides */
|
|
|
|
if(!match) {
|
|
|
|
PMList *m;
|
|
|
|
pkginfo_t *info = db_scan(db, pkg->name, INFRQ_DESC | INFRQ_DEPENDS);
|
|
|
|
if(info != NULL) {
|
|
|
|
for(m = info->provides; m; m = m->next) {
|
|
|
|
haystack = strdup(m->data);
|
|
|
|
strtoupper(haystack);
|
|
|
|
if(strstr(haystack, targ)) {
|
|
|
|
match = 1;
|
|
|
|
}
|
|
|
|
FREE(haystack);
|
|
|
|
}
|
|
|
|
FREEPKG(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(match) {
|
|
|
|
printf("%s/%s %s\n ", treename, pkg->name, pkg->version);
|
|
|
|
indentprint(pkg->desc, 4);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FREE(targ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-09 18:03:48 +00:00
|
|
|
|
2003-05-30 19:56:46 +00:00
|
|
|
PMList* db_find_conflicts(pacdb_t *db, PMList *targets, char *root)
|
2002-08-09 18:03:48 +00:00
|
|
|
{
|
|
|
|
PMList *i, *j, *k;
|
|
|
|
char *filestr = NULL;
|
|
|
|
char path[PATH_MAX+1];
|
|
|
|
char *str = NULL;
|
|
|
|
struct stat buf;
|
|
|
|
PMList *conflicts = NULL;
|
2004-07-17 01:55:28 +00:00
|
|
|
char *sym = NULL, *symw = NULL, *symlink = NULL, *tempsym = NULL;
|
2002-08-09 18:03:48 +00:00
|
|
|
|
|
|
|
/* CHECK 1: check every db package against every target package */
|
|
|
|
/* XXX: I've disabled the database-against-targets check for now, as the
|
|
|
|
* many many strcmp() calls slow it down heavily and most of the
|
|
|
|
* checking is redundant to the targets-against-filesystem check.
|
|
|
|
* This will be re-enabled if I can improve performance significantly.
|
|
|
|
*
|
|
|
|
pkginfo_t *info = NULL;
|
|
|
|
char *dbstr = NULL;
|
|
|
|
rewinddir(db->dir);
|
|
|
|
while((info = db_scan(db, NULL, INFRQ_DESC | INFRQ_FILES)) != NULL) {
|
|
|
|
for(i = info->files; i; i = i->next) {
|
|
|
|
if(i->data == NULL) continue;
|
|
|
|
dbstr = (char*)i->data;
|
|
|
|
for(j = targets; j; j = j->next) {
|
|
|
|
pkginfo_t *targ = (pkginfo_t*)j->data;
|
|
|
|
if(strcmp(info->name, targ->name)) {
|
|
|
|
for(k = targ->files; k; k = k->next) {
|
|
|
|
filestr = (char*)k->data;
|
|
|
|
if(!strcmp(dbstr, filestr)) {
|
|
|
|
if(rindex(k->data, '/') == filestr+strlen(filestr)-1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MALLOC(str, 512);
|
|
|
|
snprintf(str, 512, "%s: exists in \"%s\" (target) and \"%s\" (installed)", dbstr,
|
|
|
|
targ->name, info->name);
|
|
|
|
conflicts = list_add(conflicts, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/* CHECK 2: check every target against every target */
|
|
|
|
for(i = targets; i; i = i->next) {
|
|
|
|
pkginfo_t *p1 = (pkginfo_t*)i->data;
|
|
|
|
for(j = i; j; j = j->next) {
|
|
|
|
pkginfo_t *p2 = (pkginfo_t*)j->data;
|
|
|
|
if(strcmp(p1->name, p2->name)) {
|
|
|
|
for(k = p1->files; k; k = k->next) {
|
|
|
|
filestr = k->data;
|
2003-05-30 19:56:46 +00:00
|
|
|
if(!strcmp(filestr, "._install") || !strcmp(filestr, ".INSTALL")) {
|
2002-08-09 18:03:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(rindex(filestr, '/') == filestr+strlen(filestr)-1) {
|
|
|
|
/* this filename has a trailing '/', so it's a directory -- skip it. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(is_in(filestr, p2->files)) {
|
|
|
|
MALLOC(str, 512);
|
|
|
|
snprintf(str, 512, "%s: exists in \"%s\" (target) and \"%s\" (target)",
|
|
|
|
filestr, p1->name, p2->name);
|
|
|
|
conflicts = list_add(conflicts, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CHECK 3: check every target against the filesystem */
|
|
|
|
for(i = targets; i; i = i->next) {
|
|
|
|
pkginfo_t *p = (pkginfo_t*)i->data;
|
|
|
|
pkginfo_t *dbpkg = NULL;
|
|
|
|
for(j = p->files; j; j = j->next) {
|
|
|
|
filestr = (char*)j->data;
|
2003-05-30 19:56:46 +00:00
|
|
|
snprintf(path, PATH_MAX, "%s%s", root, filestr);
|
2002-08-09 18:03:48 +00:00
|
|
|
if(!stat(path, &buf) && !S_ISDIR(buf.st_mode)) {
|
|
|
|
int ok = 0;
|
|
|
|
if(dbpkg == NULL) {
|
|
|
|
dbpkg = db_scan(db, p->name, INFRQ_DESC | INFRQ_FILES);
|
|
|
|
}
|
|
|
|
if(dbpkg && is_in(j->data, dbpkg->files)) {
|
|
|
|
ok = 1;
|
|
|
|
}
|
2004-07-17 01:55:28 +00:00
|
|
|
/* Make sure that the supposedly-conflicting file is not actually just
|
|
|
|
* a symlink that points to a path that used to exist in the package.
|
|
|
|
*/
|
|
|
|
/* Check if any part of the conflicting file's path is a symlink */
|
|
|
|
if(dbpkg && !ok) {
|
|
|
|
if(!sym) MALLOC(sym, PATH_MAX);
|
|
|
|
if(!symlink) MALLOC(symlink, PATH_MAX);
|
|
|
|
if(!tempsym) MALLOC(tempsym, PATH_MAX);
|
|
|
|
|
|
|
|
strncpy(sym, path, PATH_MAX);
|
|
|
|
symw = sym;
|
|
|
|
do {
|
|
|
|
/* Is it a symlink? */
|
|
|
|
if(!lstat(sym, &buf) && S_ISLNK(buf.st_mode)) {
|
|
|
|
memset(symlink, 0, PATH_MAX);
|
|
|
|
readlink(sym, symlink, PATH_MAX);
|
|
|
|
if(symlink[0] != '/') {
|
2004-07-22 17:11:19 +00:00
|
|
|
char *temp = strdup(sym);
|
2004-07-17 01:55:28 +00:00
|
|
|
strncpy(tempsym, symlink, PATH_MAX);
|
|
|
|
snprintf(symlink, PATH_MAX, "%s/%s", dirname(temp), tempsym);
|
|
|
|
FREE(temp);
|
|
|
|
}
|
2004-07-22 17:11:19 +00:00
|
|
|
/* If it's a directory, tack on a '/' */
|
|
|
|
if(!stat(symlink, &buf) && S_ISDIR(buf.st_mode)) {
|
|
|
|
strcat(symlink, "/");
|
|
|
|
}
|
2004-07-17 01:55:28 +00:00
|
|
|
if(strstr(symlink, root) == symlink) {
|
|
|
|
strncpy(tempsym, symlink+strlen(root), PATH_MAX-strlen(root));
|
|
|
|
/* If that part of the path used to exist in the package */
|
|
|
|
if(is_in(tempsym, dbpkg->files)) {
|
|
|
|
/* See if the modified path used to */
|
|
|
|
snprintf(tempsym, PATH_MAX, "%s%s", symlink+strlen(root), path+strlen(sym)+strlen(root));
|
|
|
|
if(is_in(tempsym, dbpkg->files)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strncpy(tempsym, symlink, PATH_MAX);
|
|
|
|
snprintf(symlink, PATH_MAX, "%s/", tempsym);
|
|
|
|
strncpy(tempsym, symlink+strlen(root), PATH_MAX-strlen(root));
|
|
|
|
/* If that part of the path (explicitly check for directory) used to exist in the package */
|
|
|
|
if(is_in(tempsym, dbpkg->files)) {
|
|
|
|
/* See if the modified path used to */
|
|
|
|
snprintf(tempsym, PATH_MAX, "%s%s", symlink+strlen(root), path+strlen(sym)+strlen(root));
|
|
|
|
if(is_in(tempsym, dbpkg->files)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
symw = dirname(sym);
|
|
|
|
strncpy(sym, symw, PATH_MAX);
|
|
|
|
} while (strncmp(sym, root, PATH_MAX));
|
|
|
|
}
|
|
|
|
if(dbpkg && !ok) {
|
|
|
|
if(!sym) MALLOC(sym, PATH_MAX);
|
|
|
|
if(!symlink) MALLOC(symlink, PATH_MAX);
|
|
|
|
if(!tempsym) MALLOC(tempsym, PATH_MAX);
|
|
|
|
for(k = dbpkg->files; k; k = k->next) {
|
|
|
|
snprintf(sym, PATH_MAX, "%s%s", root, (char *)k->data);
|
|
|
|
/* If the last part of the path is '/' then toss it */
|
|
|
|
if(rindex(sym, '/') == sym+strlen(sym)-1) {
|
|
|
|
sym[strlen(sym)-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is that a symlink? */
|
|
|
|
if(!lstat(sym, &buf) && S_ISLNK(buf.st_mode)) {
|
|
|
|
memset(symlink, 0, PATH_MAX);
|
|
|
|
readlink(sym, symlink, PATH_MAX);
|
|
|
|
/* It's not an absolute symlink, make it one */
|
|
|
|
if(symlink[0] != '/') {
|
|
|
|
strncpy(tempsym, symlink, PATH_MAX);
|
|
|
|
snprintf(symlink, PATH_MAX, "%s/%s", dirname(sym), tempsym);
|
|
|
|
}
|
|
|
|
/* Does the symlink point to a part of the conflicting path? */
|
|
|
|
if(strstr(path, symlink) == path)
|
|
|
|
{
|
|
|
|
/* Replace one with the other and check if it really did exist in the old package */
|
|
|
|
snprintf(tempsym, PATH_MAX, "%s%s", symlink, path+strlen(symlink));
|
|
|
|
if(!strncmp(tempsym, path, PATH_MAX)) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check if the conflicting file has been moved to another package/target */
|
|
|
|
if(!ok) {
|
|
|
|
/* Look at all the targets */
|
|
|
|
for(k = targets; k && !ok; k = k->next) {
|
|
|
|
pkginfo_t *p1 = (pkginfo_t*)k->data;
|
|
|
|
/* As long as they're not the current package */
|
|
|
|
if(strcmp(p1->name, p->name)) {
|
|
|
|
pkginfo_t *dbpkg2 = NULL;
|
|
|
|
dbpkg2 = db_scan(db, p1->name, INFRQ_DESC | INFRQ_FILES);
|
|
|
|
/* If it used to exist in there, but doesn't anymore */
|
2004-07-22 17:11:19 +00:00
|
|
|
if(dbpkg2 && !is_in(filestr, p1->files) && is_in(filestr, dbpkg2->files)) {
|
2004-07-17 01:55:28 +00:00
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
FREE(dbpkg2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-08-09 18:03:48 +00:00
|
|
|
if(!ok) {
|
|
|
|
MALLOC(str, 512);
|
|
|
|
snprintf(str, 512, "%s: exists in filesystem", path);
|
|
|
|
conflicts = list_add(conflicts, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-29 00:58:58 +00:00
|
|
|
FREEPKG(dbpkg);
|
2002-08-09 18:03:48 +00:00
|
|
|
}
|
|
|
|
|
2004-07-17 01:55:28 +00:00
|
|
|
if(sym) FREE(sym);
|
|
|
|
if(symlink) FREE(symlink);
|
|
|
|
if(tempsym) FREE(tempsym);
|
2002-08-09 18:03:48 +00:00
|
|
|
return(conflicts);
|
|
|
|
}
|
|
|
|
|
2003-09-15 04:58:02 +00:00
|
|
|
/* return a PMList of packages in "db" that provide "package" */
|
2003-09-03 02:09:29 +00:00
|
|
|
PMList *whatprovides(pacdb_t *db, char* package)
|
|
|
|
{
|
|
|
|
PMList *pkgs, *i = NULL;
|
|
|
|
pkginfo_t *info;
|
|
|
|
|
|
|
|
rewinddir(db->dir);
|
|
|
|
while((info = db_scan(db, NULL, INFRQ_DESC | INFRQ_DEPENDS)) != NULL) {
|
|
|
|
if(is_in(package, info->provides)) {
|
|
|
|
i = list_add(i, strdup(info->name));
|
|
|
|
}
|
2003-09-29 00:58:58 +00:00
|
|
|
FREEPKG(info);
|
2003-09-03 02:09:29 +00:00
|
|
|
}
|
|
|
|
pkgs = list_sort(i);
|
2003-09-29 00:58:58 +00:00
|
|
|
FREELIST(i);
|
2003-09-03 02:09:29 +00:00
|
|
|
|
|
|
|
return(pkgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return a list of all groups present in *db
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
PMList *find_groups(pacdb_t *db)
|
|
|
|
{
|
|
|
|
PMList *groups, *i = NULL;
|
|
|
|
PMList *lp = NULL;
|
|
|
|
pkginfo_t *info;
|
|
|
|
|
|
|
|
rewinddir(db->dir);
|
|
|
|
while((info = db_scan(db, NULL, INFRQ_DESC)) != NULL) {
|
|
|
|
for(lp = info->groups; lp; lp = lp->next) {
|
|
|
|
if(!is_in((char*)lp->data, i)) {
|
|
|
|
i = list_add(i, strdup((char*)lp->data));
|
|
|
|
}
|
|
|
|
}
|
2003-09-29 00:58:58 +00:00
|
|
|
FREEPKG(info);
|
2003-09-03 02:09:29 +00:00
|
|
|
}
|
|
|
|
groups = list_sort(i);
|
2003-09-29 00:58:58 +00:00
|
|
|
FREELIST(i);
|
2003-09-03 02:09:29 +00:00
|
|
|
|
|
|
|
return(groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return a list of all members of the specified group
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
PMList *pkg_ingroup(pacdb_t *db, char *group)
|
|
|
|
{
|
|
|
|
PMList *pkg, *i = NULL;
|
|
|
|
PMList *lp = NULL;
|
|
|
|
pkginfo_t *info;
|
|
|
|
|
|
|
|
rewinddir(db->dir);
|
|
|
|
while((info = db_scan(db, NULL, INFRQ_DESC)) != NULL) {
|
|
|
|
for(lp = info->groups; lp; lp = lp->next) {
|
|
|
|
if(!strcmp((char*)lp->data, group)) {
|
|
|
|
i = list_add(i, strdup(info->name));
|
|
|
|
}
|
|
|
|
}
|
2003-09-29 00:58:58 +00:00
|
|
|
FREEPKG(info);
|
2003-09-03 02:09:29 +00:00
|
|
|
}
|
|
|
|
pkg = list_sort(i);
|
2003-09-29 00:58:58 +00:00
|
|
|
FREELIST(i);
|
2003-09-03 02:09:29 +00:00
|
|
|
|
|
|
|
return(pkg);
|
|
|
|
}
|
|
|
|
|
2002-08-09 18:03:48 +00:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|