hush/dpkg.c

849 lines
21 KiB
C
Raw Normal View History

#include <dirent.h>
#include <getopt.h>
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 DEPENDSMAX 64 /* maximum number of depends we can handle */
2001-02-10 02:05:24 +00:00
/* Should we do full dependency checking? */
2001-02-10 02:05:24 +00:00
#define DODEPENDS 1
/* Should we do debugging? */
//#define DODEBUG 1
2001-02-10 02:05:24 +00:00
#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
/* from dpkg-deb.c */
static const int dpkg_deb_contents = 1;
static const int dpkg_deb_control = 2;
// const int dpkg_deb_info = 4;
static const int dpkg_deb_extract = 8;
static const int dpkg_deb_verbose_extract = 16;
static const int dpkg_deb_list = 32;
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 infodir[] = "/var/lib/dpkg/info/";
static const char udpkg_quiet[] = "UDPKG_QUIET";
2001-02-10 02:05:24 +00:00
//static const int status_want_unknown = 1;
static const int status_want_install = 2;
//static const int status_want_hold = 3;
//static const int status_want_deinstall = 4;
//static const int status_want_purge = 5;
static const int status_flag_ok = 1;
//static const int status_flag_reinstreq = 2;
//static const int status_flag_hold = 3;
//static const int status_flag_holdreinstreq = 4;
//static const int status_statusnoninstalled = 1;
static const int status_status_unpacked = 2;
static const int status_status_halfconfigured = 3;
static const int status_status_installed = 4;
static const int status_status_halfinstalled = 5;
//static const int status_statusconfigfiles = 6;
//static const int status_statuspostinstfailed = 7;
//static const int status_statusremovalfailed = 8;
static const char *status_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 };
static const char *status_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 };
static const char *status_words_status[] = { "not-installed", "unpacked", "half-configured", "installed",
"half-installed", "config-files", "post-inst-failed", "removal-failed", 0 };
static const int color_white = 0;
static const int color_grey = 1;
static 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 char status_want;
unsigned char status_flag;
unsigned char status_status;
2001-02-10 02:05:24 +00:00
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
/*
* NOTE: this was handled by a "rm -rf" shell command
* Maybe theis behaviour should be integrated into the rm applet
* (i dont appreciate the rm applets recursive action fn)-bug1
*/
static int remove_dir(const char *dirname)
{
struct dirent *fp;
DIR *dp = opendir(dirname);
while ((fp = readdir(dp)) != NULL) {
struct stat statbuf;
char *filename;
filename = (char *) xcalloc(1, strlen(dirname) + strlen(fp->d_name) + 2);
strcpy(filename, dirname);
strcat(filename, fp->d_name);
lstat(filename, &statbuf);
if ((strcmp(fp->d_name, ".") != 0) && (strcmp(fp->d_name, "..") != 0)) {
if (S_ISDIR(statbuf.st_mode)) {
remove_dir(strcat(filename, "/"));
}
else if (remove(filename) == -1) {
perror_msg(filename);
}
}
}
remove(dirname);
return EXIT_SUCCESS;
}
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
p = xstrdup(dependsstr);
2001-02-11 01:40:23 +00:00
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 *)xmalloc(sizeof(struct package_t));
2001-02-10 02:05:24 +00:00
/* 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) ||
((chk = *(package_t **)found) && (chk->status_flag & status_flag_ok) &&
(chk->status_status & status_status_installed))) {
2001-02-11 01:40:23 +00:00
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) {
error_msg("Too many dependencies for %s", 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) {
error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
2001-02-10 02:05:24 +00:00
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 char status_parse(const char *line, const char **status_words)
2001-02-10 02:05:24 +00:00
{
unsigned char status_num;
int i = 0;
2001-02-10 02:05:24 +00:00
while (status_words[i] != 0) {
if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
status_num = (char)i;
return(status_num);
2001-02-10 02:05:24 +00:00
}
i++;
2001-02-10 02:05:24 +00:00
}
/* parse error */
error_msg("Invalid status word");
return(0);
2001-02-10 02:05:24 +00:00
}
/*
* Read a control file (or a stanza of a status file) and parse it,
* filling parsed fields into the package structure
*/
static int 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) - 1] = '\0';
2001-02-11 01:40:23 +00:00
if (strlen(line) == 0) {
break;
}
else if (strstr(line, "Package: ") == line) {
p->package = xstrdup(line + 9);
}
else if (strstr(line, "Status: ") == line) {
char *word_pointer;
word_pointer = strchr(line, ' ') + 1;
p->status_want = status_parse(word_pointer, status_words_want);
word_pointer = strchr(word_pointer, ' ') + 1;
p->status_flag = status_parse(word_pointer, status_words_flag);
word_pointer = strchr(word_pointer, ' ') + 1;
p->status_status = status_parse(word_pointer, status_words_status);
}
else if (strstr(line, "Depends: ") == line) {
p->depends = xstrdup(line + 9);
}
else if (strstr(line, "Provides: ") == line) {
p->provides = xstrdup(line + 10);
}
else if (strstr(line, "Description: ") == line) {
p->description = xstrdup(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);
return EXIT_SUCCESS;
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
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
if ((f = wfopen(statusfile, "r")) == NULL) {
return(NULL);
}
while (!feof(f)) {
m = (package_t *)xcalloc(1, 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 *)xcalloc(1, sizeof(package_t));
p->package = xstrdup(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 {
2001-02-10 02:05:24 +00:00
/*
* 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_want = m->status_want;
t->status_flag = m->status_flag;
t->status_status = m->status_status;
2001-02-10 02:05:24 +00:00
}
}
}
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 = NULL;
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
if ((fout = wfopen(new_statusfile, "w")) == NULL) {
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");
}
/*
* Dont use wfopen here, handle errors ourself
*/
if ((fin = fopen(statusfile, "r")) != NULL) {
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
line[strlen(line) - 1] = '\0'; /* trim newline */
/* 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 && strcmp(line + 9,
pkg->package) != 0; pkg = pkg->next) ;
2001-02-11 01:40:23 +00:00
locpkg.package = line + 9;
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;
}
}
if (pkg != 0) {
continue;
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s %s %s",
status_words_want[statpkg->status_want - 1],
status_words_flag[statpkg->status_flag - 1],
status_words_status[statpkg->status_status - 1]);
}
fprintf(fout, "%s\n", line);
2001-02-10 02:05:24 +00:00
}
fclose(fin);
2001-02-10 02:05:24 +00:00
}
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 %s %s\n",
pkg->package, status_words_want[pkg->status_want - 1],
status_words_flag[pkg->status_flag - 1],
status_words_status[pkg->status_status - 1]);
2001-02-10 02:05:24 +00:00
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(fout);
/*
* Its ok if renaming statusfile fails becasue it doesnt exist
*/
if (rename(statusfile, bak_statusfile) == -1) {
struct stat stat_buf;
if (stat(statusfile, &stat_buf) == 0) {
error_msg("Couldnt create backup status file");
return(EXIT_FAILURE);
}
error_msg("No status file found, creating new one");
}
2001-02-10 02:05:24 +00:00
if (rename(new_statusfile, statusfile) == -1) {
error_msg("Couldnt create status file");
return(EXIT_FAILURE);
}
return(EXIT_SUCCESS);
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 = 0;
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) {
error_msg("postinst exited with status %d\n", r);
pkg->status_status = status_status_halfconfigured;
2001-02-10 02:05:24 +00:00
return 1;
}
}
pkg->status_status = status_status_installed;
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;
int status = TRUE;
char *cwd = xgetcwd(0);
2001-04-11 16:23:35 +00:00
char *src_filename = NULL;
char *dst_filename = NULL;
// char *lst_file = NULL;
char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
"conffiles", "md5sums", "shlibs", "templates" };
2001-02-10 02:05:24 +00:00
DPRINTF("Unpacking %s\n", pkg->package);
if(cwd==NULL)
exit(EXIT_FAILURE);
2001-02-10 02:05:24 +00:00
chdir("/");
2001-04-11 16:23:35 +00:00
deb_extract(pkg->file, dpkg_deb_extract, "/");
2001-02-11 01:40:23 +00:00
/* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
struct stat src_stat_buf;
2001-04-11 16:23:35 +00:00
FILE *src_file = NULL, *dst_file = NULL;
/* The full path of the current location of the admin file */
2001-04-11 16:23:35 +00:00
src_filename = xrealloc(src_filename, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(src_filename, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
/* the full path of where we want the file to be copied to */
2001-04-11 16:23:35 +00:00
dst_filename = xrealloc(dst_filename, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(dst_filename, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
/*
* copy admin file to permanent home
* NOTE: Maybe merge this behaviour into libb/copy_file.c
*/
2001-04-11 16:23:35 +00:00
if (lstat(src_filename, &src_stat_buf) == 0) {
if ((src_file = wfopen(src_filename, "r")) != NULL) {
if ((dst_file = wfopen(dst_filename, "w")) == NULL) {
status = FALSE;
2001-04-11 16:23:35 +00:00
perror_msg("Opening %s", dst_filename);
}
2001-04-11 16:23:35 +00:00
copy_file_chunk(src_file, dst_file, src_stat_buf.st_size);
fclose(src_file);
fclose(dst_file);
} else {
status = FALSE;
2001-04-11 16:23:35 +00:00
error_msg("couldnt open [%s]\n", src_filename);
}
2001-02-11 01:40:23 +00:00
}
}
/*
* create the list file
* FIXME: currently this dumps the lst to stdout instead of a file
*/
/* lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
strcpy(lst_file, infodir);
strcat(lst_file, pkg->package);
strcat(lst_file, ".list");
deb_extract(dpkg_deb_list, NULL, pkg->file);
*/
pkg->status_want = status_want_install;
pkg->status_flag = status_flag_ok;
if (status == TRUE) {
pkg->status_status = status_status_unpacked;
2001-02-11 01:40:23 +00:00
} else {
pkg->status_status = status_status_halfinstalled;
2001-02-10 02:05:24 +00:00
}
2001-02-10 02:05:24 +00:00
chdir(cwd);
return r;
}
/*
* Extract and parse the control.tar.gz from the specified package
*/
2001-02-10 14:26:48 +00:00
static int dpkg_unpackcontrol(package_t *pkg)
2001-02-10 02:05:24 +00:00
{
char *tmp_name;
FILE *file;
int length;
2001-02-10 02:05:24 +00:00
/* clean the temp directory (dpkgcidir) be recreating it */
remove_dir(dpkgcidir);
2001-04-08 07:23:53 +00:00
if (create_path(dpkgcidir, S_IRWXU) == FALSE) {
return EXIT_FAILURE;
2001-02-11 01:40:23 +00:00
}
2001-02-10 02:05:24 +00:00
/*
* Get the package name from the file name,
* first remove the directories
*/
if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
tmp_name = pkg->file;
} else {
tmp_name++;
2001-02-10 02:05:24 +00:00
}
/* now remove trailing version numbers etc */
length = strcspn(tmp_name, "_.");
pkg->package = (char *) xcalloc(1, length + 1);
/* store the package name */
strncpy(pkg->package, tmp_name, length);
2001-02-11 01:40:23 +00:00
/* work out the full extraction path */
tmp_name = (char *) xcalloc(1, strlen(dpkgcidir) + strlen(pkg->package) + 9);
strcpy(tmp_name, dpkgcidir);
strcat(tmp_name, pkg->package);
/* extract control.tar.gz to the full extraction path */
2001-04-11 16:23:35 +00:00
deb_extract(pkg->file, dpkg_deb_control, tmp_name);
/* parse the extracted control file */
strcat(tmp_name, "/control");
if ((file = wfopen(tmp_name, "r")) == NULL) {
return EXIT_FAILURE;
}
if (control_read(file, pkg) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
2001-02-10 02:05:24 +00:00
}
static int dpkg_unpack(package_t *pkgs, void *status)
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
for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
if ((r = dpkg_dounpack(pkg)) != 0 ) {
break;
}
2001-02-10 02:05:24 +00:00
}
status_merge(status, pkgs);
remove_dir(dpkgcidir);
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return r;
}
static int dpkg_configure(package_t *pkgs, void *status)
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-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) {
error_msg("Trying to configure %s, but it is not installed", pkg->package);
2001-02-10 02:05:24 +00:00
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;
}
static int dpkg_install(package_t *pkgs, void *status)
2001-02-10 02:05:24 +00:00
{
2001-02-10 14:26:48 +00:00
package_t *p, *ordered = 0;
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
/* Stage 1: parse all the control information */
for (p = pkgs; p != 0; p = p->next) {
if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
return(EXIT_FAILURE);
2001-02-10 02:05:24 +00:00
}
}
2001-02-10 02:05:24 +00:00
/* 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_want = status_want_install;
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_flag = status_flag_ok;
2001-02-10 02:05:24 +00:00
DPRINTF("Installing %s\n", p->package);
if (dpkg_dounpack(p) != 0) {
perror_msg(p->file);
}
if (dpkg_doconfigure(p) != 0) {
perror_msg(p->file);
2001-02-10 02:05:24 +00:00
}
}
if (ordered != 0) {
2001-02-10 02:05:24 +00:00
status_merge(status, pkgs);
}
remove_dir(dpkgcidir);
2001-02-11 01:40:23 +00:00
2001-02-10 02:05:24 +00:00
return 0;
}
/*
* Not implemented yet
*
static int dpkg_remove(package_t *pkgs, void *status)
2001-02-10 02:05:24 +00:00
{
2001-02-10 14:26:48 +00:00
package_t *p;
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 02:05:24 +00:00
2001-02-10 14:26:48 +00:00
extern int dpkg_main(int argc, char **argv)
2001-02-10 02:05:24 +00:00
{
const int arg_install = 1;
const int arg_unpack = 2;
const int arg_configure = 4;
2001-02-10 14:26:48 +00:00
package_t *p, *packages = NULL;
void *status = NULL;
char opt = 0;
int optflag = 0;
2001-02-11 01:40:23 +00:00
while ((opt = getopt(argc, argv, "iruc")) != -1) {
switch (opt) {
case 'i':
optflag |= arg_install;
break;
case 'u':
optflag |= arg_unpack;
break;
case 'c':
optflag |= arg_configure;
break;
default:
show_usage();
}
}
while (optind < argc) {
p = (package_t *) xcalloc(1, sizeof(package_t));
if (optflag & arg_configure) {
p->package = xstrdup(argv[optind]);
} else {
p->file = xstrdup(argv[optind]);
}
p->next = packages;
packages = p;
2001-02-11 01:40:23 +00:00
optind++;
2001-02-10 02:05:24 +00:00
}
2001-02-11 01:40:23 +00:00
2001-04-08 07:23:53 +00:00
create_path(dpkgcidir, S_IRWXU);
create_path(infodir, S_IRWXU);
status = status_read();
if (optflag & arg_install) {
return dpkg_install(packages, status);
}
else if (optflag & arg_unpack) {
return dpkg_unpack(packages, status);
}
else if (optflag & arg_configure) {
return dpkg_configure(packages, status);
2001-02-10 02:05:24 +00:00
}
return(EXIT_FAILURE);
}