ndk-busybox/archival/dpkg.c

835 lines
20 KiB
C
Raw Normal View History

2001-02-10 02:05:24 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <sys/types.h>
#include <sys/stat.h>
2001-02-10 14:26:48 +00:00
2001-02-10 02:05:24 +00:00
#include "busybox.h"
//#define PACKAGE "udpkg"
//#define VERSION "0.1"
/*
* Should we do full dependency checking?
*/
#define DODEPENDS 1
/*
* Should we do debugging?
*/
#define DODEBUG 0
#ifdef DODEBUG
#define SYSTEM(x) do_system(x)
#define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
#else
#define SYSTEM(x) system(x)
#define DPRINTF(fmt,args...) /* nothing */
#endif
#define BUFSIZE 4096
#define DEPENDSMAX 64 /* maximum number of depends we can handle */
2001-02-11 01:40:23 +00:00
static const char statusfile[] = "/var/lib/dpkg/status.udeb";
static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
static const char rm_dpkgcidir[] = "rm -rf /var/lib/dpkg/tmp.ci/";
static const char infodir[] = "/var/lib/dpkg/info/";
static const char udpkg_quiet[] = "UDPKG_QUIET";
2001-02-10 02:05:24 +00:00
//static const int status_wantstart = 0;
//static const int status_wantunknown = (1 << 0);
static const int status_wantinstall = (1 << 1);
//static const int status_wanthold = (1 << 2);
//static const int status_wantdeinstall = (1 << 3);
//static const int status_wantpurge = (1 << 4);
static const int status_wantmask = 31;
//static const int status_flagstart = 5;
static const int status_flagok = (1 << 5); /* 32 */
//static const int status_flagreinstreq = (1 << 6);
//static const int status_flaghold = (1 << 7);
//static const int status_flagholdreinstreq = (1 << 8);
static const int status_flagmask = 480;
//static const int status_statusstart = 9;
//static const int status_statusnoninstalled = (1 << 9); /* 512 */
static const int status_statusunpacked = (1 << 10);
static const int status_statushalfconfigured = (1 << 11);
static const int status_statusinstalled = (1 << 12);
static const int status_statushalfinstalled = (1 << 13);
//static const int status_statusconfigfiles = (1 << 14);
//static const int status_statuspostinstfailed = (1 << 15);
//static const int status_statusremovalfailed = (1 << 16);
static const int status_statusmask = 130560; /* i assume status_statusinstalled is supposed to be included */
static const char *statuswords[][10] = {
{ (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
{ (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
{ (char *) 9, "not-installed", "unpacked", "half-configured",
"installed", "half-installed", "config-files",
"post-inst-failed", "removal-failed", 0 }
};
const int color_white = 0;
const int color_grey = 1;
const int color_black = 2;
2001-02-10 02:05:24 +00:00
/* data structures */
2001-02-10 14:26:48 +00:00
typedef struct package_s {
2001-02-10 02:05:24 +00:00
char *file;
char *package;
char *version;
char *depends;
char *provides;
char *description;
int installer_menu_item;
unsigned long status;
char color; /* for topo-sort */
2001-02-10 14:26:48 +00:00
struct package_s *requiredfor[DEPENDSMAX];
2001-02-10 02:05:24 +00:00
unsigned short requiredcount;
2001-02-10 14:26:48 +00:00
struct package_s *next;
} package_t;
2001-02-10 02:05:24 +00:00
2001-02-11 01:40:23 +00:00
#ifdef DODEBUG
static int do_system(const char *cmd)
{
DPRINTF("cmd is %s\n", cmd);
return system(cmd);
}
#else
#define do_system(cmd) system(cmd)
#endif
2001-02-10 14:26:48 +00:00
static int package_compare(const void *p1, const void *p2)
{
return strcmp(((package_t *)p1)->package,
((package_t *)p2)->package);
}
2001-02-10 02:05:24 +00:00
#ifdef DODEPENDS
#include <ctype.h>
static char **depends_split(const char *dependsstr)
{
static char *dependsvec[DEPENDSMAX];
char *p;
int i = 0;
dependsvec[0] = 0;
2001-02-11 01:40:23 +00:00
if (dependsstr == 0) {
goto end;
}
2001-02-10 02:05:24 +00:00
2001-02-11 01:40:23 +00:00
p = strdup(dependsstr);
while (*p != 0 && *p != '\n') {
if (*p != ' ') {
if (*p == ',') {
*p = 0;
dependsvec[++i] = 0;
} else {
2001-02-11 01:40:23 +00:00
if (dependsvec[i] == 0) {
dependsvec[i] = p;
}
}
2001-02-11 01:40:23 +00:00
} else {
*p = 0; /* eat the space... */
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
p++;
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
*p = 0;
end:
2001-02-10 02:05:24 +00:00
dependsvec[i+1] = 0;
return dependsvec;
}
2001-02-11 01:40:23 +00:00
/* Topological sort algorithm:
* ordered is the output list, pkgs is the dependency graph, pkg is
* the current node
*
* recursively add all the adjacent nodes to the ordered list, marking
* each one as visited along the way
*
* yes, this algorithm looks a bit odd when all the params have the
* same type :-)
*/
2001-02-10 14:26:48 +00:00
static void depends_sort_visit(package_t **ordered, package_t *pkgs,
package_t *pkg)
2001-02-10 02:05:24 +00:00
{
unsigned short i;
/* mark node as processing */
pkg->color = color_grey;
2001-02-10 02:05:24 +00:00
/* visit each not-yet-visited node */
for (i = 0; i < pkg->requiredcount; i++)
if (pkg->requiredfor[i]->color == color_white)
2001-02-10 02:05:24 +00:00
depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
#if 0
/* add it to the list */
newnode = (struct package_t *)malloc(sizeof(struct package_t));
/* make a shallow copy */
*newnode = *pkg;
newnode->next = *ordered;
*ordered = newnode;
#endif
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
pkg->next = *ordered;
*ordered = pkg;
/* mark node as done */
pkg->color = color_black;
2001-02-10 02:05:24 +00:00
}
2001-02-10 14:26:48 +00:00
static package_t *depends_sort(package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
/* TODO: it needs to break cycles in the to-be-installed package
* graph... */
2001-02-10 14:26:48 +00:00
package_t *ordered = NULL;
package_t *pkg;
2001-02-10 02:05:24 +00:00
2001-02-11 01:40:23 +00:00
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
pkg->color = color_white;
2001-02-11 01:40:23 +00:00
}
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
if (pkg->color == color_white) {
2001-02-10 02:05:24 +00:00
depends_sort_visit(&ordered, pkgs, pkg);
2001-02-11 01:40:23 +00:00
}
}
2001-02-10 02:05:24 +00:00
/* Leaks the old list... return the new one... */
return ordered;
}
/* resolve package dependencies --
* for each package in the list of packages to be installed, we parse its
* dependency info to determine if the dependent packages are either
* already installed, or are scheduled to be installed. If both tests fail
* than bail.
*
* The algorithm here is O(n^2*m) where n = number of packages to be
* installed and m is the # of dependencies per package. Not a terribly
* efficient algorithm, but given that at any one time you are unlikely
* to install a very large number of packages it doesn't really matter
*/
2001-02-10 14:26:48 +00:00
static package_t *depends_resolve(package_t *pkgs, void *status)
2001-02-10 02:05:24 +00:00
{
2001-02-10 14:26:48 +00:00
package_t *pkg, *chk;
package_t dependpkg;
2001-02-10 02:05:24 +00:00
char **dependsvec;
int i;
void *found;
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
2001-02-10 02:05:24 +00:00
dependsvec = depends_split(pkg->depends);
i = 0;
while (dependsvec[i] != 0) {
2001-02-10 02:05:24 +00:00
/* Check for dependencies; first look for installed packages */
dependpkg.package = dependsvec[i];
if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
2001-02-10 14:26:48 +00:00
((chk = *(package_t **)found) &&
(chk->status & (status_flagok | status_statusinstalled)) !=
2001-02-11 01:40:23 +00:00
(status_flagok | status_statusinstalled))) {
2001-02-10 02:05:24 +00:00
/* if it fails, we look through the list of packages we are going to
* install */
for (chk = pkgs; chk != 0; chk = chk->next) {
2001-02-11 01:40:23 +00:00
if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
if (chk->requiredcount >= DEPENDSMAX) {
2001-02-11 01:40:23 +00:00
fprintf(stderr, "Too many dependencies for %s\n", chk->package);
2001-02-10 02:05:24 +00:00
return 0;
}
if (chk != pkg) {
2001-02-10 02:05:24 +00:00
chk->requiredfor[chk->requiredcount++] = pkg;
}
2001-02-10 02:05:24 +00:00
break;
}
}
if (chk == 0) {
2001-02-10 02:05:24 +00:00
fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
return 0;
}
}
i++;
}
}
return depends_sort(pkgs);
}
#endif
/* Status file handling routines
*
* This is a fairly minimalistic implementation. there are two main functions
* that are supported:
*
* 1) reading the entire status file:
* the status file is read into memory as a binary-tree, with just the
* package and status info preserved
*
* 2) merging the status file
* control info from (new) packages is merged into the status file,
* replacing any pre-existing entries. when a merge happens, status info
* read using the status_read function is written back to the status file
*/
static unsigned long status_parse(const char *line)
{
char *p;
int i, j;
unsigned long l = 0;
2001-02-11 01:40:23 +00:00
for (i = 0; i < 3; i++) {
if ((p = strchr(line, ' ')) != NULL) {
*p = 0;
}
2001-02-10 02:05:24 +00:00
j = 1;
while (statuswords[i][j] != 0) {
if (strcmp(line, statuswords[i][j]) == 0) {
2001-02-10 02:05:24 +00:00
l |= (1 << ((int)statuswords[i][0] + j - 1));
break;
}
j++;
}
/* parse error */
if (statuswords[i][j] == 0) {
return 0;
}
2001-02-10 02:05:24 +00:00
line = p+1;
}
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return l;
}
static const char *status_print(unsigned long flags)
{
/* this function returns a static buffer... */
static char buf[256];
int i, j;
buf[0] = 0;
for (i = 0; i < 3; i++) {
2001-02-10 02:05:24 +00:00
j = 1;
while (statuswords[i][j] != 0) {
if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0) {
2001-02-10 02:05:24 +00:00
strcat(buf, statuswords[i][j]);
if (i < 2) strcat(buf, " ");
break;
}
j++;
}
if (statuswords[i][j] == 0) {
2001-02-10 02:05:24 +00:00
fprintf(stderr, "corrupted status flag!!\n");
return NULL;
}
}
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return buf;
}
/*
* Read a control file (or a stanza of a status file) and parse it,
* filling parsed fields into the package structure
*/
static void control_read(FILE *file, package_t *p)
2001-02-10 02:05:24 +00:00
{
char *line;
while ((line = get_line_from_file(file)) != NULL) {
line[strlen(line)] = 0;
2001-02-11 01:40:23 +00:00
if (strlen(line) == 0) {
break;
} else
if (strstr(line, "Package: ") == line) {
p->package = strdup(line + 9);
} else
if (strstr(line, "Status: ") == line) {
p->status = status_parse(line + 8);
} else
if (strstr(line, "Depends: ") == line) {
p->depends = strdup(line + 9);
} else
if (strstr(line, "Provides: ") == line) {
p->provides = strdup(line + 10);
} else
if (strstr(line, "Description: ") == line) {
p->description = strdup(line + 13);
2001-02-10 02:05:24 +00:00
/* This is specific to the Debian Installer. Ifdef? */
} else
if (strstr(line, "installer-menu-item: ") == line) {
p->installer_menu_item = atoi(line + 21);
2001-02-10 02:05:24 +00:00
}
/* TODO: localized descriptions */
}
free(line);
2001-02-10 02:05:24 +00:00
}
2001-02-10 14:26:48 +00:00
static void *status_read(void)
2001-02-10 02:05:24 +00:00
{
FILE *f;
void *status = 0;
2001-02-10 14:26:48 +00:00
package_t *m = 0, *p = 0, *t = 0;
2001-02-10 02:05:24 +00:00
2001-02-11 01:40:23 +00:00
if ((f = fopen(statusfile, "r")) == NULL) {
perror(statusfile);
2001-02-10 02:05:24 +00:00
return 0;
}
2001-02-11 01:40:23 +00:00
if (getenv(udpkg_quiet) == NULL) {
2001-02-10 02:05:24 +00:00
printf("(Reading database...)\n");
}
2001-02-11 01:40:23 +00:00
while (!feof(f)) {
m = (package_t *)xmalloc(sizeof(package_t));
2001-02-10 14:26:48 +00:00
memset(m, 0, sizeof(package_t));
2001-02-10 02:05:24 +00:00
control_read(f, m);
if (m->package) {
2001-02-10 02:05:24 +00:00
/*
* If there is an item in the tree by this name,
* it must be a virtual package; insert real
* package in preference.
*/
tdelete(m, &status, package_compare);
tsearch(m, &status, package_compare);
if (m->provides) {
2001-02-10 02:05:24 +00:00
/*
* A "Provides" triggers the insertion
* of a pseudo package into the status
* binary-tree.
*/
p = (package_t *)xmalloc(sizeof(package_t));
2001-02-10 14:26:48 +00:00
memset(p, 0, sizeof(package_t));
2001-02-10 02:05:24 +00:00
p->package = strdup(m->provides);
2001-02-10 14:26:48 +00:00
t = *(package_t **)tsearch(p, &status, package_compare);
2001-02-11 01:40:23 +00:00
if (t != p) {
2001-02-10 02:05:24 +00:00
free(p->package);
free(p);
}
else {
/*
* Pseudo package status is the
* same as the status of the
* package providing it
* FIXME: (not quite right, if 2
* packages of different statuses
* provide it).
*/
t->status = m->status;
}
}
}
else {
2001-02-10 02:05:24 +00:00
free(m);
}
}
fclose(f);
return status;
}
2001-02-10 14:26:48 +00:00
static int status_merge(void *status, package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
FILE *fin, *fout;
char *line;
2001-02-10 14:26:48 +00:00
package_t *pkg = 0, *statpkg = 0;
package_t locpkg;
2001-02-10 02:05:24 +00:00
int r = 0;
2001-02-11 01:40:23 +00:00
if ((fin = fopen(statusfile, "r")) == NULL) {
perror(statusfile);
2001-02-10 02:05:24 +00:00
return 0;
}
2001-02-11 01:40:23 +00:00
if ((fout = fopen(new_statusfile, "w")) == NULL) {
perror(new_statusfile);
2001-02-10 02:05:24 +00:00
return 0;
}
if (getenv(udpkg_quiet) == NULL) {
2001-02-10 02:05:24 +00:00
printf("(Updating database...)\n");
}
2001-02-11 01:40:23 +00:00
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
line[strlen(line)] = 0; /* trim newline */
2001-02-10 02:05:24 +00:00
/* If we see a package header, find out if it's a package
* that we have processed. if so, we skip that block for
* now (write it at the end).
*
* we also look at packages in the status cache and update
* their status fields
*/
if (strstr(line, "Package: ") == line) {
for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
pkg->package, strlen(line) - 9) != 0;
2001-02-10 02:05:24 +00:00
pkg = pkg->next) ;
locpkg.package = line + 9;
2001-02-10 02:05:24 +00:00
statpkg = tfind(&locpkg, &status, package_compare);
/* note: statpkg should be non-zero, unless the status
* file was changed while we are processing (no locking
* is currently done...
*/
if (statpkg != 0) {
statpkg = *(package_t **)statpkg;
}
2001-02-10 02:05:24 +00:00
}
if (pkg != 0) {
continue;
2001-02-10 02:05:24 +00:00
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s",
status_print(statpkg->status));
}
fputs(line, fout);
2001-02-10 02:05:24 +00:00
fputc('\n', fout);
}
free(line);
2001-02-10 02:05:24 +00:00
// Print out packages we processed.
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
fprintf(fout, "Package: %s\nStatus: %s\n",
pkg->package, status_print(pkg->status));
if (pkg->depends)
fprintf(fout, "Depends: %s\n", pkg->depends);
if (pkg->provides)
fprintf(fout, "Provides: %s\n", pkg->provides);
if (pkg->installer_menu_item)
fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
if (pkg->description)
fprintf(fout, "Description: %s\n", pkg->description);
fputc('\n', fout);
}
fclose(fin);
fclose(fout);
2001-02-11 01:40:23 +00:00
r = rename(statusfile, bak_statusfile);
if (r == 0) {
2001-02-11 01:40:23 +00:00
r = rename(new_statusfile, statusfile);
}
2001-02-10 02:05:24 +00:00
2001-02-11 01:40:23 +00:00
return 0;
2001-02-10 02:05:24 +00:00
}
static int is_file(const char *fn)
{
struct stat statbuf;
if (stat(fn, &statbuf) < 0) {
return 0;
}
2001-02-10 02:05:24 +00:00
return S_ISREG(statbuf.st_mode);
}
2001-02-10 14:26:48 +00:00
static int dpkg_doconfigure(package_t *pkg)
2001-02-10 02:05:24 +00:00
{
int r;
char postinst[1024];
char buf[1024];
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
DPRINTF("Configuring %s\n", pkg->package);
pkg->status &= status_statusmask;
snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
2001-02-11 01:40:23 +00:00
if (is_file(postinst)) {
2001-02-10 02:05:24 +00:00
snprintf(buf, sizeof(buf), "%s configure", postinst);
if ((r = do_system(buf)) != 0) {
2001-02-10 02:05:24 +00:00
fprintf(stderr, "postinst exited with status %d\n", r);
pkg->status |= status_statushalfconfigured;
2001-02-10 02:05:24 +00:00
return 1;
}
}
pkg->status |= status_statusinstalled;
2001-02-10 02:05:24 +00:00
return 0;
}
2001-02-10 14:26:48 +00:00
static int dpkg_dounpack(package_t *pkg)
2001-02-10 02:05:24 +00:00
{
2001-02-11 01:40:23 +00:00
int r = 0, i;
2001-02-10 02:05:24 +00:00
char *cwd, *p;
FILE *infp, *outfp;
char buf[1024], buf2[1024];
char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
2001-02-11 01:40:23 +00:00
"conffiles", "md5sums", "shlibs", "templates" };
2001-02-10 02:05:24 +00:00
DPRINTF("Unpacking %s\n", pkg->package);
cwd = getcwd(0, 0);
chdir("/");
snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
2001-02-11 01:40:23 +00:00
if (SYSTEM(buf) != 0) {
goto end;
}
/* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
snprintf(buf, sizeof(buf), "%s%s/%s",
dpkgcidir, pkg->package, adminscripts[i]);
snprintf(buf2, sizeof(buf), "%s%s.%s",
infodir, pkg->package, adminscripts[i]);
if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0) {
fprintf(stderr, "Cannot copy %s to %s: %s\n",
buf, buf2, strerror(errno));
r = 1;
break;
}
/* ugly hack to create the list file; should
* probably do something more elegant
*
* why oh why does dpkg create the list file
* so oddly...
*/
snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -tf -", pkg->file);
snprintf(buf2, sizeof(buf2), "%s%s.list", infodir, pkg->package);
if ((infp = popen(buf, "r")) == NULL || (outfp = fopen(buf2, "w")) == NULL) {
fprintf(stderr, "Cannot create %s\n", buf2);
r = 1;
break;
}
while (fgets(buf, sizeof(buf), infp) && !feof(infp)) {
p = buf;
if (*p == '.') {
p++;
}
if ((*p == '/') && (*(p + 1) == '\n')) {
*(p + 1) = '.';
*(p + 2) = '\n';
*(p + 3) = 0;
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
if (p[strlen(p) - 2] == '/') {
p[strlen(p) - 2] = '\n';
p[strlen(p) - 1] = 0;
}
fputs(p, outfp);
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
fclose(infp);
fclose(outfp);
}
pkg->status &= status_wantmask;
pkg->status |= status_wantinstall;
pkg->status &= status_flagmask;
pkg->status |= status_flagok;
pkg->status &= status_statusmask;
if (r == 0) {
pkg->status |= status_statusunpacked;
} else {
pkg->status |= status_statushalfinstalled;
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
end:
2001-02-10 02:05:24 +00:00
chdir(cwd);
return r;
}
2001-02-10 14:26:48 +00:00
static int dpkg_doinstall(package_t *pkg)
2001-02-10 02:05:24 +00:00
{
DPRINTF("Installing %s\n", pkg->package);
return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
}
2001-02-10 14:26:48 +00:00
static int dpkg_unpackcontrol(package_t *pkg)
2001-02-10 02:05:24 +00:00
{
int r = 1;
char *cwd = 0;
char *p;
char buf[1024];
FILE *f;
p = strrchr(pkg->file, '/');
if (p) p++; else p = pkg->file;
p = pkg->package = strdup(p);
2001-02-11 01:40:23 +00:00
while (*p != 0 && *p != '_' && *p != '.') {
p++;
}
2001-02-10 02:05:24 +00:00
*p = 0;
cwd = getcwd(0, 0);
2001-02-11 01:40:23 +00:00
snprintf(buf, sizeof(buf), "%s%s", dpkgcidir, pkg->package);
2001-02-10 02:05:24 +00:00
DPRINTF("dir = %s\n", buf);
2001-02-11 01:40:23 +00:00
if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0) {
2001-02-11 01:40:23 +00:00
snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -", pkg->file);
if (SYSTEM(buf) == 0) {
2001-02-10 02:05:24 +00:00
if ((f = fopen("control", "r")) != NULL) {
control_read(f, pkg);
r = 0;
}
}
}
chdir(cwd);
free(cwd);
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return r;
}
2001-02-10 14:26:48 +00:00
static int dpkg_unpack(package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
int r = 0;
2001-02-10 14:26:48 +00:00
package_t *pkg;
2001-02-10 02:05:24 +00:00
void *status = status_read();
2001-02-11 01:40:23 +00:00
if (SYSTEM(rm_dpkgcidir) != 0 ||
mkdir(dpkgcidir, S_IRWXU) != 0) {
2001-02-10 02:05:24 +00:00
perror("mkdir");
return 1;
}
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
2001-02-10 02:05:24 +00:00
dpkg_unpackcontrol(pkg);
r = dpkg_dounpack(pkg);
if (r != 0) break;
}
status_merge(status, pkgs);
2001-02-11 01:40:23 +00:00
SYSTEM(rm_dpkgcidir);
2001-02-10 02:05:24 +00:00
return r;
}
2001-02-10 14:26:48 +00:00
static int dpkg_configure(package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
int r = 0;
void *found;
2001-02-10 14:26:48 +00:00
package_t *pkg;
2001-02-10 02:05:24 +00:00
void *status = status_read();
2001-02-11 01:40:23 +00:00
for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
2001-02-10 02:05:24 +00:00
found = tfind(pkg, &status, package_compare);
2001-02-11 01:40:23 +00:00
if (found == 0) {
2001-02-10 02:05:24 +00:00
fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
r = 1;
2001-02-11 01:40:23 +00:00
}
/* configure the package listed in the status file;
* not pkg, as we have info only for the latter
*/
else {
2001-02-10 14:26:48 +00:00
r = dpkg_doconfigure(*(package_t **)found);
2001-02-10 02:05:24 +00:00
}
}
status_merge(status, 0);
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return r;
}
2001-02-10 14:26:48 +00:00
static int dpkg_install(package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
2001-02-10 14:26:48 +00:00
package_t *p, *ordered = 0;
2001-02-10 02:05:24 +00:00
void *status = status_read();
2001-02-11 01:40:23 +00:00
if (SYSTEM(rm_dpkgcidir) != 0 ||
mkdir(dpkgcidir, S_IRWXU) != 0) {
2001-02-10 02:05:24 +00:00
perror("mkdir");
return 1;
}
/* Stage 1: parse all the control information */
for (p = pkgs; p != 0; p = p->next)
if (dpkg_unpackcontrol(p) != 0) {
2001-02-10 02:05:24 +00:00
perror(p->file);
/* force loop break, and prevents further ops */
pkgs = 0;
}
/* Stage 2: resolve dependencies */
#ifdef DODEPENDS
ordered = depends_resolve(pkgs, status);
#else
ordered = pkgs;
#endif
/* Stage 3: install */
for (p = ordered; p != 0; p = p->next) {
p->status &= status_wantmask;
p->status |= status_wantinstall;
2001-02-10 02:05:24 +00:00
/* for now the flag is always set to ok... this is probably
* not what we want
*/
p->status &= status_flagmask;
p->status |= status_flagok;
2001-02-10 02:05:24 +00:00
if (dpkg_doinstall(p) != 0) {
2001-02-10 02:05:24 +00:00
perror(p->file);
}
}
if (ordered != 0) {
2001-02-10 02:05:24 +00:00
status_merge(status, pkgs);
}
2001-02-11 01:40:23 +00:00
SYSTEM(rm_dpkgcidir);
2001-02-10 02:05:24 +00:00
return 0;
}
2001-02-10 14:26:48 +00:00
static int dpkg_remove(package_t *pkgs)
2001-02-10 02:05:24 +00:00
{
2001-02-10 14:26:48 +00:00
package_t *p;
2001-02-10 02:05:24 +00:00
void *status = status_read();
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
for (p = pkgs; p != 0; p = p->next)
{
}
status_merge(status, 0);
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return 0;
}
2001-02-10 14:26:48 +00:00
extern int dpkg_main(int argc, char **argv)
2001-02-10 02:05:24 +00:00
{
char opt = 0;
char *s;
2001-02-10 14:26:48 +00:00
package_t *p, *packages = NULL;
2001-02-10 02:05:24 +00:00
char *cwd = getcwd(0, 0);
2001-02-11 01:40:23 +00:00
while (*++argv) {
2001-02-10 02:05:24 +00:00
if (**argv == '-') {
/* Nasty little hack to "parse" long options. */
s = *argv;
2001-02-11 01:40:23 +00:00
while (*s == '-') {
2001-02-10 02:05:24 +00:00
s++;
2001-02-11 01:40:23 +00:00
}
2001-02-10 02:05:24 +00:00
opt=s[0];
} else {
p = (package_t *)xmalloc(sizeof(package_t));
2001-02-10 14:26:48 +00:00
memset(p, 0, sizeof(package_t));
2001-02-11 01:40:23 +00:00
if (**argv == '/') {
2001-02-10 02:05:24 +00:00
p->file = *argv;
} else
if (opt != 'c') {
p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
sprintf(p->file, "%s/%s", cwd, *argv);
} else {
2001-02-10 02:05:24 +00:00
p->package = strdup(*argv);
}
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
p->next = packages;
packages = p;
2001-02-11 01:40:23 +00:00
}
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
switch (opt) {
case 'i':
return dpkg_install(packages);
case 'r':
return dpkg_remove(packages);
case 'u':
return dpkg_unpack(packages);
case 'c':
return dpkg_configure(packages);
2001-02-11 01:40:23 +00:00
default :
usage(dpkg_usage);
return 0;
2001-02-10 02:05:24 +00:00
}
}