#include "cd9660.h"
#include "iso9660_rrip.h"
#include <sys/cdefs.h>
#include <util.h>
static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
static int cd9660_write_path_tables(iso9660_disk *, FILE *);
static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *);
static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t,
const unsigned char *, int);
#if 0
static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
#endif
static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t);
int
cd9660_write_image(iso9660_disk *diskStructure, const char* image)
{
FILE *fd;
int status;
unsigned char buf[CD9660_SECTOR_SIZE];
if ((fd = fopen(image, "w+")) == NULL) {
err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
image);
}
if (diskStructure->verbose_level > 0)
printf("Writing image\n");
if (diskStructure->has_generic_bootimage) {
status = cd9660_copy_file(diskStructure, fd, 0,
diskStructure->generic_bootimage);
if (status == 0) {
warnx("%s: Error writing generic boot image",
__func__);
goto cleanup_bad_image;
}
}
status = cd9660_write_volume_descriptors(diskStructure, fd);
if (status == 0) {
warnx("%s: Error writing volume descriptors to image",
__func__);
goto cleanup_bad_image;
}
if (diskStructure->verbose_level > 0)
printf("Volume descriptors written\n");
status = cd9660_write_path_tables(diskStructure, fd);
if (status == 0) {
warnx("%s: Error writing path tables to image", __func__);
goto cleanup_bad_image;
}
if (diskStructure->verbose_level > 0)
printf("Path tables written\n");
status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode);
if (status == 0) {
warnx("%s: Error writing files to image", __func__);
goto cleanup_bad_image;
}
if (diskStructure->is_bootable) {
cd9660_write_boot(diskStructure, fd);
}
memset(buf, 0, CD9660_SECTOR_SIZE);
cd9660_write_filedata(diskStructure, fd,
diskStructure->totalSectors - 1, buf, 1);
if (diskStructure->verbose_level > 0)
printf("Files written\n");
fclose(fd);
if (diskStructure->verbose_level > 0)
printf("Image closed\n");
return 1;
cleanup_bad_image:
fclose(fd);
if (!diskStructure->keep_bad_images)
unlink(image);
if (diskStructure->verbose_level > 0)
printf("Bad image cleaned up\n");
return 0;
}
static int
cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd)
{
volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor;
while (vd_temp != NULL) {
cd9660_write_filedata(diskStructure, fd, vd_temp->sector,
vd_temp->volumeDescriptorData, 1);
vd_temp = vd_temp->next;
}
return 1;
}
static int
cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector,
int mode)
{
int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize,
diskStructure->pathTableLength);
unsigned char *buffer;
unsigned char *buffer_head;
int len, ret;
path_table_entry temp_entry;
cd9660node *ptcur;
buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
buffer_head = buffer;
ptcur = diskStructure->rootNode;
while (ptcur != NULL) {
memset(&temp_entry, 0, sizeof(path_table_entry));
temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
temp_entry.extended_attribute_length[0] =
ptcur->isoDirRecord->ext_attr_length[0];
memcpy(temp_entry.name, ptcur->isoDirRecord->name,
temp_entry.length[0] + 1);
len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
if (mode == LITTLE_ENDIAN) {
cd9660_731(ptcur->fileDataSector,
temp_entry.first_sector);
cd9660_721((ptcur->parent == NULL ?
1 : ptcur->parent->ptnumber),
temp_entry.parent_number);
} else {
cd9660_732(ptcur->fileDataSector,
temp_entry.first_sector);
cd9660_722((ptcur->parent == NULL ?
1 : ptcur->parent->ptnumber),
temp_entry.parent_number);
}
memcpy(buffer, &temp_entry, len);
buffer += len;
ptcur = ptcur->ptnext;
}
ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head,
path_table_sectors);
free(buffer_head);
return ret;
}
static int
cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd)
{
if (cd9660_write_path_table(diskStructure, fd,
diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
return 0;
if (cd9660_write_path_table(diskStructure, fd,
diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0)
return 0;
return 1;
}
static int
cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode)
{
char *buf;
char *temp_file_name;
int ret;
off_t working_sector;
int cur_sector_offset;
iso_directory_record_cd9660 temp_record;
cd9660node *temp;
int rv = 0;
temp_file_name = ecalloc(PATH_MAX, 1);
buf = emalloc(diskStructure->sectorSize);
if ((writenode->level != 0) &&
!(writenode->node->type & S_IFDIR)) {
fsinode *inode = writenode->node->inode;
if ((inode->flags & FI_WRITTEN) != 0) {
INODE_WARNX(("%s: skipping written inode %d", __func__,
(int)inode->st.st_ino));
} else if (writenode->fileDataLength > 0) {
INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
__func__, (int)inode->st.st_ino, inode->ino));
inode->flags |= FI_WRITTEN;
if (writenode->node->contents == NULL)
cd9660_compute_full_filename(writenode,
temp_file_name);
ret = cd9660_copy_file(diskStructure, fd,
writenode->fileDataSector,
(writenode->node->contents != NULL) ?
writenode->node->contents : temp_file_name);
if (ret == 0)
goto out;
}
} else {
cur_sector_offset = 0;
working_sector = writenode->fileDataSector;
if (fseeko(fd, working_sector * diskStructure->sectorSize,
SEEK_SET) == -1)
err(1, "fseeko");
TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
memcpy(&temp_record, temp->isoDirRecord,
sizeof(iso_directory_record_cd9660));
temp_record.length[0] =
cd9660_compute_record_size(diskStructure, temp);
if (temp_record.length[0] + cur_sector_offset >=
diskStructure->sectorSize) {
cur_sector_offset = 0;
working_sector++;
if (fseeko(fd, working_sector *
diskStructure->sectorSize, SEEK_SET) == -1)
err(1, "fseeko");
}
(void)fwrite(&temp_record, 1,
temp->isoDirRecord->length[0], fd);
if (diskStructure->rock_ridge_enabled) {
cd9660_write_rr(diskStructure, fd, temp,
cur_sector_offset, working_sector);
}
if (fseeko(fd, working_sector *
diskStructure->sectorSize + cur_sector_offset +
temp_record.length[0] - temp->su_tail_size,
SEEK_SET) == -1)
err(1, "fseeko");
if (temp->su_tail_size > 0)
fwrite(temp->su_tail_data, 1,
temp->su_tail_size, fd);
if (ferror(fd)) {
warnx("%s: write error", __func__);
goto out;
}
cur_sector_offset += temp_record.length[0];
}
TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0)
goto out;
}
}
rv = 1;
out:
free(temp_file_name);
free(buf);
return rv;
}
static int
cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector,
const unsigned char *buf, int numsecs)
{
off_t curpos;
size_t success;
curpos = ftello(fd);
if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1)
err(1, "fseeko");
success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd);
if (fseeko(fd, curpos, SEEK_SET) == -1)
err(1, "fseeko");
if (success == 1)
success = diskStructure->sectorSize * numsecs;
return success;
}
#if 0
static int
cd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
const unsigned char* buffer)
{
static int working_sector = -1;
static char buf[CD9660_SECTOR_SIZE];
return 0;
}
#endif
int
cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector,
const char *filename)
{
FILE *rf;
int bytes_read;
int buf_size = diskStructure->sectorSize;
char *buf;
buf = emalloc(buf_size);
if ((rf = fopen(filename, "rb")) == NULL) {
warn("%s: cannot open %s", __func__, filename);
free(buf);
return 0;
}
if (diskStructure->verbose_level > 1)
printf("Writing file: %s\n",filename);
if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1)
err(1, "fseeko");
while (!feof(rf)) {
bytes_read = fread(buf,1,buf_size,rf);
if (ferror(rf)) {
warn("%s: fread", __func__);
free(buf);
(void)fclose(rf);
return 0;
}
fwrite(buf,1,bytes_read,fd);
if (ferror(fd)) {
warn("%s: fwrite", __func__);
free(buf);
(void)fclose(rf);
return 0;
}
}
fclose(rf);
free(buf);
return 1;
}
static void
cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode,
off_t offset, off_t sector)
{
int in_ca = 0;
struct ISO_SUSP_ATTRIBUTES *myattr;
offset += writenode->isoDirRecord->length[0];
if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
-1)
err(1, "fseeko");
TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
if (!in_ca) {
offset += CD9660_SUSP_ENTRY_SIZE(myattr);
if (myattr->last_in_suf) {
if (fseeko(fd, ((off_t)diskStructure->
susp_continuation_area_start_sector *
diskStructure->sectorSize)
+ writenode->susp_entry_ce_start,
SEEK_SET) == -1)
err(1, "fseeko");
in_ca = 1;
}
}
}
if (in_ca)
if (fseeko(fd, sector * diskStructure->sectorSize + offset,
SEEK_SET) == -1)
err(1, "fseeko");
}