2007-12-15 12:13:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Vreixo Formoso
|
|
|
|
*
|
|
|
|
* This file is part of the libisofs project; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation. See COPYING file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filesrc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "node.h"
|
2007-12-19 23:25:25 +00:00
|
|
|
#include "util.h"
|
2007-12-20 21:17:18 +00:00
|
|
|
#include "writer.h"
|
|
|
|
#include "messages.h"
|
2007-12-30 21:04:41 +00:00
|
|
|
#include "image.h"
|
2007-12-15 12:13:49 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-12-20 21:17:18 +00:00
|
|
|
#include <string.h>
|
2007-12-16 18:10:47 +00:00
|
|
|
|
2007-12-19 23:25:25 +00:00
|
|
|
int iso_file_src_cmp(const void *n1, const void *n2)
|
2007-12-15 12:13:49 +00:00
|
|
|
{
|
|
|
|
const IsoFileSrc *f1, *f2;
|
2007-12-18 19:46:28 +00:00
|
|
|
unsigned int fs_id1, fs_id2;
|
|
|
|
dev_t dev_id1, dev_id2;
|
|
|
|
ino_t ino_id1, ino_id2;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-15 12:13:49 +00:00
|
|
|
f1 = (const IsoFileSrc *)n1;
|
|
|
|
f2 = (const IsoFileSrc *)n2;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-10 16:56:39 +00:00
|
|
|
iso_stream_get_id(f1->stream, &fs_id1, &dev_id1, &ino_id1);
|
|
|
|
iso_stream_get_id(f2->stream, &fs_id2, &dev_id2, &ino_id2);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-18 19:46:28 +00:00
|
|
|
if (fs_id1 < fs_id2) {
|
2007-12-15 12:13:49 +00:00
|
|
|
return -1;
|
2007-12-18 19:46:28 +00:00
|
|
|
} else if (fs_id1 > fs_id2) {
|
2007-12-15 12:13:49 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* files belong to the same fs */
|
2007-12-18 19:46:28 +00:00
|
|
|
if (dev_id1 > dev_id2) {
|
2007-12-15 12:13:49 +00:00
|
|
|
return -1;
|
2007-12-18 19:46:28 +00:00
|
|
|
} else if (dev_id1 < dev_id2) {
|
2007-12-15 12:13:49 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* files belong to same device in same fs */
|
2007-12-28 21:10:17 +00:00
|
|
|
return (ino_id1 < ino_id2) ? -1 : (ino_id1 > ino_id2) ? 1 : 0;
|
2007-12-15 12:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iso_file_src_create(Ecma119Image *img, IsoFile *file, IsoFileSrc **src)
|
|
|
|
{
|
2008-01-10 16:56:39 +00:00
|
|
|
int ret;
|
2007-12-15 12:13:49 +00:00
|
|
|
IsoFileSrc *fsrc;
|
|
|
|
unsigned int fs_id;
|
|
|
|
dev_t dev_id;
|
|
|
|
ino_t ino_id;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-15 12:13:49 +00:00
|
|
|
if (img == NULL || file == NULL || src == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-10 16:56:39 +00:00
|
|
|
iso_stream_get_id(file->stream, &fs_id, &dev_id, &ino_id);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-10 16:56:39 +00:00
|
|
|
fsrc = malloc(sizeof(IsoFileSrc));
|
|
|
|
if (fsrc == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-10 16:56:39 +00:00
|
|
|
/* fill key and other atts */
|
|
|
|
fsrc->prev_img = file->msblock ? 1 : 0;
|
|
|
|
fsrc->block = file->msblock;
|
|
|
|
fsrc->sort_weight = file->sort_weight;
|
|
|
|
fsrc->stream = file->stream;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-10 16:56:39 +00:00
|
|
|
/* insert the filesrc in the tree */
|
|
|
|
ret = iso_rbtree_insert(img->files, fsrc, (void**)src);
|
|
|
|
if (ret <= 0) {
|
|
|
|
free(fsrc);
|
|
|
|
return ret;
|
2007-12-15 12:13:49 +00:00
|
|
|
}
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-19 23:25:25 +00:00
|
|
|
void iso_file_src_free(void *node)
|
2007-12-16 18:10:47 +00:00
|
|
|
{
|
2007-12-19 23:25:25 +00:00
|
|
|
free(node);
|
2007-12-16 18:10:47 +00:00
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
|
|
|
|
off_t iso_file_src_get_size(IsoFileSrc *file)
|
|
|
|
{
|
2007-12-20 19:58:03 +00:00
|
|
|
return iso_stream_get_size(file->stream);
|
2007-12-18 19:46:28 +00:00
|
|
|
}
|
2007-12-20 21:17:18 +00:00
|
|
|
|
2007-12-28 21:10:17 +00:00
|
|
|
static int cmp_by_weight(const void *f1, const void *f2)
|
2007-12-20 21:17:18 +00:00
|
|
|
{
|
|
|
|
IsoFileSrc *f = *((IsoFileSrc**)f1);
|
|
|
|
IsoFileSrc *g = *((IsoFileSrc**)f2);
|
|
|
|
/* higher weighted first */
|
|
|
|
return g->sort_weight - f->sort_weight;
|
|
|
|
}
|
|
|
|
|
2008-01-04 22:54:31 +00:00
|
|
|
static
|
|
|
|
int is_ms_file(void *arg)
|
|
|
|
{
|
|
|
|
IsoFileSrc *f = (IsoFileSrc *)arg;
|
|
|
|
return f->prev_img ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
static
|
|
|
|
int filesrc_writer_compute_data_blocks(IsoImageWriter *writer)
|
|
|
|
{
|
|
|
|
size_t i, size;
|
|
|
|
Ecma119Image *t;
|
|
|
|
IsoFileSrc **filelist;
|
2008-01-04 22:54:31 +00:00
|
|
|
int (*inc_item)(void *);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
t = writer->target;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-04 22:54:31 +00:00
|
|
|
/* on appendable images, ms files shouldn't be included */
|
|
|
|
if (t->appendable) {
|
|
|
|
inc_item = is_ms_file;
|
|
|
|
} else {
|
|
|
|
inc_item = NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* store the filesrcs in a array */
|
2008-01-04 22:54:31 +00:00
|
|
|
filelist = (IsoFileSrc**)iso_rbtree_to_array(t->files, inc_item, &size);
|
2007-12-20 21:17:18 +00:00
|
|
|
if (filelist == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
|
|
|
/* sort files by weight, if needed */
|
2007-12-20 21:17:18 +00:00
|
|
|
if (t->sort_files) {
|
2008-01-04 09:11:25 +00:00
|
|
|
qsort(filelist, size, sizeof(void*), cmp_by_weight);
|
2007-12-20 21:17:18 +00:00
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* fill block value */
|
|
|
|
for (i = 0; i < size; ++i) {
|
|
|
|
IsoFileSrc *file = filelist[i];
|
|
|
|
file->block = t->curblock;
|
|
|
|
t->curblock += div_up(iso_file_src_get_size(file), BLOCK_SIZE);
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* the list is only needed by this writer, store locally */
|
|
|
|
writer->data = filelist;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int filesrc_writer_write_vol_desc(IsoImageWriter *writer)
|
|
|
|
{
|
|
|
|
/* nothing needed */
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open a file, i.e., its Stream */
|
|
|
|
static inline
|
|
|
|
int filesrc_open(IsoFileSrc *file)
|
|
|
|
{
|
|
|
|
return iso_stream_open(file->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
int filesrc_close(IsoFileSrc *file)
|
|
|
|
{
|
|
|
|
return iso_stream_close(file->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
* 1 ok, 0 EOF, < 0 error
|
|
|
|
*/
|
2007-12-28 21:10:17 +00:00
|
|
|
static
|
2007-12-20 21:17:18 +00:00
|
|
|
int filesrc_read(IsoFileSrc *file, char *buf, size_t count)
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* loop to ensure the full buffer is filled */
|
|
|
|
do {
|
|
|
|
ssize_t result;
|
|
|
|
result = iso_stream_read(file->stream, buf + bytes, count - bytes);
|
2007-12-28 21:10:17 +00:00
|
|
|
if (result < 0) {
|
|
|
|
/* fill buffer with 0s and return */
|
2007-12-20 21:17:18 +00:00
|
|
|
memset(buf + bytes, 0, count - bytes);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (result == 0)
|
|
|
|
break;
|
2007-12-28 21:10:17 +00:00
|
|
|
bytes += result;
|
2007-12-20 21:17:18 +00:00
|
|
|
} while (bytes < count);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
if (bytes < count) {
|
|
|
|
/* eof */
|
|
|
|
memset(buf + bytes, 0, count - bytes);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int filesrc_writer_write_data(IsoImageWriter *writer)
|
|
|
|
{
|
|
|
|
int res;
|
2008-01-04 22:54:31 +00:00
|
|
|
size_t i, b;
|
2007-12-20 21:17:18 +00:00
|
|
|
Ecma119Image *t;
|
2008-01-04 22:54:31 +00:00
|
|
|
IsoFileSrc *file;
|
2007-12-20 21:17:18 +00:00
|
|
|
IsoFileSrc **filelist;
|
|
|
|
char buffer[BLOCK_SIZE];
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
t = writer->target;
|
|
|
|
filelist = writer->data;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-30 21:04:41 +00:00
|
|
|
iso_msg_debug(t->image->messenger, "Writing Files...");
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-04 22:54:31 +00:00
|
|
|
i = 0;
|
|
|
|
while ((file = filelist[i++]) != NULL) {
|
2007-12-20 21:17:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO WARNING
|
|
|
|
* when we allow files greater than 4GB, current div_up implementation
|
|
|
|
* can overflow!!
|
|
|
|
*/
|
|
|
|
uint32_t nblocks = div_up(iso_file_src_get_size(file), BLOCK_SIZE);
|
|
|
|
|
|
|
|
res = filesrc_open(file);
|
|
|
|
if (res < 0) {
|
|
|
|
/*
|
|
|
|
* UPS, very ugly error, the best we can do is just to write
|
|
|
|
* 0's to image
|
|
|
|
*/
|
2007-12-29 00:58:42 +00:00
|
|
|
char *name = iso_stream_get_name(file->stream);
|
2007-12-30 21:04:41 +00:00
|
|
|
iso_msg_sorry(t->image->messenger, LIBISO_FILE_CANT_WRITE,
|
|
|
|
"File \"%s\" can't be opened. Filling with 0s.", name);
|
2007-12-29 00:58:42 +00:00
|
|
|
free(name);
|
2007-12-28 21:10:17 +00:00
|
|
|
memset(buffer, 0, BLOCK_SIZE);
|
|
|
|
for (b = 0; b < nblocks; ++b) {
|
2007-12-20 21:17:18 +00:00
|
|
|
res = iso_write(t, buffer, BLOCK_SIZE);
|
|
|
|
if (res < 0) {
|
|
|
|
/* ko, writer error, we need to go out! */
|
|
|
|
return res;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
}
|
|
|
|
continue;
|
2007-12-20 21:17:18 +00:00
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* write file contents to image */
|
|
|
|
for (b = 0; b < nblocks; ++b) {
|
|
|
|
int wres;
|
|
|
|
res = filesrc_read(file, buffer, BLOCK_SIZE);
|
|
|
|
wres = iso_write(t, buffer, BLOCK_SIZE);
|
|
|
|
if (wres < 0) {
|
|
|
|
/* ko, writer error, we need to go out! */
|
|
|
|
return wres;
|
|
|
|
}
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
if (b < nblocks) {
|
|
|
|
/* premature end of file, due to error or eof */
|
2007-12-29 00:58:42 +00:00
|
|
|
char *name = iso_stream_get_name(file->stream);
|
2007-12-20 21:17:18 +00:00
|
|
|
if (res < 0) {
|
|
|
|
/* error */
|
2007-12-30 21:04:41 +00:00
|
|
|
iso_msg_sorry(t->image->messenger, LIBISO_FILE_CANT_WRITE,
|
2007-12-29 00:58:42 +00:00
|
|
|
"Read error in file %s.", name);
|
2007-12-20 21:17:18 +00:00
|
|
|
} else {
|
|
|
|
/* eof */
|
2007-12-30 21:04:41 +00:00
|
|
|
iso_msg_sorry(t->image->messenger, LIBISO_FILE_CANT_WRITE,
|
2007-12-29 00:58:42 +00:00
|
|
|
"Premature end of file %s.", name);
|
2007-12-20 21:17:18 +00:00
|
|
|
}
|
2007-12-29 00:58:42 +00:00
|
|
|
free(name);
|
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* fill with 0s */
|
2007-12-30 21:04:41 +00:00
|
|
|
iso_msg_sorry(t->image->messenger, LIBISO_FILE_CANT_WRITE,
|
|
|
|
"Filling with 0");
|
2007-12-20 21:17:18 +00:00
|
|
|
memset(buffer, 0, BLOCK_SIZE);
|
|
|
|
while (b++ < nblocks) {
|
|
|
|
res = iso_write(t, buffer, BLOCK_SIZE);
|
|
|
|
if (res < 0) {
|
|
|
|
/* ko, writer error, we need to go out! */
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
filesrc_close(file);
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int filesrc_writer_free_data(IsoImageWriter *writer)
|
|
|
|
{
|
|
|
|
/* free the list of files (contents are free together with the tree) */
|
|
|
|
free(writer->data);
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int iso_file_src_writer_create(Ecma119Image *target)
|
|
|
|
{
|
|
|
|
IsoImageWriter *writer;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
writer = malloc(sizeof(IsoImageWriter));
|
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
writer->compute_data_blocks = filesrc_writer_compute_data_blocks;
|
|
|
|
writer->write_vol_desc = filesrc_writer_write_vol_desc;
|
|
|
|
writer->write_data = filesrc_writer_write_data;
|
|
|
|
writer->free_data = filesrc_writer_free_data;
|
|
|
|
writer->data = NULL;
|
|
|
|
writer->target = target;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* add this writer to image */
|
|
|
|
target->writers[target->nwriters++] = writer;
|
|
|
|
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|