libisoburn/test/xorrisoburn.c

503 lines
14 KiB
C

/* Adapter to libisoburn, libisofs and libburn for xorriso,
a command line oriented batch and dialog tool which creates, loads,
manipulates and burns ISO 9660 filesystem images.
Copyright 2007 Thomas Schmitt, <scdbackup@gmx.net>
Provided under GPL version 2.
*/
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
/* ------------------------------------------------------------------------ */
/* The library which does the ISO 9660 / RockRidge manipulations */
#include <libisofs/libisofs.h>
/* The library which does MMC optical drive operations */
#include <libburn/libburn.h>
/* The library which enhances overwriteable media with ISO 9660 multi-session
capabilities via the method invented by Andy Polyakov for growisofs */
#include <libisoburn/libisoburn.h>
/* The official xorriso options API. "No shortcuts" */
#include "xorriso.h"
/* The inner description of XorrisO */
#include "xorriso_private.h"
/* The inner isofs- and burn-library interface */
#include "xorrisoburn.h"
/* ------------------------------------------------------------------------ */
int Xorriso_startup_libraries(struct XorrisO *xorriso, int flag)
{
int ret;
char *handler_prefix= NULL;
char *queue_sev, *print_sev;
sprintf(xorriso->info_text, "Starting up libraries ...\n");
Xorriso_info(xorriso, 0);
handler_prefix= calloc(strlen(xorriso->progname)+3+1, 1);
if(handler_prefix==NULL) {
sprintf(xorriso->info_text,
"Cannot allocate memory for initializing libraries");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
ret= isoburn_initialize();
if(ret==0) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Cannot initialize libraries");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
free(handler_prefix);
return(0);
}
queue_sev= "DEBUG";
if(xorriso->library_msg_direct_print) {
/* >>> need option for controlling this in XorrisO.
See also Xorriso_msgs_submit */;
print_sev= "DEBUG";
} else
print_sev= "NEVER";
iso_msgs_set_severities(queue_sev, print_sev, "libisofs : ");
burn_msgs_set_severities(queue_sev, print_sev, "libburn : ");
/* ??? >>> do we want united queues ? */
/* burn_set_messenger(iso_get_messenger()); */
sprintf(handler_prefix, "%s : ", xorriso->progname);
burn_set_signal_handling(handler_prefix, NULL, 0);
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Library startup done.\n");
Xorriso_info(xorriso, 0);
free(handler_prefix);
return(1);
}
/* @param flag bit0=aquire as isoburn input drive
bit1=aquire as libburn output drive (as isoburn drive if bit0)
*/
int Xorriso_aquire_drive(struct XorrisO *xorriso, char *adr, int flag)
{
int ret;
struct burn_drive_info *dinfo= NULL;
struct burn_drive *drive;
enum burn_disc_status state;
struct iso_volset *volset;
struct isoburn_read_opts ropts;
if((flag&3)!=3) {
sprintf(xorriso->info_text,
"XORRISOBURN program error : Xorriso_aquire_drive bit0+bit1 not set");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(0);
}
ret= Xorriso_give_up_drive(xorriso, flag&3);
if(ret<=0)
return(ret);
ret= isoburn_drive_scan_and_grab(&dinfo, adr, 1);
Xorriso_process_msg_queues(xorriso,0);
if(ret<=0) {
sprintf(xorriso->info_text,"Cannot aquire drive '%s'", adr);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
if(flag&2)
xorriso->out_drive_handle= dinfo;
if(flag&1)
xorriso->in_drive_handle= dinfo;
else
return(1);
drive= dinfo[0].drive;
/* check for invalid state */
state= isoburn_disc_get_status(drive);
Xorriso_process_msg_queues(xorriso,0);
/* >>> show drive and media status */;
if(state != BURN_DISC_BLANK && state != BURN_DISC_APPENDABLE) {
sprintf(xorriso->info_text, "Unsuitable disc status");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
/* fill read opts */
ropts.norock= 0;
ropts.nojoliet= 0;
ropts.preferjoliet= 0;
ropts.uid= 0;
ropts.gid= 0;
ropts.mode= 0555;
if(isoburn_read_volset(drive, &ropts, &volset) <= 0) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Caninot read ISO image volset");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
xorriso->in_volset_handle= volset;
ret= 1;
ex:
if(ret<=0) {
/* >>> ??? give up not-so-suitable drive ? */;
}
Xorriso_process_msg_queues(xorriso,0);
return(ret);
}
/* @param flag bit0=input drive
bit1=output drive
*/
int Xorriso_give_up_drive(struct XorrisO *xorriso, int flag)
{
int in_is_out_too;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
in_is_out_too= (xorriso->in_drive_handle == xorriso->out_drive_handle);
if((flag&1) && xorriso->in_drive_handle != NULL) {
dinfo= (struct burn_drive_info *) xorriso->in_drive_handle;
drive= dinfo[0].drive;
isoburn_drive_release(drive,0);
/* >>> this should be an official reference for this pointer which was
handed out by isoburn_read_volset() and attached to invisible
struct isoburn. Then to be processed by: iso_volset_free(volset);
*/
xorriso->in_volset_handle= NULL; /* destroyed by isoburn_drive_release() */
xorriso->volset_change_pending= 0;
burn_drive_info_free(dinfo);
xorriso->in_drive_handle= NULL;
if(in_is_out_too)
xorriso->out_drive_handle= NULL;
}
if((flag&2) && xorriso->out_drive_handle!=NULL) {
dinfo= (struct burn_drive_info *) xorriso->out_drive_handle;
drive= dinfo[0].drive;
isoburn_drive_release(drive,0);
burn_drive_info_free(dinfo);
xorriso->out_drive_handle= NULL;
}
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
int Xorriso_write_growing(struct XorrisO *xorriso, int flag)
{
int ret;
struct isoburn_source_opts sopts;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
struct burn_disc *disc;
struct burn_write_opts *burn_options;
dinfo= (struct burn_drive_info *) xorriso->in_drive_handle;
if(dinfo==NULL) {
sprintf(xorriso->info_text,"No drive aquired on attempt to write");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
drive= dinfo[0].drive;
sopts.level= 2;
sopts.flags= ECMA119_ROCKRIDGE;
if(xorriso->do_joliet)
sopts.flags|= ECMA119_ROCKRIDGE;
sopts.relaxed_constraints= 0;
sopts.copy_eltorito= 1;
sopts.no_cache_inodes= 0;
sopts.sort_files= 1;
sopts.default_mode= 0;
sopts.replace_dir_mode= 0;
sopts.dir_mode= 0555;
sopts.replace_file_mode= 0;
sopts.file_mode= 0444;
sopts.replace_uid= (xorriso->do_global_uid);
sopts.uid= xorriso->global_uid;
sopts.replace_gid= (xorriso->do_global_gid);
sopts.gid= xorriso->global_gid;
sopts.input_charset= NULL;
sopts.ouput_charset= NULL;
/* >>> -fs : isoburn_prepare_disc() needs fifo parameters */
if (isoburn_prepare_disc(drive, &disc, &sopts) <= 0) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Cannot prepare disc");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
burn_options= burn_write_opts_new(drive);
if(burn_options==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Cannot allocate option set");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
burn_write_opts_set_simulate(burn_options, !!xorriso->do_dummy);
burn_write_opts_set_multi(burn_options, !xorriso->do_close);
burn_drive_set_speed(drive, xorriso->speed, xorriso->speed);
burn_write_opts_set_underrun_proof(burn_options, 1);
isoburn_disc_write(burn_options, disc);
burn_write_opts_free(burn_options);
ret= Xorriso_pacifier_loop(xorriso, drive, 0);
if(ret<=0)
return(ret);
ret= isoburn_activate_session(drive);
Xorriso_process_msg_queues(xorriso,0);
if(ret<=0) {
sprintf(xorriso->info_text,
"Could not write new set of volume descriptors");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(ret);
}
/* >>> cleanup disc ? */
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
int Xorriso_pacifier_loop(struct XorrisO *xorriso, struct burn_drive *drive,
int flag)
{
int ret, size, free_bytes, i;
struct burn_progress progress;
char *status_text;
while(burn_drive_get_status(drive, NULL) == BURN_DRIVE_SPAWNING)
usleep(100002);
while(burn_drive_get_status(drive, &progress) != BURN_DRIVE_IDLE) {
sprintf(xorriso->info_text, "Writing: sector %d of %d",
progress.sector, progress.sectors);
ret= isoburn_get_fifo_status(drive, &size, &free_bytes, &status_text);
if(ret>0 )
sprintf(xorriso->info_text+strlen(xorriso->info_text),
" [fifo %s, %2d%% fill]", status_text,
(int) (100.0-100.0*((double) free_bytes)/(double) size));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "UPDATE", 0);
for(i= 0; i<10; i++) {
Xorriso_process_msg_queues(xorriso, 0);
usleep(100000);
}
}
return(1);
}
int Xorriso_graft_in(struct XorrisO *xorriso, char *disk_path, char *img_path,
int flag)
{
struct iso_volume *volume;
struct iso_tree_radd_dir_behavior behav= {NULL, 0, 0};
char path[SfileadrL], *apt, *npt;
struct iso_tree_node_dir *dir;
struct iso_tree_node *node;
int done= 0, is_dir= 0, l;
struct stat stbuf;
if(xorriso->in_volset_handle==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"No volset is loaded.");
if(xorriso->indev[0]==0)
sprintf(xorriso->info_text," No -dev or -indev is selected.");
else
sprintf(xorriso->info_text," Possible program error with drive '%s'.",
xorriso->indev);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
strncpy(path, img_path, sizeof(path)-1);
path[sizeof(path)-1]= 0;
apt= npt= path;
volume= iso_volset_get_volume(
(struct iso_volset *) xorriso->in_volset_handle, 0);
if(lstat(disk_path, &stbuf) == -1) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Cannot determine attributes of source file '%s'",disk_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "SORRY", 0);
return(0);
}
if(S_ISDIR(stbuf.st_mode))
is_dir= 1;
else if(!(S_ISREG(stbuf.st_mode) || S_ISLNK(stbuf.st_mode))) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Source file '%s' is of non-supported file type", disk_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else {
l= strlen(img_path);
if(l>0)
if(img_path[l-1]=='/')
l= 0;
if(l==0) {
sprintf(xorriso->info_text,
"Source '%s' is not a directory. Target '%s' would be.",
disk_path, img_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
}
}
dir= iso_volume_get_root(volume);
if(dir==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"While grafting '%s' : no root node available", img_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(0);
}
for(npt= apt; !done; apt= npt+1) {
npt= strchr(apt, '/');
if(npt==NULL) {
npt= apt+strlen(apt);
done= 1;
} else
*npt= 0;
if(*apt==0) {
*apt= '/';
apt++;
if(done)
goto attach_source;
continue;
}
node= iso_tree_volume_path_to_node(volume,path);
if(node!=NULL) {
if(iso_tree_node_get_type(node)!=LIBISO_NODE_DIR) {
Xorriso_process_msg_queues(xorriso,0);
if(done) {
/* >>> handle overwrite situation */;
}
sprintf(xorriso->info_text,
"While grafting '%s' : '%s' exists and is not a directory",
img_path, path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
dir= (struct iso_tree_node_dir *) node;
} else if(is_dir || !done) {
dir= iso_tree_add_dir(dir, apt);
if(dir==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"While grafting '%s' : could not insert '%s'", img_path, path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
}
if(done) {
attach_source:;
if(is_dir) {
iso_tree_radd_dir(dir, disk_path, &behav);
} else {
node= iso_tree_add_node(dir, disk_path);
if(node == NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"While grafting '%s'='%s' : libisofs_errno = %d",
img_path, disk_path, libisofs_errno);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
iso_tree_node_set_name(node, apt);
}
} else
*npt= '/';
}
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Added %s '%s'='%s'", (is_dir ? "directory" : "node"),
img_path, disk_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
xorriso->volset_change_pending= 1;
return(1);
}
int Xorriso__text_to_sev(char *severity_name, int *severity_number, int flag)
{
int ret= 1;
if(severity_name[0]==0)
*severity_number= 0;
else
ret= burn_text_to_sev(severity_name, severity_number, 0);
return(ret);
}
int Xorriso_process_msg_queues(struct XorrisO *xorriso, int flag)
{
int ret, error_code= 0, os_errno= 0, count= 0, pass;
char severity[80];
for(pass= 0; pass< 2; pass++) {
while(1) {
if(pass==0)
ret= iso_msgs_obtain("ALL", &error_code, xorriso->info_text, &os_errno,
severity);
else
ret= burn_msgs_obtain("ALL", &error_code, xorriso->info_text, &os_errno,
severity);
if(ret<=0)
break;
Xorriso_msgs_submit(xorriso, error_code, xorriso->info_text, os_errno,
severity, (pass+1)<<2);
count++;
}
}
if(xorriso->library_msg_direct_print && count>0) {
sprintf(xorriso->info_text," (%d library messages repeated by xorriso)\n",
count);
Xorriso_info(xorriso, 0);
}
return(1);
}