int main(int argc, char** argv) { int i; if(argc < 4) errx(-1, "missing arguments"); int nproc = atoi(argv[1]); char* inputname = argv[2]; char* outputname = argv[3]; int inputfd = openx(inputname, O_RDONLY); int outputfd = openx(outputname, O_RDWR | O_CREAT); off_t inputsize = argc > 4 ? parsesize(argv[4]) : statfdsize(inputfd, inputname); off_t outputsize = DISTINCT_CRC_VALUES * sizeof(count_t); uint8_t* input = mapinput(inputfd, inputsize, inputname); count_t* output = mapoutput(outputfd, outputsize, outputname); pid_t children[nproc]; pid_t dead; for(i = 0; i < nproc; i++) if((children[i] = spawnworker(i, nproc, input, inputsize, output)) < 0) break; int status; while(notempty(children, nproc)) if((dead = wait(&status)) > 0) if(handledead(children, nproc, dead, status)) break; terminate(children, nproc); return messy(children, nproc) ? -1 : 0; }
void parseopts(int argc, char **argv) { int c; /* initialize default option values */ psc_dynarray_init(&opts.exclude); psc_dynarray_init(&opts.files); psc_dynarray_init(&opts.filter); psc_dynarray_init(&opts.include); opts.progress = 1; opts.psync_path = "psync"; opts.rsh = "ssh " "-oControlPath=none " "-oCompression=no " "-oKbdInteractiveAuthentication=no " "-oNumberOfPasswordPrompts=1"; opts.streams = getnstreams(getnprocessors()); while ((c = getopt_long(argc, argv, "0468aB:bCcDdEEe:f:gHhIiKkLlmN:nOoPpqRrST:tuVvWxyz", longopts, NULL)) != -1) { switch (c) { case '0': opts.from0 = 1; break; case '4': opts.ipv4 = 1; break; case '6': opts.ipv6 = 1; break; case '8': opts._8_bit_output = 1; break; case 'a': opts.devices = 1; opts.group = 1; opts.links = 1; opts.owner = 1; opts.perms = 1; opts.recursive = 1; opts.specials = 1; opts.times = 1; break; case 'B': if (!parsesize(&opts.block_size, optarg, 1)) err(1, "-B %s", optarg); break; case 'b': opts.backup = 1; break; case 'C': opts.cvs_exclude = 1; break; case 'c': opts.checksum = 1; break; case 'D': opts.devices = 1; opts.specials = 1; break; case 'd': opts.dirs = 1; break; case 'E': opts.extended_attributes = 1; break; case 'e': opts.rsh = optarg; break; case 'f': push_filter(&opts.filter, optarg, FPT_INCL); break; case 'g': opts.group = 1; break; case 'H': opts.hard_links = 1; break; case 'h': opts.human_readable = 1; break; case 'I': opts.ignore_times = 1; break; case 'i': opts.itemize_changes = 1; break; case 'K': opts.keep_dirlinks = 1; break; case 'k': opts.copy_dirlinks = 1; break; case 'L': opts.copy_links = 1; break; case 'l': opts.links = 1; break; case 'm': opts.prune_empty_dirs = 1; break; case 'N': if (!parsenum(&opts.streams, optarg, 0, MAX_STREAMS)) err(1, "streams: %s", optarg); break; case 'n': opts.dry_run = 1; break; case 'O': opts.omit_dir_times = 1; break; case 'o': opts.owner = 1; break; case 'P': opts.progress = 1; opts.partial = 1; break; case 'p': opts.perms = 1; break; case 'q': opts.quiet = 1; break; case 'R': opts.relative = 1; break; case 'r': opts.recursive = 1; break; case 'S': opts.sparse = 1; break; case 'T': opts.temp_dir = optarg; break; case 't': opts.times = 1; break; case 'u': opts.update = 1; break; case 'V': fprintf(stderr, "psync version %d\n", PSYNC_VERSION); exit(0); break; case 'v': opts.verbose = 1; break; case 'W': opts.whole_file = 1; break; case 'x': opts.one_file_system = 1; break; case 'y': opts.fuzzy = 1; break; case 'z': opts.compress = 1; break; case OPT_ADDRESS: opts.address = optarg; break; case OPT_BWLIMIT: if (!parsesize(&opts.bwlimit, optarg, 1024)) err(1, "--bwlimit=%s", optarg); break; case OPT_CHMOD: opts.chmod = optarg; break; case OPT_COMPARE_DEST: opts.compare_dest = optarg; break; case OPT_COMPRESS_LEVEL: if (!parsenum(&opts.compress_level, optarg, 0, 10)) err(1, "--compress-level=%s", optarg); break; case OPT_COPY_DEST: opts.copy_dest = optarg; break; case OPT_EXCLUDE: push_filter(&opts.filter, optarg, FPT_EXCL); break; case OPT_EXCLUDE_FROM: pushfile(&opts.filter, optarg, push_filter, FPT_EXCL); break; case OPT_FILES_FROM: pushfile(&opts.files, optarg, push_files_from, FPT_INCL); break; case OPT_INCLUDE: push_filter(&opts.filter, optarg, FPT_INCL); break; case OPT_INCLUDE_FROM: pushfile(&opts.filter, optarg, push_filter, FPT_INCL); break; case OPT_LINK_DEST: opts.link_dest = optarg; break; case OPT_LOG_FILE: opts.log_file = optarg; break; case OPT_LOG_FILE_FORMAT: opts.log_file_format = optarg; break; case OPT_MAX_DELETE: if (!parsenum(&opts.max_delete, optarg, 0, INT_MAX)) err(1, "--max-delete=%s", optarg); break; case OPT_MAX_SIZE: if (!parsesize(&opts.max_size, optarg, 1)) err(1, "--max-size=%s", optarg); break; case OPT_MIN_SIZE: if (!parsesize(&opts.min_size, optarg, 1)) err(1, "--min-size=%s", optarg); break; case OPT_MODIFY_WINDOW: if (!parsenum(&opts.modify_window, optarg, 0, INT_MAX)) err(1, "--modify-window=%s", optarg); break; case OPT_ONLY_WRITE_BATCH:opts.write_batch = optarg; break; case OPT_OUT_FORMAT: opts.out_format = optarg; break; case OPT_PORT: if (!parsenum(&opts.port, optarg, 0, 65535)) err(1, "--port=%s", optarg); break; case OPT_PARTIAL_DIR: opts.partial_dir = optarg; break; case OPT_PASSWORD_FILE: opts.password_file = optarg; break; case OPT_PSYNC_PATH: opts.psync_path = optarg; break; case OPT_READ_BATCH: opts.read_batch = optarg; break; case OPT_SOCKOPTS: opts.sockopts = optarg; break; case OPT_SUFFIX: opts.suffix = optarg; break; case OPT_TIMEOUT: if (!parsenum(&opts.timeout, optarg, 0, INT_MAX)) err(1, "--timeout=%s", optarg); break; case OPT_WRITE_BATCH: opts.write_batch = optarg; break; /* psync specific options */ case OPT_DSTDIR: opts.dstdir = optarg; break; case OPT_PUPPET: if (!parsenum(&opts.puppet, optarg, 0, 1000000)) err(1, "--PUPPET=%s", optarg); break; case 0: break; default: warn("invalid option: -%c", c); usage(); } } }