static char * build_cache_tree(const char *path) { LOG(LOG_DEBUG, "building cache dir for '%s'", path); char *local = NULL; char *tmp_local = NULL; char *dir = NULL; struct stat st; /* ignore the leading spaces */ while (path && '/' == *path) path++; local = tmpstr_printf("%s/%s", conf->cache_dir, path); tmp_local = strdup(local); if (! tmp_local) { LOG(LOG_CRIT, "strdup(%s): %s", tmp_local, strerror(errno)); return NULL; } dir = tmpstr_printf("%s", dirname(tmp_local)); if (-1 == stat(dir, &st)) { if (ENOENT == errno) mkdir_tree(dir); else LOG(LOG_ERR, "stat(%s): %s", dir, strerror(errno)); } free(tmp_local); return local; }
void set_server_details() { char id[20]; int num; safe_strncpy(id, check_server_id_on_command_line(), sizeof(id)); if (!strcmp(id, "")) { safe_strncpy(id, "main", sizeof(id)); } num = find_server_from_id(id); if (num == -1) { // Oops... what they they specify on the command line? LOG_ERROR("Server profile not found in servers.lst for server: %s. Failover to server: main.", id); // Failover to the main server num = find_server_from_id("main"); if (num == -1) { // Error, this is a problem! LOG_ERROR("Fatal error: Server profile not found in servers.lst for server: main"); exit(1); } } // We found a valid profile so set some vars LOG_DEBUG("Using the server profile: %s", servers[num].id); cur_server = num; safe_strncpy((char *)server_address, (char *)servers[num].address, sizeof(server_address)); port = servers[num].port; // Check if the config directory for the profile exists and if not then create and // copy main's ini file into it if (!check_configdir()) { char src[1000]; char dest[1000]; mkdir_tree(get_path_config(), 0); // First, try to copy the ini file out of $CONF/main safe_snprintf(src, sizeof(src), "%smain/el.ini", get_path_config_base()); safe_snprintf(dest, sizeof(dest), "%sel.ini", get_path_config()); copy_file(src, dest); // Secondly, try to copy the ini file out of $CONF (this will fail without harm if above succeeds) safe_snprintf(src, sizeof(src), "%s/el.ini", get_path_config_base()); safe_snprintf(dest, sizeof(dest), "%sel.ini", get_path_config()); copy_file(src, dest); } }
int pkg_archive_extract (Pkg pkg, PkgContentFile *con_files, struct archive *a, char **file_path, PkgContentFile *con_file, const char *prefix, INSTALL_STEP_CALLBACK(s_callback)){ struct archive_entry *entry; char *file, *dir; const char *link_s; int r, rval = 1, fd = 0, len; u_int i = 0, ii; u_short ok = 0; pboolean is_slink, is_hlink; if(a == NULL) RETURN_P_ERR(P_ERR_INVALID_DESCRIPTOR, -1); for(;;){ r = archive_read_next_header(a, &entry); if(r == ARCHIVE_EOF){ rval = 0; break; } else if(r != ARCHIVE_OK){ rval = -1; break; } is_slink = is_hlink = 0; if((link_s = archive_entry_symlink(entry)) != NULL){ is_slink = 1; } else if((link_s = archive_entry_hardlink(entry)) != NULL){ is_hlink = 1; } file = (char *)archive_entry_pathname(entry); if(file == NULL){ rval = -1; break; } char *fp; if(file[0] != '+') for(i = 0; con_files[i] != NULL; i++){ len = strlen(con_files[i]->cwd); fp = con_files[i]->file; fp = fp + len; if(fp[0] == '/') fp++; if(strlen(fp) == strlen(file)) if(!strcmp(fp, file)){ ok = 1; break; } } if(ok){ if(s_callback != NULL) s_callback(0, 0, con_files[i]->file, PSTEP_T_FCPY, 0, STAGE_PRE_T); *con_file = con_files[i]; *file_path = strdup(con_files[i]->file); len = strlen(*file_path); dir = (char *)malloc(len); assert(dir != NULL); memset(dir, 0, len); strncpy(dir, *file_path, len); for(ii = (len - 1);ii != 0;ii--) if(dir[ii] == '/'){ dir[ii] = (char)0; break; } if(access(dir, F_OK) == -1) r = mkdir_tree(dir, DEF_O_MODE); } if(ok && (is_slink || is_hlink)){ const char *dir; char *dir_s = NULL; *file_path = strdup(con_files[i]->file); dir = _find_link(link_s, con_files); if(!dir){ asprintf(&dir_s, "%s/%s", prefix, link_s); assert(dir_s != NULL); dir = dir_s; } if(is_slink){ r = symlink(dir, con_files[i]->file); } else r = link(dir, con_files[i]->file); free(dir_s); if(r == -1) RETURN_P_ERRNO(-1); break; } else if(ok){ if(!r){ fd = open(*file_path, O_CREAT | O_WRONLY, DEF_O_MODE); if(fd == -1){ rval = -1; } else { rval = archive_read_data_into_fd(a, fd); if(rval < 0){ rval = -1; } else rval = 1; close(fd); char *script = NULL; if(con_files[i]->mode != NULL){ asprintf(&script, "chmod %s %s 2>/dev/null", con_files[i]->mode, *file_path); assert(script != NULL); pkg_exec_script(script); free(script); } if(con_files[i]->group != NULL || con_files[i]->user != NULL){ char *group; char *user; if(con_files[i]->group != NULL){ asprintf(&group, ":%s", con_files[i]->group); assert(group != NULL); } else group = ""; if(con_files[i]->user != NULL){ user = con_files[i]->user; } else user = getenv("USER"); asprintf(&script, "chown %s%s %s 2>/dev/null", user, group, *file_path); assert(script != NULL); pkg_exec_script(script); free(script); if(group[0] != '\0') free(group); } } } else rval = -1; free(dir); break; } } if(rval == -1){ if(fd == -1){ RETURN_P_ERRNO(-1); } else RETURN_P_LARCHIVE(-1, a); } return rval; }