2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* db.c
|
|
|
|
*
|
2006-01-02 14:55:35 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2005-03-14 20:51:43 -05: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 <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2005-10-06 03:39:44 -04:00
|
|
|
#ifdef CYGWIN
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#endif
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
2005-03-28 03:21:17 -05:00
|
|
|
#include "log.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "util.h"
|
2006-02-20 15:41:40 -05:00
|
|
|
#include "error.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "group.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "db.h"
|
2005-03-28 03:21:17 -05:00
|
|
|
#include "alpm.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
/* Open a database and return a pmdb_t handle */
|
2006-02-17 17:35:26 -05:00
|
|
|
pmdb_t *_alpm_db_open(char *dbpath, char *treename, int mode)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
pmdb_t *db;
|
|
|
|
|
2006-02-16 16:02:39 -05:00
|
|
|
if(dbpath == NULL || treename == NULL) {
|
2005-03-14 20:51:43 -05:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2005-05-03 13:41:19 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "opening database '%s'", treename);
|
|
|
|
|
2006-02-20 15:41:40 -05:00
|
|
|
db = (pmdb_t *)malloc(sizeof(pmdb_t));
|
|
|
|
if(db == NULL) {
|
|
|
|
RET_ERR(PM_ERR_MEMORY, NULL);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-02-20 15:41:40 -05:00
|
|
|
db->path = (char *)malloc(strlen(dbpath)+strlen(treename)+2);
|
|
|
|
if(db->path == NULL) {
|
|
|
|
RET_ERR(PM_ERR_MEMORY, NULL);
|
|
|
|
}
|
2006-02-16 16:02:39 -05:00
|
|
|
sprintf(db->path, "%s/%s", dbpath, treename);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
db->dir = opendir(db->path);
|
|
|
|
if(db->dir == NULL) {
|
2006-02-15 17:51:28 -05:00
|
|
|
if(mode & DB_O_CREATE) {
|
|
|
|
_alpm_log(PM_LOG_WARNING, "could not open database '%s' -- try creating it", treename);
|
2006-02-22 15:49:33 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "creating database '%s'", treename);
|
2006-02-16 16:02:39 -05:00
|
|
|
if(mkdir(db->path, 0755) == 0) {
|
2006-02-15 17:51:28 -05:00
|
|
|
db->dir = opendir(db->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!(mode & DB_O_CREATE) || db->dir == NULL) {
|
|
|
|
FREE(db->path);
|
|
|
|
FREE(db);
|
|
|
|
return(NULL);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2005-03-29 12:18:59 -05:00
|
|
|
STRNCPY(db->treename, treename, DB_TREENAME_LEN);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
db->pkgcache = NULL;
|
|
|
|
db->grpcache = NULL;
|
|
|
|
|
|
|
|
return(db);
|
|
|
|
}
|
|
|
|
|
2006-02-22 15:49:33 -05:00
|
|
|
void _alpm_db_close(void *data)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-02-22 15:49:33 -05:00
|
|
|
pmdb_t *db = data;
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
if(db == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-03 13:41:19 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "closing database '%s'", db->treename);
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
if(db->dir) {
|
|
|
|
closedir(db->dir);
|
|
|
|
db->dir = NULL;
|
|
|
|
}
|
|
|
|
FREE(db->path);
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
_alpm_db_free_pkgcache(db);
|
|
|
|
_alpm_db_free_grpcache(db);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
free(db);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
void _alpm_db_rewind(pmdb_t *db)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
if(db == NULL || db->dir == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rewinddir(db->dir);
|
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
pmpkg_t *_alpm_db_scan(pmdb_t *db, char *target, unsigned int inforeq)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
struct dirent *ent = NULL;
|
2005-03-16 16:49:18 -05:00
|
|
|
struct stat sbuf;
|
|
|
|
char path[PATH_MAX];
|
2005-10-08 17:36:47 -04:00
|
|
|
char name[PKG_FULLNAME_LEN];
|
2005-03-14 20:51:43 -05:00
|
|
|
char *ptr = NULL;
|
2005-05-02 03:50:51 -04:00
|
|
|
int found = 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
pmpkg_t *pkg;
|
|
|
|
|
|
|
|
if(db == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(target != NULL) {
|
|
|
|
/* search for a specific package (by name only) */
|
2006-02-17 17:35:26 -05:00
|
|
|
_alpm_db_rewind(db);
|
2005-03-14 20:51:43 -05:00
|
|
|
while(!found && (ent = readdir(db->dir)) != NULL) {
|
|
|
|
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-08 17:36:47 -04:00
|
|
|
STRNCPY(name, ent->d_name, PKG_FULLNAME_LEN);
|
2005-03-16 16:49:18 -05:00
|
|
|
/* stat the entry, make sure it's a directory */
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s", db->path, name);
|
|
|
|
if(stat(path, &sbuf) || !S_ISDIR(sbuf.st_mode)) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* normal iteration */
|
2005-03-16 16:49:18 -05:00
|
|
|
int isdir = 0;
|
|
|
|
while(!isdir) {
|
2005-03-14 20:51:43 -05:00
|
|
|
ent = readdir(db->dir);
|
|
|
|
if(ent == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2005-03-16 16:49:18 -05:00
|
|
|
/* stat the entry, make sure it's a directory */
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s", db->path, ent->d_name);
|
|
|
|
if(!stat(path, &sbuf) && S_ISDIR(sbuf.st_mode)) {
|
|
|
|
isdir = 1;
|
|
|
|
}
|
|
|
|
if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
|
|
|
|
isdir = 0;
|
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
pkg = _alpm_pkg_new(NULL, NULL);
|
2005-03-14 20:51:43 -05:00
|
|
|
if(pkg == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2006-02-17 17:35:26 -05:00
|
|
|
if(_alpm_db_read(db, ent->d_name, inforeq, pkg) == -1) {
|
2005-03-14 20:51:43 -05:00
|
|
|
FREEPKG(pkg);
|
|
|
|
}
|
|
|
|
|
2005-05-02 03:50:51 -04:00
|
|
|
return(pkg);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
int _alpm_db_read(pmdb_t *db, char *name, unsigned int inforeq, pmpkg_t *info)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
struct stat buf;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char line[512];
|
|
|
|
|
|
|
|
if(db == NULL || name == NULL || info == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s", db->path, name);
|
|
|
|
if(stat(path, &buf)) {
|
|
|
|
/* directory doesn't exist or can't be opened */
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2005-10-08 17:32:14 -04:00
|
|
|
if(info->name[0] == 0) {
|
2006-02-17 17:35:26 -05:00
|
|
|
if(_alpm_pkg_splitname(name, info->name, info->version) == -1) {
|
2005-10-08 17:32:14 -04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* DESC */
|
|
|
|
if(inforeq & INFRQ_DESC) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s/desc", db->path, name);
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
2005-03-28 03:21:17 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, "%s (%s)", path, strerror(errno));
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
while(!feof(fp)) {
|
|
|
|
if(fgets(line, 256, fp) == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_alpm_strtrim(line);
|
2005-10-08 17:32:14 -04:00
|
|
|
if(!strcmp(line, "%DESC%")) {
|
2005-03-14 20:51:43 -05:00
|
|
|
if(fgets(info->desc, sizeof(info->desc), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->desc);
|
|
|
|
} else if(!strcmp(line, "%GROUPS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->groups = _alpm_list_add(info->groups, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%URL%")) {
|
|
|
|
if(fgets(info->url, sizeof(info->url), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->url);
|
|
|
|
} else if(!strcmp(line, "%LICENSE%")) {
|
2005-08-20 01:54:45 -04:00
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->license = _alpm_list_add(info->license, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%ARCH%")) {
|
|
|
|
if(fgets(info->arch, sizeof(info->arch), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->arch);
|
|
|
|
} else if(!strcmp(line, "%BUILDDATE%")) {
|
|
|
|
if(fgets(info->builddate, sizeof(info->builddate), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->builddate);
|
|
|
|
} else if(!strcmp(line, "%INSTALLDATE%")) {
|
|
|
|
if(fgets(info->installdate, sizeof(info->installdate), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->installdate);
|
|
|
|
} else if(!strcmp(line, "%PACKAGER%")) {
|
|
|
|
if(fgets(info->packager, sizeof(info->packager), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(info->packager);
|
|
|
|
} else if(!strcmp(line, "%REASON%")) {
|
|
|
|
char tmp[32];
|
|
|
|
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(tmp);
|
|
|
|
info->reason = atol(tmp);
|
2005-04-24 07:24:18 -04:00
|
|
|
} else if(!strcmp(line, "%SIZE%") || !strcmp(line, "%CSIZE%")) {
|
2005-03-14 20:51:43 -05:00
|
|
|
/* NOTE: the CSIZE and SIZE fields both share the "size" field
|
|
|
|
* in the pkginfo_t struct. This can be done b/c CSIZE
|
|
|
|
* is currently only used in sync databases, and SIZE is
|
|
|
|
* only used in local databases.
|
|
|
|
*/
|
|
|
|
char tmp[32];
|
|
|
|
if(fgets(tmp, sizeof(tmp), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
_alpm_strtrim(tmp);
|
|
|
|
info->size = atol(tmp);
|
|
|
|
} else if(!strcmp(line, "%MD5SUM%")) {
|
|
|
|
/* MD5SUM tag only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
if(fgets(info->md5sum, sizeof(info->md5sum), fp) == NULL) {
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2005-12-26 01:40:29 -05:00
|
|
|
/* XXX: these are only here as backwards-compatibility for pacman 2.x
|
|
|
|
* sync repos.... in pacman3, they have been moved to DEPENDS.
|
|
|
|
* Remove this when we move to pacman3 repos.
|
|
|
|
*/
|
|
|
|
} else if(!strcmp(line, "%REPLACES%")) {
|
|
|
|
/* the REPLACES tag is special -- it only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->replaces = _alpm_list_add(info->replaces, strdup(line));
|
2005-12-26 01:40:29 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%FORCE%")) {
|
|
|
|
/* FORCE tag only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
info->force = 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-27 14:08:35 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FILES */
|
|
|
|
if(inforeq & INFRQ_FILES) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s/files", db->path, name);
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
2005-03-28 03:21:17 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, "%s (%s)", path, strerror(errno));
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
while(fgets(line, 256, fp)) {
|
|
|
|
_alpm_strtrim(line);
|
|
|
|
if(!strcmp(line, "%FILES%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->files = _alpm_list_add(info->files, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%BACKUP%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->backup = _alpm_list_add(info->backup, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-27 14:08:35 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DEPENDS */
|
|
|
|
if(inforeq & INFRQ_DEPENDS) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s/depends", db->path, name);
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
if(fp == NULL) {
|
2005-03-28 03:21:17 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, "%s (%s)", path, strerror(errno));
|
2006-01-27 14:08:35 -05:00
|
|
|
goto error;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
while(!feof(fp)) {
|
|
|
|
fgets(line, 255, fp);
|
|
|
|
_alpm_strtrim(line);
|
|
|
|
if(!strcmp(line, "%DEPENDS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->depends = _alpm_list_add(info->depends, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%REQUIREDBY%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->requiredby = _alpm_list_add(info->requiredby, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%CONFLICTS%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->conflicts = _alpm_list_add(info->conflicts, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%PROVIDES%")) {
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->provides = _alpm_list_add(info->provides, strdup(line));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2005-10-08 16:43:25 -04:00
|
|
|
} else if(!strcmp(line, "%REPLACES%")) {
|
|
|
|
/* the REPLACES tag is special -- it only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
|
2006-02-17 17:35:26 -05:00
|
|
|
info->replaces = _alpm_list_add(info->replaces, strdup(line));
|
2005-10-08 16:43:25 -04:00
|
|
|
}
|
|
|
|
} else if(!strcmp(line, "%FORCE%")) {
|
|
|
|
/* FORCE tag only appears in sync repositories,
|
|
|
|
* not the local one. */
|
|
|
|
info->force = 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-27 14:08:35 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* INSTALL */
|
|
|
|
if(inforeq & INFRQ_SCRIPLET) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s/install", db->path, name);
|
|
|
|
if(!stat(path, &buf)) {
|
|
|
|
info->scriptlet = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* internal */
|
|
|
|
info->infolevel |= inforeq;
|
|
|
|
|
|
|
|
return(0);
|
2006-01-27 14:08:35 -05:00
|
|
|
|
|
|
|
error:
|
|
|
|
if(fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return(-1);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
int _alpm_db_write(pmdb_t *db, pmpkg_t *info, unsigned int inforeq)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
char topdir[PATH_MAX];
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
mode_t oldmask;
|
|
|
|
PMList *lp = NULL;
|
2006-01-27 14:12:15 -05:00
|
|
|
int retval = 0;
|
2006-02-16 16:02:39 -05:00
|
|
|
int local = 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
if(db == NULL || info == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2005-03-28 03:21:17 -05:00
|
|
|
snprintf(topdir, PATH_MAX, "%s/%s-%s", db->path, info->name, info->version);
|
2005-03-14 20:51:43 -05:00
|
|
|
oldmask = umask(0000);
|
|
|
|
mkdir(topdir, 0755);
|
|
|
|
/* make sure we have a sane umask */
|
|
|
|
umask(0022);
|
|
|
|
|
2006-02-16 16:02:39 -05:00
|
|
|
if(strcmp(db->treename, "local") == 0) {
|
|
|
|
local = 1;
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* DESC */
|
|
|
|
if(inforeq & INFRQ_DESC) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/desc", topdir);
|
|
|
|
if((fp = fopen(path, "w")) == NULL) {
|
2005-03-29 15:58:43 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, "db_write: could not open file %s/desc", db->treename);
|
2006-01-27 14:12:15 -05:00
|
|
|
retval = 1;
|
|
|
|
goto cleanup;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
fprintf(fp, "%%NAME%%\n%s\n\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%%VERSION%%\n%s\n\n", info->name, info->version);
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->desc[0]) {
|
|
|
|
fprintf(fp, "%%DESC%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->desc);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->groups) {
|
|
|
|
fputs("%GROUPS%\n", fp);
|
|
|
|
for(lp = info->groups; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
2006-02-16 16:02:39 -05:00
|
|
|
if(local) {
|
|
|
|
if(info->url[0]) {
|
|
|
|
fprintf(fp, "%%URL%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->url);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->license) {
|
|
|
|
fputs("%LICENSE%\n", fp);
|
|
|
|
for(lp = info->license; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
|
|
|
if(info->arch[0]) {
|
|
|
|
fprintf(fp, "%%ARCH%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->arch);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->builddate[0]) {
|
|
|
|
fprintf(fp, "%%BUILDDATE%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->builddate);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->installdate[0]) {
|
|
|
|
fprintf(fp, "%%INSTALLDATE%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->installdate);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->packager[0]) {
|
|
|
|
fprintf(fp, "%%PACKAGER%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->packager);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->size) {
|
|
|
|
fprintf(fp, "%%SIZE%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%ld\n\n", info->size);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->reason) {
|
|
|
|
fprintf(fp, "%%REASON%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%d\n\n", info->reason);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(info->size) {
|
|
|
|
fprintf(fp, "%%CSIZE%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%ld\n\n", info->size);
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
if(info->reason) {
|
|
|
|
fprintf(fp, "%%MD5SUM%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"%s\n\n", info->md5sum);
|
2006-02-11 02:53:36 -05:00
|
|
|
}
|
2005-08-20 01:54:45 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
fclose(fp);
|
2006-01-27 14:12:15 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FILES */
|
2006-02-17 17:35:26 -05:00
|
|
|
if(local && (inforeq & INFRQ_FILES)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
snprintf(path, PATH_MAX, "%s/files", topdir);
|
|
|
|
if((fp = fopen(path, "w")) == NULL) {
|
2005-03-29 15:58:43 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, "db_write: could not open file %s/files", db->treename);
|
2006-01-27 14:12:15 -05:00
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->files) {
|
|
|
|
fprintf(fp, "%%FILES%%\n");
|
|
|
|
for(lp = info->files; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->backup) {
|
|
|
|
fprintf(fp, "%%BACKUP%%\n");
|
|
|
|
for(lp = info->backup; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-27 14:12:15 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DEPENDS */
|
|
|
|
if(inforeq & INFRQ_DEPENDS) {
|
|
|
|
snprintf(path, PATH_MAX, "%s/depends", topdir);
|
|
|
|
if((fp = fopen(path, "w")) == NULL) {
|
2005-10-06 03:24:33 -04:00
|
|
|
_alpm_log(PM_LOG_ERROR, "db_write: could not open file %s/depends", db->treename);
|
2006-01-27 14:12:15 -05:00
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->depends) {
|
|
|
|
fputs("%DEPENDS%\n", fp);
|
|
|
|
for(lp = info->depends; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-16 16:02:39 -05:00
|
|
|
if(local && info->requiredby) {
|
2006-02-11 02:53:36 -05:00
|
|
|
fputs("%REQUIREDBY%\n", fp);
|
|
|
|
for(lp = info->requiredby; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->conflicts) {
|
|
|
|
fputs("%CONFLICTS%\n", fp);
|
|
|
|
for(lp = info->conflicts; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-11 02:53:36 -05:00
|
|
|
if(info->provides) {
|
|
|
|
fputs("%PROVIDES%\n", fp);
|
|
|
|
for(lp = info->provides; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-02-16 16:02:39 -05:00
|
|
|
if(!local) {
|
|
|
|
if(info->replaces) {
|
|
|
|
fputs("%REPLACES%\n", fp);
|
|
|
|
for(lp = info->replaces; lp; lp = lp->next) {
|
|
|
|
fprintf(fp, "%s\n", (char *)lp->data);
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
|
|
|
if(info->force) {
|
|
|
|
fprintf(fp, "%%FORCE%%\n"
|
2006-02-20 13:45:26 -05:00
|
|
|
"\n");
|
2006-02-16 16:02:39 -05:00
|
|
|
}
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
fclose(fp);
|
2006-01-27 14:12:15 -05:00
|
|
|
fp = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* INSTALL */
|
|
|
|
/* nothing needed here (script is automatically extracted) */
|
|
|
|
|
2006-01-27 14:12:15 -05:00
|
|
|
cleanup:
|
2005-03-14 20:51:43 -05:00
|
|
|
umask(oldmask);
|
|
|
|
|
2006-01-27 14:12:15 -05:00
|
|
|
if(fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2005-03-28 03:21:17 -05:00
|
|
|
|
2006-01-27 14:12:15 -05:00
|
|
|
return(retval);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-02-17 17:35:26 -05:00
|
|
|
int _alpm_db_remove(pmdb_t *db, pmpkg_t *info)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
char topdir[PATH_MAX];
|
|
|
|
char file[PATH_MAX];
|
|
|
|
|
|
|
|
if(db == NULL || info == NULL) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(topdir, PATH_MAX, "%s/%s-%s", db->path, info->name, info->version);
|
|
|
|
|
|
|
|
/* DESC */
|
|
|
|
snprintf(file, PATH_MAX, "%s/desc", topdir);
|
|
|
|
unlink(file);
|
|
|
|
/* FILES */
|
|
|
|
snprintf(file, PATH_MAX, "%s/files", topdir);
|
|
|
|
unlink(file);
|
|
|
|
/* DEPENDS */
|
|
|
|
snprintf(file, PATH_MAX, "%s/depends", topdir);
|
|
|
|
unlink(file);
|
|
|
|
/* INSTALL */
|
|
|
|
snprintf(file, PATH_MAX, "%s/install", topdir);
|
|
|
|
unlink(file);
|
2005-03-28 03:21:17 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* Package directory */
|
|
|
|
if(rmdir(topdir) == -1) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|