*
*/
+#include <fcntl.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
}
}
}
- name_hashtable[probe_address] = xstrdup(key);
+ name_hashtable[probe_address] = bb_xstrdup(key);
return(probe_address);
}
int ret;
if (version1 == NULL) {
- version1 = xstrdup("");
+ version1 = bb_xstrdup("");
}
if (version2 == NULL) {
- version2 = xstrdup("");
+ version2 = bb_xstrdup("");
}
upstream_len1 = strlen(version1);
upstream_len2 = strlen(version2);
while ((len1 < upstream_len1) || (len2 < upstream_len2)) {
/* Compare non-digit section */
tmp_int = strcspn(&version1[len1], "0123456789");
- name1_char = xstrndup(&version1[len1], tmp_int);
+ name1_char = bb_xstrndup(&version1[len1], tmp_int);
len1 += tmp_int;
tmp_int = strcspn(&version2[len2], "0123456789");
- name2_char = xstrndup(&version2[len2], tmp_int);
+ name2_char = bb_xstrndup(&version2[len2], tmp_int);
len2 += tmp_int;
tmp_int = strcmp(name1_char, name2_char);
free(name1_char);
/* Compare digits */
tmp_int = strspn(&version1[len1], "0123456789");
- name1_char = xstrndup(&version1[len1], tmp_int);
+ name1_char = bb_xstrndup(&version1[len1], tmp_int);
len1 += tmp_int;
tmp_int = strspn(&version2[len2], "0123456789");
- name2_char = xstrndup(&version2[len2], tmp_int);
+ name2_char = bb_xstrndup(&version2[len2], tmp_int);
len2 += tmp_int;
ver_num1 = atoi(name1_char);
ver_num2 = atoi(name2_char);
}
/* Compare upstream version */
- upstream_ver1 = xstrdup(ver1_ptr);
- upstream_ver2 = xstrdup(ver2_ptr);
+ upstream_ver1 = bb_xstrdup(ver1_ptr);
+ upstream_ver2 = bb_xstrdup(ver2_ptr);
/* Chop off debian version, and store for later use */
deb_ver1 = strrchr(upstream_ver1, '-');
*/
void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type)
{
- char *line = xstrdup(whole_line);
+ char *line = bb_xstrdup(whole_line);
char *line2;
char *line_ptr1 = NULL;
char *line_ptr2 = NULL;
field = strtok_r(line, ",", &line_ptr1);
do {
- line2 = xstrdup(field);
+ line2 = bb_xstrdup(field);
field2 = strtok_r(line2, "|", &line_ptr2);
if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) {
type = EDGE_OR_DEPENDS;
else if (strncmp(version, ">=", offset_ch) == 0) {
edge->operator = VER_MORE_EQUAL;
} else {
- error_msg_and_die("Illegal operator\n");
+ bb_error_msg_and_die("Illegal operator\n");
}
}
/* skip to start of version numbers */
unsigned short i;
if (node) {
for (i = 0; i < node->num_of_edges; i++) {
- if (node->edge[i]) {
- free(node->edge[i]);
- }
- }
- if (node->edge) {
- free(node->edge);
+ free(node->edge[i]);
}
+ free(node->edge);
free(node);
}
}
unsigned int fill_package_struct(char *control_buffer)
{
common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t));
-
+ const char *field_names[] = { "Package", "Version", "Pre-Depends", "Depends",
+ "Replaces", "Provides", "Conflicts", "Suggests", "Recommends", "Enhances", 0};
char *field_name;
char *field_value;
int field_start = 0;
new_node->version = search_name_hashtable("unknown");
while (field_start < buffer_length) {
+ unsigned short field_num;
+
field_start += read_package_field(&control_buffer[field_start],
&field_name, &field_value);
if (field_name == NULL) {
- goto fill_package_struct_cleanup; // Oh no, the dreaded goto statement !!
+ goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */
}
- if (strcmp(field_name, "Package") == 0) {
- new_node->name = search_name_hashtable(field_value);
- }
- else if (strcmp(field_name, "Version") == 0) {
- new_node->version = search_name_hashtable(field_value);
- }
- else if (strcmp(field_name, "Pre-Depends") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS);
- }
- else if (strcmp(field_name, "Depends") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_DEPENDS);
- }
- else if (strcmp(field_name, "Replaces") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_REPLACES);
- }
- else if (strcmp(field_name, "Provides") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_PROVIDES);
- }
- else if (strcmp(field_name, "Conflicts") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_CONFLICTS);
- }
- else if (strcmp(field_name, "Suggests") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_SUGGESTS);
- }
- else if (strcmp(field_name, "Recommends") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS);
- }
- else if (strcmp(field_name, "Enhances") == 0) {
- add_split_dependencies(new_node, field_value, EDGE_ENHANCES);
+ field_num = compare_string_array(field_names, field_name);
+ switch(field_num) {
+ case 0: /* Package */
+ new_node->name = search_name_hashtable(field_value);
+ break;
+ case 1: /* Version */
+ new_node->version = search_name_hashtable(field_value);
+ break;
+ case 2: /* Pre-Depends */
+ add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS);
+ break;
+ case 3: /* Depends */
+ add_split_dependencies(new_node, field_value, EDGE_DEPENDS);
+ break;
+ case 4: /* Replaces */
+ add_split_dependencies(new_node, field_value, EDGE_REPLACES);
+ break;
+ case 5: /* Provides */
+ add_split_dependencies(new_node, field_value, EDGE_PROVIDES);
+ break;
+ case 6: /* Conflicts */
+ add_split_dependencies(new_node, field_value, EDGE_CONFLICTS);
+ break;
+ case 7: /* Suggests */
+ add_split_dependencies(new_node, field_value, EDGE_SUGGESTS);
+ break;
+ case 8: /* Recommends */
+ add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS);
+ break;
+ case 9: /* Enhances */
+ add_split_dependencies(new_node, field_value, EDGE_ENHANCES);
+ break;
}
fill_package_struct_cleanup:
- if (field_name) {
- free(field_name);
- }
- if (field_value) {
- free(field_value);
- }
+ free(field_name);
+ free(field_value);
}
if (new_node->version == search_name_hashtable("unknown")) {
status_string += strspn(status_string, " ");
}
len = strcspn(status_string, " \n\0");
- state_sub_string = xstrndup(status_string, len);
+ state_sub_string = bb_xstrndup(status_string, len);
state_sub_num = search_name_hashtable(state_sub_string);
free(state_sub_string);
return(state_sub_num);
status_len = new_value_len;
break;
default:
- error_msg_and_die("DEBUG ONLY: this shouldnt happen");
+ bb_error_msg_and_die("DEBUG ONLY: this shouldnt happen");
}
new_status = (char *) xmalloc(want_len + flag_len + status_len + 3);
status_node_t *status_node = NULL;
unsigned int status_num;
- status_file = xfopen(filename, "r");
+ status_file = bb_xfopen(filename, "r");
while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) {
const unsigned int package_num = fill_package_struct(control_buffer);
if (package_num != -1) {
if (status_line != NULL) {
status_line += 7;
status_line += strspn(status_line, " \n\t");
- status_line = xstrndup(status_line, strcspn(status_line, "\n\0"));
+ status_line = bb_xstrndup(status_line, strcspn(status_line, "\n\0"));
status_node->status = search_name_hashtable(status_line);
free(status_line);
}
/* This could do with a cleanup */
void write_status_file(deb_file_t **deb_file)
{
- FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r");
- FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w");
+ FILE *old_status_file = bb_xfopen("/var/lib/dpkg/status", "r");
+ FILE *new_status_file = bb_xfopen("/var/lib/dpkg/status.udeb", "w");
char *package_name;
char *status_from_file;
char *control_buffer = NULL;
tmp_string += 8;
tmp_string += strspn(tmp_string, " \n\t");
- package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
+ package_name = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
write_flag = FALSE;
tmp_string = strstr(control_buffer, "Status:");
if (tmp_string != NULL) {
/* Seperate the status value from the control buffer */
tmp_string += 7;
tmp_string += strspn(tmp_string, " \n\t");
- status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
+ status_from_file = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n"));
} else {
status_from_file = NULL;
}
}
/* This is temperary, debugging only */
if (deb_file[i] == NULL) {
- error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name);
+ bb_error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name);
}
}
else if (strcmp("not-installed", name_hashtable[state_status]) == 0) {
fprintf(new_status_file, "%s\n\n", control_buffer);
}
- if (status_from_file != NULL) {
- free(status_from_file);
- }
+ free(status_from_file);
free(package_name);
free(control_buffer);
}
if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
struct stat stat_buf;
if (stat("/var/lib/dpkg/status", &stat_buf) == 0) {
- error_msg_and_die("Couldnt create backup status file");
+ bb_error_msg_and_die("Couldnt create backup status file");
}
/* Its ok if renaming the status file fails becasue status
* file doesnt exist, maybe we are starting from scratch */
- error_msg("No status file found, creating new one");
+ bb_error_msg("No status file found, creating new one");
}
if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) {
- error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
+ bb_error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
}
}
}
if (result) {
- error_msg_and_die("Package %s conflicts with %s",
+ bb_error_msg_and_die("Package %s conflicts with %s",
name_hashtable[package_node->name],
name_hashtable[package_edge->name]);
}
}
if (result) {
- error_msg_and_die("Package %s pre-depends on %s, but it is not installed",
+ bb_error_msg_and_die("Package %s pre-depends on %s, but it is not installed",
name_hashtable[package_node->name],
name_hashtable[package_edge->name]);
}
}
/* It must be already installed, or to be installed */
if (result) {
- error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed",
+ bb_error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed",
name_hashtable[package_node->name],
name_hashtable[package_edge->name]);
}
FILE *list_stream;
char **file_list = NULL;
char *line = NULL;
- int length = 0;
int count = 0;
/* dont use [xw]fopen here, handle error ourself */
return(NULL);
}
- while (getline(&line, &length, list_stream) != -1) {
+ while ((line = bb_get_chomped_line_from_file(list_stream)) != NULL) {
file_list = xrealloc(file_list, sizeof(char *) * (count + 2));
- chomp(line);
- file_list[count] = xstrdup(line);
+ file_list[count] = line;
count++;
}
fclose(list_stream);
- free(line);
if (count == 0) {
return(NULL);
/* run prerm script */
return_value = run_package_script(package_name, "prerm");
if (return_value == -1) {
- error_msg_and_die("script failed, prerm failure");
+ bb_error_msg_and_die("script failed, prerm failure");
}
/* Create a list of files to remove, and a seperate list of those to keep */
/* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
exclude_files = xmalloc(sizeof(char*) * 3);
- exclude_files[0] = xstrdup(conffile_name);
+ exclude_files[0] = bb_xstrdup(conffile_name);
exclude_files[1] = xmalloc(package_name_length + 27);
sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name);
exclude_files[2] = NULL;
/* run prerm script */
if (run_package_script(package_name, "prerm") != 0) {
- error_msg_and_die("script failed, prerm failure");
+ bb_error_msg_and_die("script failed, prerm failure");
}
/* Create a list of files to remove */
/* run postrm script */
if (run_package_script(package_name, "postrm") == -1) {
- error_msg_and_die("postrm fialure.. set status to what?");
+ bb_error_msg_and_die("postrm fialure.. set status to what?");
}
/* Change package status */
set_status(status_num, "not-installed", 3);
}
-void unpack_package(deb_file_t *deb_file)
+static archive_handle_t *init_archive_deb_ar(const char *filename)
+{
+ archive_handle_t *ar_handle;
+
+ /* Setup an ar archive handle that refers to the gzip sub archive */
+ ar_handle = init_handle();
+ ar_handle->filter = filter_accept_list_reassign;
+ ar_handle->src_fd = bb_xopen(filename, O_RDONLY);
+
+ return(ar_handle);
+}
+
+static void init_archive_deb_control(archive_handle_t *ar_handle)
+{
+ archive_handle_t *tar_handle;
+
+ /* Setup the tar archive handle */
+ tar_handle = init_handle();
+ tar_handle->filter = filter_accept_list;
+ tar_handle->src_fd = ar_handle->src_fd;
+
+ /* We dont care about data.tar.* or debian-binary, just control.tar.* */
+#ifdef CONFIG_FEATURE_DEB_TAR_GZ
+ ar_handle->accept = llist_add_to(NULL, "control.tar.gz");
+#endif
+#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
+ ar_handle->accept = llist_add_to(ar_handle->accept, "control.tar.bz2");
+#endif
+
+ /* Assign the tar handle as a subarchive of the ar handle */
+ ar_handle->sub_archive = tar_handle;
+
+ return;
+}
+
+static void init_archive_deb_data(archive_handle_t *ar_handle)
+{
+ archive_handle_t *tar_handle;
+
+ /* Setup the tar archive handle */
+ tar_handle = init_handle();
+ tar_handle->filter = filter_accept_all;
+ tar_handle->src_fd = ar_handle->src_fd;
+
+ /* We dont care about data.tar.* or debian-binary, just control.tar.* */
+#ifdef CONFIG_FEATURE_DEB_TAR_GZ
+ tar_handle->accept = llist_add_to(NULL, "data.tar.gz");
+#endif
+#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
+ tar_handle->accept = llist_add_to(ar_handle->accept, "data.tar.bz2");
+#endif
+
+ /* Assign the tar handle as a subarchive of the ar handle */
+ ar_handle->sub_archive = tar_handle;
+
+ return;
+}
+
+static char *deb_extract_control_file_to_buffer(archive_handle_t *ar_handle, llist_t *accept)
+{
+ ar_handle->sub_archive->action_data = data_extract_to_buffer;
+ ar_handle->sub_archive->accept = accept;
+
+ unpack_ar_archive(ar_handle);
+ close(ar_handle->src_fd);
+
+ return(ar_handle->sub_archive->buffer);
+}
+
+static void data_extract_all_prefix(archive_handle_t *archive_handle)
+{
+ char *name_ptr = archive_handle->file_header->name;
+
+ name_ptr += strspn(name_ptr, "./");
+ if (name_ptr[0] != '\0') {
+ archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 2 + strlen(name_ptr));
+ strcpy(archive_handle->file_header->name, archive_handle->buffer);
+ strcat(archive_handle->file_header->name, name_ptr);
+ data_extract_all(archive_handle);
+ }
+ return;
+}
+
+static void unpack_package(deb_file_t *deb_file)
{
const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
const unsigned int status_num = search_status_hashtable(package_name);
const unsigned int status_package_num = status_hashtable[status_num]->package;
-
- FILE *out_stream;
char *info_prefix;
+ archive_handle_t *archive_handle;
+ FILE *out_stream;
/* If existing version, remove it first */
if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) {
/* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */
info_prefix = (char *) xmalloc(strlen(package_name) + 20 + 4 + 2);
sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name);
- deb_extract(deb_file->filename, stdout, (extract_quiet | extract_control_tar_gz | extract_all_to_fs | extract_unconditional), info_prefix, NULL);
+ archive_handle = init_archive_deb_ar(deb_file->filename);
+ init_archive_deb_control(archive_handle);
+ archive_handle->sub_archive->action_data = data_extract_all_prefix;
+ archive_handle->sub_archive->buffer = info_prefix;
+ unpack_ar_archive(archive_handle);
/* Run the preinst prior to extracting */
if (run_package_script(package_name, "preinst") != 0) {
/* when preinst returns exit code != 0 then quit installation process */
- error_msg_and_die("subprocess pre-installation script returned error.");
+ bb_error_msg_and_die("subprocess pre-installation script returned error.");
}
/* Extract data.tar.gz to the root directory */
- deb_extract(deb_file->filename, stdout, (extract_quiet | extract_data_tar_gz | extract_all_to_fs | extract_unconditional), "/", NULL);
+ archive_handle = init_archive_deb_ar(deb_file->filename);
+ init_archive_deb_data(archive_handle);
+ unpack_ar_archive(archive_handle);
/* Create the list file */
strcat(info_prefix, "list");
- out_stream = xfopen(info_prefix, "w");
- deb_extract(deb_file->filename, out_stream, (extract_quiet | extract_data_tar_gz | extract_list), "/", NULL);
+ out_stream = bb_xfopen(info_prefix, "w");
+ while (archive_handle->passed) {
+ /* blindly skip over the leading '.' */
+ fputs(archive_handle->passed->data + 1, out_stream);
+ fputc('\n', out_stream);
+ archive_handle->passed = archive_handle->passed->link;
+ }
fclose(out_stream);
/* change status */
/* Run the postinst script */
if (run_package_script(package_name, "postinst") != 0) {
/* TODO: handle failure gracefully */
- error_msg_and_die("postrm failure.. set status to what?");
+ bb_error_msg_and_die("postrm failure.. set status to what?");
}
/* Change status to reflect success */
set_status(status_num, "install", 1);
dpkg_opt |= dpkg_opt_filename;
break;
default:
- show_usage();
+ bb_show_usage();
}
}
/* check for non-otion argument if expected */
if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) {
- show_usage();
+ bb_show_usage();
}
/* puts("(Reading database ... xxxxx files and directories installed.)"); */
deb_file = xrealloc(deb_file, sizeof(deb_file_t *) * (deb_count + 2));
deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t));
if (dpkg_opt & dpkg_opt_filename) {
- deb_file[deb_count]->filename = xstrdup(argv[optind]);
- deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
+ archive_handle_t *archive_handle;
+ llist_t *control_list = NULL;
+
+ /* Extract the control file */
+ control_list = llist_add_to(NULL, "./control");
+ archive_handle = init_archive_deb_ar(argv[optind]);
+ init_archive_deb_control(archive_handle);
+ deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
if (deb_file[deb_count]->control_file == NULL) {
- error_msg_and_die("Couldnt extract control file");
+ bb_error_msg_and_die("Couldnt extract control file");
}
+ deb_file[deb_count]->filename = bb_xstrdup(argv[optind]);
package_num = fill_package_struct(deb_file[deb_count]->control_file);
if (package_num == -1) {
- error_msg("Invalid control file in %s", argv[optind]);
+ bb_error_msg("Invalid control file in %s", argv[optind]);
continue;
}
deb_file[deb_count]->package = (unsigned int) package_num;
+
/* Add the package to the status hashtable */
if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) {
status_node = (status_node_t *) xmalloc(sizeof(status_node_t));
search_name_hashtable(argv[optind]),
search_name_hashtable("ANY"), VER_ANY);
if (package_hashtable[deb_file[deb_count]->package] == NULL) {
- error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
+ bb_error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
}
state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3);
if (dpkg_opt & dpkg_opt_remove) {
if ((strcmp(name_hashtable[state_status], "not-installed") == 0) ||
(strcmp(name_hashtable[state_status], "config-files") == 0)) {
- error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
+ bb_error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
}
}
else if (dpkg_opt & dpkg_opt_purge) {
/* if package status is "conf-files" then its ok */
if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
- error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
+ bb_error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
}
}
}
/* TODO: check dependencies before removing */
if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) {
if (!check_deps(deb_file, 0, deb_count)) {
- error_msg_and_die("Dependency check failed");
+ bb_error_msg_and_die("Dependency check failed");
}
}
free(deb_file);
for (i = 0; i < NAME_HASH_PRIME; i++) {
- if (name_hashtable[i] != NULL) {
- free(name_hashtable[i]);
- }
+ free(name_hashtable[i]);
}
for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
}
for (i = 0; i < STATUS_HASH_PRIME; i++) {
- if (status_hashtable[i] != NULL) {
- free(status_hashtable[i]);
- }
+ free(status_hashtable[i]);
}
return(EXIT_SUCCESS);