Migration to iso_file_src in file writing function.
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
/*
|
||||
* Implementation of iso_file_src for:
|
||||
*
|
||||
* a) read from local filesystem files
|
||||
* b) read from previous session / image files
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
@@ -10,9 +17,24 @@
|
||||
|
||||
#include "file_src.h"
|
||||
#include "messages.h"
|
||||
#include "libisofs.h"
|
||||
#include "util.h"
|
||||
|
||||
#define BLOCK_SIZE 2048
|
||||
|
||||
|
||||
void iso_file_src_free(struct iso_file_src *src)
|
||||
{
|
||||
src->free_data(src);
|
||||
free(src);
|
||||
}
|
||||
|
||||
/*
|
||||
* ==========================================================================
|
||||
* A) FILE SRC FOR LOCAL FILESYSTEM FILES
|
||||
* ==========================================================================
|
||||
*/
|
||||
|
||||
struct local_file_data {
|
||||
int fd; /* the file descriptor, once the file is opened */
|
||||
off_t size; /* file size */
|
||||
@@ -117,7 +139,10 @@ off_t lf_get_size(struct iso_file_src *src)
|
||||
static
|
||||
void lf_free_data(struct iso_file_src *src)
|
||||
{
|
||||
free(src->data);
|
||||
struct local_file_data *data;
|
||||
data = (struct local_file_data*) src->data;
|
||||
free(data->path);
|
||||
free(data);
|
||||
}
|
||||
|
||||
struct iso_file_src*
|
||||
@@ -161,3 +186,121 @@ iso_file_src_from_path(const char *path)
|
||||
src->data = data;
|
||||
return src;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ==========================================================================
|
||||
* B) FILE SRC FOR PREVIOUS IMAGE FILES
|
||||
* ==========================================================================
|
||||
*/
|
||||
|
||||
struct prev_img_file_data {
|
||||
int block; /* block where image begins */
|
||||
off_t size; /* file size */
|
||||
int nblocks; /* file size in blocks */
|
||||
int current; /* last block read from file */
|
||||
int error;
|
||||
struct data_source *src; /* source for reading from previous image */
|
||||
};
|
||||
|
||||
static
|
||||
int pi_open(struct iso_file_src *src)
|
||||
{
|
||||
struct prev_img_file_data *data;
|
||||
assert(src);
|
||||
|
||||
data = (struct prev_img_file_data*) src->data;
|
||||
data->current = data->block;
|
||||
data->error = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static
|
||||
void pi_close(struct iso_file_src *src)
|
||||
{
|
||||
/* nothing needed */
|
||||
return;
|
||||
}
|
||||
|
||||
static
|
||||
int pi_read_block(struct iso_file_src *src, unsigned char *buffer)
|
||||
{
|
||||
int res;
|
||||
struct prev_img_file_data *data;
|
||||
assert(src);
|
||||
assert(buffer);
|
||||
|
||||
data = (struct prev_img_file_data*) src->data;
|
||||
|
||||
if (data->current >= data->block + data->nblocks) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (data->error) {
|
||||
memset(buffer, 0, BLOCK_SIZE);
|
||||
data->current++;
|
||||
return -2;
|
||||
}
|
||||
|
||||
res = data->src->read_block(data->src, data->current++, buffer);
|
||||
if (res < 0) {
|
||||
iso_msg_sorry(LIBISO_CANT_READ_IMG,
|
||||
"Problem reading file from previous image");
|
||||
/* fill buffer with 0s and return */
|
||||
memset(buffer, 0, BLOCK_SIZE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data->current >= data->block + data->nblocks) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
off_t pi_get_size(struct iso_file_src *src)
|
||||
{
|
||||
struct prev_img_file_data *data;
|
||||
assert(src);
|
||||
|
||||
data = (struct prev_img_file_data*) src->data;
|
||||
return data->size;
|
||||
}
|
||||
|
||||
static
|
||||
void pi_free_data(struct iso_file_src *src)
|
||||
{
|
||||
free(src->data);
|
||||
}
|
||||
|
||||
struct iso_file_src*
|
||||
iso_file_src_from_prev_img(struct data_source* data_src, int block, off_t size)
|
||||
{
|
||||
struct iso_file_src *src;
|
||||
struct prev_img_file_data *data;
|
||||
|
||||
data = malloc(sizeof(struct prev_img_file_data));
|
||||
if (!data)
|
||||
return NULL;
|
||||
|
||||
src = malloc(sizeof(struct iso_file_src));
|
||||
if (!src) {
|
||||
free(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data->block = block;
|
||||
data->size = size;
|
||||
data->nblocks = div_up(size, BLOCK_SIZE);
|
||||
data->src = data_src;
|
||||
|
||||
src->open = pi_open;
|
||||
src->close = pi_close;
|
||||
src->read_block = pi_read_block;
|
||||
src->get_size = pi_get_size;
|
||||
src->free_data = pi_free_data;
|
||||
|
||||
src->data = data;
|
||||
return src;
|
||||
}
|
||||
|
Reference in New Issue
Block a user