int conffile_has_been_modified(conffile_t * conffile) { char *md5sum; char *filename = conffile->name; char *root_filename; int ret = 1; if (conffile->value == NULL) { opkg_msg(NOTICE, "Conffile %s has no md5sum.\n", conffile->name); return 1; } root_filename = root_filename_alloc(filename); md5sum = file_md5sum_alloc(root_filename); if (md5sum && (ret = strcmp(md5sum, conffile->value))) { opkg_msg(INFO, "Conffile %s:\n\told md5=%s\n\tnew md5=%s\n", conffile->name, md5sum, conffile->value); } free(root_filename); if (md5sum) free(md5sum); return ret; }
int conffile_has_been_modified(ipkg_conf_t *conf, conffile_t *conffile) { char *md5sum; char *filename = conffile->name; char *root_filename; int ret; if (conffile->value == NULL) { ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name); return 1; } root_filename = root_filename_alloc(conf, filename); md5sum = file_md5sum_alloc(root_filename); ret = strcmp(md5sum, conffile->value); if (ret) { ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__, conffile->name, md5sum, conffile->value); } free(root_filename); free(md5sum); return ret; }
int recursively_install(char* pkg_name, char* pkg_version, char* install_root_name, char* link_to_root, char* overlay_path, int is_upgrade, int overwrite_config, int overwrite_other_package_files, char* tmp_dir, opkg_conf* conf, string_map* package_data, string_map* install_called_pkgs) { int err=0; /* variables not allocated in this function, do not need to be freed */ string_map* install_pkg_data = get_package_with_version(package_data, pkg_name, pkg_version); char* src_file_path = get_string_map_element(install_pkg_data, "Install-File-Location"); char* src_id = get_string_map_element(install_pkg_data, "Source-ID"); char* pkg_filename = get_string_map_element(install_pkg_data, "Filename"); string_map* pkg_dependencies = get_string_map_element(install_pkg_data, "Required-Depends"); char* install_root_path = get_string_map_element(conf->dest_names, install_root_name); char* link_root_path = link_to_root != NULL && safe_strcmp(link_to_root, install_root_name) != 0 ? get_string_map_element(conf->dest_names, link_to_root) : NULL; char* base_url = NULL; /* variables that may need to be freed */ char* pkg_dest = NULL; string_map* files_to_link = NULL; char* info_dir = NULL; char* control_name_prefix = NULL; char* list_file_name = NULL; string_map* conf_files = NULL; string_map* copied_conf_files = NULL; int install_root_len = strlen(install_root_path); char* fs_terminated_install_root = install_root_path[install_root_len-1] == '/' ? strdup(install_root_path) : dynamic_strcat(2, install_root_path, "/"); int overlay_root_len; char* fs_terminated_overlay_root = NULL; if(overlay_path != NULL) { overlay_root_len = strlen(overlay_path); fs_terminated_overlay_root = overlay_path[overlay_root_len-1] == '/' ? strdup(overlay_path) : dynamic_strcat(2, overlay_path, "/"); } else { fs_terminated_overlay_root = strdup(fs_terminated_install_root); } int link_root_len; char* fs_terminated_link_root = NULL; if(link_root_path != NULL) { link_root_len = strlen(link_root_path); fs_terminated_link_root = link_root_path[link_root_len-1] == '/' ? strdup(link_root_path) : dynamic_strcat(2, link_root_path, "/"); } set_string_map_element(install_called_pkgs, pkg_name, strdup("D")); if(pkg_dependencies != NULL) { //recurse unsigned long num_deps; char** deps = get_string_map_keys(pkg_dependencies, &num_deps); int dep_index; for(dep_index=0; err == 0 && dep_index < num_deps ; dep_index++) { if(get_string_map_element(install_called_pkgs, deps[dep_index]) == NULL ) { char** dep_def = get_string_map_element(pkg_dependencies, deps[dep_index]); int is_current; char* matching_version; string_map* dep_pkg = get_package_current_or_latest_matching(package_data, deps[dep_index], dep_def, &is_current, &matching_version); if(dep_pkg != NULL) { char* dep_status = get_string_map_element(dep_pkg, "Status"); if(strstr(dep_status, " half-installed") != NULL) { err = recursively_install(deps[dep_index], matching_version, install_root_name, link_to_root, overlay_path, is_upgrade, overwrite_config, overwrite_other_package_files, tmp_dir, conf, package_data, install_called_pkgs); } } else { err = 1; } } } } if(install_root_path == NULL || ( src_id == NULL && install_pkg_data == NULL) || (pkg_filename == NULL && install_pkg_data == NULL) ) { //sanity check err = 1; } if(err == 0) { printf("Preparing to install package %s...\n", pkg_name); } if(err == 0 && src_file_path == NULL) { //determine source url string_map* src_lists[2] = { conf->gzip_sources, conf->plain_sources }; int src_list_index; for(src_list_index=0; src_list_index < 2 && base_url == NULL; src_list_index++) { base_url = (char*)get_string_map_element(src_lists[src_list_index], src_id); } err = base_url == NULL ? 1 : err; if(err == 1) { fprintf(stderr, "ERROR: Could determine download URL for package %s\n", pkg_name); } } if(err == 0 && src_file_path == NULL) { //download package printf("\tDownloading...\n"); char* src_url = dynamic_strcat(3, base_url, "/", pkg_filename); pkg_dest = dynamic_strcat(3, tmp_dir, "/", pkg_name); FILE* package_file = fopen(pkg_dest, "w"); if(package_file != NULL) { err = write_url_to_stream(src_url, "gpkg", NULL, package_file, NULL); fclose(package_file); } else { err = 1; } if(err == 1) { fprintf(stderr, "ERROR: Could not download package %s\n", pkg_name); } free(src_url); } if(err == 0 && src_file_path != NULL) { pkg_dest = strdup(src_file_path); } if(err == 0 && src_file_path == NULL) { //check md5sum char* md5sum = file_md5sum_alloc(pkg_dest); char* expected_md5sum = (char*)get_string_map_element(install_pkg_data, "MD5Sum"); //printf("md5sum = %s\n", md5sum); //printf("package md5sum = %s\n", (char*)get_string_map_element(install_pkg_data, "MD5Sum")); if(md5sum == NULL || expected_md5sum == NULL) { fprintf(stderr, "ERROR: Expected MD5Sum for %s not specified, cannot verify package\n", pkg_name); err = 1; } else if (safe_strcmp(md5sum, expected_md5sum) != 0) { fprintf(stderr, "ERROR: MD5Sum mismatch for %s package\n", pkg_name); fprintf(stderr, " Expected: %s\n", expected_md5sum); fprintf(stderr, " Downloaded: %s\n\n", md5sum); err = 1; } else { printf("\tDownloaded %s successfully.\n\tInstalling %s...\n", pkg_name, pkg_name); } if(md5sum != NULL) { free(md5sum); } } if(err == 0) { // Extract list file contaiing list of files to install info_dir = dynamic_strcat(2, fs_terminated_overlay_root, "usr/lib/opkg/info"); control_name_prefix = dynamic_strcat(4, info_dir, "/", pkg_name, "."); list_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".list"); mkdir_p(info_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); FILE* list_file = fopen(list_file_name, "w"); deb_extract( pkg_dest, list_file, extract_quiet | extract_data_tar_gz | extract_list, NULL, NULL, &err); fclose(list_file); if(err) { rm_r(list_file_name); fprintf(stderr, "ERROR: could not extract file list from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //extract control files deb_extract( pkg_dest, stderr, extract_control_tar_gz | extract_all_to_fs| extract_preserve_date | extract_unconditional, control_name_prefix, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract control files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //check for file conflicts & correct list file to contain real root name in file paths unsigned long num_list_lines; char** list_file_lines = get_file_lines(list_file_name, &num_list_lines); char* conf_file_path = dynamic_strcat(4, info_dir, "/", pkg_name, ".conffiles"); if(path_exists(conf_file_path)) { unsigned long num_conf_lines; char** conf_file_lines = get_file_lines(conf_file_path, &num_conf_lines); int conf_line_index; conf_files = initialize_string_map(1); for(conf_line_index=0; conf_line_index < num_conf_lines; conf_line_index++) { char* adjusted_conf_path = dynamic_strcat(2, fs_terminated_install_root, conf_file_lines[conf_line_index] + 1); set_string_map_element(conf_files, adjusted_conf_path, strdup("D")); free(adjusted_conf_path); } free_null_terminated_string_array(conf_file_lines); } free(conf_file_path); FILE* list_file = fopen(list_file_name, "w"); int line_index; for(line_index=0; line_index < num_list_lines && (!err) ; line_index++) { int line_len = strlen( list_file_lines[line_index] ); if(line_len > 2) { if(list_file_lines[line_index][0] == '.' && list_file_lines[line_index][1] == '/' && list_file_lines[line_index][line_len-1] != '/') { char* adjusted_file_path = dynamic_strcat(2, fs_terminated_install_root, list_file_lines[line_index] + 2); int is_conf_file = conf_files != NULL ? (get_string_map_element(conf_files, adjusted_file_path) != NULL ? 1 : 0) : 0; if(strcmp(pkg_name, "opkg") == 0 && strcmp(list_file_lines[line_index], "./bin/opkg") == 0 && path_exists("/bin/opkg") == PATH_IS_SYMLINK) { //very special case: we're installing opkg, and here all the preliminary checks have already been passed //remove symlink placeholder to gpkg from /bin/opkg if it exists rm_r("/bin/opkg"); } err = path_exists(adjusted_file_path) && is_conf_file == 0 && overwrite_other_package_files == 0 ? 1 : 0; if(err) { fprintf(stderr, "ERROR: file '%s'\n", adjusted_file_path); fprintf(stderr, " from package %s already exists.\n\n", pkg_name); } else { fprintf(list_file, "%s\n", adjusted_file_path); if(link_root_path != NULL) { char* link_to_path = dynamic_strcat(2, fs_terminated_link_root, list_file_lines[line_index] + 2); files_to_link = files_to_link == NULL ? initialize_string_map(1) : files_to_link; set_string_map_element(files_to_link, adjusted_file_path, link_to_path); //don't free link_to_path, should be freed with files_to_link map } if(is_conf_file && path_exists(adjusted_file_path) && overwrite_config == 0) { char* tmp_conf_path = dynamic_strcat(2, tmp_dir, adjusted_file_path); cp(adjusted_file_path, tmp_conf_path); copied_conf_files = copied_conf_files == NULL ? initialize_string_map(1) : copied_conf_files; set_string_map_element(copied_conf_files, adjusted_file_path, tmp_conf_path); //don't free tmp_conf_path, should be freed with copied_conf_files map } } free(adjusted_file_path); } } } fclose(list_file); if(list_file_lines != NULL) { free_null_terminated_string_array(list_file_lines); } } if(err == 0) { //run preinst err = run_script_if_exists(install_root_path, link_root_path, pkg_name, "preinst", (is_upgrade ? "upgrade" : "install") ); } if(err == 0) { //extract package files deb_extract( pkg_dest, stderr, extract_data_tar_gz | extract_all_to_fs| extract_preserve_date| extract_unconditional, fs_terminated_overlay_root, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract application files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } //move any conf files back if(copied_conf_files != NULL) { unsigned long num_conf_paths; char** conf_paths = get_string_map_keys(copied_conf_files, &num_conf_paths); int conf_index; for(conf_index=0; conf_index < num_conf_paths; conf_index++) { char* tmp_conf_path = get_string_map_element(copied_conf_files, conf_paths[conf_index]); cp(tmp_conf_path, conf_paths[conf_index]); } destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_conf_paths); if(conf_paths != NULL ) { free_null_terminated_string_array(conf_paths); } copied_conf_files = NULL; } } if(err == 0 && files_to_link != NULL) { unsigned long num_files; char** real_files = get_string_map_keys(files_to_link, &num_files); int file_index; if(num_files > 0) { char* link_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".linked"); FILE* link_file = fopen(link_file_name, "w"); for(file_index=0; link_file != NULL && file_index < num_files; file_index++) { char* link_path = get_string_map_element(files_to_link, real_files[file_index]); if(!path_exists(link_path)) { int sym_success; mkdir_p(link_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); rm_r(link_path); sym_success = symlink(real_files[file_index], link_path); fprintf(link_file, "%s\n", link_path); } } if(link_file != NULL) { fclose(link_file); } free(link_file_name); } destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_files); free_null_terminated_string_array(real_files); files_to_link = NULL; } if(err == 0) { //run postinst int warn = run_script_if_exists(install_root_path, link_root_path, pkg_name, "postinst", (is_upgrade ? "upgrade" : "install") ); if(warn != 0) { fprintf(stderr, "Warning: postinstall script failed for package %s.\n", pkg_name); } } if(err == 0) { // remove downloaded package file in tmp dir & print success if(src_file_path == NULL) { rm_r(pkg_dest); } printf("\tSuccessfully installed %s.\n", pkg_name); } //cleanup unsigned long num_destroyed; free_if_not_null(pkg_dest); free_if_not_null(info_dir); free_if_not_null(control_name_prefix); free_if_not_null(list_file_name); free_if_not_null(fs_terminated_install_root); free_if_not_null(fs_terminated_overlay_root); free_if_not_null(fs_terminated_link_root); if(files_to_link != NULL) { destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(conf_files != NULL) { destroy_string_map(conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(copied_conf_files!= NULL){ destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } return err; }