void generate_tmp_file_name(const char prefix[], char buf[], size_t buf_len) { snprintf(buf, buf_len, "%s/%s", get_tmpdir(), prefix); #ifdef _WIN32 to_forward_slash(buf); #endif copy_str(buf, buf_len, make_name_unique(buf)); }
/* * dispatch_image: * Throw some image data at the display process. */ void dispatch_image(const char *mname, const unsigned char *data, const size_t len) { const char *name; name = tmpfile_write(mname, data, len); if (name == NULL) return; if (!play_media) printf("%s/%s\n", get_tmpdir(), name); #ifndef NO_DISPLAY_WINDOW else display_send_img(name, TMPNAMELEN); #endif /* !NO_DISPLAY_WINDOW */ }
/* Runs command in a background and redirects its stdout and stderr streams to * file streams which are set. Returns (pid_t)0 or (pid_t)-1 on error. */ static pid_t background_and_capture_internal(char cmd[], int user_sh, FILE **out, FILE **err, int out_pipe[2], int err_pipe[2]) { wchar_t *args[4]; char cwd[PATH_MAX]; int code; wchar_t *final_wide_cmd; wchar_t *wide_sh = NULL; if(_dup2(out_pipe[1], _fileno(stdout)) != 0) return (pid_t)-1; if(_dup2(err_pipe[1], _fileno(stderr)) != 0) return (pid_t)-1; cwd[0] = '\0'; if(get_cwd(cwd, sizeof(cwd)) != NULL) { if(is_unc_path(cwd)) { (void)chdir(get_tmpdir()); } } final_wide_cmd = to_wide(cmd); wide_sh = to_wide(user_sh ? cfg.shell : "cmd"); if(!user_sh || curr_stats.shell_type == ST_CMD) { args[0] = wide_sh; args[1] = L"/C"; args[2] = final_wide_cmd; args[3] = NULL; } else { args[0] = wide_sh; args[1] = L"-c"; args[2] = final_wide_cmd; args[3] = NULL; } code = _wspawnvp(P_NOWAIT, args[0], (const wchar_t **)args); free(wide_sh); free(final_wide_cmd); if(is_unc_path(cwd)) { (void)chdir(cwd); } if(code == 0) { return (pid_t)-1; } if((*out = _fdopen(out_pipe[0], "r")) == NULL) return (pid_t)-1; if((*err = _fdopen(err_pipe[0], "r")) == NULL) { fclose(*out); return (pid_t)-1; } return 0; }
static void * extract(void *p) { locale_t archive_locale; locale_t old_locale; struct archive *a; struct archive *ext = NULL; struct archive_entry *entry = NULL; int r; int flags; struct extract_data *data = (struct extract_data *)p; flags = data->flags; int exitval = -EFAULT; /* * Enable system locale - change from the standard (C) to system locale. * This allows libarchive (in case it is activated) to handle filenames. * We only change LC_CTYPE since libarchive only needs the charset set. * We don't use LC_ALL because it causes problems on some systems. * We restore the original LC_CTYPE after extraction to avoid side effects. * We use uselocale instead of setlocale to avoid setting LC_CTYPE globally. * See on libarchive Website for a more complete description of the issue: * https://github.com/libarchive/libarchive/issues/587 * https://github.com/libarchive/libarchive/wiki/Filenames */ archive_locale = newlocale(LC_CTYPE_MASK, "", (locale_t)0); old_locale = uselocale(archive_locale); a = archive_read_new(); if (!a) { goto out; } ext = archive_write_disk_new(); if (!ext) { goto out; } archive_write_disk_set_options(ext, flags); /* * Note: archive_write_disk_set_standard_lookup() is useful * here, but it requires library routines that can add 500k or * more to a static executable. */ archive_read_support_format_all(a); archive_read_support_filter_all(a); /* * On my system, enabling other archive formats adds 20k-30k * each. Enabling gzip decompression adds about 20k. * Enabling bzip2 is more expensive because the libbz2 library * isn't very well factored. */ char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); if ((r = archive_read_open_filename(a, FIFO, 4096))) { ERROR("archive_read_open_filename(): %s %d", archive_error_string(a), r); goto out; } for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { ERROR("archive_read_next_header(): %s %d", archive_error_string(a), 1); goto out; } if (debug) TRACE("Extracting %s", archive_entry_pathname(entry)); r = archive_write_header(ext, entry); if (r != ARCHIVE_OK) TRACE("archive_write_header(): %s", archive_error_string(ext)); else { copy_data(a, ext); r = archive_write_finish_entry(ext); if (r != ARCHIVE_OK) { ERROR("archive_write_finish_entry(): %s", archive_error_string(ext)); goto out; } } } exitval = 0; out: if (ext) { r = archive_write_free(ext); if (r) { ERROR("archive_write_free(): %s %d", archive_error_string(a), r); exitval = -EFAULT; } } if (a) { archive_read_close(a); archive_read_free(a); } uselocale(old_locale); data->exitval = exitval; pthread_exit(NULL); }
static int remove_directory(const char* path) { char* dpath = alloca(strlen(get_tmpdir())+strlen(path)+1); sprintf(dpath, "%s%s", get_tmpdir(), path); return nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS); }
static int install_archive_image(struct img_type *img, void __attribute__ ((__unused__)) *data) { char path[255]; int fdout = -1; int ret = -1; int thread_ret = -1; char pwd[256] = "\0"; struct extract_data tf; pthread_attr_t attr; int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; int is_mounted = 0; int exitval = -EFAULT; char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); char * FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); if (strlen(img->path) == 0) { TRACE("Missing path attribute"); return -1; } if (use_mount) { ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem);
static int install_from_file(char *fname, int check) { int fdsw; off_t pos; int ret; if (!strlen(fname)) { ERROR("Image not found...please reboot"); exit(EXIT_FAILURE); } fdsw = open(fname, O_RDONLY); if (fdsw < 0) { fdsw = searching_for_image(fname); if (fdsw < 0) { ERROR("Image Software cannot be read...exiting !"); exit(EXIT_FAILURE); } } pos = 0; ret = extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME, &pos); #ifdef CONFIG_SIGNED_IMAGES ret |= extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME ".sig", &pos); #endif /* * Check if files could be extracted */ if (ret) { ERROR("Failed to extract meta information"); exit(EXIT_FAILURE); } char* swdescfilename = alloca(strlen(get_tmpdir())+strlen(SW_DESCRIPTION_FILENAME)+1); sprintf(swdescfilename, "%s%s", get_tmpdir(), SW_DESCRIPTION_FILENAME); ret = parse(&swcfg, swdescfilename); if (ret) { ERROR("failed to parse " SW_DESCRIPTION_FILENAME "!"); exit(EXIT_FAILURE); } if (check_hw_compatibility(&swcfg)) { ERROR("SW not compatible with hardware"); exit(EXIT_FAILURE); } if (cpio_scan(fdsw, &swcfg, pos) < 0) { ERROR("failed to scan for pos '%ld'!", pos); close(fdsw); exit(EXIT_FAILURE); } /* * Check if all files described in sw-description * are in the image */ ret = check_provided(&swcfg.images); if (ret) { ERROR("failed to check images!"); exit(EXIT_FAILURE); } ret = check_provided(&swcfg.scripts); if (ret) { ERROR("failed to check scripts!"); exit(EXIT_FAILURE); } if (check) { fprintf(stdout, "successfully checked '%s'\n", fname); exit(EXIT_SUCCESS); } #ifdef CONFIG_MTD mtd_cleanup(); scan_mtd_devices(); #endif /* * Set "recovery_status" as begin of the transaction" */ if (swcfg.bootloader_transaction_marker) { bootloader_env_set(BOOTVAR_TRANSACTION, "in_progress"); } ret = install_images(&swcfg, fdsw, 1); swupdate_progress_end(ret == 0 ? SUCCESS : FAILURE); close(fdsw); if (ret) { fprintf(stdout, "Software updated failed\n"); return EXIT_FAILURE; } if (swcfg.bootloader_transaction_marker) { bootloader_env_unset(BOOTVAR_TRANSACTION); } fprintf(stdout, "Software updated successfully\n"); fprintf(stdout, "Please reboot the device to start the new software\n"); return EXIT_SUCCESS; }
static void create_directory(const char* path) { char* dpath = alloca(strlen(get_tmpdir())+strlen(path)+1); sprintf(dpath, "%s%s", get_tmpdir(), path); mkdir(dpath, 0777); }
void init_config(void) { cfg.num_bookmarks = 0; cfg.vim_filter = 0; cfg.show_one_window = 0; cfg.history_len = 15; (void)hist_init(&cfg.cmd_hist, cfg.history_len); (void)hist_init(&cfg.search_hist, cfg.history_len); (void)hist_init(&cfg.prompt_hist, cfg.history_len); (void)hist_init(&cfg.filter_hist, cfg.history_len); cfg.auto_execute = 0; cfg.time_format = strdup(" %m/%d %H:%M"); cfg.wrap_quick_view = 1; cfg.use_iec_prefixes = 0; cfg.undo_levels = 100; cfg.sort_numbers = 0; cfg.follow_links = 1; cfg.fast_run = 0; cfg.confirm = 1; cfg.vi_command = strdup("vim"); cfg.vi_cmd_bg = 0; cfg.vi_x_command = strdup(""); cfg.vi_x_cmd_bg = 0; cfg.use_trash = 1; { char fuse_home[PATH_MAX]; int update_stat; snprintf(fuse_home, sizeof(fuse_home), "%s/vifm_FUSE", get_tmpdir()); update_stat = set_fuse_home(fuse_home); assert(update_stat == 0); } cfg.use_term_multiplexer = 0; cfg.use_vim_help = 0; cfg.wild_menu = 0; cfg.ignore_case = 0; cfg.smart_case = 0; cfg.hl_search = 1; cfg.vifm_info = VIFMINFO_BOOKMARKS; cfg.auto_ch_pos = 1; cfg.timeout_len = 1000; cfg.scroll_off = 0; cfg.gdefault = 0; #ifndef _WIN32 cfg.slow_fs_list = strdup(""); #endif cfg.scroll_bind = 0; cfg.wrap_scan = 1; cfg.inc_search = 0; cfg.selection_is_primary = 1; cfg.tab_switches_pane = 1; cfg.last_status = 1; cfg.tab_stop = 8; cfg.ruler_format = strdup("%=%l/%S "); cfg.status_line = strdup(""); cfg.lines = INT_MIN; cfg.columns = INT_MIN; cfg.dot_dirs = DD_NONROOT_PARENT; cfg.trunc_normal_sb_msgs = 0; cfg.filter_inverted_by_default = 1; cfg.apropos_prg = strdup("apropos %a"); cfg.find_prg = strdup("find %s %a -print , " "-type d \\( ! -readable -o ! -executable \\) -prune"); cfg.grep_prg = strdup("grep -n -H -I -r %i %a %s"); cfg.locate_prg = strdup("locate %a"); #ifndef _WIN32 snprintf(cfg.log_file, sizeof(cfg.log_file), "/var/log/vifm-startup-log"); #else GetModuleFileNameA(NULL, cfg.log_file, sizeof(cfg.log_file)); to_forward_slash(cfg.log_file); *strrchr(cfg.log_file, '/') = '\0'; strcat(cfg.log_file, "/startup-log"); #endif #ifndef _WIN32 cfg.shell = strdup(env_get_def("SHELL", "sh")); #else cfg.shell = strdup(env_get_def("SHELL", "cmd")); #endif #ifndef _WIN32 /* Maximum argument length to pass to the shell */ if((cfg.max_args = sysconf(_SC_ARG_MAX)) == 0) #endif cfg.max_args = 4096; /* POSIX MINIMUM */ memset(&cfg.decorations, '\0', sizeof(cfg.decorations)); cfg.decorations[DIRECTORY][DECORATION_SUFFIX] = '/'; }
gboolean pipe_event(GIOChannel chan, GIOCondition cond, gpointer data) { static char *path; char name[TMPNAMELEN]; ssize_t rr; int nimgs = 0; if (!path) path = xmalloc(strlen(get_tmpdir()) + TMPNAMELEN); /* We are sent messages of size TMPNAMELEN containing a null-terminated * file name. */ while (nimgs < 4 && (rr = xread(imgpipe_readfd, name, sizeof name)) == sizeof name) { int saveimg = 0; struct stat st; ++nimgs; sprintf(path, "%s/%s", get_tmpdir(), name); if (stat(path, &st) == -1) continue; log_msg(LOG_INFO, "received image %s of size %d", name, (int)st.st_size); /* Check to see whether this looks like an image we're interested in. */ if (st.st_size > 100) { /* Small images are probably bollocks. */ img i = img_new(); if (!img_load_file(i, path, header, unknown)) log_msg(LOG_WARNING, "%s: bogus image (err = %d)", name, i->err); else { if (i->width > 8 && i->height > 8) { if (img_load(i, full, i->type)) { /* slot in the new image at some plausible place. */ int w, h; if (i->width > width - 2 * BORDER) w = width - 2 * BORDER; else w = i->width; if (i->height > height - 2 * BORDER) h = height - 2 * BORDER; else h = i->height; /* is there space on this row? */ if (width - wrx < w) { /* no */ scroll_backing_image(h + BORDER); wrx = BORDER; rowheight = h + BORDER; } if (rowheight < h + BORDER) { scroll_backing_image(h + BORDER - rowheight); rowheight = h + BORDER; } img_simple_blt(backing_image, wrx, wry - h, i, 0, 0, w, h); add_image_rectangle(path, wrx, wry - h, w, h); saveimg = 1; if (beep_on_image) write(1, "\a", 1); update_window(); wrx += w + BORDER; } else log_msg(LOG_WARNING, "%s: bogus image (err = %d)", name, i->err); } else log_msg(LOG_WARNING, "%s: image dimensions (%d x %d) too small to bother with", name, i->width, i->height); } img_delete(i); } else log_msg(LOG_WARNING, "image data too small (%d bytes) to bother with", (int)st.st_size); if (!saveimg) unlink(name); } if (rr == -1 && errno != EINTR && errno != EAGAIN) { log_msg(LOG_ERROR, "display pipe read() failed, reason: %s", strerror(errno)); gtk_main_quit(); } else if (rr == 0) { /* pipe closed, exit. */ gtk_main_quit(); } return TRUE; }
static void * extract(void *p) { struct archive *a; struct archive *ext; struct archive_entry *entry; int r; int flags; struct extract_data *data = (struct extract_data *)p; flags = data->flags; a = archive_read_new(); ext = archive_write_disk_new(); archive_write_disk_set_options(ext, flags); /* * Note: archive_write_disk_set_standard_lookup() is useful * here, but it requires library routines that can add 500k or * more to a static executable. */ archive_read_support_format_all(a); archive_read_support_filter_all(a); /* * On my system, enabling other archive formats adds 20k-30k * each. Enabling gzip decompression adds about 20k. * Enabling bzip2 is more expensive because the libbz2 library * isn't very well factored. */ char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); if ((r = archive_read_open_filename(a, FIFO, 4096))) { ERROR("archive_read_open_filename(): %s %d", archive_error_string(a), r); pthread_exit((void *)-1); } for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_EOF) break; if (r != ARCHIVE_OK) { ERROR("archive_read_next_header(): %s %d", archive_error_string(a), 1); pthread_exit((void *)-1); } if (debug) TRACE("Extracting %s", archive_entry_pathname(entry)); r = archive_write_header(ext, entry); if (r != ARCHIVE_OK) TRACE("archive_write_header(): %s", archive_error_string(ext)); else { copy_data(a, ext); r = archive_write_finish_entry(ext); if (r != ARCHIVE_OK) { ERROR("archive_write_finish_entry(): %s", archive_error_string(ext)); pthread_exit((void *)-1); } } } archive_read_close(a); archive_read_free(a); pthread_exit((void *)0); }
pthread_exit((void *)0); } static int install_archive_image(struct img_type *img, void __attribute__ ((__unused__)) *data) { char path[255]; int fdout; int ret = 0; char pwd[256]; struct extract_data tf; pthread_attr_t attr; void *status; int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); if (strlen(img->path) == 0) { TRACE("Missing path attribute"); return -1; } if (use_mount) { ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem); if (ret) { ERROR("Device %s with filesystem %s cannot be mounted", img->device, img->filesystem);
void init_config(void) { cfg.show_one_window = 0; cfg.history_len = 15; (void)hist_init(&cfg.cmd_hist, cfg.history_len); (void)hist_init(&cfg.search_hist, cfg.history_len); (void)hist_init(&cfg.prompt_hist, cfg.history_len); (void)hist_init(&cfg.filter_hist, cfg.history_len); cfg.auto_execute = 0; cfg.time_format = strdup(" %m/%d %H:%M"); cfg.wrap_quick_view = 1; cfg.use_iec_prefixes = 0; cfg.undo_levels = 100; cfg.sort_numbers = 0; cfg.follow_links = 1; cfg.fast_run = 0; cfg.confirm = 1; cfg.vi_command = strdup("vim"); cfg.vi_cmd_bg = 0; cfg.vi_x_command = strdup(""); cfg.vi_x_cmd_bg = 0; cfg.use_trash = 1; { char fuse_home[PATH_MAX]; int update_stat; snprintf(fuse_home, sizeof(fuse_home), "%s/vifm_FUSE", get_tmpdir()); update_stat = set_fuse_home(fuse_home); assert(update_stat == 0); } cfg.use_term_multiplexer = 0; cfg.use_vim_help = 0; cfg.wild_menu = 0; cfg.ignore_case = 0; cfg.smart_case = 0; cfg.hl_search = 1; cfg.vifm_info = VIFMINFO_BOOKMARKS; cfg.auto_ch_pos = 1; cfg.timeout_len = 1000; cfg.scroll_off = 0; cfg.gdefault = 0; #ifndef _WIN32 cfg.slow_fs_list = strdup(""); #endif cfg.scroll_bind = 0; cfg.wrap_scan = 1; cfg.inc_search = 0; cfg.selection_is_primary = 1; cfg.tab_switches_pane = 1; cfg.use_system_calls = 0; cfg.last_status = 1; cfg.tab_stop = 8; cfg.ruler_format = strdup("%=%l/%S "); cfg.status_line = strdup(""); cfg.lines = INT_MIN; cfg.columns = INT_MIN; cfg.dot_dirs = DD_NONROOT_PARENT; cfg.trunc_normal_sb_msgs = 0; cfg.filter_inverted_by_default = 1; cfg.apropos_prg = strdup("apropos %a"); cfg.find_prg = strdup("find %s %a -print , " "-type d \\( ! -readable -o ! -executable \\) -prune"); cfg.grep_prg = strdup("grep -n -H -I -r %i %a %s"); cfg.locate_prg = strdup("locate %a"); cfg.cd_path = strdup(env_get_def("CDPATH", DEFAULT_CD_PATH)); replace_char(cfg.cd_path, ':', ','); cfg.filelist_col_padding = 1; cfg.side_borders_visible = 1; cfg.border_filler = strdup(" "); #ifndef _WIN32 copy_str(cfg.log_file, sizeof(cfg.log_file), "/var/log/vifm-startup-log"); #else { char exe_dir[PATH_MAX]; (void)get_exe_dir(exe_dir, sizeof(exe_dir)); snprintf(cfg.log_file, sizeof(cfg.log_file), "%s/startup-log", exe_dir); } #endif cfg_set_shell(env_get_def("SHELL", DEFAULT_SHELL_CMD)); memset(&cfg.decorations, '\0', sizeof(cfg.decorations)); cfg.decorations[DIRECTORY][DECORATION_SUFFIX] = '/'; }