Some fixes, but still broken... (cont)

Make better use of some libbb functions
New remove dir code to avoid depending on the rm applet
dont use copy_file() it doesnt fail elegantly
Use getopt.
Generate correct /var/lib/dpkg/info/ files
Status file is broken, working on it now
This commit is contained in:
Glenn L McGrath 2001-04-08 05:27:18 +00:00
parent f608da4288
commit 0e757a2f3f
2 changed files with 324 additions and 272 deletions

View File

@ -1,3 +1,5 @@
#include <dirent.h>
#include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -116,18 +118,34 @@ static int package_compare(const void *p1, const void *p2)
((package_t *)p2)->package); ((package_t *)p2)->package);
} }
static int remove_dpkgcidir() /*
* 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)
{ {
char *rm_dpkgcidir = NULL; struct dirent *fp;
DIR *dp = opendir(dirname);
while ((fp = readdir(dp)) != NULL) {
struct stat statbuf;
char *filename;
rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8); filename = (char *) xcalloc(1, strlen(dirname) + strlen(fp->d_name) + 2);
strcpy(rm_dpkgcidir, "rm -rf "); strcpy(filename, dirname);
strcat(rm_dpkgcidir, dpkgcidir); strcat(filename, fp->d_name);
lstat(filename, &statbuf);
if (SYSTEM(rm_dpkgcidir) != 0) { if ((strcmp(fp->d_name, ".") != 0) && (strcmp(fp->d_name, "..") != 0)) {
perror("mkdir "); if (S_ISDIR(statbuf.st_mode)) {
return EXIT_FAILURE; remove_dir(strcat(filename, "/"));
}
else if (remove(filename) == -1) {
perror_msg(filename);
}
}
} }
remove(dirname);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -145,7 +163,7 @@ static char **depends_split(const char *dependsstr)
goto end; goto end;
} }
p = strdup(dependsstr); p = xstrdup(dependsstr);
while (*p != 0 && *p != '\n') { while (*p != 0 && *p != '\n') {
if (*p != ' ') { if (*p != ' ') {
if (*p == ',') { if (*p == ',') {
@ -363,25 +381,25 @@ static int control_read(FILE *file, package_t *p)
char *line; char *line;
while ((line = get_line_from_file(file)) != NULL) { while ((line = get_line_from_file(file)) != NULL) {
line[strlen(line)] = 0; line[strlen(line) - 1] = '\0';
if (strlen(line) == 0) { if (strlen(line) == 0) {
break; break;
} else } else
if (strstr(line, "Package: ") == line) { if (strstr(line, "Package: ") == line) {
p->package = strdup(line + 9); p->package = xstrdup(line + 9);
} else } else
if (strstr(line, "Status: ") == line) { if (strstr(line, "Status: ") == line) {
p->status = status_parse(line + 8); p->status = status_parse(line + 8);
} else } else
if (strstr(line, "Depends: ") == line) { if (strstr(line, "Depends: ") == line) {
p->depends = strdup(line + 9); p->depends = xstrdup(line + 9);
} else } else
if (strstr(line, "Provides: ") == line) { if (strstr(line, "Provides: ") == line) {
p->provides = strdup(line + 10); p->provides = xstrdup(line + 10);
} else } else
if (strstr(line, "Description: ") == line) { if (strstr(line, "Description: ") == line) {
p->description = strdup(line + 13); p->description = xstrdup(line + 13);
/* This is specific to the Debian Installer. Ifdef? */ /* This is specific to the Debian Installer. Ifdef? */
} else } else
if (strstr(line, "installer-menu-item: ") == line) { if (strstr(line, "installer-menu-item: ") == line) {
@ -400,7 +418,7 @@ static void *status_read(void)
package_t *m = 0, *p = 0, *t = 0; package_t *m = 0, *p = 0, *t = 0;
if ((f = fopen(statusfile, "r")) == NULL) { if ((f = fopen(statusfile, "r")) == NULL) {
perror(statusfile); perror_msg(statusfile);
return 0; return 0;
} }
@ -409,8 +427,7 @@ static void *status_read(void)
} }
while (!feof(f)) { while (!feof(f)) {
m = (package_t *)xmalloc(sizeof(package_t)); m = (package_t *)xcalloc(1, sizeof(package_t));
memset(m, 0, sizeof(package_t));
control_read(f, m); control_read(f, m);
if (m->package) { if (m->package) {
/* /*
@ -426,9 +443,8 @@ static void *status_read(void)
* of a pseudo package into the status * of a pseudo package into the status
* binary-tree. * binary-tree.
*/ */
p = (package_t *)xmalloc(sizeof(package_t)); p = (package_t *)xcalloc(1, sizeof(package_t));
memset(p, 0, sizeof(package_t)); p->package = xstrdup(m->provides);
p->package = strdup(m->provides);
t = *(package_t **)tsearch(p, &status, package_compare); t = *(package_t **)tsearch(p, &status, package_compare);
if (t != p) { if (t != p) {
@ -459,57 +475,52 @@ static void *status_read(void)
static int status_merge(void *status, package_t *pkgs) static int status_merge(void *status, package_t *pkgs)
{ {
FILE *fin, *fout; FILE *fin, *fout;
char *line; char *line = NULL;
package_t *pkg = 0, *statpkg = 0; package_t *pkg = 0, *statpkg = 0;
package_t locpkg; package_t locpkg;
int r = 0; int r = 0;
if ((fin = fopen(statusfile, "r")) == NULL) { if ((fout = wfopen(new_statusfile, "w")) == NULL) {
perror(statusfile);
return 0;
}
if ((fout = fopen(new_statusfile, "w")) == NULL) {
perror(new_statusfile);
return 0; return 0;
} }
if (getenv(udpkg_quiet) == NULL) { if (getenv(udpkg_quiet) == NULL) {
printf("(Updating database...)\n"); printf("(Updating database...)\n");
} }
if ((fin = wfopen(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) ;
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { locpkg.package = line + 9;
line[strlen(line)] = 0; /* trim newline */ statpkg = tfind(&locpkg, &status, package_compare);
/* 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;
pkg = pkg->next) ;
locpkg.package = line + 9;
statpkg = tfind(&locpkg, &status, package_compare);
/* note: statpkg should be non-zero, unless the status /* note: statpkg should be non-zero, unless the status
* file was changed while we are processing (no locking * file was changed while we are processing (no locking
* is currently done... * is currently done...
*/ */
if (statpkg != 0) { if (statpkg != 0) {
statpkg = *(package_t **)statpkg; statpkg = *(package_t **)statpkg;
}
} }
if (pkg != 0) {
continue;
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s",
status_print(statpkg->status));
}
fputs(line, fout);
fputc('\n', fout);
} }
if (pkg != 0) {
continue;
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s",
status_print(statpkg->status));
}
fputs(line, fout);
fputc('\n', fout);
} }
free(line); free(line);
@ -576,13 +587,11 @@ static int dpkg_dounpack(package_t *pkg)
{ {
int r = 0, i; int r = 0, i;
char *cwd; char *cwd;
FILE *outfp;
char *src_file = NULL; char *src_file = NULL;
char *dst_file = NULL; char *dst_file = NULL;
char *lst_file = NULL; // char *lst_file = NULL;
char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst", char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
"/conffiles", "/md5sums", "/shlibs", "/templates" }; "conffiles", "md5sums", "shlibs", "templates" };
char buf[1024], buf2[1024];
DPRINTF("Unpacking %s\n", pkg->package); DPRINTF("Unpacking %s\n", pkg->package);
@ -592,36 +601,46 @@ static int dpkg_dounpack(package_t *pkg)
/* Installs the package scripts into the info directory */ /* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) { for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
struct stat src_stat_buf;
int src_fd = 0, dst_fd = 0;
/* The full path of the current location of the admin file */ /* The full path of the current location of the admin file */
src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
strcpy(src_file, dpkgcidir); sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
strcat(src_file, pkg->package);
strcat(src_file, adminscripts[i]);
/* the full path of where we want the file to be copied to */ /* the full path of where we want the file to be copied to */
dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
strcpy(dst_file, infodir); sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
strcat(dst_file, pkg->package);
strcat(dst_file, adminscripts[i]);
/* copy admin file to permanent home */ /*
if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) { * copy admin file to permanent home
error_msg_and_die("Cannot copy %s to %s ", buf, buf2); * NOTE: Maybe merge this behaviour into libb/copy_file.c
*/
if (lstat(src_file, &src_stat_buf) == 0) {
if ((src_fd = open(src_file, O_RDONLY)) != -1) {
if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) {
perror_msg("Opening %s", dst_file);
}
copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size);
close(src_fd);
close(dst_fd);
} else {
error_msg("couldnt open [%s]\n", src_file);
}
} }
/* create the list 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);
printf("done\n");
getchar();
fclose(outfp);
} }
/*
* 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 &= status_wantmask; pkg->status &= status_wantmask;
pkg->status |= status_wantinstall; pkg->status |= status_wantinstall;
pkg->status &= status_flagmask; pkg->status &= status_flagmask;
@ -647,7 +666,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
int length; int length;
/* clean the temp directory (dpkgcidir) be recreating it */ /* clean the temp directory (dpkgcidir) be recreating it */
remove_dpkgcidir(); remove_dir(dpkgcidir);
if (mkdir(dpkgcidir, S_IRWXU) != 0) { if (mkdir(dpkgcidir, S_IRWXU) != 0) {
perror("mkdir"); perror("mkdir");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -664,13 +683,12 @@ static int dpkg_unpackcontrol(package_t *pkg)
} }
/* now remove trailing version numbers etc */ /* now remove trailing version numbers etc */
length = strcspn(tmp_name, "_."); length = strcspn(tmp_name, "_.");
pkg->package = (char *) xmalloc(length + 1); pkg->package = (char *) xcalloc(1, length + 1);
/* store the package name */ /* store the package name */
strncpy(pkg->package, tmp_name, length); strncpy(pkg->package, tmp_name, length);
/* work out the full extraction path */ /* work out the full extraction path */
tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9); tmp_name = (char *) xcalloc(1, strlen(dpkgcidir) + strlen(pkg->package) + 9);
memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
strcpy(tmp_name, dpkgcidir); strcpy(tmp_name, dpkgcidir);
strcat(tmp_name, pkg->package); strcat(tmp_name, pkg->package);
@ -679,7 +697,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
/* parse the extracted control file */ /* parse the extracted control file */
strcat(tmp_name, "/control"); strcat(tmp_name, "/control");
if ((file = fopen(tmp_name, "r")) == NULL) { if ((file = wfopen(tmp_name, "r")) == NULL) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (control_read(file, pkg) == EXIT_FAILURE) { if (control_read(file, pkg) == EXIT_FAILURE) {
@ -703,7 +721,7 @@ static int dpkg_unpack(package_t *pkgs, void *status)
} }
} }
status_merge(status, pkgs); status_merge(status, pkgs);
remove_dpkgcidir(); remove_dir(dpkgcidir);
return r; return r;
} }
@ -740,8 +758,7 @@ static int dpkg_install(package_t *pkgs, void *status)
/* Stage 1: parse all the control information */ /* Stage 1: parse all the control information */
for (p = pkgs; p != 0; p = p->next) { for (p = pkgs; p != 0; p = p->next) {
if (dpkg_unpackcontrol(p) == EXIT_FAILURE) { if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
perror(p->file); return(EXIT_FAILURE);
return EXIT_FAILURE;
} }
} }
@ -765,21 +782,24 @@ static int dpkg_install(package_t *pkgs, void *status)
DPRINTF("Installing %s\n", p->package); DPRINTF("Installing %s\n", p->package);
if (dpkg_dounpack(p) != 0) { if (dpkg_dounpack(p) != 0) {
perror(p->file); perror_msg(p->file);
} }
if (dpkg_doconfigure(p) != 0) { if (dpkg_doconfigure(p) != 0) {
perror(p->file); perror_msg(p->file);
} }
} }
if (ordered != 0) { if (ordered != 0) {
status_merge(status, pkgs); status_merge(status, pkgs);
} }
remove_dpkgcidir(); remove_dir(dpkgcidir);
return 0; return 0;
} }
/*
* Not implemented yet
*
static int dpkg_remove(package_t *pkgs, void *status) static int dpkg_remove(package_t *pkgs, void *status)
{ {
package_t *p; package_t *p;
@ -791,55 +811,61 @@ static int dpkg_remove(package_t *pkgs, void *status)
return 0; return 0;
} }
*/
extern int dpkg_main(int argc, char **argv) extern int dpkg_main(int argc, char **argv)
{ {
char opt = 0; const int arg_install = 1;
char *s; const int arg_unpack = 2;
const int arg_configure = 4;
package_t *p, *packages = NULL; package_t *p, *packages = NULL;
char *cwd = getcwd(0, 0);
void *status = NULL; void *status = NULL;
char opt = 0;
int optflag = 0;
while (*++argv) { while ((opt = getopt(argc, argv, "iruc")) != -1) {
if (**argv == '-') { switch (opt) {
/* Nasty little hack to "parse" long options. */ case 'i':
s = *argv; optflag |= arg_install;
while (*s == '-') { break;
s++; case 'u':
} optflag |= arg_unpack;
opt=s[0]; break;
} else { case 'c':
p = (package_t *)xmalloc(sizeof(package_t)); optflag |= arg_configure;
memset(p, 0, sizeof(package_t)); break;
default:
if (**argv == '/') { show_usage();
p->file = *argv; }
} else
if (opt != 'c') {
p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
sprintf(p->file, "%s/%s", cwd, *argv);
} else {
p->package = strdup(*argv);
}
p->next = packages;
packages = p;
}
} }
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;
optind++;
}
create_path(dpkgcidir, 0755);
create_path(infodir, 0755);
status = status_read(); status = status_read();
switch (opt) { if (optflag & arg_install) {
case 'i': return dpkg_install(packages, status);
return dpkg_install(packages, status);
case 'r':
return dpkg_remove(packages, status);
case 'u':
return dpkg_unpack(packages, status);
case 'c':
return dpkg_configure(packages, status);
default :
show_usage();
return EXIT_FAILURE;
} }
else if (optflag & arg_unpack) {
return dpkg_unpack(packages, status);
}
else if (optflag & arg_configure) {
return dpkg_configure(packages, status);
}
return(EXIT_FAILURE);
} }

298
dpkg.c
View File

@ -1,3 +1,5 @@
#include <dirent.h>
#include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -116,18 +118,34 @@ static int package_compare(const void *p1, const void *p2)
((package_t *)p2)->package); ((package_t *)p2)->package);
} }
static int remove_dpkgcidir() /*
* 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)
{ {
char *rm_dpkgcidir = NULL; struct dirent *fp;
DIR *dp = opendir(dirname);
while ((fp = readdir(dp)) != NULL) {
struct stat statbuf;
char *filename;
rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8); filename = (char *) xcalloc(1, strlen(dirname) + strlen(fp->d_name) + 2);
strcpy(rm_dpkgcidir, "rm -rf "); strcpy(filename, dirname);
strcat(rm_dpkgcidir, dpkgcidir); strcat(filename, fp->d_name);
lstat(filename, &statbuf);
if (SYSTEM(rm_dpkgcidir) != 0) { if ((strcmp(fp->d_name, ".") != 0) && (strcmp(fp->d_name, "..") != 0)) {
perror("mkdir "); if (S_ISDIR(statbuf.st_mode)) {
return EXIT_FAILURE; remove_dir(strcat(filename, "/"));
}
else if (remove(filename) == -1) {
perror_msg(filename);
}
}
} }
remove(dirname);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -145,7 +163,7 @@ static char **depends_split(const char *dependsstr)
goto end; goto end;
} }
p = strdup(dependsstr); p = xstrdup(dependsstr);
while (*p != 0 && *p != '\n') { while (*p != 0 && *p != '\n') {
if (*p != ' ') { if (*p != ' ') {
if (*p == ',') { if (*p == ',') {
@ -363,25 +381,25 @@ static int control_read(FILE *file, package_t *p)
char *line; char *line;
while ((line = get_line_from_file(file)) != NULL) { while ((line = get_line_from_file(file)) != NULL) {
line[strlen(line)] = 0; line[strlen(line) - 1] = '\0';
if (strlen(line) == 0) { if (strlen(line) == 0) {
break; break;
} else } else
if (strstr(line, "Package: ") == line) { if (strstr(line, "Package: ") == line) {
p->package = strdup(line + 9); p->package = xstrdup(line + 9);
} else } else
if (strstr(line, "Status: ") == line) { if (strstr(line, "Status: ") == line) {
p->status = status_parse(line + 8); p->status = status_parse(line + 8);
} else } else
if (strstr(line, "Depends: ") == line) { if (strstr(line, "Depends: ") == line) {
p->depends = strdup(line + 9); p->depends = xstrdup(line + 9);
} else } else
if (strstr(line, "Provides: ") == line) { if (strstr(line, "Provides: ") == line) {
p->provides = strdup(line + 10); p->provides = xstrdup(line + 10);
} else } else
if (strstr(line, "Description: ") == line) { if (strstr(line, "Description: ") == line) {
p->description = strdup(line + 13); p->description = xstrdup(line + 13);
/* This is specific to the Debian Installer. Ifdef? */ /* This is specific to the Debian Installer. Ifdef? */
} else } else
if (strstr(line, "installer-menu-item: ") == line) { if (strstr(line, "installer-menu-item: ") == line) {
@ -400,7 +418,7 @@ static void *status_read(void)
package_t *m = 0, *p = 0, *t = 0; package_t *m = 0, *p = 0, *t = 0;
if ((f = fopen(statusfile, "r")) == NULL) { if ((f = fopen(statusfile, "r")) == NULL) {
perror(statusfile); perror_msg(statusfile);
return 0; return 0;
} }
@ -409,8 +427,7 @@ static void *status_read(void)
} }
while (!feof(f)) { while (!feof(f)) {
m = (package_t *)xmalloc(sizeof(package_t)); m = (package_t *)xcalloc(1, sizeof(package_t));
memset(m, 0, sizeof(package_t));
control_read(f, m); control_read(f, m);
if (m->package) { if (m->package) {
/* /*
@ -426,9 +443,8 @@ static void *status_read(void)
* of a pseudo package into the status * of a pseudo package into the status
* binary-tree. * binary-tree.
*/ */
p = (package_t *)xmalloc(sizeof(package_t)); p = (package_t *)xcalloc(1, sizeof(package_t));
memset(p, 0, sizeof(package_t)); p->package = xstrdup(m->provides);
p->package = strdup(m->provides);
t = *(package_t **)tsearch(p, &status, package_compare); t = *(package_t **)tsearch(p, &status, package_compare);
if (t != p) { if (t != p) {
@ -459,57 +475,52 @@ static void *status_read(void)
static int status_merge(void *status, package_t *pkgs) static int status_merge(void *status, package_t *pkgs)
{ {
FILE *fin, *fout; FILE *fin, *fout;
char *line; char *line = NULL;
package_t *pkg = 0, *statpkg = 0; package_t *pkg = 0, *statpkg = 0;
package_t locpkg; package_t locpkg;
int r = 0; int r = 0;
if ((fin = fopen(statusfile, "r")) == NULL) { if ((fout = wfopen(new_statusfile, "w")) == NULL) {
perror(statusfile);
return 0;
}
if ((fout = fopen(new_statusfile, "w")) == NULL) {
perror(new_statusfile);
return 0; return 0;
} }
if (getenv(udpkg_quiet) == NULL) { if (getenv(udpkg_quiet) == NULL) {
printf("(Updating database...)\n"); printf("(Updating database...)\n");
} }
if ((fin = wfopen(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) ;
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { locpkg.package = line + 9;
line[strlen(line)] = 0; /* trim newline */ statpkg = tfind(&locpkg, &status, package_compare);
/* 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;
pkg = pkg->next) ;
locpkg.package = line + 9;
statpkg = tfind(&locpkg, &status, package_compare);
/* note: statpkg should be non-zero, unless the status /* note: statpkg should be non-zero, unless the status
* file was changed while we are processing (no locking * file was changed while we are processing (no locking
* is currently done... * is currently done...
*/ */
if (statpkg != 0) { if (statpkg != 0) {
statpkg = *(package_t **)statpkg; statpkg = *(package_t **)statpkg;
}
} }
if (pkg != 0) {
continue;
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s",
status_print(statpkg->status));
}
fputs(line, fout);
fputc('\n', fout);
} }
if (pkg != 0) {
continue;
}
if (strstr(line, "Status: ") == line && statpkg != 0) {
snprintf(line, sizeof(line), "Status: %s",
status_print(statpkg->status));
}
fputs(line, fout);
fputc('\n', fout);
} }
free(line); free(line);
@ -576,13 +587,11 @@ static int dpkg_dounpack(package_t *pkg)
{ {
int r = 0, i; int r = 0, i;
char *cwd; char *cwd;
FILE *outfp;
char *src_file = NULL; char *src_file = NULL;
char *dst_file = NULL; char *dst_file = NULL;
char *lst_file = NULL; // char *lst_file = NULL;
char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst", char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
"/conffiles", "/md5sums", "/shlibs", "/templates" }; "conffiles", "md5sums", "shlibs", "templates" };
char buf[1024], buf2[1024];
DPRINTF("Unpacking %s\n", pkg->package); DPRINTF("Unpacking %s\n", pkg->package);
@ -592,36 +601,46 @@ static int dpkg_dounpack(package_t *pkg)
/* Installs the package scripts into the info directory */ /* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) { for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
struct stat src_stat_buf;
int src_fd = 0, dst_fd = 0;
/* The full path of the current location of the admin file */ /* The full path of the current location of the admin file */
src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
strcpy(src_file, dpkgcidir); sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
strcat(src_file, pkg->package);
strcat(src_file, adminscripts[i]);
/* the full path of where we want the file to be copied to */ /* the full path of where we want the file to be copied to */
dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
strcpy(dst_file, infodir); sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
strcat(dst_file, pkg->package);
strcat(dst_file, adminscripts[i]);
/* copy admin file to permanent home */ /*
if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) { * copy admin file to permanent home
error_msg_and_die("Cannot copy %s to %s ", buf, buf2); * NOTE: Maybe merge this behaviour into libb/copy_file.c
*/
if (lstat(src_file, &src_stat_buf) == 0) {
if ((src_fd = open(src_file, O_RDONLY)) != -1) {
if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) {
perror_msg("Opening %s", dst_file);
}
copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size);
close(src_fd);
close(dst_fd);
} else {
error_msg("couldnt open [%s]\n", src_file);
}
} }
/* create the list 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);
printf("done\n");
getchar();
fclose(outfp);
} }
/*
* 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 &= status_wantmask; pkg->status &= status_wantmask;
pkg->status |= status_wantinstall; pkg->status |= status_wantinstall;
pkg->status &= status_flagmask; pkg->status &= status_flagmask;
@ -647,7 +666,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
int length; int length;
/* clean the temp directory (dpkgcidir) be recreating it */ /* clean the temp directory (dpkgcidir) be recreating it */
remove_dpkgcidir(); remove_dir(dpkgcidir);
if (mkdir(dpkgcidir, S_IRWXU) != 0) { if (mkdir(dpkgcidir, S_IRWXU) != 0) {
perror("mkdir"); perror("mkdir");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -664,13 +683,12 @@ static int dpkg_unpackcontrol(package_t *pkg)
} }
/* now remove trailing version numbers etc */ /* now remove trailing version numbers etc */
length = strcspn(tmp_name, "_."); length = strcspn(tmp_name, "_.");
pkg->package = (char *) xmalloc(length + 1); pkg->package = (char *) xcalloc(1, length + 1);
/* store the package name */ /* store the package name */
strncpy(pkg->package, tmp_name, length); strncpy(pkg->package, tmp_name, length);
/* work out the full extraction path */ /* work out the full extraction path */
tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9); tmp_name = (char *) xcalloc(1, strlen(dpkgcidir) + strlen(pkg->package) + 9);
memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
strcpy(tmp_name, dpkgcidir); strcpy(tmp_name, dpkgcidir);
strcat(tmp_name, pkg->package); strcat(tmp_name, pkg->package);
@ -679,7 +697,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
/* parse the extracted control file */ /* parse the extracted control file */
strcat(tmp_name, "/control"); strcat(tmp_name, "/control");
if ((file = fopen(tmp_name, "r")) == NULL) { if ((file = wfopen(tmp_name, "r")) == NULL) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (control_read(file, pkg) == EXIT_FAILURE) { if (control_read(file, pkg) == EXIT_FAILURE) {
@ -703,7 +721,7 @@ static int dpkg_unpack(package_t *pkgs, void *status)
} }
} }
status_merge(status, pkgs); status_merge(status, pkgs);
remove_dpkgcidir(); remove_dir(dpkgcidir);
return r; return r;
} }
@ -740,8 +758,7 @@ static int dpkg_install(package_t *pkgs, void *status)
/* Stage 1: parse all the control information */ /* Stage 1: parse all the control information */
for (p = pkgs; p != 0; p = p->next) { for (p = pkgs; p != 0; p = p->next) {
if (dpkg_unpackcontrol(p) == EXIT_FAILURE) { if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
perror(p->file); return(EXIT_FAILURE);
return EXIT_FAILURE;
} }
} }
@ -765,21 +782,24 @@ static int dpkg_install(package_t *pkgs, void *status)
DPRINTF("Installing %s\n", p->package); DPRINTF("Installing %s\n", p->package);
if (dpkg_dounpack(p) != 0) { if (dpkg_dounpack(p) != 0) {
perror(p->file); perror_msg(p->file);
} }
if (dpkg_doconfigure(p) != 0) { if (dpkg_doconfigure(p) != 0) {
perror(p->file); perror_msg(p->file);
} }
} }
if (ordered != 0) { if (ordered != 0) {
status_merge(status, pkgs); status_merge(status, pkgs);
} }
remove_dpkgcidir(); remove_dir(dpkgcidir);
return 0; return 0;
} }
/*
* Not implemented yet
*
static int dpkg_remove(package_t *pkgs, void *status) static int dpkg_remove(package_t *pkgs, void *status)
{ {
package_t *p; package_t *p;
@ -791,55 +811,61 @@ static int dpkg_remove(package_t *pkgs, void *status)
return 0; return 0;
} }
*/
extern int dpkg_main(int argc, char **argv) extern int dpkg_main(int argc, char **argv)
{ {
char opt = 0; const int arg_install = 1;
char *s; const int arg_unpack = 2;
const int arg_configure = 4;
package_t *p, *packages = NULL; package_t *p, *packages = NULL;
char *cwd = getcwd(0, 0);
void *status = NULL; void *status = NULL;
char opt = 0;
int optflag = 0;
while (*++argv) { while ((opt = getopt(argc, argv, "iruc")) != -1) {
if (**argv == '-') { switch (opt) {
/* Nasty little hack to "parse" long options. */ case 'i':
s = *argv; optflag |= arg_install;
while (*s == '-') { break;
s++; case 'u':
} optflag |= arg_unpack;
opt=s[0]; break;
} else { case 'c':
p = (package_t *)xmalloc(sizeof(package_t)); optflag |= arg_configure;
memset(p, 0, sizeof(package_t)); break;
default:
if (**argv == '/') { show_usage();
p->file = *argv; }
} else
if (opt != 'c') {
p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
sprintf(p->file, "%s/%s", cwd, *argv);
} else {
p->package = strdup(*argv);
}
p->next = packages;
packages = p;
}
} }
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;
optind++;
}
create_path(dpkgcidir, 0755);
create_path(infodir, 0755);
status = status_read(); status = status_read();
switch (opt) { if (optflag & arg_install) {
case 'i': return dpkg_install(packages, status);
return dpkg_install(packages, status);
case 'r':
return dpkg_remove(packages, status);
case 'u':
return dpkg_unpack(packages, status);
case 'c':
return dpkg_configure(packages, status);
default :
show_usage();
return EXIT_FAILURE;
} }
else if (optflag & arg_unpack) {
return dpkg_unpack(packages, status);
}
else if (optflag & arg_configure) {
return dpkg_configure(packages, status);
}
return(EXIT_FAILURE);
} }