int main(int argc, char** argv) { // Parse arguments size_t blocksize = 4096; if (argc >= 3 && strcmp(argv[1], "-b") == 0) { blocksize = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } // Allocate buffer, open files assert(blocksize > 0); char* buf = malloc(blocksize); const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); // Copy file data while (1) { ssize_t amount = io61_read(inf, buf, blocksize); if (amount <= 0) break; io61_write(outf, buf, amount); } io61_close(inf); io61_close(outf); io61_profile_end(); }
int main(int argc, char** argv) { // Parse arguments size_t blocksize = 1; size_t stride = 1024; while (argc >= 3) { if (strcmp(argv[1], "-b") == 0) { blocksize = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-s") == 0) { stride = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else break; } // Allocate buffer, open files, measure file sizes assert(blocksize > 0); char* buf = malloc(blocksize); const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); size_t inf_size = io61_filesize(inf); if ((ssize_t) inf_size < 0) { fprintf(stderr, "stridecat61: input file is not seekable\n"); exit(1); } io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); // Copy file data size_t pos = 0, written = 0; while (written < inf_size) { // Copy a block ssize_t amount = io61_read(inf, buf, blocksize); if (amount <= 0) break; io61_write(outf, buf, amount); written += amount; // Move `inf` file position to next stride pos += stride; if (pos >= inf_size) { pos = (pos % stride) + blocksize; if (pos + blocksize > stride) blocksize = stride - pos; } int r = io61_seek(inf, pos); assert(r >= 0); } io61_close(inf); io61_close(outf); io61_profile_end(); }
int main(int argc, char* argv[]) { // Parse arguments io61_arguments args = io61_parse_arguments(argc, argv, "b:o:#"); size_t block_size = args.block_size ? args.block_size : 1; // Allocate buffer, open files char* buf = (char*) malloc(block_size); int nfiles = args.n_input_files; io61_profile_begin(); io61_file** infs = (io61_file**) calloc(nfiles, sizeof(io61_file*)); for (int i = 0; i < nfiles; ++i) infs[i] = io61_open_check(args.input_files[i], O_RDONLY); io61_file* outf = io61_open_check(args.output_file, O_WRONLY | O_CREAT | O_TRUNC); // Copy file data int whichf = 0, ndeadfiles = 0; while (ndeadfiles != nfiles) { if (infs[whichf]) { ssize_t amount = io61_read(infs[whichf], buf, block_size); if (amount <= 0) { io61_close(infs[whichf]); infs[whichf] = NULL; ++ndeadfiles; } else io61_write(outf, buf, amount); } whichf = (whichf + 1) % nfiles; } io61_close(outf); io61_profile_end(); free(infs); free(buf); }
int main(int argc, char** argv) { // Parse arguments size_t blocksize = 4096; size_t inf_size = -1; srandom(83419); while (argc >= 3) { if (strcmp(argv[1], "-b") == 0) { blocksize = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-r") == 0) { srandom(strtoul(argv[2], 0, 0)); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-s") == 0) { inf_size = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else break; } // Allocate buffer, open files, measure file sizes assert(blocksize > 0); char* buf = (char*) malloc(blocksize); const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); if ((ssize_t) inf_size < 0) inf_size = io61_filesize(inf); if ((ssize_t) inf_size < 0) { fprintf(stderr, "reordercat61: can't get size of input file\n"); exit(1); } if (io61_seek(inf, 0) < 0) { fprintf(stderr, "reordercat61: input file is not seekable\n"); exit(1); } io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); if (io61_seek(outf, 0) < 0) { fprintf(stderr, "reordercat61: output file is not seekable\n"); exit(1); } // Calculate random permutation of file's blocks size_t nblocks = inf_size / blocksize; if (nblocks > (30 << 20)) { fprintf(stderr, "reordercat61: file too large\n"); exit(1); } else if (nblocks * blocksize != inf_size) { fprintf(stderr, "reordercat61: input file size not a multiple of block size\n"); exit(1); } size_t* blockpos = (size_t*) malloc(sizeof(size_t) * nblocks); for (size_t i = 0; i < nblocks; ++i) blockpos[i] = i; // Copy file data while (nblocks != 0) { // Choose block to read size_t index = random() % nblocks; size_t pos = blockpos[index] * blocksize; blockpos[index] = blockpos[nblocks - 1]; --nblocks; // Transfer that block io61_seek(inf, pos); ssize_t amount = io61_read(inf, buf, blocksize); if (amount <= 0) break; io61_seek(outf, pos); io61_write(outf, buf, amount); } io61_close(inf); io61_close(outf); io61_profile_end(); free(buf); }