Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b5e14ead authored by Geremy Condra's avatar Geremy Condra Committed by Android (Google) Code Review
Browse files

Merge "Add append2simg."

parents 5660da25 de807f2e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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 −0
Original line number Diff line number Diff line
/*
 * 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);
}