2007-12-15 15:55:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Vreixo Formoso
|
2007-12-20 23:05:41 +00:00
|
|
|
* Copyright (c) 2007 Mario Danic
|
2007-12-15 15:55:44 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-12-16 18:10:47 +00:00
|
|
|
#include "libisofs.h"
|
2007-12-15 15:55:44 +00:00
|
|
|
#include "ecma119.h"
|
2007-12-16 18:10:47 +00:00
|
|
|
#include "ecma119_tree.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "filesrc.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "writer.h"
|
2007-12-20 23:35:43 +00:00
|
|
|
#include "messages.h"
|
2007-12-24 13:24:09 +00:00
|
|
|
#include "rockridge.h"
|
2007-12-17 23:20:03 +00:00
|
|
|
#include "util.h"
|
2007-12-16 18:10:47 +00:00
|
|
|
|
|
|
|
#include "libburn/libburn.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-12-17 20:12:51 +00:00
|
|
|
#include <time.h>
|
2007-12-17 23:20:03 +00:00
|
|
|
#include <string.h>
|
2007-12-16 18:10:47 +00:00
|
|
|
|
2007-12-17 22:22:19 +00:00
|
|
|
static
|
|
|
|
void ecma119_image_free(Ecma119Image *t)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ecma119_node_free(t->root);
|
|
|
|
iso_image_unref(t->image);
|
2007-12-19 23:25:25 +00:00
|
|
|
iso_rbtree_destroy(t->files, iso_file_src_free);
|
2007-12-23 14:39:41 +00:00
|
|
|
iso_ring_buffer_free(t->buffer);
|
2007-12-17 22:22:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < t->nwriters; ++i) {
|
|
|
|
IsoImageWriter *writer = t->writers[i];
|
|
|
|
writer->free_data(writer);
|
|
|
|
free(writer);
|
|
|
|
}
|
2007-12-18 20:55:01 +00:00
|
|
|
free(t->input_charset);
|
2007-12-17 22:22:19 +00:00
|
|
|
free(t->writers);
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
|
2007-12-25 17:59:20 +00:00
|
|
|
/**
|
|
|
|
* Check if we should add version number ";" to the given node name.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int need_version_number(Ecma119Image *t, Ecma119Node *n)
|
|
|
|
{
|
|
|
|
if (t->omit_version_numbers) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (n->type == ECMA119_DIR || n->type == ECMA119_PLACEHOLDER) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 23:20:03 +00:00
|
|
|
/**
|
|
|
|
* Compute the size of a directory entry for a single node
|
|
|
|
*/
|
|
|
|
static
|
2007-12-21 22:08:21 +00:00
|
|
|
size_t calc_dirent_len(Ecma119Image *t, Ecma119Node *n)
|
2007-12-17 23:20:03 +00:00
|
|
|
{
|
|
|
|
int ret = n->iso_name ? strlen(n->iso_name) + 33 : 34;
|
2007-12-25 17:59:20 +00:00
|
|
|
if (need_version_number(t, n)) {
|
2007-12-21 22:08:21 +00:00
|
|
|
ret += 2; /* take into account version numbers */
|
|
|
|
}
|
2007-12-17 23:20:03 +00:00
|
|
|
if (ret % 2) ret++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the total size of all directory entries of a single dir,
|
|
|
|
* acording to ECMA-119 6.8.1.1
|
2007-12-24 13:24:09 +00:00
|
|
|
*
|
|
|
|
* This also take into account the size needed for RR entries and
|
|
|
|
* SUSP continuation areas (SUSP, 5.1).
|
|
|
|
*
|
|
|
|
* @param ce
|
|
|
|
* Will be filled with the size needed for Continuation Areas
|
|
|
|
* @return
|
|
|
|
* The size needed for all dir entries of the given dir, without
|
|
|
|
* taking into account the continuation areas.
|
2007-12-17 23:20:03 +00:00
|
|
|
*/
|
|
|
|
static
|
2007-12-24 13:24:09 +00:00
|
|
|
size_t calc_dir_size(Ecma119Image *t, Ecma119Node *dir, size_t *ce)
|
2007-12-17 23:20:03 +00:00
|
|
|
{
|
|
|
|
size_t i, len;
|
2007-12-24 13:24:09 +00:00
|
|
|
size_t ce_len = 0;
|
2007-12-17 23:20:03 +00:00
|
|
|
|
|
|
|
/* size of "." and ".." entries */
|
2007-12-24 15:29:57 +00:00
|
|
|
len = 34 + 34;
|
2007-12-24 13:24:09 +00:00
|
|
|
if (t->rockridge) {
|
|
|
|
len += rrip_calc_len(t, dir, 1, 255 - 34, &ce_len);
|
|
|
|
*ce += ce_len;
|
|
|
|
len += rrip_calc_len(t, dir, 2, 255 - 34, &ce_len);
|
|
|
|
*ce += ce_len;
|
|
|
|
}
|
|
|
|
|
2007-12-17 23:20:03 +00:00
|
|
|
for (i = 0; i < dir->info.dir.nchildren; ++i) {
|
2007-12-24 13:24:09 +00:00
|
|
|
size_t remaining;
|
2007-12-17 23:20:03 +00:00
|
|
|
Ecma119Node *child = dir->info.dir.children[i];
|
2007-12-21 22:08:21 +00:00
|
|
|
size_t dirent_len = calc_dirent_len(t, child);
|
2007-12-24 13:24:09 +00:00
|
|
|
if (t->rockridge) {
|
|
|
|
dirent_len += rrip_calc_len(t, child, 0, 255 - dirent_len,
|
|
|
|
&ce_len);
|
|
|
|
*ce += ce_len;
|
|
|
|
}
|
|
|
|
remaining = BLOCK_SIZE - (len % BLOCK_SIZE);
|
2007-12-17 23:20:03 +00:00
|
|
|
if (dirent_len > remaining) {
|
|
|
|
/* child directory entry doesn't fit on block */
|
|
|
|
len += remaining + dirent_len;
|
|
|
|
} else {
|
|
|
|
len += dirent_len;
|
|
|
|
}
|
|
|
|
}
|
2007-12-24 13:24:09 +00:00
|
|
|
/* cache the len */
|
|
|
|
dir->info.dir.len = len;
|
2007-12-17 23:20:03 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void calc_dir_pos(Ecma119Image *t, Ecma119Node *dir)
|
|
|
|
{
|
|
|
|
size_t i, len;
|
2007-12-24 13:24:09 +00:00
|
|
|
size_t ce_len = 0;
|
2007-12-17 23:20:03 +00:00
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
t->ndirs++;
|
2007-12-17 23:20:03 +00:00
|
|
|
dir->info.dir.block = t->curblock;
|
2007-12-24 13:24:09 +00:00
|
|
|
len = calc_dir_size(t, dir, &ce_len);
|
2007-12-17 23:20:03 +00:00
|
|
|
t->curblock += div_up(len, BLOCK_SIZE);
|
2007-12-24 13:24:09 +00:00
|
|
|
if (t->rockridge) {
|
|
|
|
t->curblock += div_up(ce_len, BLOCK_SIZE);
|
|
|
|
}
|
2007-12-17 23:20:03 +00:00
|
|
|
for (i = 0; i < dir->info.dir.nchildren; i++) {
|
|
|
|
Ecma119Node *child = dir->info.dir.children[i];
|
|
|
|
if (child->type == ECMA119_DIR) {
|
|
|
|
calc_dir_pos(t, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-22 15:02:29 +00:00
|
|
|
/**
|
|
|
|
* Compute the length of the path table, in bytes.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
uint32_t calc_path_table_size(Ecma119Node *dir)
|
|
|
|
{
|
|
|
|
uint32_t size;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* size of path table for this entry */
|
|
|
|
size = 8;
|
|
|
|
size += dir->iso_name ? strlen(dir->iso_name) : 1;
|
2007-12-25 22:33:37 +00:00
|
|
|
size += (size % 2);
|
2007-12-22 15:02:29 +00:00
|
|
|
|
|
|
|
/* and recurse */
|
|
|
|
for (i = 0; i < dir->info.dir.nchildren; i++) {
|
|
|
|
Ecma119Node *child = dir->info.dir.children[i];
|
|
|
|
if (child->type == ECMA119_DIR) {
|
|
|
|
size += calc_path_table_size(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2007-12-17 22:22:19 +00:00
|
|
|
static
|
|
|
|
int ecma119_writer_compute_data_blocks(IsoImageWriter *writer)
|
|
|
|
{
|
2007-12-17 23:20:03 +00:00
|
|
|
Ecma119Image *target;
|
|
|
|
uint32_t path_table_size;
|
|
|
|
|
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = writer->target;
|
|
|
|
|
|
|
|
/* compute position of directories */
|
2007-12-22 19:49:30 +00:00
|
|
|
iso_msg_debug(target->image, "Computing position of dir structure");
|
2007-12-17 23:20:03 +00:00
|
|
|
target->ndirs = 0;
|
|
|
|
calc_dir_pos(target, target->root);
|
|
|
|
|
|
|
|
/* compute length of pathlist */
|
2007-12-22 19:49:30 +00:00
|
|
|
iso_msg_debug(target->image, "Computing length of pathlist");
|
2007-12-22 15:02:29 +00:00
|
|
|
path_table_size = calc_path_table_size(target->root);
|
2007-12-17 23:20:03 +00:00
|
|
|
|
|
|
|
/* compute location for path tables */
|
|
|
|
target->l_path_table_pos = target->curblock;
|
|
|
|
target->curblock += div_up(path_table_size, BLOCK_SIZE);
|
|
|
|
target->m_path_table_pos = target->curblock;
|
|
|
|
target->curblock += div_up(path_table_size, BLOCK_SIZE);
|
2007-12-18 20:55:01 +00:00
|
|
|
target->path_table_size = path_table_size;
|
2007-12-17 23:20:03 +00:00
|
|
|
|
|
|
|
return ISO_SUCCESS;
|
2007-12-17 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
2007-12-18 20:55:01 +00:00
|
|
|
/**
|
2007-12-20 23:05:41 +00:00
|
|
|
* Write a single directory record (ECMA-119, 9.1)
|
|
|
|
*
|
|
|
|
* @param file_id
|
2007-12-24 15:29:57 +00:00
|
|
|
* if >= 0, we use it instead of the filename (for "." and ".." entries).
|
2007-12-20 23:05:41 +00:00
|
|
|
* @param len_fi
|
|
|
|
* Computed length of the file identifier. Total size of the directory
|
2007-12-24 15:29:57 +00:00
|
|
|
* entry will be len + 33 + padding if needed (ECMA-119, 9.1.12)
|
|
|
|
* @param info
|
|
|
|
* SUSP entries for the given directory record. It will be NULL for the
|
|
|
|
* root directory record in the PVD (ECMA-119, 8.4.18) (in order to
|
|
|
|
* distinguish it from the "." entry in the root directory)
|
2007-12-20 23:05:41 +00:00
|
|
|
*/
|
|
|
|
static
|
|
|
|
void write_one_dir_record(Ecma119Image *t, Ecma119Node *node, int file_id,
|
2007-12-24 15:29:57 +00:00
|
|
|
uint8_t *buf, size_t len_fi, struct susp_info *info)
|
2007-12-20 23:05:41 +00:00
|
|
|
{
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t block;
|
2007-12-24 15:29:57 +00:00
|
|
|
uint8_t len_dr; /*< size of dir entry without SUSP fields */
|
|
|
|
uint8_t *name = (file_id >= 0) ? (uint8_t*)&file_id :
|
|
|
|
(uint8_t*)node->iso_name;
|
2007-12-20 23:05:41 +00:00
|
|
|
|
|
|
|
struct ecma119_dir_record *rec = (struct ecma119_dir_record*)buf;
|
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
len_dr = 33 + len_fi + (len_fi % 2 ? 0 : 1);
|
2007-12-20 23:05:41 +00:00
|
|
|
|
2007-12-21 22:08:21 +00:00
|
|
|
memcpy(rec->file_id, name, len_fi);
|
|
|
|
|
2007-12-25 17:59:20 +00:00
|
|
|
if (need_version_number(t, node)) {
|
2007-12-21 22:08:21 +00:00
|
|
|
len_dr += 2;
|
|
|
|
rec->file_id[len_fi++] = ';';
|
|
|
|
rec->file_id[len_fi++] = '1';
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:05:41 +00:00
|
|
|
if (node->type == ECMA119_DIR) {
|
2007-12-24 13:24:09 +00:00
|
|
|
/* use the cached length */
|
|
|
|
len = node->info.dir.len;
|
2007-12-20 23:05:41 +00:00
|
|
|
block = node->info.dir.block;
|
|
|
|
} else if (node->type == ECMA119_FILE) {
|
|
|
|
len = iso_file_src_get_size(node->info.file);
|
|
|
|
block = node->info.file->block;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* for nodes other than files and dirs, we set both
|
|
|
|
* len and block to 0
|
|
|
|
*/
|
|
|
|
len = 0;
|
|
|
|
block = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For ".." entry we need to write the parent info!
|
|
|
|
*/
|
|
|
|
if (file_id == 1 && node->parent)
|
|
|
|
node = node->parent;
|
|
|
|
|
2007-12-24 15:29:57 +00:00
|
|
|
rec->len_dr[0] = len_dr + (info != NULL ? info->suf_len : 0);
|
2007-12-20 23:05:41 +00:00
|
|
|
iso_bb(rec->block, block, 4);
|
|
|
|
iso_bb(rec->length, len, 4);
|
|
|
|
iso_datetime_7(rec->recording_time, t->now);
|
|
|
|
rec->flags[0] = (node->type == ECMA119_DIR) ? 2 : 0;
|
|
|
|
iso_bb(rec->vol_seq_number, 1, 2);
|
|
|
|
rec->len_fi[0] = len_fi;
|
2007-12-24 15:29:57 +00:00
|
|
|
|
|
|
|
/* and finally write the SUSP fields */
|
|
|
|
if (info != NULL) {
|
|
|
|
rrip_write_susp_fields(t, info, buf + len_dr);
|
|
|
|
}
|
2007-12-20 23:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the Primary Volume Descriptor (ECMA-119, 8.4)
|
2007-12-18 20:55:01 +00:00
|
|
|
*/
|
2007-12-17 22:22:19 +00:00
|
|
|
static
|
|
|
|
int ecma119_writer_write_vol_desc(IsoImageWriter *writer)
|
|
|
|
{
|
2007-12-18 20:55:01 +00:00
|
|
|
IsoImage *image;
|
|
|
|
Ecma119Image *t;
|
|
|
|
struct ecma119_pri_vol_desc vol;
|
|
|
|
|
|
|
|
char *vol_id, *pub_id, *data_id, *volset_id;
|
|
|
|
char *system_id, *application_id, *copyright_file_id;
|
|
|
|
char *abstract_file_id, *biblio_file_id;
|
|
|
|
|
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = writer->target;
|
|
|
|
image = t->image;
|
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
iso_msg_debug(image, "Write Primary Volume Descriptor");
|
2007-12-18 20:55:01 +00:00
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
memset(&vol, 0, sizeof(struct ecma119_pri_vol_desc));
|
|
|
|
|
|
|
|
str2d_char(t->input_charset, image->volume_id, &vol_id);
|
|
|
|
str2a_char(t->input_charset, image->publisher_id, &pub_id);
|
|
|
|
str2a_char(t->input_charset, image->data_preparer_id, &data_id);
|
|
|
|
str2d_char(t->input_charset, image->volset_id, &volset_id);
|
2007-12-18 20:55:01 +00:00
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
str2a_char(t->input_charset, image->system_id, &system_id);
|
|
|
|
str2a_char(t->input_charset, image->application_id, &application_id);
|
|
|
|
str2d_char(t->input_charset, image->copyright_file_id, ©right_file_id);
|
|
|
|
str2d_char(t->input_charset, image->abstract_file_id, &abstract_file_id);
|
|
|
|
str2d_char(t->input_charset, image->biblio_file_id, &biblio_file_id);
|
2007-12-18 20:55:01 +00:00
|
|
|
|
|
|
|
vol.vol_desc_type[0] = 1;
|
|
|
|
memcpy(vol.std_identifier, "CD001", 5);
|
|
|
|
vol.vol_desc_version[0] = 1;
|
2007-12-25 17:59:20 +00:00
|
|
|
if (system_id) {
|
2007-12-18 20:55:01 +00:00
|
|
|
strncpy((char*)vol.system_id, system_id, 32);
|
2007-12-25 17:59:20 +00:00
|
|
|
} else {
|
2007-12-18 20:55:01 +00:00
|
|
|
/* put linux by default? */
|
2007-12-25 17:59:20 +00:00
|
|
|
memcpy(vol.system_id, "LINUX", 5);
|
|
|
|
}
|
|
|
|
if (vol_id) {
|
2007-12-18 20:55:01 +00:00
|
|
|
strncpy((char*)vol.volume_id, vol_id, 32);
|
2007-12-25 17:59:20 +00:00
|
|
|
}
|
2007-12-18 20:55:01 +00:00
|
|
|
iso_bb(vol.vol_space_size, t->vol_space_size, 4);
|
|
|
|
iso_bb(vol.vol_set_size, 1, 2);
|
|
|
|
iso_bb(vol.vol_seq_number, 1, 2);
|
|
|
|
iso_bb(vol.block_size, BLOCK_SIZE, 2);
|
|
|
|
iso_bb(vol.path_table_size, t->path_table_size, 4);
|
|
|
|
iso_lsb(vol.l_path_table_pos, t->l_path_table_pos, 4);
|
|
|
|
iso_msb(vol.m_path_table_pos, t->m_path_table_pos, 4);
|
|
|
|
|
2007-12-24 15:29:57 +00:00
|
|
|
write_one_dir_record(t, t->root, 0, vol.root_dir_record, 1, NULL);
|
2007-12-18 20:55:01 +00:00
|
|
|
|
|
|
|
if (volset_id)
|
|
|
|
strncpy((char*)vol.vol_set_id, volset_id, 128);
|
|
|
|
if (pub_id)
|
|
|
|
strncpy((char*)vol.publisher_id, pub_id, 128);
|
|
|
|
if (data_id)
|
|
|
|
strncpy((char*)vol.data_prep_id, data_id, 128);
|
|
|
|
|
|
|
|
if (application_id)
|
|
|
|
strncpy((char*)vol.application_id, application_id, 128);
|
|
|
|
if (copyright_file_id)
|
|
|
|
strncpy((char*)vol.copyright_file_id, copyright_file_id, 37);
|
|
|
|
if (abstract_file_id)
|
|
|
|
strncpy((char*)vol.abstract_file_id, abstract_file_id, 37);
|
|
|
|
if (biblio_file_id)
|
|
|
|
strncpy((char*)vol.bibliographic_file_id, biblio_file_id, 37);
|
|
|
|
|
|
|
|
iso_datetime_17(vol.vol_creation_time, t->now);
|
|
|
|
iso_datetime_17(vol.vol_modification_time, t->now);
|
|
|
|
iso_datetime_17(vol.vol_effective_time, t->now);
|
|
|
|
vol.file_structure_version[0] = 1;
|
|
|
|
|
|
|
|
free(vol_id);
|
|
|
|
free(volset_id);
|
|
|
|
free(pub_id);
|
|
|
|
free(data_id);
|
|
|
|
free(system_id);
|
|
|
|
free(application_id);
|
|
|
|
free(copyright_file_id);
|
|
|
|
free(abstract_file_id);
|
|
|
|
free(biblio_file_id);
|
|
|
|
|
|
|
|
/* Finally write the Volume Descriptor */
|
|
|
|
return iso_write(t, &vol, sizeof(struct ecma119_pri_vol_desc));
|
2007-12-17 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
2007-12-20 23:35:43 +00:00
|
|
|
static
|
|
|
|
int write_one_dir(Ecma119Image *t, Ecma119Node *dir)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t buffer[BLOCK_SIZE];
|
|
|
|
size_t i;
|
|
|
|
size_t fi_len, len;
|
2007-12-24 15:29:57 +00:00
|
|
|
struct susp_info info;
|
2007-12-20 23:35:43 +00:00
|
|
|
|
|
|
|
/* buf will point to current write position on buffer */
|
|
|
|
uint8_t *buf = buffer;
|
|
|
|
|
|
|
|
/* initialize buffer with 0s */
|
|
|
|
memset(buffer, 0, BLOCK_SIZE);
|
2007-12-24 15:29:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set susp_info to 0's, this way code for both plain ECMA-119 and
|
|
|
|
* RR is very similar
|
|
|
|
*/
|
|
|
|
memset(&info, 0, sizeof(struct susp_info));
|
|
|
|
if (t->rockridge) {
|
|
|
|
/* initialize the ce_block, it might be needed */
|
2007-12-25 17:35:10 +00:00
|
|
|
info.ce_block = dir->info.dir.block + div_up(dir->info.dir.len,
|
|
|
|
BLOCK_SIZE);
|
2007-12-24 15:29:57 +00:00
|
|
|
}
|
2007-12-20 23:35:43 +00:00
|
|
|
|
|
|
|
/* write the "." and ".." entries first */
|
2007-12-24 15:29:57 +00:00
|
|
|
if (t->rockridge) {
|
|
|
|
ret = rrip_get_susp_fields(t, dir, 1, 255 - 32, &info);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 17:35:10 +00:00
|
|
|
len = 34 + info.suf_len;
|
2007-12-24 15:29:57 +00:00
|
|
|
write_one_dir_record(t, dir, 0, buf, 1, &info);
|
2007-12-25 17:35:10 +00:00
|
|
|
buf += len;
|
2007-12-20 23:35:43 +00:00
|
|
|
|
2007-12-24 15:29:57 +00:00
|
|
|
if (t->rockridge) {
|
|
|
|
ret = rrip_get_susp_fields(t, dir, 2, 255 - 32, &info);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 17:35:10 +00:00
|
|
|
len = 34 + info.suf_len;
|
2007-12-24 15:29:57 +00:00
|
|
|
write_one_dir_record(t, dir, 1, buf, 1, &info);
|
2007-12-25 17:35:10 +00:00
|
|
|
buf += len;
|
2007-12-20 23:35:43 +00:00
|
|
|
|
|
|
|
for (i = 0; i < dir->info.dir.nchildren; i++) {
|
|
|
|
Ecma119Node *child = dir->info.dir.children[i];
|
|
|
|
|
|
|
|
/* compute len of directory entry */
|
|
|
|
fi_len = strlen(child->iso_name);
|
2007-12-22 13:45:00 +00:00
|
|
|
len = fi_len + 33 + (fi_len % 2 ? 0 : 1);
|
2007-12-26 17:19:00 +00:00
|
|
|
if (need_version_number(t, child)) {
|
2007-12-21 22:08:21 +00:00
|
|
|
len += 2;
|
|
|
|
}
|
2007-12-24 15:29:57 +00:00
|
|
|
|
|
|
|
/* get the SUSP fields if rockridge is enabled */
|
|
|
|
if (t->rockridge) {
|
|
|
|
ret = rrip_get_susp_fields(t, child, 0, 255 - len, &info);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
len += info.suf_len;
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:35:43 +00:00
|
|
|
if ( (buf + len - buffer) > BLOCK_SIZE ) {
|
|
|
|
/* dir doesn't fit in current block */
|
|
|
|
ret = iso_write(t, buffer, BLOCK_SIZE);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
memset(buffer, 0, BLOCK_SIZE);
|
|
|
|
buf = buffer;
|
|
|
|
}
|
|
|
|
/* write the directory entry in any case */
|
2007-12-24 15:29:57 +00:00
|
|
|
write_one_dir_record(t, child, -1, buf, fi_len, &info);
|
2007-12-20 23:35:43 +00:00
|
|
|
buf += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the last block */
|
|
|
|
ret = iso_write(t, buffer, BLOCK_SIZE);
|
2007-12-24 15:29:57 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the Continuation Area if needed */
|
|
|
|
if (info.ce_len > 0) {
|
|
|
|
ret = rrip_write_ce_fields(t, &info);
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:35:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int write_dirs(Ecma119Image *t, Ecma119Node *root)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* write all directory entries for this dir */
|
|
|
|
ret = write_one_dir(t, root);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recurse */
|
|
|
|
for (i = 0; i < root->info.dir.nchildren; i++) {
|
|
|
|
Ecma119Node *child = root->info.dir.children[i];
|
|
|
|
if (child->type == ECMA119_DIR) {
|
|
|
|
ret = write_dirs(t, child);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-21 21:53:48 +00:00
|
|
|
static
|
|
|
|
int write_path_table(Ecma119Image *t, Ecma119Node **pathlist, int l_type)
|
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
uint8_t buf[64]; /* 64 is just a convenient size larger enought */
|
|
|
|
struct ecma119_path_table_record *rec;
|
|
|
|
void (*write_int)(uint8_t*, uint32_t, int);
|
|
|
|
Ecma119Node *dir;
|
|
|
|
uint32_t path_table_size;
|
|
|
|
int parent = 0;
|
|
|
|
int ret = ISO_SUCCESS;
|
|
|
|
|
|
|
|
path_table_size = 0;
|
|
|
|
write_int = l_type ? iso_lsb : iso_msb;
|
|
|
|
|
|
|
|
for (i = 0; i < t->ndirs; i++) {
|
|
|
|
dir = pathlist[i];
|
|
|
|
|
|
|
|
/* find the index of the parent in the table */
|
|
|
|
while ((i) && pathlist[parent] != dir->parent) {
|
|
|
|
parent++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the Path Table Record (ECMA-119, 9.4) */
|
|
|
|
memset(buf, 0, 64);
|
|
|
|
rec = (struct ecma119_path_table_record*) buf;
|
|
|
|
rec->len_di[0] = dir->parent ? (uint8_t) strlen(dir->iso_name) : 1;
|
|
|
|
rec->len_xa[0] = 0;
|
|
|
|
write_int(rec->block, dir->info.dir.block, 4);
|
|
|
|
write_int(rec->parent, parent + 1, 2);
|
|
|
|
if (dir->parent) {
|
|
|
|
memcpy(rec->dir_id, dir->iso_name, rec->len_di[0]);
|
|
|
|
}
|
|
|
|
len = 8 + rec->len_di[0] + (rec->len_di[0] % 2);
|
|
|
|
ret = iso_write(t, buf, len);
|
|
|
|
if (ret < 0) {
|
|
|
|
/* error */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
path_table_size += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we need to fill the last block with zeros */
|
|
|
|
path_table_size %= BLOCK_SIZE;
|
|
|
|
if (path_table_size) {
|
|
|
|
uint8_t zeros[BLOCK_SIZE];
|
|
|
|
len = BLOCK_SIZE - path_table_size;
|
|
|
|
memset(zeros, 0, len);
|
|
|
|
ret = iso_write(t, zeros, len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int write_path_tables(Ecma119Image *t)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i, j, cur;
|
|
|
|
Ecma119Node **pathlist;
|
|
|
|
|
|
|
|
iso_msg_debug(t->image, "Writing ISO Path tables");
|
|
|
|
|
|
|
|
/* allocate temporal pathlist */
|
|
|
|
pathlist = malloc(sizeof(void*) * t->ndirs);
|
|
|
|
if (pathlist == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
pathlist[0] = t->root;
|
|
|
|
cur = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < t->ndirs; i++) {
|
|
|
|
Ecma119Node *dir = pathlist[i];
|
|
|
|
for (j = 0; j < dir->info.dir.nchildren; j++) {
|
|
|
|
Ecma119Node *child = dir->info.dir.children[j];
|
|
|
|
if (child->type == ECMA119_DIR) {
|
|
|
|
pathlist[cur++] = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write L Path Table */
|
|
|
|
ret = write_path_table(t, pathlist, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto write_path_tables_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write L Path Table */
|
|
|
|
ret = write_path_table(t, pathlist, 0);
|
|
|
|
|
|
|
|
write_path_tables_exit:;
|
|
|
|
free(pathlist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:35:43 +00:00
|
|
|
/**
|
|
|
|
* Write both the directory structure (ECMA-119, 6.8) and the L and M
|
|
|
|
* Path Tables (ECMA-119, 6.9).
|
|
|
|
*/
|
2007-12-17 22:22:19 +00:00
|
|
|
static
|
|
|
|
int ecma119_writer_write_data(IsoImageWriter *writer)
|
|
|
|
{
|
2007-12-20 23:35:43 +00:00
|
|
|
int ret;
|
|
|
|
Ecma119Image *t;
|
2007-12-20 23:05:41 +00:00
|
|
|
|
2007-12-20 23:35:43 +00:00
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
t = writer->target;
|
|
|
|
|
|
|
|
/* first of all, we write the directory structure */
|
|
|
|
ret = write_dirs(t, t->root);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2007-12-20 23:05:41 +00:00
|
|
|
|
2007-12-21 21:53:48 +00:00
|
|
|
/* and write the path tables */
|
|
|
|
ret = write_path_tables(t);
|
|
|
|
|
|
|
|
return ret;
|
2007-12-17 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int ecma119_writer_free_data(IsoImageWriter *writer)
|
|
|
|
{
|
2007-12-21 21:53:48 +00:00
|
|
|
/* nothing to do */
|
|
|
|
return ISO_SUCCESS;
|
2007-12-17 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ecma119_writer_create(Ecma119Image *target)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
IsoImageWriter *writer;
|
|
|
|
|
|
|
|
writer = malloc(sizeof(IsoImageWriter));
|
|
|
|
if (writer == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer->compute_data_blocks = ecma119_writer_compute_data_blocks;
|
|
|
|
writer->write_vol_desc = ecma119_writer_write_vol_desc;
|
|
|
|
writer->write_data = ecma119_writer_write_data;
|
|
|
|
writer->free_data = ecma119_writer_free_data;
|
|
|
|
writer->data = NULL;
|
|
|
|
writer->target = target;
|
|
|
|
|
|
|
|
/* add this writer to image */
|
|
|
|
target->writers[target->nwriters++] = writer;
|
|
|
|
|
2007-12-22 19:49:30 +00:00
|
|
|
iso_msg_debug(target->image, "Creating low level ECMA-119 tree...");
|
2007-12-17 22:22:19 +00:00
|
|
|
ret = ecma119_tree_create(target);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we need the volume descriptor */
|
|
|
|
target->curblock++;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-22 13:45:00 +00:00
|
|
|
static
|
2007-12-22 16:09:28 +00:00
|
|
|
void *write_function(void *arg)
|
2007-12-22 13:45:00 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
size_t i;
|
|
|
|
uint8_t buf[BLOCK_SIZE];
|
|
|
|
IsoImageWriter *writer;
|
|
|
|
|
2007-12-22 16:09:28 +00:00
|
|
|
Ecma119Image *target = (Ecma119Image*)arg;
|
2007-12-22 13:45:00 +00:00
|
|
|
iso_msg_debug(target->image, "Starting image writing...");
|
|
|
|
|
|
|
|
/* Write System Area, 16 blocks of zeros (ECMA-119, 6.2.1) */
|
|
|
|
memset(buf, 0, BLOCK_SIZE);
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
res = iso_write(target, buf, BLOCK_SIZE);
|
|
|
|
if (res < 0) {
|
|
|
|
goto write_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write volume descriptors, one per writer */
|
|
|
|
iso_msg_debug(target->image, "Write volume descriptors");
|
|
|
|
for (i = 0; i < target->nwriters; ++i) {
|
|
|
|
writer = target->writers[i];
|
|
|
|
res = writer->write_vol_desc(writer);
|
|
|
|
if (res < 0) {
|
|
|
|
goto write_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write Volume Descriptor Set Terminator (ECMA-119, 8.3) */
|
|
|
|
{
|
|
|
|
struct ecma119_vol_desc_terminator *vol;
|
|
|
|
vol = (struct ecma119_vol_desc_terminator *) buf;
|
|
|
|
|
|
|
|
vol->vol_desc_type[0] = 255;
|
|
|
|
memcpy(vol->std_identifier, "CD001", 5);
|
|
|
|
vol->vol_desc_version[0] = 1;
|
|
|
|
|
|
|
|
res = iso_write(target, buf, BLOCK_SIZE);
|
|
|
|
if (res < 0) {
|
|
|
|
goto write_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write data for each writer */
|
|
|
|
for (i = 0; i < target->nwriters; ++i) {
|
|
|
|
writer = target->writers[i];
|
|
|
|
res = writer->write_data(writer);
|
|
|
|
if (res < 0) {
|
|
|
|
goto write_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 01:46:35 +00:00
|
|
|
iso_ring_buffer_writer_close(target->buffer);
|
2007-12-22 16:09:28 +00:00
|
|
|
pthread_exit(NULL);
|
2007-12-22 13:45:00 +00:00
|
|
|
|
|
|
|
write_error:;
|
|
|
|
iso_msg_fatal(target->image, LIBISO_WRITE_ERROR,
|
|
|
|
"Image write error, code %d", res);
|
2007-12-23 01:46:35 +00:00
|
|
|
iso_ring_buffer_writer_close(target->buffer);
|
2007-12-22 16:09:28 +00:00
|
|
|
pthread_exit(NULL);
|
2007-12-22 13:45:00 +00:00
|
|
|
}
|
|
|
|
|
2007-12-16 18:10:47 +00:00
|
|
|
static
|
|
|
|
int ecma119_image_new(IsoImage *src, Ecma119WriteOpts *opts,
|
|
|
|
Ecma119Image **img)
|
|
|
|
{
|
2007-12-17 22:22:19 +00:00
|
|
|
int ret, i;
|
2007-12-16 18:10:47 +00:00
|
|
|
Ecma119Image *target;
|
|
|
|
|
|
|
|
/* 1. Allocate target and copy opts there */
|
|
|
|
target = calloc(1, sizeof(Ecma119Image));
|
|
|
|
if (target == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-12-19 23:25:25 +00:00
|
|
|
/* create the tree for file caching */
|
|
|
|
ret = iso_rbtree_new(iso_file_src_cmp, &(target->files));
|
|
|
|
if (ret < 0) {
|
|
|
|
free(target);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-17 20:12:51 +00:00
|
|
|
target->image = src;
|
2007-12-17 22:22:19 +00:00
|
|
|
iso_image_ref(src);
|
|
|
|
|
2007-12-17 20:12:51 +00:00
|
|
|
target->iso_level = opts->level;
|
2007-12-27 09:43:06 +00:00
|
|
|
target->rockridge = opts->rockridge;
|
2007-12-25 17:51:00 +00:00
|
|
|
target->ino = 0;
|
2007-12-23 01:58:54 +00:00
|
|
|
target->omit_version_numbers = opts->omit_version_numbers;
|
2007-12-27 09:43:06 +00:00
|
|
|
target->allow_deep_paths = opts->allow_deep_paths;
|
2007-12-17 23:20:03 +00:00
|
|
|
target->sort_files = opts->sort_files;
|
2007-12-17 20:12:51 +00:00
|
|
|
|
|
|
|
target->now = time(NULL);
|
2007-12-18 20:55:01 +00:00
|
|
|
target->ms_block = 0;
|
|
|
|
target->input_charset = strdup("UTF-8"); //TODO
|
2007-12-17 20:12:51 +00:00
|
|
|
|
2007-12-16 18:10:47 +00:00
|
|
|
/*
|
|
|
|
* 2. Based on those options, create needed writers: iso, joliet...
|
|
|
|
* Each writer inits its structures and stores needed info into
|
|
|
|
* target.
|
|
|
|
* If the writer needs an volume descriptor, it increments image
|
|
|
|
* current block.
|
|
|
|
* Finally, create Writer for files.
|
|
|
|
*/
|
2007-12-18 20:55:01 +00:00
|
|
|
target->curblock = target->ms_block + 16;
|
2007-12-17 22:22:19 +00:00
|
|
|
|
|
|
|
/* the number of writers is dependent of the extensions */
|
|
|
|
target->writers = malloc(2 * sizeof(void*));
|
|
|
|
if (target->writers == NULL) {
|
|
|
|
iso_image_unref(src);
|
|
|
|
free(target);
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create writer for ECMA-119 structure */
|
|
|
|
ret = ecma119_writer_create(target);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto target_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Volume Descriptor Set Terminator */
|
|
|
|
target->curblock++;
|
|
|
|
|
2007-12-20 21:17:18 +00:00
|
|
|
/* create writer for file contents */
|
|
|
|
ret = iso_file_src_writer_create(target);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto target_cleanup;
|
|
|
|
}
|
2007-12-16 18:10:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 3.
|
|
|
|
* Call compute_data_blocks() in each Writer.
|
|
|
|
* That function computes the size needed by its structures and
|
|
|
|
* increments image current block propertly.
|
|
|
|
*/
|
2007-12-17 22:22:19 +00:00
|
|
|
for (i = 0; i < target->nwriters; ++i) {
|
|
|
|
IsoImageWriter *writer = target->writers[i];
|
|
|
|
ret = writer->compute_data_blocks(writer);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto target_cleanup;
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 18:10:47 +00:00
|
|
|
|
2007-12-18 20:55:01 +00:00
|
|
|
/*
|
|
|
|
* The volume space size is just the size of the last session, in
|
|
|
|
* case of ms images.
|
|
|
|
*/
|
|
|
|
target->total_size = (target->curblock - target->ms_block) * BLOCK_SIZE;
|
|
|
|
target->vol_space_size = target->curblock - target->ms_block;
|
|
|
|
|
2007-12-22 16:09:28 +00:00
|
|
|
/* 4. Create and start writting thread */
|
|
|
|
|
2007-12-23 01:46:35 +00:00
|
|
|
/* create the ring buffer */
|
|
|
|
ret = iso_ring_buffer_new(&target->buffer);
|
2007-12-22 16:09:28 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
goto target_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure the thread is created joinable */
|
|
|
|
pthread_attr_init(&(target->th_attr));
|
|
|
|
pthread_attr_setdetachstate(&(target->th_attr), PTHREAD_CREATE_JOINABLE);
|
|
|
|
|
2007-12-23 01:24:21 +00:00
|
|
|
ret = pthread_create(&(target->wthread), &(target->th_attr),
|
|
|
|
write_function, (void *) target);
|
2007-12-22 16:09:28 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
iso_msg_fatal(target->image, LIBISO_THREAD_ERROR,
|
|
|
|
"Cannot create writer thread");
|
|