diff options
author | Geremy Condra <gcondra@google.com> | 2013-07-08 14:04:02 -0700 |
---|---|---|
committer | Geremy Condra <gcondra@google.com> | 2013-08-06 22:16:07 -0700 |
commit | de807f2ed8af63a75174933c13705a7529a82914 (patch) | |
tree | 4c036589e7eede43beb2c87fd04d72e224550129 /libsparse | |
parent | 3ad3d1c4b5856d4e314febc5671c74e78a76db00 (diff) | |
download | system_core-de807f2ed8af63a75174933c13705a7529a82914.tar.gz system_core-de807f2ed8af63a75174933c13705a7529a82914.tar.bz2 system_core-de807f2ed8af63a75174933c13705a7529a82914.zip |
Add append2simg.
Append2simg is a small utility designed to glue data to the end
of a sparse image. Beware that it doesn't do anything clever with
that data.
Change-Id: I33af330cb4f77e0842375797a35aebbb6af60163
Diffstat (limited to 'libsparse')
-rw-r--r-- | libsparse/Android.mk | 4 | ||||
-rw-r--r-- | libsparse/append2simg.c | 112 |
2 files changed, 114 insertions, 2 deletions
diff --git a/libsparse/Android.mk b/libsparse/Android.mk index 9025cc025..a21e090c4 100644 --- a/libsparse/Android.mk +++ b/libsparse/Android.mk @@ -82,8 +82,8 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) -LOCAL_SRC_FILES := simg2simg.c -LOCAL_MODULE := simg2simg +LOCAL_SRC_FILES := append2simg.c +LOCAL_MODULE := append2simg LOCAL_STATIC_LIBRARIES := \ libsparse_host \ libz diff --git a/libsparse/append2simg.c b/libsparse/append2simg.c new file mode 100644 index 000000000..180584f48 --- /dev/null +++ b/libsparse/append2simg.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _FILE_OFFSET_BITS 64 +#define _LARGEFILE64_SOURCE 1 + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sparse/sparse.h> +#include "sparse_file.h" +#include "backed_block.h" + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +#if defined(__APPLE__) && defined(__MACH__) +#define lseek64 lseek +#endif +#if defined(__APPLE__) && defined(__MACH__) +#define lseek64 lseek +#define off64_t off_t +#endif + +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; + + if (argc == 3) { + output_path = argv[1]; + input_path = argv[2]; + } else { + usage(); + 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, 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; + + lseek64(output, 0, SEEK_SET); + if (sparse_file_write(sparse_output, output, false, true, false) < 0) { + fprintf(stderr, "Failed to write sparse file\n"); + exit(-1); + } + + sparse_file_destroy(sparse_output); + close(output); + close(input); + exit(0); +} |