/*
* Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
* Perez-Rathke and Ram Vedam. All rights reserved.
*
* This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
* Alan Perez-Rathke and Ram Vedam.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
/*
* Write the image
* Writes the entire image
* @param const char* The filename for the image
* @returns int 1 on success, 0 on failure
*/
int
cd9660_write_image(iso9660_disk *diskStructure, const char* image)
{
FILE *fd;
int status;
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;
}
}
/* Write the volume descriptors */
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");
/*
* Write the path tables: there are actually four, but right
* now we are only concearned with two.
*/
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");
/* Write the directories and files */
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);
}
/* Write padding bits. This is temporary */
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;
}
/*
* Write out an individual path table
* Used just to keep redundant code to a minimum
* @param FILE *fd Valid file pointer
* @param int Sector to start writing path table to
* @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
* @returns int 1 on success, 0 on failure
*/
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;
path_table_entry temp_entry;
cd9660node *ptcur;
/*
* Write out the path tables to disk
* Each file descriptor should be pointed to by the PVD, so we know which
* sector to copy them to. One thing to watch out for: the only path tables
* stored are in the endian mode that the application is compiled for. So,
* the first thing to do is write out that path table, then to write the one
* in the other endian mode requires to convert the endianness of each entry
* in the table. The best way to do this would be to create a temporary
* path_table_entry structure, then for each path table entry, copy it to
* the temporary entry, translate, then copy that to disk.
*
* @param FILE* Valid file descriptor
* @returns int 0 on failure, 1 on success
*/
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;
/*
* Write a file to disk
* Writes a file, its directory record, and its data to disk
* This file is designed to be called RECURSIVELY, so initially call it
* with the root node. All of the records should store what sector the
* file goes in, so no computation should be necessary.
*
* @param int fd Valid file descriptor
* @param struct cd9660node* writenode Pointer to the file to be written
* @returns int 0 on failure, 1 on success
*/
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;
/* Todo : clean up variables */
temp_file_name = ecalloc(CD9660MAXPATH + 1, 1);
buf = emalloc(diskStructure->sectorSize);
if ((writenode->level != 0) &&
!(writenode->node->type & S_IFDIR)) {
fsinode *inode = writenode->node->inode;
/* Only attempt to write unwritten files that have length. */
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;
cd9660_compute_full_filename(writenode,
temp_file_name);
ret = cd9660_copy_file(diskStructure, fd,
writenode->fileDataSector, temp_file_name);
if (ret == 0)
goto out;
}
} else {
/*
* Here is a new revelation that ECMA didnt explain
* (at least not well).
* ALL . and .. records store the name "\0" and "\1"
* resepctively. So, for each directory, we have to
* make a new node.
*
* This is where it gets kinda messy, since we have to
* be careful of sector boundaries
*/
cur_sector_offset = 0;
working_sector = writenode->fileDataSector;
if (fseeko(fd, working_sector * diskStructure->sectorSize,
SEEK_SET) == -1)
err(EXIT_FAILURE, "fseeko");
/*
* Now loop over children, writing out their directory
* records - beware of sector boundaries
*/
TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
/*
* Copy the temporary record and adjust its size
* if necessary
*/
memcpy(&temp_record, temp->isoDirRecord,
sizeof(iso_directory_record_cd9660));
/*
* Wrapper function to write a buffer (one sector) to disk.
* Seeks and writes the buffer.
* NOTE: You dont NEED to use this function, but it might make your
* life easier if you have to write things that align to a sector
* (such as volume descriptors).
*
* @param int fd Valid file descriptor
* @param int sector Sector number to write to
* @param const unsigned char* Buffer to write. This should be the
* size of a sector, and if only a portion
* is written, the rest should be set to 0.
*/
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(EXIT_FAILURE, "fseeko");
offset += writenode->isoDirRecord->length[0];
if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
-1)
err(EXIT_FAILURE, "fseeko");
/* Offset now points at the end of the record */
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) {
/*
* Point the offset to the start of this
* record's CE area
*/
if (fseeko(fd, ((off_t)diskStructure->
susp_continuation_area_start_sector *
diskStructure->sectorSize)
+ writenode->susp_entry_ce_start,
SEEK_SET) == -1)
err(EXIT_FAILURE, "fseeko");
in_ca = 1;
}
}
}
/*
* If we had to go to the continuation area, head back to
* where we should be.
*/
if (in_ca)
if (fseeko(fd, sector * diskStructure->sectorSize + offset,
SEEK_SET) == -1)
err(EXIT_FAILURE, "fseeko");
}