Fix mtar file size truncation to 4G

This commit is contained in:
Michael Krasnyk 2018-04-10 00:48:00 +02:00
parent 473ebfcbf6
commit 397bb694fd
2 changed files with 30 additions and 26 deletions

View File

@ -41,7 +41,7 @@ typedef struct {
} mtar_raw_header_t;
static unsigned round_up(unsigned n, unsigned incr) {
static mtar_size_t round_up(mtar_size_t n, unsigned incr) {
return n + (incr - n % incr) % incr;
}
@ -60,14 +60,14 @@ static unsigned checksum(const mtar_raw_header_t* rh) {
}
static int tread(mtar_t *tar, void *data, unsigned size) {
static int tread(mtar_t *tar, void *data, mtar_size_t size) {
int err = tar->read(tar, data, size);
tar->pos += size;
return err;
}
static int twrite(mtar_t *tar, const void *data, unsigned size) {
static int twrite(mtar_t *tar, const void *data, mtar_size_t size) {
int err = tar->write(tar, data, size);
tar->pos += size;
return err;
@ -105,7 +105,7 @@ static int raw_to_header(mtar_header_t *h, const mtar_raw_header_t *rh) {
/* Load raw header into header */
sscanf(rh->mode, "%o", &h->mode);
sscanf(rh->owner, "%o", &h->owner);
sscanf(rh->size, "%o", &h->size);
sscanf(rh->size, "%lo", &h->size);
sscanf(rh->mtime, "%o", &h->mtime);
h->type = rh->type;
strcpy(h->name, rh->name);
@ -122,7 +122,7 @@ static int header_to_raw(mtar_raw_header_t *rh, const mtar_header_t *h) {
memset(rh, 0, sizeof(*rh));
sprintf(rh->mode, "%o", h->mode);
sprintf(rh->owner, "%o", h->owner);
sprintf(rh->size, "%o", h->size);
sprintf(rh->size, "%lo", h->size);
sprintf(rh->mtime, "%o", h->mtime);
rh->type = h->type ? h->type : MTAR_TREG;
strcpy(rh->name, h->name);
@ -153,17 +153,17 @@ const char* mtar_strerror(int err) {
}
static int file_write(mtar_t *tar, const void *data, unsigned size) {
unsigned res = fwrite(data, 1, size, tar->stream);
static int file_write(mtar_t *tar, const void *data, mtar_size_t size) {
mtar_size_t res = fwrite(data, 1, size, tar->stream);
return (res == size) ? MTAR_ESUCCESS : MTAR_EWRITEFAIL;
}
static int file_read(mtar_t *tar, void *data, unsigned size) {
unsigned res = fread(data, 1, size, tar->stream);
static int file_read(mtar_t *tar, void *data, mtar_size_t size) {
mtar_size_t res = fread(data, 1, size, tar->stream);
return (res == size) ? MTAR_ESUCCESS : MTAR_EREADFAIL;
}
static int file_seek(mtar_t *tar, unsigned offset) {
static int file_seek(mtar_t *tar, mtar_size_t offset) {
int res = fseek(tar->stream, offset, SEEK_SET);
return (res == 0) ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
}
@ -213,7 +213,7 @@ int mtar_close(mtar_t *tar) {
}
int mtar_seek(mtar_t *tar, unsigned pos) {
int mtar_seek(mtar_t *tar, mtar_size_t pos) {
int err = tar->seek(tar, pos);
tar->pos = pos;
return err;
@ -228,7 +228,8 @@ int mtar_rewind(mtar_t *tar) {
int mtar_next(mtar_t *tar) {
int err, n;
mtar_size_t n;
int err;
mtar_header_t h;
/* Load header */
err = mtar_read_header(tar, &h);
@ -287,7 +288,7 @@ int mtar_read_header(mtar_t *tar, mtar_header_t *h) {
}
int mtar_read_data(mtar_t *tar, void *ptr, unsigned size) {
int mtar_read_data(mtar_t *tar, void *ptr, mtar_size_t size) {
int err;
/* If we have no remaining data then this is the first read, we get the size,
* set the remaining data and seek to the beginning of the data */
@ -329,7 +330,7 @@ int mtar_write_header(mtar_t *tar, const mtar_header_t *h) {
}
int mtar_write_file_header(mtar_t *tar, const char *name, unsigned size) {
int mtar_write_file_header(mtar_t *tar, const char *name, mtar_size_t size) {
mtar_header_t h;
/* Build header */
memset(&h, 0, sizeof(h));
@ -354,7 +355,7 @@ int mtar_write_dir_header(mtar_t *tar, const char *name) {
}
int mtar_write_data(mtar_t *tar, const void *data, unsigned size) {
int mtar_write_data(mtar_t *tar, const void *data, mtar_size_t size) {
int err;
/* Write data */
err = twrite(tar, data, size);

View File

@ -10,9 +10,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MTAR_VERSION "0.1.0"
typedef size_t mtar_size_t;
enum {
MTAR_ESUCCESS = 0,
MTAR_EFAILURE = -1,
@ -38,7 +41,7 @@ enum {
typedef struct {
unsigned mode;
unsigned owner;
unsigned size;
mtar_size_t size;
unsigned mtime;
unsigned type;
char name[100];
@ -49,14 +52,14 @@ typedef struct {
typedef struct mtar_t mtar_t;
struct mtar_t {
int (*read)(mtar_t *tar, void *data, unsigned size);
int (*write)(mtar_t *tar, const void *data, unsigned size);
int (*seek)(mtar_t *tar, unsigned pos);
int (*read)(mtar_t *tar, void *data, mtar_size_t size);
int (*write)(mtar_t *tar, const void *data, mtar_size_t size);
int (*seek)(mtar_t *tar, mtar_size_t pos);
int (*close)(mtar_t *tar);
void *stream;
unsigned pos;
unsigned remaining_data;
unsigned last_header;
mtar_size_t pos;
mtar_size_t remaining_data;
mtar_size_t last_header;
};
@ -65,17 +68,17 @@ const char* mtar_strerror(int err);
int mtar_open(mtar_t *tar, const char *filename, const char *mode);
int mtar_close(mtar_t *tar);
int mtar_seek(mtar_t *tar, unsigned pos);
int mtar_seek(mtar_t *tar, mtar_size_t pos);
int mtar_rewind(mtar_t *tar);
int mtar_next(mtar_t *tar);
int mtar_find(mtar_t *tar, const char *name, mtar_header_t *h);
int mtar_read_header(mtar_t *tar, mtar_header_t *h);
int mtar_read_data(mtar_t *tar, void *ptr, unsigned size);
int mtar_read_data(mtar_t *tar, void *ptr, mtar_size_t size);
int mtar_write_header(mtar_t *tar, const mtar_header_t *h);
int mtar_write_file_header(mtar_t *tar, const char *name, unsigned size);
int mtar_write_file_header(mtar_t *tar, const char *name, mtar_size_t size);
int mtar_write_dir_header(mtar_t *tar, const char *name);
int mtar_write_data(mtar_t *tar, const void *data, unsigned size);
int mtar_write_data(mtar_t *tar, const void *data, mtar_size_t size);
int mtar_finalize(mtar_t *tar);