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
This commit is contained in:
Geremy Condra 2013-07-08 14:04:02 -07:00
parent 3ad3d1c4b5
commit de807f2ed8
2 changed files with 114 additions and 2 deletions

View file

@ -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

112
libsparse/append2simg.c Normal file
View file

@ -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);
}