static void check_overwrite (void * data) { ImportExportJob * job = data; job->filename = gtk_file_chooser_get_uri ((GtkFileChooser *) job->selector); if (! job->filename) return; if (job->save && vfs_file_test (job->filename, G_FILE_TEST_EXISTS)) confirm_overwrite (job); else finish_job (data); }
SIGNAL_CALLBACK void on_execute_button_clicked (GtkWidget *button) { GtkTreeIter iter; gboolean valid; Settings * user_settings; GList * rows; /* gui should prevent this from happening */ if (processing) return; if (confirm_overwrite()) { keep_going = TRUE; user_settings = settings_get_from_gui(); settings_on_execute = settings_copy(user_settings); rows = NULL; valid = gtk_tree_model_get_iter_first( GTK_TREE_MODEL(list_store), &iter); if (valid) { hide_sections_for_execute(); } else { message_box(" No files to process (click Browse...) "); } while (valid && keep_going) { GtkTreeRowReference * ref; GtkTreePath * path; path = gtk_tree_model_get_path(GTK_TREE_MODEL(list_store), &iter); ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(list_store), path); rows = g_list_append(rows, ref); valid = gtk_tree_model_iter_next( GTK_TREE_MODEL(list_store), &iter); } process_items_from_list(rows, TRUE); settings_delete(user_settings); } }
EXPORT void audgui_export_playlist (void) { int list = aud_playlist_get_active (); int id = aud_playlist_get_unique_id (list); char * def = aud_playlist_get_filename (list); char * filename = select_file (TRUE, def); str_unref (def); if (! filename || (vfs_file_test (filename, G_FILE_TEST_EXISTS) && ! confirm_overwrite (filename))) return; if ((list = aud_playlist_by_unique_id (id)) < 0) return; aud_playlist_save (list, filename); g_free (filename); }
/************************************************************************ * * * User has clicked the "Load" or "Save" button. * * */ static void file_action_proc(Panel_item i, Event *) { char *filename; /* pointer to a selected filename */ err_msg(""); if ((filename = (char *) xv_get(fhdl->filename, PANEL_VALUE)) == NULL){ err_msg("No filename selected"); return; } if (curfinfo == NULL){ err_msg("No registered function"); return; } if ((Flist_state) xv_get(i, PANEL_CLIENT_DATA) == FILELIST_LOAD){ if (curfinfo->loadfunc){ curfinfo->loadfunc(dirname, filename); } }else if ((Flist_state) xv_get(i, PANEL_CLIENT_DATA) == FILELIST_SAVE){ if (curfinfo->savefunc){ char path[1025]; sprintf(path,"%s/%s", dirname, filename); struct stat statbuf; int staterr = stat(path, &statbuf); if ( staterr || confirm_overwrite(filename) ){ curfinfo->savefunc(dirname, filename); } } }else if ((Flist_state) xv_get(i, PANEL_CLIENT_DATA) == FILELIST_LOAD_ALL){ if (curfinfo->loadallfunc){ curfinfo->loadallfunc(dirname, filename); } }else{ err_msg("No registered flist state"); return; } }
/************************************************************************ * * * User has entered a filename. If it is a directory, go into that * * directory and list all directory files. Otherwise, return to * * the user function call. * * */ static void file_execute_proc(void) { static char markers[] = "/"; /* List of spurious symbols at end of names */ char fullname[MAXPATHLEN]; char filename[MAXPATHLEN]; char *p; /* Temp */ char *q; char *uname; /* user seleted filename */ int i; struct stat statbuf; /* status of a file */ uname = (char *) xv_get(fhdl->filename, PANEL_VALUE); if (*uname == NULL){ err_msg("No file name entered"); return; } if (expand_filename(uname, filename) == NULL){ err_msg("Can't expand filename"); return; } // Strip off any trailing markers while ( (p=strpbrk(filename+strlen(filename)-1, markers)) && (p > filename)) // Don't remove everything { *p = 0; } if (*filename == '/'){ // We have given a full path name strcpy(fullname, filename); }else{ strcpy(fullname, dirname); strcat(fullname, "/"); strcat(fullname, filename); } // Avoid uncontrolled pathname growth // Remove excess slashes ("//" is same as "/") while (p=strstr(fullname, "//")){ // Move portion of string beyond first "/" down one place do{ *p = *(p+1); } while (*p++); } // Remove "/./" elements while (p=strstr(fullname, "/./")){ // Move portion of string beyond "/." down two places do{ *p = *(p+2); } while (*p++); } // Remove trailing "/." i = strlen(fullname); if (strcmp(fullname+i-2, "/.") == 0){ // Filename ends in "/."; cut it off fullname[i-2] = 0; } // Remove "/../" elements (along with preceding element) while (p=strstr(fullname, "/../")){ q = p + 3; // Point to following "/" while ( (*(--p) != '/') && (p > fullname) ); // Point to previous "/" do{ *p = *q++; // Move following stuff down } while (*p++); } // Remove trailing "/.." (along with preceding element) i = strlen(fullname); if (strcmp(fullname+i-3, "/..") == 0){ // Filename ends in "/.."; cut off last two elements for (i=0; (i<2) && (p=strrchr(fullname, '/')); i++){ *p = 0; } } // Make sure something is left of the name if ( *fullname == 0 ){ strcpy(fullname, "/"); } int staterr; if ((staterr=stat(fullname, &statbuf)) == 0 && (statbuf.st_mode & S_IFMT) == S_IFDIR) { strcpy(dirname, fullname); /* Get the files */ if (file_listing(dirname) == NOT_OK){ err_msg("Error in file_listing"); return; } /* Update the directory message */ show_dir_path(short_path(dirname)); xv_set(fhdl->filename, PANEL_VALUE, "", NULL); /* Remember new default directory */ if (curfinfo){ free(curfinfo->dirpath); curfinfo->dirpath = strdup(dirname); } }else{ if (curfinfo == NULL){ err_msg("No registered function"); return; } /* in this case we will only load/save one file */ if (xv_get(fhdl->load_but, PANEL_SHOW_ITEM) == TRUE){ if (staterr){ err_msg("Can't find file: %s", short_path(fullname)); return; }else if ((statbuf.st_mode & S_IFMT) != S_IFREG){ err_msg("Error: Invalid file or directory."); return; }else{ if (curfinfo->loadfunc){ curfinfo->loadfunc(dirname, filename); } } }else{ if (curfinfo->savefunc && (staterr||confirm_overwrite(filename))){ curfinfo->savefunc(dirname, filename); } } } }
int main(int argc, char **argv) { int c, compress, pstdout, is_forced, index = 0, rebgzip = 0, reindex = 0; BGZF *fp; void *buffer; long start, end, size; char *index_fname = NULL; int threads = 1; static const struct option loptions[] = { {"help", no_argument, NULL, 'h'}, {"offset", required_argument, NULL, 'b'}, {"stdout", no_argument, NULL, 'c'}, {"decompress", no_argument, NULL, 'd'}, {"force", no_argument, NULL, 'f'}, {"index", no_argument, NULL, 'i'}, {"index-name", required_argument, NULL, 'I'}, {"reindex", no_argument, NULL, 'r'}, {"rebgzip",no_argument,NULL,'g'}, {"size", required_argument, NULL, 's'}, {"threads", required_argument, NULL, '@'}, {"version", no_argument, NULL, 1}, {NULL, 0, NULL, 0} }; compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0; while((c = getopt_long(argc, argv, "cdh?fb:@:s:iI:gr",loptions,NULL)) >= 0){ switch(c){ case 'd': compress = 0; break; case 'c': pstdout = 1; break; case 'b': start = atol(optarg); compress = 0; pstdout = 1; break; case 's': size = atol(optarg); pstdout = 1; break; case 'f': is_forced = 1; break; case 'i': index = 1; break; case 'I': index_fname = optarg; break; case 'g': rebgzip = 1; break; case 'r': reindex = 1; compress = 0; break; case '@': threads = atoi(optarg); break; case 1: printf( "bgzip (htslib) %s\n" "Copyright (C) 2017 Genome Research Ltd.\n", hts_version()); return EXIT_SUCCESS; case 'h': case '?': return bgzip_main_usage(); } } if (size >= 0) end = start + size; if (end >= 0 && end < start) { fprintf(stderr, "[bgzip] Illegal region: [%ld, %ld]\n", start, end); return 1; } if (compress == 1) { struct stat sbuf; int f_src = fileno(stdin); if ( argc>optind ) { if ( stat(argv[optind],&sbuf)<0 ) { fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]); return 1; } if ((f_src = open(argv[optind], O_RDONLY)) < 0) { fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]); return 1; } if (pstdout) fp = bgzf_open("-", "w"); else { char *name = malloc(strlen(argv[optind]) + 5); strcpy(name, argv[optind]); strcat(name, ".gz"); fp = bgzf_open(name, is_forced? "w" : "wx"); if (fp == NULL && errno == EEXIST && confirm_overwrite(name)) fp = bgzf_open(name, "w"); if (fp == NULL) { fprintf(stderr, "[bgzip] can't create %s: %s\n", name, strerror(errno)); free(name); return 1; } free(name); } } else if (!pstdout && isatty(fileno((FILE *)stdout)) ) return bgzip_main_usage(); else if ( index && !index_fname ) { fprintf(stderr, "[bgzip] Index file name expected when writing to stdout\n"); return 1; } else fp = bgzf_open("-", "w"); if ( index && rebgzip ) { fprintf(stderr, "[bgzip] Can't produce a index and rebgzip simultaneously\n"); return 1; } if ( rebgzip && !index_fname ) { fprintf(stderr, "[bgzip] Index file name expected when writing to stdout\n"); return 1; } if (threads > 1) bgzf_mt(fp, threads, 256); if ( index ) bgzf_index_build_init(fp); buffer = malloc(WINDOW_SIZE); #ifdef _WIN32 _setmode(f_src, O_BINARY); #endif if (rebgzip){ if ( bgzf_index_load(fp, index_fname, NULL) < 0 ) error("Could not load index: %s.gzi\n", argv[optind]); while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0) if (bgzf_block_write(fp, buffer, c) < 0) error("Could not write %d bytes: Error %d\n", c, fp->errcode); } else { while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0) if (bgzf_write(fp, buffer, c) < 0) error("Could not write %d bytes: Error %d\n", c, fp->errcode); } if ( index ) { if (index_fname) { if (bgzf_index_dump(fp, index_fname, NULL) < 0) error("Could not write index to '%s'\n", index_fname); } else { if (bgzf_index_dump(fp, argv[optind], ".gz.gzi") < 0) error("Could not write index to '%s.gz.gzi'", argv[optind]); } } if (bgzf_close(fp) < 0) error("Close failed: Error %d", fp->errcode); if (argc > optind && !pstdout) unlink(argv[optind]); free(buffer); close(f_src); return 0; } else if ( reindex ) { if ( argc>optind ) { fp = bgzf_open(argv[optind], "r"); if ( !fp ) error("[bgzip] Could not open file: %s\n", argv[optind]); } else { if ( !index_fname ) error("[bgzip] Index file name expected when reading from stdin\n"); fp = bgzf_open("-", "r"); if ( !fp ) error("[bgzip] Could not read from stdin: %s\n", strerror(errno)); } buffer = malloc(BGZF_BLOCK_SIZE); bgzf_index_build_init(fp); int ret; while ( (ret=bgzf_read(fp, buffer, BGZF_BLOCK_SIZE))>0 ) ; free(buffer); if ( ret<0 ) error("Is the file gzipped or bgzipped? The latter is required for indexing.\n"); if ( index_fname ) { if (bgzf_index_dump(fp, index_fname, NULL) < 0) error("Could not write index to '%s'\n", index_fname); } else { if (bgzf_index_dump(fp, argv[optind], ".gzi") < 0) error("Could not write index to '%s.gzi'\n", argv[optind]); } if ( bgzf_close(fp)<0 ) error("Close failed: Error %d\n",fp->errcode); return 0; } else { struct stat sbuf; int f_dst; if ( argc>optind ) { if ( stat(argv[optind],&sbuf)<0 ) { fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]); return 1; } char *name; int len = strlen(argv[optind]); if ( strcmp(argv[optind]+len-3,".gz") ) { fprintf(stderr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]); return 1; } fp = bgzf_open(argv[optind], "r"); if (fp == NULL) { fprintf(stderr, "[bgzip] Could not open file: %s\n", argv[optind]); return 1; } if (pstdout) { f_dst = fileno(stdout); } else { const int wrflags = O_WRONLY | O_CREAT | O_TRUNC; name = strdup(argv[optind]); name[strlen(name) - 3] = '\0'; f_dst = open(name, is_forced? wrflags : wrflags|O_EXCL, 0666); if (f_dst < 0 && errno == EEXIST && confirm_overwrite(name)) f_dst = open(name, wrflags, 0666); if (f_dst < 0) { fprintf(stderr, "[bgzip] can't create %s: %s\n", name, strerror(errno)); free(name); return 1; } free(name); } } else if (!pstdout && isatty(fileno((FILE *)stdin)) ) return bgzip_main_usage(); else { f_dst = fileno(stdout); fp = bgzf_open("-", "r"); if (fp == NULL) { fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno)); return 1; } } if (threads > 1) bgzf_mt(fp, threads, 256); buffer = malloc(WINDOW_SIZE); if ( start>0 ) { if ( bgzf_index_load(fp, argv[optind], ".gzi") < 0 ) error("Could not load index: %s.gzi\n", argv[optind]); if ( bgzf_useek(fp, start, SEEK_SET) < 0 ) error("Could not seek to %d-th (uncompressd) byte\n", start); } #ifdef _WIN32 _setmode(f_dst, O_BINARY); #endif while (1) { if (end < 0) c = bgzf_read(fp, buffer, WINDOW_SIZE); else c = bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start)); if (c == 0) break; if (c < 0) error("Could not read %d bytes: Error %d\n", (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start), fp->errcode); start += c; if ( write(f_dst, buffer, c) != c ) { #ifdef _WIN32 if (GetLastError() != ERROR_NO_DATA) #endif error("Could not write %d bytes\n", c); } if (end >= 0 && start >= end) break; } free(buffer); if (bgzf_close(fp) < 0) error("Close failed: Error %d\n",fp->errcode); if (!pstdout) unlink(argv[optind]); return 0; } }