diff options
author | Jerry Zhang <zhangjerry@google.com> | 2018-06-12 16:42:09 -0700 |
---|---|---|
committer | Jerry Zhang <zhangjerry@google.com> | 2018-06-13 20:32:24 +0000 |
commit | 7b444f08c17ed1b82ea1a1560e109c0a173e700f (patch) | |
tree | b73fa22396eefbd3e71d151c581ae957383aaf7c /libsparse/append2simg.cpp | |
parent | 5a755077958dd4f7fb668a125b57dea8d85cd9a8 (diff) |
libsparse: Add .clang-format and reformat to google3 style
Bug: 78793464
Test: compiles
Change-Id: I8e44ba77195a12fc2bac7d4276bbc4aa95149b31
Diffstat (limited to 'libsparse/append2simg.cpp')
-rw-r--r-- | libsparse/append2simg.cpp | 188 |
1 files changed, 93 insertions, 95 deletions
diff --git a/libsparse/append2simg.cpp b/libsparse/append2simg.cpp index 83450a0d9..99f433981 100644 --- a/libsparse/append2simg.cpp +++ b/libsparse/append2simg.cpp @@ -25,8 +25,8 @@ #include <unistd.h> #include <sparse/sparse.h> -#include "sparse_file.h" #include "backed_block.h" +#include "sparse_file.h" #ifndef O_BINARY #define O_BINARY 0 @@ -40,100 +40,98 @@ #define off64_t off_t #endif -void usage() -{ - fprintf(stderr, "Usage: append2simg <output> <input>\n"); +void usage() { + fprintf(stderr, "Usage: append2simg <output> <input>\n"); } -int main(int argc, char *argv[]) -{ - int output; - int output_block; - char *output_path; - struct sparse_file *sparse_output; - - int input; - char *input_path; - off64_t input_len; - - int tmp_fd; - char *tmp_path; - - int ret; - - if (argc == 3) { - output_path = argv[1]; - input_path = argv[2]; - } else { - usage(); - exit(-1); - } - - ret = asprintf(&tmp_path, "%s.append2simg", output_path); - if (ret < 0) { - fprintf(stderr, "Couldn't allocate filename\n"); - exit(-1); - } - - output = open(output_path, O_RDWR | O_BINARY); - if (output < 0) { - fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno)); - exit(-1); - } - - sparse_output = sparse_file_import_auto(output, false, true); - if (!sparse_output) { - fprintf(stderr, "Couldn't import output file\n"); - exit(-1); - } - - input = open(input_path, O_RDONLY | O_BINARY); - if (input < 0) { - fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno)); - exit(-1); - } - - input_len = lseek64(input, 0, SEEK_END); - if (input_len < 0) { - fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno)); - exit(-1); - } else if (input_len % sparse_output->block_size) { - fprintf(stderr, "Input file is not a multiple of the output file's block size"); - exit(-1); - } - lseek64(input, 0, SEEK_SET); - - output_block = sparse_output->len / sparse_output->block_size; - if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) { - fprintf(stderr, "Couldn't add input file\n"); - exit(-1); - } - sparse_output->len += input_len; - - tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664); - if (tmp_fd < 0) { - fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno)); - exit(-1); - } - - lseek64(output, 0, SEEK_SET); - if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) { - fprintf(stderr, "Failed to write sparse file\n"); - exit(-1); - } - - sparse_file_destroy(sparse_output); - close(tmp_fd); - close(output); - close(input); - - ret = rename(tmp_path, output_path); - if (ret < 0) { - fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno)); - exit(-1); - } - - free(tmp_path); - - exit(0); +int main(int argc, char* argv[]) { + int output; + int output_block; + char* output_path; + struct sparse_file* sparse_output; + + int input; + char* input_path; + off64_t input_len; + + int tmp_fd; + char* tmp_path; + + int ret; + + if (argc == 3) { + output_path = argv[1]; + input_path = argv[2]; + } else { + usage(); + exit(-1); + } + + ret = asprintf(&tmp_path, "%s.append2simg", output_path); + if (ret < 0) { + fprintf(stderr, "Couldn't allocate filename\n"); + exit(-1); + } + + output = open(output_path, O_RDWR | O_BINARY); + if (output < 0) { + fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno)); + exit(-1); + } + + sparse_output = sparse_file_import_auto(output, false, true); + if (!sparse_output) { + fprintf(stderr, "Couldn't import output file\n"); + exit(-1); + } + + input = open(input_path, O_RDONLY | O_BINARY); + if (input < 0) { + fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno)); + exit(-1); + } + + input_len = lseek64(input, 0, SEEK_END); + if (input_len < 0) { + fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno)); + exit(-1); + } else if (input_len % sparse_output->block_size) { + fprintf(stderr, "Input file is not a multiple of the output file's block size"); + exit(-1); + } + lseek64(input, 0, SEEK_SET); + + output_block = sparse_output->len / sparse_output->block_size; + if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) { + fprintf(stderr, "Couldn't add input file\n"); + exit(-1); + } + sparse_output->len += input_len; + + tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664); + if (tmp_fd < 0) { + fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno)); + exit(-1); + } + + lseek64(output, 0, SEEK_SET); + if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) { + fprintf(stderr, "Failed to write sparse file\n"); + exit(-1); + } + + sparse_file_destroy(sparse_output); + close(tmp_fd); + close(output); + close(input); + + ret = rename(tmp_path, output_path); + if (ret < 0) { + fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno)); + exit(-1); + } + + free(tmp_path); + + exit(0); } |