/** Re-zip an unpacked OpenRaster zip archive * * \param [in] dir Path to the directory */ void pack_directory( QDir dir ){ if( !dir.exists() ){ qWarning( "Path must be a directory!" ); return; } //Get mimetype QFile mime( dir.absolutePath() + "/mimetype" ); if( !mime.open( QIODevice::ReadOnly ) ){ qWarning( "mimetype missing" ); return; } QString mimetype = mime.readAll(); //Get stack QFile stack_file( dir.absolutePath() + "/stack.xml" ); if( !stack_file.open( QIODevice::ReadOnly ) ){ qWarning( "stack.xml missing" ); return; } QString stack = stack_file.readAll(); //Get thumbnail QList<std::pair<QString,QByteArray>> files; append_all_files( files, dir, "Thumbnails" ); //Get data files append_all_files( files, dir, "data" ); OraSaver::save( dir.dirName() + ".packed.cgcompress", mimetype, stack, files ); }
int main (int argc, char **argv) { /* Program requires >= 4 arguments */ if(argc < 3) usage(stderr, argv[0], "Insufficient arguments", 1); /* Variable to hold flag currently * being ready by getopt() */ int c; int operation_alters_arch = 0; unsigned int timeout = 0; /* Struct for use with GNU getopt_long() */ struct option longopts[] = { { "quick", no_argument, 0, 'q' }, { "extract", no_argument, 0, 'x' }, { "contents", no_argument, 0, 't' }, { "verbose", no_argument, 0, 'v' }, { "delete", no_argument, 0, 'd' }, { "append", no_argument, 0, 'A' }, { "when", required_argument, 0, 'w' }, { 0, 0, 0, 0 } }; int opt_index = 0; /* * Loop adapted from example given at * http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html * * Loop through command line arguments, incrementing * 'seen' when we encounter a mandatory argument. * * All options besides '-v' require an argument. */ while((c = getopt_long(argc, argv, "qxtvdAw:", longopts, &opt_index)) != -1) { switch(c) { case 'q': case 'x': case 't': case 'v': case 'd': case 'A': case 'w': if(operation != none) { fprintf(stderr, "two different operation options specified\n"); exit(EXIT_FAILURE); } break; } switch(c) { case 'q': operation_alters_arch = 1; operation = append; break; case 'x': operation = extract; break; case 't': operation = contents; break; case 'v': operation = verbose; break; case 'd': operation_alters_arch = 1; operation = delete; break; case 'A': operation_alters_arch = 1; operation = append_all; break; case 'w': operation_alters_arch = 1; operation = append_all; timeout = strtoul(optarg, 0, 10); break; case('?'): default: usage(stderr, argv[0], "unrecognized option", 1); break; } } /* Print usage and exit if no operation * was given */ if(operation == none) usage(stderr, argv[0], "'none' operation specified", 1); /* Array for remaining arguments. * Assumes remaining arguments * are filepaths. */ int count = argc - optind - 1; char *archname = argv[optind++]; if(archname == NULL) usage(stderr, argv[0], "no archive file specified", 1); int arch; /* Temporarily set umask to 0000 in order to * create file with permissions 0666 using * DFLTMODE */ umask(0000); if((operation == append || operation == append_all) && access(archname, R_OK | W_OK) == -1) { printf("%s: creating %s\n", argv[0], archname); arch = open(archname, O_CREAT | O_RDWR, DFLTMODE); } else { arch = open(archname, O_RDWR, DFLTMODE); } umask(DFLTUMASK); if(arch == -1) { fprintf(stderr, "error opening archive\n"); perror("open"); exit(EXIT_FAILURE); } if(isempty(archname)) write_armag(arch); char *files[count]; int i; for(i = 0; optind < argc; i++, optind++) files[i] = argv[optind]; switch(operation) { case append: append_files(arch, archname, files, count); break; case append_all: if(count != 0 && timeout == 0) usage(stderr, argv[0], "-A option takes no arguments", 1); else if(count != 0) usage(stderr, argv[0], "-w option takes no arguments", 1); append_all_files(arch, archname, timeout, &is_reg_file); break; case extract: extract_files(arch, files, count); break; case contents: map_over_members(arch, &print_member_concise, files, count, -1); break; case verbose: map_over_members(arch, &print_member_verbose, files, count, -1); break; case delete: delete_files(arch, archname, files, count); break; case none: default: usage(stderr, argv[0], "no operation option specified", 1); } if(operation_alters_arch != 1) { if(close(arch) == -1) { fprintf(stderr, "failed to close archive %s\n", archname); perror("close"); exit(EXIT_FAILURE); } } return 0; }