Skip to content

Commit

Permalink
lib: chunkio: sync changes
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 11, 2018
1 parent a755d93 commit 2a99007
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/chunkio/deps/crc32/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
# define htole16(x) OSSwapHostToLittleInt16(x)
# define be16toh(x) OSSwapBigToHostInt16(x)
# define le16toh(x) OSSwapLittleToHostInt16(x)
#elif defined(_WIN32)
# define htobe16(x) htons(x)
# define htole16(x) (x)
# define be16toh(x) ntohs(x)
# define le16toh(x) (x)
#else
# include <endian.h>
#endif
Expand Down
2 changes: 2 additions & 0 deletions lib/chunkio/include/chunkio/chunkio.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ int cio_meta_write(struct cio_chunk *ch, char *buf, size_t size);
int cio_meta_cmp(struct cio_chunk *ch, char *meta_buf, int meta_len);
int cio_meta_read(struct cio_chunk *ch, char **meta_buf, int *meta_len);

ssize_t cio_chunk_get_real_size(struct cio_chunk *ch);

#endif
3 changes: 2 additions & 1 deletion lib/chunkio/include/chunkio/cio_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ int cio_chunk_write(struct cio_chunk *ch, const void *buf, size_t count);
int cio_chunk_write_at(struct cio_chunk *ch, off_t offset,
const void *buf, size_t count);
int cio_chunk_sync(struct cio_chunk *ch);
void *cio_chunk_get_content(struct cio_chunk *ch, size_t *size);
int cio_chunk_get_content(struct cio_chunk *ch, char **buf, size_t *size);
ssize_t cio_chunk_get_content_size(struct cio_chunk *ch);
ssize_t cio_chunk_get_real_size(struct cio_chunk *ch);
size_t cio_chunk_get_content_end_pos(struct cio_chunk *ch);
void cio_chunk_close_stream(struct cio_stream *st);
char *cio_chunk_hash(struct cio_chunk *ch);
Expand Down
1 change: 1 addition & 0 deletions lib/chunkio/include/chunkio/cio_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ char *cio_file_hash(struct cio_file *cf);
void cio_file_hash_print(struct cio_file *cf);
void cio_file_calculate_checksum(struct cio_file *cf, crc_t *out);
void cio_file_scan_dump(struct cio_ctx *ctx, struct cio_stream *st);
int cio_file_read_prepare(struct cio_ctx *ctx, struct cio_chunk *ch);

#endif
34 changes: 30 additions & 4 deletions lib/chunkio/src/cio_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ int cio_chunk_sync(struct cio_chunk *ch)
return ret;
}

void *cio_chunk_get_content(struct cio_chunk *ch, size_t *size)
int cio_chunk_get_content(struct cio_chunk *ch, char **buf, size_t *size)
{
int ret;
int type;
struct cio_memfs *mf;
struct cio_file *cf;
Expand All @@ -167,15 +168,21 @@ void *cio_chunk_get_content(struct cio_chunk *ch, size_t *size)
if (type == CIO_STORE_MEM) {
mf = ch->backend;
*size = mf->buf_len;
return mf->buf_data;
*buf = mf->buf_data;
return 0;
}
else if (type == CIO_STORE_FS) {
cf = ch->backend;
ret = cio_file_read_prepare(ch->ctx, ch);
if (ret == -1) {
return -1;
}
*size = cf->data_size;
return cio_file_st_get_content(cf->map);
*buf = cio_file_st_get_content(cf->map);
return 0;
}

return NULL;
return -1;
}

size_t cio_chunk_get_content_end_pos(struct cio_chunk *ch)
Expand Down Expand Up @@ -217,6 +224,25 @@ ssize_t cio_chunk_get_content_size(struct cio_chunk *ch)
return -1;
}

ssize_t cio_chunk_get_real_size(struct cio_chunk *ch)
{
int type;
struct cio_memfs *mf;
struct cio_file *cf;

type = ch->st->type;
if (type == CIO_STORE_MEM) {
mf = ch->backend;
return mf->buf_len;
}
else if (type == CIO_STORE_FS) {
cf = ch->backend;
return cf->fs_size;
}

return -1;
}

void cio_chunk_close_stream(struct cio_stream *st)
{
struct mk_list *tmp;
Expand Down
38 changes: 33 additions & 5 deletions lib/chunkio/src/cio_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ static size_t get_available_size(struct cio_file *cf)
static int cio_file_format_check(struct cio_chunk *ch,
struct cio_file *cf, int flags)
{
char *p;
unsigned char *p;
crc_t crc_check;
crc_t crc;

p = cf->map;
p = (unsigned char *) cf->map;

/* If the file is empty, put the structure on it */
if (cf->fs_size == 0) {
Expand Down Expand Up @@ -166,7 +166,7 @@ static int cio_file_format_check(struct cio_chunk *ch,
}

/* Get hash stored in the mmap */
p = cio_file_st_get_hash(cf->map);
p = (unsigned char *) cio_file_st_get_hash(cf->map);

/* Calculate data checksum in variable */
cio_file_calculate_checksum(cf, &crc);
Expand Down Expand Up @@ -202,7 +202,7 @@ static int mmap_file(struct cio_ctx *ctx, struct cio_chunk *ch, size_t size)

cf = (struct cio_file *) ch->backend;
if (cf->map != NULL) {
return -1;
return 0;
}

/* Check if some previous content exists */
Expand Down Expand Up @@ -274,13 +274,31 @@ static int mmap_file(struct cio_ctx *ctx, struct cio_chunk *ch, size_t size)
cf->fs_size = 0;
}

cio_file_format_check(ch, cf, cf->flags);
ret = cio_file_format_check(ch, cf, cf->flags);
if (ret == -1) {
return -1;
}

cf->st_content = cio_file_st_get_content(cf->map);
cio_log_debug(ctx, "%s:%s mapped OK", ch->st->name, ch->name);

return 0;
}

int cio_file_read_prepare(struct cio_ctx *ctx, struct cio_chunk *ch)

{
int ret;
struct cio_file *cf = ch->backend;

if (!cf->map) {
ret = mmap_file(ctx, ch, 0);
return ret;
}

return 0;
}

/*
* Open or create a data file: the following behavior is expected depending
* of the passed flags:
Expand All @@ -303,6 +321,7 @@ struct cio_file *cio_file_open(struct cio_ctx *ctx,
int len;
char *path;
struct cio_file *cf;
struct stat fst;

len = strlen(ch->name);
if (len == 1 && (ch->name[0] == '.' || ch->name[0] == '/')) {
Expand Down Expand Up @@ -358,6 +377,15 @@ struct cio_file *cio_file_open(struct cio_ctx *ctx,
}
ch->backend = cf;

/* Store the current real size */
ret = fstat(cf->fd, &fst);
if (ret == -1) {
cio_errno();
cio_file_close(ch, CIO_FALSE);
return NULL;
}
cf->fs_size = fst.st_size;

/*
* Map the file 'only' if it was not opened in read-only mode. There some
* cases where the caller can have multiple files to be processed and don't
Expand Down

0 comments on commit 2a99007

Please sign in to comment.