Merge "libsparse: Fix verbose_error string usage"

am: 0bdf6539d6

Change-Id: I9769a91fac4a3acf4e3e1c7b02b896217c6d9d3d
This commit is contained in:
Chris Fries 2017-04-18 22:00:48 +00:00 committed by android-build-merger
commit 006ea1aebc
5 changed files with 60 additions and 53 deletions

View file

@ -10,17 +10,23 @@ cc_library {
"sparse.c", "sparse.c",
"sparse_crc32.c", "sparse_crc32.c",
"sparse_err.c", "sparse_err.c",
"sparse_read.c", "sparse_read.cpp",
], ],
cflags: ["-Werror"], cflags: ["-Werror"],
local_include_dirs: ["include"], local_include_dirs: ["include"],
export_include_dirs: ["include"], export_include_dirs: ["include"],
target: { target: {
host: { host: {
shared_libs: ["libz-host"], shared_libs: [
"libz-host",
"libbase",
],
}, },
android: { android: {
shared_libs: ["libz"], shared_libs: [
"libz",
"libbase",
],
}, },
windows: { windows: {
enabled: true, enabled: true,
@ -38,6 +44,7 @@ cc_binary {
static_libs: [ static_libs: [
"libsparse", "libsparse",
"libz", "libz",
"libbase",
], ],
cflags: ["-Werror"], cflags: ["-Werror"],
@ -50,6 +57,7 @@ cc_binary {
static_libs: [ static_libs: [
"libsparse", "libsparse",
"libz", "libz",
"libbase",
], ],
cflags: ["-Werror"], cflags: ["-Werror"],
@ -61,6 +69,7 @@ cc_binary_host {
static_libs: [ static_libs: [
"libsparse", "libsparse",
"libz", "libz",
"libbase",
], ],
cflags: ["-Werror"], cflags: ["-Werror"],

View file

@ -17,6 +17,10 @@
#ifndef _OUTPUT_FILE_H_ #ifndef _OUTPUT_FILE_H_
#define _OUTPUT_FILE_H_ #define _OUTPUT_FILE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <sparse/sparse.h> #include <sparse/sparse.h>
struct output_file; struct output_file;
@ -38,4 +42,8 @@ void output_file_close(struct output_file *out);
int read_all(int fd, void *buf, size_t len); int read_all(int fd, void *buf, size_t len);
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -17,6 +17,10 @@
#ifndef _LIBSPARSE_SPARSE_FILE_H_ #ifndef _LIBSPARSE_SPARSE_FILE_H_
#define _LIBSPARSE_SPARSE_FILE_H_ #define _LIBSPARSE_SPARSE_FILE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <sparse/sparse.h> #include <sparse/sparse.h>
struct sparse_file { struct sparse_file {
@ -28,5 +32,8 @@ struct sparse_file {
struct output_file *out; struct output_file *out;
}; };
#ifdef __cplusplus
}
#endif
#endif /* _LIBSPARSE_SPARSE_FILE_H_ */ #endif /* _LIBSPARSE_SPARSE_FILE_H_ */

View file

@ -18,6 +18,10 @@
#define _LIBSPARSE_SPARSE_FORMAT_H_ #define _LIBSPARSE_SPARSE_FORMAT_H_
#include "sparse_defs.h" #include "sparse_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct sparse_header { typedef struct sparse_header {
__le32 magic; /* 0xed26ff3a */ __le32 magic; /* 0xed26ff3a */
__le16 major_version; /* (0x1) - reject images with higher major versions */ __le16 major_version; /* (0x1) - reject images with higher major versions */
@ -52,4 +56,8 @@ typedef struct chunk_header {
* For a CRC32 chunk, it's 4 bytes of CRC32 * For a CRC32 chunk, it's 4 bytes of CRC32
*/ */
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE 1 #define _LARGEFILE64_SOURCE 1
#include <algorithm>
#include <inttypes.h> #include <inttypes.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdarg.h> #include <stdarg.h>
@ -25,17 +25,19 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string>
#include <unistd.h> #include <unistd.h>
#include <sparse/sparse.h> #include <sparse/sparse.h>
#include "android-base/stringprintf.h"
#include "defs.h" #include "defs.h"
#include "output_file.h" #include "output_file.h"
#include "sparse_crc32.h" #include "sparse_crc32.h"
#include "sparse_file.h" #include "sparse_file.h"
#include "sparse_format.h" #include "sparse_format.h"
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
#define lseek64 lseek #define lseek64 lseek
#define off64_t off_t #define off64_t off_t
@ -45,57 +47,30 @@
#define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) #define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
#define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) #define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
#define COPY_BUF_SIZE (1024U*1024U) static constexpr int64_t COPY_BUF_SIZE = 1024 * 1024;
static char *copybuf; static char *copybuf;
#define min(a, b) \ static std::string ErrorString(int err)
({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) {
if (err == -EOVERFLOW) return "EOF while reading file";
if (err == -EINVAL) return "Invalid sparse file format";
if (err == -ENOMEM) return "Failed allocation while reading file";
return android::base::StringPrintf("Unknown error %d", err);
}
static void verbose_error(bool verbose, int err, const char *fmt, ...) static void verbose_error(bool verbose, int err, const char *fmt, ...)
{ {
char *s = ""; if (!verbose) return;
char *at = "";
std::string msg = ErrorString(err);
if (fmt) { if (fmt) {
msg += " at ";
va_list argp; va_list argp;
int size;
va_start(argp, fmt); va_start(argp, fmt);
size = vsnprintf(NULL, 0, fmt, argp); android::base::StringAppendV(&msg, fmt, argp);
va_end(argp); va_end(argp);
if (size < 0) {
return;
}
at = malloc(size + 1);
if (at == NULL) {
return;
}
va_start(argp, fmt);
vsnprintf(at, size, fmt, argp);
va_end(argp);
at[size] = 0;
s = " at ";
}
if (verbose) {
#ifndef _WIN32
if (err == -EOVERFLOW) {
sparse_print_verbose("EOF while reading file%s%s\n", s, at);
} else
#endif
if (err == -EINVAL) {
sparse_print_verbose("Invalid sparse file format%s%s\n", s, at);
} else if (err == -ENOMEM) {
sparse_print_verbose("Failed allocation while reading file%s%s\n",
s, at);
} else {
sparse_print_verbose("Unknown error %d%s%s\n", err, s, at);
}
}
if (fmt) {
free(at);
} }
sparse_print_verbose("%s\n", msg.c_str());
} }
static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size,
@ -104,7 +79,7 @@ static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size,
{ {
int ret; int ret;
int chunk; int chunk;
unsigned int len = blocks * s->block_size; int64_t len = blocks * s->block_size;
if (chunk_size % s->block_size != 0) { if (chunk_size % s->block_size != 0) {
return -EINVAL; return -EINVAL;
@ -121,7 +96,7 @@ static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size,
if (crc32) { if (crc32) {
while (len) { while (len) {
chunk = min(len, COPY_BUF_SIZE); chunk = std::min(len, COPY_BUF_SIZE);
ret = read_all(fd, copybuf, chunk); ret = read_all(fd, copybuf, chunk);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
@ -168,7 +143,7 @@ static int process_fill_chunk(struct sparse_file *s, unsigned int chunk_size,
} }
while (len) { while (len) {
chunk = min(len, COPY_BUF_SIZE); chunk = std::min(len, COPY_BUF_SIZE);
*crc32 = sparse_crc32(*crc32, copybuf, chunk); *crc32 = sparse_crc32(*crc32, copybuf, chunk);
len -= chunk; len -= chunk;
} }
@ -190,7 +165,7 @@ static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size,
memset(copybuf, 0, COPY_BUF_SIZE); memset(copybuf, 0, COPY_BUF_SIZE);
while (len) { while (len) {
int chunk = min(len, COPY_BUF_SIZE); int chunk = std::min(len, COPY_BUF_SIZE);
*crc32 = sparse_crc32(*crc32, copybuf, chunk); *crc32 = sparse_crc32(*crc32, copybuf, chunk);
len -= chunk; len -= chunk;
} }
@ -284,7 +259,7 @@ static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc)
off64_t offset; off64_t offset;
if (!copybuf) { if (!copybuf) {
copybuf = malloc(COPY_BUF_SIZE); copybuf = (char *)malloc(COPY_BUF_SIZE);
} }
if (!copybuf) { if (!copybuf) {
@ -357,7 +332,7 @@ static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc)
static int sparse_file_read_normal(struct sparse_file *s, int fd) static int sparse_file_read_normal(struct sparse_file *s, int fd)
{ {
int ret; int ret;
uint32_t *buf = malloc(s->block_size); uint32_t *buf = (uint32_t *)malloc(s->block_size);
unsigned int block = 0; unsigned int block = 0;
int64_t remain = s->len; int64_t remain = s->len;
int64_t offset = 0; int64_t offset = 0;
@ -370,7 +345,7 @@ static int sparse_file_read_normal(struct sparse_file *s, int fd)
} }
while (remain > 0) { while (remain > 0) {
to_read = min(remain, s->block_size); to_read = std::min(remain, (int64_t)(s->block_size));
ret = read_all(fd, buf, to_read); ret = read_all(fd, buf, to_read);
if (ret < 0) { if (ret < 0) {
error("failed to read sparse file"); error("failed to read sparse file");