Skip to content

Commit e512570

Browse files
committed
Import libsparse sources
Signed-off-by: Jo-Philipp Wich <[email protected]>
1 parent 58ace71 commit e512570

20 files changed

+3454
-0
lines changed

libsparse/Android.mk

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2010 The Android Open Source Project
2+
3+
LOCAL_PATH:= $(call my-dir)
4+
5+
libsparse_src_files := \
6+
backed_block.c \
7+
output_file.c \
8+
sparse.c \
9+
sparse_crc32.c \
10+
sparse_err.c \
11+
sparse_read.c
12+
13+
14+
include $(CLEAR_VARS)
15+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
16+
LOCAL_SRC_FILES := $(libsparse_src_files)
17+
LOCAL_MODULE := libsparse_host
18+
LOCAL_STATIC_LIBRARIES := libz
19+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
20+
LOCAL_CFLAGS := -Werror
21+
include $(BUILD_HOST_STATIC_LIBRARY)
22+
23+
24+
include $(CLEAR_VARS)
25+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
26+
LOCAL_SRC_FILES := $(libsparse_src_files)
27+
LOCAL_MODULE := libsparse
28+
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
29+
LOCAL_SHARED_LIBRARIES := \
30+
libz
31+
LOCAL_CFLAGS := -Werror
32+
include $(BUILD_SHARED_LIBRARY)
33+
34+
35+
include $(CLEAR_VARS)
36+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
37+
LOCAL_SRC_FILES := $(libsparse_src_files)
38+
LOCAL_MODULE := libsparse_static
39+
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
40+
LOCAL_STATIC_LIBRARIES := libz
41+
LOCAL_CFLAGS := -Werror
42+
include $(BUILD_STATIC_LIBRARY)
43+
44+
45+
include $(CLEAR_VARS)
46+
LOCAL_SRC_FILES := simg2img.c \
47+
sparse_crc32.c
48+
LOCAL_MODULE := simg2img_host
49+
# Need a unique module name, but exe should still be called simg2img
50+
LOCAL_MODULE_STEM := simg2img
51+
LOCAL_STATIC_LIBRARIES := \
52+
libsparse_host \
53+
libz
54+
LOCAL_CFLAGS := -Werror
55+
include $(BUILD_HOST_EXECUTABLE)
56+
57+
58+
include $(CLEAR_VARS)
59+
LOCAL_SRC_FILES := simg2img.c \
60+
sparse_crc32.c
61+
LOCAL_MODULE := simg2img
62+
LOCAL_STATIC_LIBRARIES := \
63+
libsparse_static \
64+
libz
65+
LOCAL_CFLAGS := -Werror
66+
include $(BUILD_EXECUTABLE)
67+
68+
69+
include $(CLEAR_VARS)
70+
LOCAL_SRC_FILES := img2simg.c
71+
LOCAL_MODULE := img2simg_host
72+
# Need a unique module name, but exe should still be called simg2img
73+
LOCAL_MODULE_STEM := img2simg
74+
LOCAL_STATIC_LIBRARIES := \
75+
libsparse_host \
76+
libz
77+
LOCAL_CFLAGS := -Werror
78+
include $(BUILD_HOST_EXECUTABLE)
79+
80+
81+
include $(CLEAR_VARS)
82+
LOCAL_SRC_FILES := img2simg.c
83+
LOCAL_MODULE := img2simg
84+
LOCAL_STATIC_LIBRARIES := \
85+
libsparse_static \
86+
libz
87+
LOCAL_CFLAGS := -Werror
88+
include $(BUILD_EXECUTABLE)
89+
90+
91+
ifneq ($(HOST_OS),windows)
92+
93+
include $(CLEAR_VARS)
94+
LOCAL_SRC_FILES := append2simg.c
95+
LOCAL_MODULE := append2simg
96+
LOCAL_STATIC_LIBRARIES := \
97+
libsparse_host \
98+
libz
99+
LOCAL_CFLAGS := -Werror
100+
include $(BUILD_HOST_EXECUTABLE)
101+
102+
endif
103+
104+
include $(CLEAR_VARS)
105+
LOCAL_MODULE := simg_dump.py
106+
LOCAL_SRC_FILES := simg_dump.py
107+
LOCAL_MODULE_CLASS := EXECUTABLES
108+
LOCAL_IS_HOST_MODULE := true
109+
LOCAL_CFLAGS := -Werror
110+
include $(BUILD_PREBUILT)

libsparse/append2simg.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#define _FILE_OFFSET_BITS 64
18+
#define _LARGEFILE64_SOURCE 1
19+
#define _GNU_SOURCE
20+
21+
#include <errno.h>
22+
#include <fcntl.h>
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <string.h>
26+
#include <unistd.h>
27+
28+
#include <sparse/sparse.h>
29+
#include "sparse_file.h"
30+
#include "backed_block.h"
31+
32+
#ifndef O_BINARY
33+
#define O_BINARY 0
34+
#endif
35+
36+
#if defined(__APPLE__) && defined(__MACH__)
37+
#define lseek64 lseek
38+
#endif
39+
#if defined(__APPLE__) && defined(__MACH__)
40+
#define lseek64 lseek
41+
#define off64_t off_t
42+
#endif
43+
44+
void usage()
45+
{
46+
fprintf(stderr, "Usage: append2simg <output> <input>\n");
47+
}
48+
49+
int main(int argc, char *argv[])
50+
{
51+
int output;
52+
int output_block;
53+
char *output_path;
54+
struct sparse_file *sparse_output;
55+
56+
int input;
57+
char *input_path;
58+
off64_t input_len;
59+
60+
int tmp_fd;
61+
char *tmp_path;
62+
63+
int ret;
64+
65+
if (argc == 3) {
66+
output_path = argv[1];
67+
input_path = argv[2];
68+
} else {
69+
usage();
70+
exit(-1);
71+
}
72+
73+
ret = asprintf(&tmp_path, "%s.append2simg", output_path);
74+
if (ret < 0) {
75+
fprintf(stderr, "Couldn't allocate filename\n");
76+
exit(-1);
77+
}
78+
79+
output = open(output_path, O_RDWR | O_BINARY);
80+
if (output < 0) {
81+
fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno));
82+
exit(-1);
83+
}
84+
85+
sparse_output = sparse_file_import_auto(output, true, true);
86+
if (!sparse_output) {
87+
fprintf(stderr, "Couldn't import output file\n");
88+
exit(-1);
89+
}
90+
91+
input = open(input_path, O_RDONLY | O_BINARY);
92+
if (input < 0) {
93+
fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno));
94+
exit(-1);
95+
}
96+
97+
input_len = lseek64(input, 0, SEEK_END);
98+
if (input_len < 0) {
99+
fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno));
100+
exit(-1);
101+
} else if (input_len % sparse_output->block_size) {
102+
fprintf(stderr, "Input file is not a multiple of the output file's block size");
103+
exit(-1);
104+
}
105+
lseek64(input, 0, SEEK_SET);
106+
107+
output_block = sparse_output->len / sparse_output->block_size;
108+
if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) {
109+
fprintf(stderr, "Couldn't add input file\n");
110+
exit(-1);
111+
}
112+
sparse_output->len += input_len;
113+
114+
tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664);
115+
if (tmp_fd < 0) {
116+
fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno));
117+
exit(-1);
118+
}
119+
120+
lseek64(output, 0, SEEK_SET);
121+
if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) {
122+
fprintf(stderr, "Failed to write sparse file\n");
123+
exit(-1);
124+
}
125+
126+
sparse_file_destroy(sparse_output);
127+
close(tmp_fd);
128+
close(output);
129+
close(input);
130+
131+
ret = rename(tmp_path, output_path);
132+
if (ret < 0) {
133+
fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno));
134+
exit(-1);
135+
}
136+
137+
free(tmp_path);
138+
139+
exit(0);
140+
}

0 commit comments

Comments
 (0)