libisoburn/test/xorrisoburn.c

2547 lines
77 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>
#include <time.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_pacifier_loop(struct XorrisO *xorriso, struct burn_drive *drive,
int flag);
/* ------------------------------------------------------------------------ */
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) {
sprintf(xorriso->info_text, "Cannot initialize libraries");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
free(handler_prefix);
return(0);
}
xorriso->libs_are_started= 1;
queue_sev= "DEBUG";
if(xorriso->library_msg_direct_print) {
/* >>> need option for controlling this in XorrisO.
See also Xorriso_msgs_submit */;
print_sev= xorriso->report_about_text;
} 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=path is in source filesystem , bit1= unconditionally */
int Xorriso_much_too_long(struct XorrisO *xorriso, int len, int flag)
{
if(len>=SfileadrL || (flag&2)) {
sprintf(xorriso->info_text,
"Path given for %s is much too long (%d)",
((flag&1) ? "local filesystem" : "ISO image"), len);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
return(1);
}
/* @param flag bit1= obtain outdrive, else indrive */
int Xorriso_get_drive_handles(struct XorrisO *xorriso,
struct burn_drive_info **dinfo,
struct burn_drive **drive,
char *attempt, int flag)
{
if(flag&2)
*dinfo= (struct burn_drive_info *) xorriso->out_drive_handle;
else
*dinfo= (struct burn_drive_info *) xorriso->in_drive_handle;
if(*dinfo==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "No drive aquired %s", attempt);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
*drive= (*dinfo)[0].drive;
return((*drive)!=NULL);
}
/* @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(Sfile_str(xorriso->indev, adr, 0)<=0)
return(-1);
}
if(flag&1) {
xorriso->in_drive_handle= dinfo;
if(Sfile_str(xorriso->outdev, adr, 0)<=0)
return(-1);
} 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, "Disc status unsuitable for writing");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
}
/* 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,"Cannot read ISO image volset");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
xorriso->in_volset_handle= volset;
Xorriso_process_msg_queues(xorriso,0);
Xorriso_toc(xorriso, 1);
ret= 1;
ex:
if(ret<0) {
/* >>> give up drive */;
}
Xorriso_process_msg_queues(xorriso,0);
return(ret);
}
/* @param flag bit0=input drive
bit1=output drive
bit2=eject
*/
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) {
Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to give drive up", 0);
if(drive!=NULL)
isoburn_drive_release(drive,!!(flag&4));
if(xorriso->in_volset_handle!=NULL)
iso_volset_free((struct iso_volset *) xorriso->in_volset_handle);
xorriso->in_volset_handle= NULL;
xorriso->volset_change_pending= 0;
if(dinfo!=NULL)
burn_drive_info_free(dinfo);
xorriso->in_drive_handle= NULL;
xorriso->indev[0]= 0;
if(in_is_out_too) {
xorriso->out_drive_handle= NULL;
xorriso->outdev[0]= 0;
}
}
if((flag&2) && xorriso->out_drive_handle!=NULL) {
Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to give drive up", 2);
if(drive!=NULL)
isoburn_drive_release(drive,!!(flag&4));
if(dinfo!=NULL)
burn_drive_info_free(dinfo);
xorriso->out_drive_handle= NULL;
xorriso->outdev[0]= 0;
}
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
int Xorriso_make_write_options(
struct XorrisO *xorriso, struct burn_drive *drive,
struct burn_write_opts **burn_options, int flag)
{
*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);
return(1);
}
/* @param flag bit0= do not write but only prepare and return size in sectors
*/
int Xorriso_write_growing(struct XorrisO *xorriso, int flag)
{
int ret, media_space;
struct isoburn_source_opts sopts;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
struct burn_disc *disc= NULL;
struct burn_write_opts *burn_options;
off_t readcounter= 0,writecounter= 0;
int num_sessions= 0, num_tracks= 0;
struct burn_session **sessions;
struct burn_track **tracks;
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to write", 2);
if(ret<=0)
return(0);
sopts.level= 2;
sopts.flags= ECMA119_ROCKRIDGE;
if(xorriso->do_joliet)
sopts.flags|= ECMA119_JOLIET;
sopts.relaxed_constraints= 0;
sopts.copy_eltorito= 1;
sopts.no_cache_inodes= 0;
sopts.sort_files= 1;
sopts.default_mode= 1;
sopts.replace_dir_mode= xorriso->do_global_mode;
sopts.dir_mode= xorriso->global_dir_mode;
sopts.replace_file_mode= xorriso->do_global_mode;
sopts.file_mode= xorriso->global_file_mode;
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;
sopts.fifo_size= xorriso->fs * 2048;
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);
{ret= 0; goto ex;}
}
ret= Xorriso_make_write_options(xorriso, drive, &burn_options, 0);
if(ret<=0)
goto ex;
ret= burn_disc_get_sectors(disc);
if(flag&1)
goto ex;
media_space= burn_disc_available_space(drive, burn_options) / (off_t) 2048;
if(media_space < ret) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Image size %ds exceeds free space on media %ds",
ret, media_space);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
{ret= 0; goto ex;}
}
isoburn_disc_write(burn_options, disc);
burn_write_opts_free(burn_options);
ret= Xorriso_pacifier_loop(xorriso, drive, 0);
if(ret<=0)
goto ex;
if(!isoburn_drive_wrote_well(drive)) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"libburn indicates failure with writing.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
sessions= burn_disc_get_sessions(disc, &num_sessions);
if(num_sessions>0) {
tracks= burn_session_get_tracks(sessions[0], &num_tracks);
if(tracks!=NULL && num_tracks>0) {
burn_track_get_counters(tracks[0],&readcounter,&writecounter);
sprintf(xorriso->info_text,
"ISO image produced: %d sectors. Written to media: %d sectors\n",
(int) readcounter/2048, (int) writecounter/2048);
Xorriso_info(xorriso, 0);
}
}
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);
goto ex;
}
sprintf(xorriso->info_text, "Writing completed sucessfully.\n\n");
Xorriso_info(xorriso, 0);
ret= 1;
ex:;
if(disc!=NULL)
burn_disc_free(disc);
Xorriso_process_msg_queues(xorriso,0);
return(ret);
}
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;
enum burn_drive_status drive_status;
double start_time, current_time;
start_time= Sfile_microtime(0);
while(burn_drive_get_status(drive, NULL) == BURN_DRIVE_SPAWNING)
usleep(100002);
while(1) {
drive_status= burn_drive_get_status(drive, &progress);
if(drive_status == BURN_DRIVE_IDLE)
break;
current_time= Sfile_microtime(0);
if(drive_status == BURN_DRIVE_WRITING && progress.sectors > 0) {
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));
} else if(drive_status == BURN_DRIVE_CLOSING_SESSION ||
drive_status == BURN_DRIVE_CLOSING_TRACK)
sprintf(xorriso->info_text,
"Closing track/session. Working since %.f seconds",
current_time-start_time);
else if(drive_status == BURN_DRIVE_FORMATTING)
sprintf(xorriso->info_text, "Formatting. Working since %.f seconds",
current_time-start_time);
else
sprintf(xorriso->info_text,
"Thank you for being patient since %.f seconds",
current_time-start_time);
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_get_volume(struct XorrisO *xorriso, struct iso_volume **volume,
int flag)
{
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+strlen(xorriso->info_text),
" No -dev or -indev is selected.");
else
sprintf(xorriso->info_text+strlen(xorriso->info_text),
" Possible program error with drive '%s'.", xorriso->indev);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
*volume= iso_volset_get_volume(
(struct iso_volset *) xorriso->in_volset_handle, 0);
return(*volume != NULL);
}
/* @param eff_path returns resulting effective path.
Must provide at least SfileadrL bytes of storage.
@param flag bit0= do not produce problem events (unless faulty path format)
bit1= work purely literally, do not use libisofs
bit2= (with bit1) this is an address in the disk world
@return -1 = faulty path format, 0 = not found ,
1 = found simple node , 2 = found directory
*/
int Xorriso_normalize_img_path(struct XorrisO *xorriso, char *img_path,
char eff_path[], int flag)
{
int ret, is_dir= 0, done= 0;
struct iso_volume *volume;
struct iso_tree_node_dir *dir= NULL;
struct iso_tree_node *node= NULL;
char path[SfileadrL], *apt, *npt, sfe[4*SfileadrL], *cpt;
eff_path[0]= 0;
if(img_path[0]==0)
return(2); /* root directory */
if(!(flag&2)) {
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
}
if(img_path[0]!='/') {
if(flag&4)
strcpy(path, xorriso->wdx);
else
strcpy(path, xorriso->wdi);
ret= Sfile_add_to_path(path, img_path, 0);
if(ret<=0)
goto much_too_long;
} else
if(Sfile_str(path, img_path, 0)<=0)
return(-1);
apt= npt= path;
if(path[0]!='/') {
sprintf(xorriso->info_text,
"Internal error: Unresolvable relative addressing in iso_rr_path '%s'",
img_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "FATAL", 0);
return(-1);
} else if(path[1]==0)
return(2); /* root directory */
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)
break;
continue;
}
if(strcmp(apt,".")==0)
continue;
if(strcmp(apt,"..")==0) {
if(!(flag&2)) {
node= (struct iso_tree_node *) dir;
if(node==NULL) {
bonked_root:;
sprintf(xorriso->info_text,
"Relative addressing in path exceeds root directory: %s",
Text_shellsafe(img_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(-1);
}
dir= iso_tree_node_get_parent(node);
}
/* truncate eff_path */;
cpt= strrchr(eff_path, '/');
if(cpt==NULL) /* ??? if not flag&2 then this is a bug */
goto bonked_root;
*cpt= 0;
continue;
}
ret= Sfile_add_to_path(eff_path, apt, 0);
if(ret<=0) {
much_too_long:;
sprintf(xorriso->info_text, "Effective path gets much too long (%d)",
strlen(eff_path)+strlen(apt)+1);
if(!(flag&1))
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(-1);
}
if(!(flag&2)) {
dir= (struct iso_tree_node_dir *) node;
node= iso_tree_volume_path_to_node(volume,eff_path);
if(node==NULL) {
sprintf(xorriso->info_text, "Cannot find in ISO image: %s",
Text_shellsafe(eff_path, sfe, 0));
if(!(flag&1))
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
if(dir==NULL) /* could be false with "/dir/.." */
dir= iso_tree_node_get_parent(node);
is_dir= LIBISO_ISDIR(node);
}
}
return(1+!!is_dir);
}
int Xorriso_get_node_by_path(struct XorrisO *xorriso,
char *in_path, char *eff_path,
struct iso_tree_node **node, int flag)
{
int ret;
char sfe[4*SfileadrL], path[SfileadrL];
struct iso_volume *volume;
ret= Xorriso_normalize_img_path(xorriso, in_path, path, 0);
if(ret<=0)
return(ret);
if(eff_path!=NULL)
strcpy(eff_path, path);
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
*node= iso_tree_volume_path_to_node(volume,path);
if(*node==NULL) {
sprintf(xorriso->info_text, "Not found in ISO image: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
return(1);
}
/* @param flag bit0= recursion is active */
int Xorriso_add_tree(struct XorrisO *xorriso, struct iso_tree_node_dir *dir,
char *img_dir_path, char *disk_dir_path, int flag)
{
struct iso_volume *volume;
struct iso_tree_node *node;
int ret, target_is_dir, source_is_dir;
struct DirseQ *dirseq= NULL;
char sfe[4*SfileadrL], sfe2[4*SfileadrL];
char disk_path[2*SfileadrL], img_path[2*SfileadrL];
char *name, *img_name;
struct stat stbuf;
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
ret= Dirseq_new(&dirseq, disk_dir_path, 1);
if(ret<0) {
sprintf(xorriso->info_text,"Failed to create source filesystem iterator");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
{ret= -1; goto ex;}
}
if(ret==0) {
sprintf(xorriso->info_text,"Cannot open as source directory: %s",
Text_shellsafe(disk_dir_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
{ret= 0; goto ex;}
}
if(Sfile_str(disk_path, disk_dir_path,0)<=0)
{ret= -1; goto ex;}
if(disk_path[0]==0 || disk_path[strlen(disk_path)-1]!='/')
strcat(disk_path,"/");
name= disk_path+strlen(disk_path);
if(Sfile_str(img_path, img_dir_path, 0)<=0)
{ret= -1; goto ex;}
if(img_path[0] || img_path[strlen(img_path)-1]!='/')
strcat(img_path,"/");
img_name= img_path+strlen(img_path);
while(1) { /* loop over directory content */
Xorriso_process_msg_queues(xorriso,0);
ret= Dirseq_next_adr(dirseq,name,0);
if(ret==0)
break;
if(ret<0) {
sprintf(xorriso->info_text,"Failed to obtain next directory entry");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
{ret= -1; goto ex;}
}
strcpy(img_name, name);
if(Xorriso_much_too_long(xorriso, strlen(img_path), 0)<=0)
goto was_problem;
if(Xorriso_much_too_long(xorriso, strlen(disk_path), 0)<=0)
goto was_problem;
if(lstat(disk_path, &stbuf)==-1) {
sprintf(xorriso->info_text,
"Cannot determine attributes of source file %s",
Text_shellsafe(disk_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "SORRY", 0);
goto was_problem;
}
source_is_dir= 0;
if(S_ISDIR(stbuf.st_mode)) {
source_is_dir= 1;
} else if(!(S_ISREG(stbuf.st_mode) || S_ISLNK(stbuf.st_mode))) {
sprintf(xorriso->info_text,"Source file %s is of non-supported file type",
Text_shellsafe(disk_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
goto was_problem;
}
/* does a node exist with this name ? */
node= iso_tree_volume_path_to_node(volume,img_path);
if(node!=NULL) {
target_is_dir= (iso_tree_node_get_type(node)==LIBISO_NODE_DIR);
if(!(target_is_dir && source_is_dir)) {
Xorriso_process_msg_queues(xorriso,0);
/* handle overwrite situation */;
if(xorriso->do_overwrite==1 ||
(xorriso->do_overwrite==2 && !target_is_dir)) {
ret= Xorriso_rmi(xorriso, NULL, img_path, 1|8);
if(ret<=0)
goto was_problem;
if(ret==3) {
sprintf(xorriso->info_text, "User revoked adding of: %s",
Text_shellsafe(disk_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
goto was_problem;
}
node= NULL;
} else {
sprintf(xorriso->info_text,
"While grafting %s : file object exists and may not be overwritten",
Text_shellsafe(img_path,sfe,0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
goto was_problem;
}
}
}
if(node==NULL)
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",
Text_shellsafe(img_path,sfe,0), Text_shellsafe(disk_path,sfe2,0),
libisofs_errno);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
goto was_problem;
}
/* name always equal to disk. Obsolete: iso_tree_node_set_name(node,name);*/
if(source_is_dir) {
ret= Xorriso_add_tree(xorriso, (struct iso_tree_node_dir *) node,
img_path, disk_path, 1);
if(ret<0)
{ret= -1; goto ex;}
if(ret==0)
goto was_problem;
}
continue;
was_problem:;
ret= Xorriso_eval_problem_status(xorriso, 0, 1);
if(ret<0)
{ret= 0; goto ex;}
}
ret= 1;
ex:
Xorriso_process_msg_queues(xorriso,0);
Dirseq_destroy(&dirseq, 0);
return(ret);
}
/** @param flag bit0= mkdir: graft in as empty directory, not as copy from disk
@return <=0 = error , 1 = added simple node , 2 = added directory */
int Xorriso_graft_in(struct XorrisO *xorriso, char *disk_path, char *img_path,
int flag)
{
struct iso_volume *volume;
char path[SfileadrL], *apt, *npt, *cpt, sfe[4*SfileadrL];
struct iso_tree_node_dir *dir;
struct iso_tree_node *node;
int done= 0, is_dir= 0, l, ret, target_is_dir, source_is_dir;
struct stat stbuf;
for(cpt= img_path; 1; cpt++) {
if(cpt[0]!='/')
break;
cpt= strstr(cpt,"/.");
if(cpt==NULL)
break;
if(cpt[2]=='.') {
if(cpt[3]=='/' || cpt[3]==0)
break;
} else if(cpt[2]=='/' || cpt[2]==0)
break;
}
if(cpt!=NULL) {
sprintf(xorriso->info_text,
"Unsupported relative addressing in iso_rr_path '%s'", img_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "SORRY", 0);
return(0);
}
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
strncpy(path, img_path, sizeof(path)-1);
path[sizeof(path)-1]= 0;
apt= npt= path;
if(!(flag&1)) {
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;
}
source_is_dir= (is_dir || (flag&1) || !done);
node= iso_tree_volume_path_to_node(volume,path);
if(node!=NULL) {
target_is_dir= (iso_tree_node_get_type(node)==LIBISO_NODE_DIR);
if(!(target_is_dir && source_is_dir)) {
Xorriso_process_msg_queues(xorriso,0);
/* handle overwrite situation */;
if(xorriso->do_overwrite==1 ||
(xorriso->do_overwrite==2 && !target_is_dir)) {
ret= Xorriso_rmi(xorriso, NULL, path, 1|8);
if(ret<=0)
return(ret);
if(ret==3) {
sprintf(xorriso->info_text, "User revoked adding of: %s",
Text_shellsafe(disk_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return(0);
}
node= NULL;
goto handle_path_node;
}
sprintf(xorriso->info_text,
"While grafting '%s' : '%s' exists and may not be overwritten",
img_path, path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
dir= (struct iso_tree_node_dir *) node;
}
handle_path_node:;
if(node==NULL && source_is_dir) { /* make a directory */
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);
}
iso_tree_node_set_ctime((struct iso_tree_node *) dir, time(NULL));
/* >>> copy properties from correspondent directory in disk_path
if there is any */;
}
if(done) {
attach_source:;
if(flag&1) {
/* directory node was created above */;
} else if(is_dir) {
#ifdef Xorriso_on_its_way_ouT
/* <<< */
{ struct iso_tree_radd_dir_behavior behav= {NULL, 0, 0};
iso_tree_radd_dir(dir, disk_path, &behav);
}
#else
ret= Xorriso_add_tree(xorriso, dir, img_path, disk_path, 0);
if(ret<=0)
return(ret);
#endif /* ! Xorriso_on_its_way_ouT */
} 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);
xorriso->volset_change_pending= 1;
return(1+!!is_dir);
}
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];
if(!xorriso->libs_are_started)
return(1);
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);
}
/* @param flag bit0=short report form
*/
int Xorriso_toc(struct XorrisO *xorriso, int flag)
{
int num_sessions= 0, num_tracks= 0, lba= 0, nwa= -1, pmin, psec, pframe, ret;
int track_count= 0, session_no, track_no, profile_no= -1;
int last_track_start= 0, last_track_size= -1, num_data= 0, is_data;
char profile_name[80],*respt;
struct burn_disc *disc= NULL;
struct burn_session **sessions;
struct burn_track **tracks;
struct burn_toc_entry toc_entry;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
enum burn_disc_status s;
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to print Table Of Content", 2);
if(ret<=0)
return(0);
respt= xorriso->result_line;
sprintf(respt, "Drive current: -indev '%s'\n", xorriso->indev);
Xorriso_result(xorriso,0);
sprintf(respt, "Drive type : vendor '%s' product '%s' revision '%s'\n",
dinfo[0].vendor, dinfo[0].product, dinfo[0].revision);
if(!(flag&1))
Xorriso_result(xorriso,0);
sprintf(respt, "Media current: ");
ret= burn_disc_get_profile(drive, &profile_no, profile_name);
if (profile_no > 0 && ret > 0) {
if (profile_name[0])
sprintf(respt+strlen(respt), "%s\n", profile_name);
else
sprintf(respt+strlen(respt), "%4.4Xh\n", profile_no);
} else
sprintf(respt+strlen(respt), "is not recognizable\n");
Xorriso_result(xorriso,0);
sprintf(respt, "Media status : ");
s= isoburn_disc_get_status(drive);
if (s == BURN_DISC_FULL) {
sprintf(respt+strlen(respt), "is written , is closed\n");
} else if (s == BURN_DISC_APPENDABLE) {
sprintf(respt+strlen(respt), "is written , is appendable\n");
} else if (s == BURN_DISC_BLANK) {
sprintf(respt+strlen(respt), "is blank\n");
} else if (s == BURN_DISC_EMPTY)
sprintf(respt+strlen(respt), "is not present\n");
else
sprintf(respt+strlen(respt), "is not recognizable\n");
Xorriso_result(xorriso,0);
if(s != BURN_DISC_FULL && s != BURN_DISC_APPENDABLE)
return(1);
if(xorriso->request_to_abort)
return(1);
disc= burn_drive_get_disc(drive);
if (disc==NULL) {
Xorriso_process_msg_queues(xorriso,0);
#define Xorriso_with_isoburn_get_min_start_bytE 1
#ifdef Xorriso_with_isoburn_get_min_start_bytE
{ off_t start_byte= 0;
ret= isoburn_get_min_start_byte(drive, &start_byte, 0);
nwa= start_byte / 2048;
}
#else
ret= isoburn_disc_track_lba_nwa(drive, NULL, 0, &lba, &nwa);
#endif
if(ret<=0) {
Xorriso_process_msg_queues(xorriso,0);
if(flag&1)
return(0);
sprintf(xorriso->info_text, "Cannot obtain Table Of Content");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
/* fabricate TOC */
sprintf(respt, "Media content: session %2d ", 1);
sprintf(respt+strlen(respt), "track %2d %s lba: %9d\n", 1, "data ", 0);
if(!(flag&1))
Xorriso_result(xorriso,0);
last_track_start= lba;
sprintf(respt, "Media content: session %2d ", 1);
sprintf(respt+strlen(respt), "leadout lba: %9d\n", nwa);
if(!(flag&1))
Xorriso_result(xorriso,0);
num_data= last_track_size= nwa;
num_sessions= 1;
} else {
sessions= burn_disc_get_sessions(disc, &num_sessions);
for (session_no= 0; session_no<num_sessions && !(xorriso->request_to_abort);
session_no++) {
tracks = burn_session_get_tracks(sessions[session_no], &num_tracks);
if (tracks==NULL)
continue;
for(track_no= 0; track_no<num_tracks && !(xorriso->request_to_abort);
track_no++) {
track_count++;
is_data= 0;
burn_track_get_entry(tracks[track_no], &toc_entry);
if (toc_entry.extensions_valid & 1) {
/* DVD extension valid */
lba= toc_entry.start_lba;
} else {
lba= burn_msf_to_lba(toc_entry.pmin, toc_entry.psec, toc_entry.pframe);
}
sprintf(respt, "Media content: session %2d ", session_no+1);
sprintf(respt+strlen(respt), "track %2d %s lba: %9d\n",
track_count, ((toc_entry.control&7)<4?"audio":"data "), lba);
if(!(flag&1))
Xorriso_result(xorriso,0);
last_track_start= lba;
if((toc_entry.control&7)>=4) /* data track */
is_data= 1;
}
burn_session_get_leadout_entry(sessions[session_no], &toc_entry);
if (toc_entry.extensions_valid & 1) {
lba= toc_entry.start_lba;
burn_lba_to_msf(lba, &pmin, &psec, &pframe);
} else {
lba= burn_msf_to_lba(pmin, psec, pframe);
lba= burn_msf_to_lba(toc_entry.pmin, toc_entry.psec, toc_entry.pframe);
}
sprintf(respt, "Media content: session %2d ", session_no+1);
sprintf(respt+strlen(respt), "leadout lba: %9d\n", lba);
if(!(flag&1))
Xorriso_result(xorriso,0);
last_track_size= lba - last_track_start;
if(is_data)
num_data+= last_track_size;
}
}
if(xorriso->request_to_abort)
return(1);
sprintf(respt, "Media summary: %d session%s, %d data blocks\n",
num_sessions, (num_sessions==1 ? "" : "s"), num_data);
Xorriso_result(xorriso,0);
if (s == BURN_DISC_APPENDABLE && nwa!=0) {
ret= isoburn_disc_track_lba_nwa(drive, NULL, 0, &lba, &nwa);
if(ret>0) {
sprintf(respt, "Media nwa : %ds\n", nwa);
if(!(flag&1))
Xorriso_result(xorriso,0);
}
}
if (disc!=NULL)
burn_disc_free(disc);
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
int Xorriso_show_devices(struct XorrisO *xorriso, int flag)
{
char adr[BURN_DRIVE_ADR_LEN];
int i;
struct burn_drive_info *drive_list= NULL;
unsigned int drive_count;
char *respt, perms[8];
struct stat stbuf;
sprintf(xorriso->info_text, "Beginning to scan for devices ...\n");
Xorriso_info(xorriso,0);
burn_drive_clear_whitelist();
while(!burn_drive_scan(&drive_list, &drive_count)) {
Xorriso_process_msg_queues(xorriso,0);
usleep(100000);
}
Xorriso_process_msg_queues(xorriso,0);
if(drive_count <= 0) {
/* >>> was a drive_list created at all ? */
/* >>> must it be freed ? */
sprintf(xorriso->info_text, "No drives found");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
sprintf(xorriso->info_text, "Full drive scan done\n");
Xorriso_info(xorriso,0);
respt= xorriso->result_line;
for(i= 0; i < drive_count && !(xorriso->request_to_abort); i++) {
if(burn_drive_get_adr(&(drive_list[i]), adr)<=0)
strcpy(adr, "-get_adr_failed-");
Xorriso_process_msg_queues(xorriso,0);
if(stat(adr,&stbuf)==-1) {
sprintf(perms,"errno=%d",errno);
} else {
strcpy(perms,"------");
if(stbuf.st_mode&S_IRUSR) perms[0]= 'r';
if(stbuf.st_mode&S_IWUSR) perms[1]= 'w';
if(stbuf.st_mode&S_IRGRP) perms[2]= 'r';
if(stbuf.st_mode&S_IWGRP) perms[3]= 'w';
if(stbuf.st_mode&S_IROTH) perms[4]= 'r';
if(stbuf.st_mode&S_IWOTH) perms[5]= 'w';
}
sprintf(respt, "%d -dev '%s' %s : '%-8.8s' '%s' \n",
i, adr, perms, drive_list[i].vendor, drive_list[i].product);
Xorriso_result(xorriso,0);
}
burn_drive_info_free(drive_list);
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
int Xorriso_tell_media_space(struct XorrisO *xorriso,
int *media_space, int *free_space, int flag)
{
int ret;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
struct burn_write_opts *burn_options;
(*free_space)= (*media_space)= 0;
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to -tell_media_space", 2);
if(ret<=0)
return(0);
ret= Xorriso_make_write_options(xorriso, drive, &burn_options, 0);
if(ret<=0)
return(-1);
(*free_space)= (*media_space)=
burn_disc_available_space(drive, burn_options) / (off_t) 2048;
burn_write_opts_free(burn_options);
if(xorriso->volset_change_pending) {
/* >>> care for modifying */
ret= Xorriso_write_growing(xorriso, 1);
if(ret>0)
(*free_space)-= ret;
}
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
/* @param flag bit0=fast , bit1=deformat
@return 0=failure, did not touch media , -1=failure, altered media
1=success, altered media , 2=success, did not touch media
*/
int Xorriso_blank_media(struct XorrisO *xorriso, int flag)
{
int ret, do_deformat= 0;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
enum burn_disc_status disc_state;
struct burn_progress p;
double percent = 1.0;
int current_profile;
char current_profile_name[80];
char mode_names[4][80]= {"all", "fast", "deformat", "deformat_quickest"};
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to -blank", 2);
if(ret<=0)
return(0);
burn_disc_get_profile(drive, &current_profile, current_profile_name);
/* >>> */;
disc_state = isoburn_disc_get_status(drive);
if(current_profile == 0x13) { /* overwriteable DVD-RW */
/* Depending on flag bit1 formatted DVD-RW will get blanked to sequential
state or pseudo blanked by invalidating an eventual ISO image. */
if(flag&2)
do_deformat= 1;
} else if(current_profile == 0x14) { /* sequential DVD-RW */
if((flag&1) && !(flag&2)) {
sprintf(xorriso->info_text,
"-blank: DVD-RW present. Mode 'fast' defaulted to mode 'all'.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
sprintf(xorriso->info_text,
"Mode 'deformat_quickest' produces single-session-only media.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "HINT", 0);
flag&= ~1;
}
} else if(disc_state == BURN_DISC_BLANK) {
sprintf(xorriso->info_text,"Blank media detected. Will leave it untouched");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return 2;
} else if(disc_state==BURN_DISC_FULL || disc_state==BURN_DISC_APPENDABLE) {
;
} else if(disc_state == BURN_DISC_EMPTY) {
sprintf(xorriso->info_text,"No media detected in drive");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return 0;
} else {
sprintf(xorriso->info_text, "Unsuitable drive and media state");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return 0;
}
if(!isoburn_disc_erasable(drive)) {
sprintf(xorriso->info_text, "Media is not of erasable type");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return 0;
}
if(xorriso->do_dummy) {
sprintf(xorriso->info_text,
"-dummy mode prevents blanking of media in mode '%s'.",
mode_names[flag&3]);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return(1);
}
sprintf(xorriso->info_text, "Beginning to blank media in mode '%s'.\n",
mode_names[flag&3]);
Xorriso_info(xorriso,0);
if(do_deformat)
burn_disc_erase(drive, (flag&1));
else
isoburn_disc_erase(drive, (flag&1));
usleep(1000000);
while (burn_drive_get_status(drive, &p) != BURN_DRIVE_IDLE) {
if(p.sectors>0 && p.sector>=0) /* display 1 to 99 percent */
percent = 1.0 + ((double) p.sector+1.0) / ((double) p.sectors) * 98.0;
sprintf(xorriso->info_text, "Blanking ( %.1f%% done )", percent);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "UPDATE", 0);
usleep(1000000);
}
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Blanking done\n");
Xorriso_info(xorriso,0);
return(1);
}
/* @return 0=failure, did not touch media , -1=failure, altered media
1=success, altered media , 2=success, did not touch media
*/
int Xorriso_format_media(struct XorrisO *xorriso, int flag)
{
int ret, mode_flag= 0;
struct burn_drive_info *dinfo;
struct burn_drive *drive;
#ifdef NIX
struct burn_progress p;
double percent = 1.0;
#endif
int current_profile;
char current_profile_name[80];
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to -format", 2);
if(ret<=0)
return(0);
burn_disc_get_profile(drive, &current_profile, current_profile_name);
if(current_profile == 0x14) {
; /* ok DVD-RW sequential */
} else if(current_profile == 0x1a) {
mode_flag= 2;
} else {
sprintf(xorriso->info_text,
"Can only -format DVD+RW and sequential DVD-RW");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
sprintf(xorriso->info_text,"Media current: %s (%4.4xh)",
current_profile_name, current_profile);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return 0;
}
if(xorriso->do_dummy) {
sprintf(xorriso->info_text, "-dummy mode prevents formatting of media.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return(1);
}
sprintf(xorriso->info_text, "Beginning to format media.\n");
Xorriso_info(xorriso, 0);
burn_disc_format(drive, (off_t) 0, mode_flag);
ret= Xorriso_pacifier_loop(xorriso, drive, 0);
if(ret<=0)
return(ret);
#ifdef NIX
usleep(1000000);
while(burn_drive_get_status(drive, &p) != BURN_DRIVE_IDLE) {
if(p.sectors>0 && p.sector>=0) /* display 1 to 99 percent */
percent = 1.0 + ((double) p.sector+1.0)
/ ((double) p.sectors) * 98.0;
sprintf(xorriso->info_text, "Formatting ( %.1f%% done )", percent);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "UPDATE", 0);
usleep(1000000);
}
burn_disc_get_profile(dinfo[0].drive, &current_profile, current_profile_name);
sprintf(xorriso->info_text, "Media type now: %4.4xh \"%s\"\n",
current_profile, current_profile_name);
Xorriso_info(xorriso, 0);
if(current_profile != 0x13 && current_profile != 0x1a) {
sprintf(xorriso->info_text,
"Failed to change media profile to desired value.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
#endif
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Formatting done\n");
Xorriso_info(xorriso,0);
return(1);
}
int Xorriso_node_from_path(struct XorrisO *xorriso, struct iso_volume *volume,
char *path, struct iso_tree_node **node, int flag)
{
char sfe[4*SfileadrL];
*node= iso_tree_volume_path_to_node(volume, path);
Xorriso_process_msg_queues(xorriso,0);
if(*node==NULL) {
sprintf(xorriso->info_text, "Cannot find path %s in loaded ISO image",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
return(1);
}
/* @param boss_iter If not NULL then this is an iterator suitable for
iso_tree_node_remove_iter() which is then to be used instead
of iso_tree_node_remove().
@param flag bit0= remove whole sub tree: rm -r
bit1= remove empty directory: rmdir
bit2= recursion: do not reassure in mode 2 "tree"
bit3= this is for overwriting and not for plain removal
@return <=0 = error
1 = removed simple node
2 = removed directory or tree
3 = did not remove on user revocation
*/
int Xorriso_rmi(struct XorrisO *xorriso, void *boss_iter,
char *path, int flag)
{
int ret, is_dir= 0, pl, not_removed= 0;
struct iso_tree_node *victim_node, *node;
struct iso_tree_node_dir *boss_node, *root_dir;
struct iso_tree_iter *iter= NULL;
struct iso_volume *volume;
char sfe[4*SfileadrL], sub_path[2*SfileadrL], *sub_name, *name;
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
goto ex;
if(Xorriso_much_too_long(xorriso, strlen(path), 0)<=0)
{ret= 0; goto ex;}
ret= Xorriso_node_from_path(xorriso, volume, path, &victim_node, 0);
if(ret<=0)
goto ex;
root_dir= iso_volume_get_root(volume);
if(((void *) root_dir) == ((void *) victim_node)) {
sprintf(xorriso->info_text, "May not delete root directory");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
if(LIBISO_ISDIR(victim_node))
is_dir= 1;
if(!is_dir) {
if(flag&2) { /* rmdir */
sprintf(xorriso->info_text, "%s in loaded ISO image is not a directory",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
} else {
if(flag&1) { /* rm -r */
if(xorriso->do_reassure==1 && !xorriso->request_not_to_ask) {
/* Iterate over subordinates and delete them */
/* this might become standard if access permissions get respected */
iter= iso_tree_node_children((struct iso_tree_node_dir *) victim_node);
Xorriso_process_msg_queues(xorriso,0);
pl= strlen(path);
strcpy(sub_path, path);
if(pl==0 || sub_path[pl-1]!='/') {
sub_path[pl++]= '/';
sub_path[pl]= 0;
}
sub_name= sub_path+pl;
while((node= iso_tree_iter_next(iter)) != NULL
&& !xorriso->request_to_abort) {
name= (char *) iso_tree_node_get_name(node);
if(Xorriso_much_too_long(xorriso, pl+1+strlen(name), 0)<=0)
{ret= 0; goto ex;}
strcpy(sub_name, name);
ret= Xorriso_rmi(xorriso, iter, sub_path, (flag&(1|2|8))|4);
if(ret<=0)
goto ex;
if(ret==3)
not_removed= 1;
}
if(not_removed) {
if(!(flag&4)) {
sprintf(xorriso->info_text, "Directory not removed: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
}
ret= 3; goto ex;
}
}
} else {
if(!(flag&2)) { /* not rmdir */
sprintf(xorriso->info_text, "%s in loaded ISO image is a directory",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
iter= iso_tree_node_children((struct iso_tree_node_dir *) victim_node);
Xorriso_process_msg_queues(xorriso,0);
if(iso_tree_iter_next(iter)!=NULL) {
sprintf(xorriso->info_text,
"Directory not empty on attempt to delete: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
}
}
if(xorriso->request_to_abort)
{ret= 3; goto ex;}
boss_node= iso_tree_node_get_parent(victim_node);
Xorriso_process_msg_queues(xorriso,0);
if(boss_node==NULL) {
sprintf(xorriso->info_text,
"Cannot find parent node of %s in loaded ISO image",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
while((xorriso->do_reassure==1 || (xorriso->do_reassure==2 && !(flag&4)))
&& !xorriso->request_not_to_ask) {
Xorriso_ls_filev(xorriso, 1, &path, (off_t) 0, 1|2); /* ls -l */
if(is_dir)
Xorriso_ls_filev(xorriso, 1, &path, (off_t) 0, 2|4); /* du -s */
if(flag&8)
sprintf(xorriso->info_text,
"File exists. Remove ? n= keep old, y= remove, x= abort, @= stop asking\n");
else
sprintf(xorriso->info_text,
"Remove above file ? n= keep it, y= remove it, x= abort, @= stop asking\n");
Xorriso_info(xorriso, 4);
ret= Xorriso_request_confirmation(xorriso, 1|2|4);
if(ret<=0)
goto ex;
if(xorriso->request_to_abort) {
if(!(flag&4)) {
sprintf(xorriso->info_text,
"Removal operation aborted by user before file: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
}
ret= 3; goto ex;
}
if(ret==3)
continue;
if(ret==6) /* yes */
break;
if(ret==4) { /* yes, do not ask again */
xorriso->request_not_to_ask= 1;
break;
}
if(ret==1) { /* no */
sprintf(xorriso->info_text, "Kept in existing state: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
ret= 3; goto ex;
}
}
if(boss_iter!=NULL) {
ret= iso_tree_node_remove_iter((struct iso_tree_iter *) boss_iter);
if(ret<0)
ret= -1;
} else
ret= iso_tree_node_remove(boss_node, victim_node);
Xorriso_process_msg_queues(xorriso,0);
if(ret==-1) {
sprintf(xorriso->info_text,
"Internal failure to remove %s from loaded ISO image",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
ret= -1; goto ex;
}
xorriso->volset_change_pending= 1;
ret= 1+!!is_dir;
ex:;
if(iter!=NULL)
iso_tree_iter_free(iter);
return(ret);
}
int Xorriso__node_name_cmp(const void *node1, const void *node2)
{
char *name1, *name2;
name1= (char *) iso_tree_node_get_name(*((struct iso_tree_node **) node1));
name2= (char *) iso_tree_node_get_name(*((struct iso_tree_node **) node2));
return(strcmp(name1,name2));
}
/* @param flag bit0= only accept directory nodes
bit1= do not report memory usage as DEBUG
bit2= do not apply search pattern but accept any node
*/
int Xorriso_sorted_node_array(struct XorrisO *xorriso,
struct iso_tree_node_dir *dir_node,
int *nodec, struct iso_tree_node ***node_array,
off_t boss_mem, int flag)
{
int i, ret, failed_at;
char *npt, mem_text[80], limit_text[80];
struct iso_tree_iter *iter= NULL;
struct iso_tree_node *node;
off_t mem;
mem= ((*nodec)+1)*sizeof(struct iso_tree_node *);
Sfile_scale((double) mem, mem_text,5,1e4,0);
if(!(flag&2)) {
sprintf(xorriso->info_text,
"Temporary memory needed for result sorting : %s", mem_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
}
if(mem > xorriso->temp_mem_limit - boss_mem) {
Sfile_scale((double) xorriso->temp_mem_limit - boss_mem,limit_text,5,1e4,1);
sprintf(xorriso->info_text,
"Cannot sort. List of matching files exceeds -temp_mem_limit (%s > %s)",
mem_text, limit_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
return(0);
}
*node_array= calloc(sizeof(struct iso_tree_node *), (*nodec)+1);
if(*node_array==NULL) {
sprintf(xorriso->info_text,
"Cannot allocate memory for %d directory entries", *nodec);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
iter= iso_tree_node_children(dir_node);
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL && i<*nodec; ) {
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
if(flag&1)
if(!LIBISO_ISDIR(node))
continue;
(*node_array)[i++]= node;
}
*nodec= i;
if(*nodec<=0)
return(1);
qsort(*node_array, *nodec, sizeof(struct iso_tree *), Xorriso__node_name_cmp);
return(1);
}
/* @param flag bit0= do not only sum up sizes but also print subdirs
*/
int Xorriso_show_du_subs(struct XorrisO *xorriso,
struct iso_tree_node_dir *dir_node,
char *abs_path, char *rel_path, off_t *size,
off_t boss_mem, int flag)
{
int i, ret, no_sort= 0, filec= 0, l;
struct iso_tree_iter *iter= NULL;
struct iso_tree_node *node, **node_array= NULL;
char path[SfileadrL], show_path[SfileadrL], *name, sfe[4*SfileadrL];
off_t sub_size, report_size, mem= 0;
*size= 0;
iter= iso_tree_node_children(dir_node);
if(iter==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Cannot obtain ISO directory iterator");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
{ret= -1; goto ex;}
}
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL; ) {
sub_size= 0;
name= (char *) iso_tree_node_get_name(node);
strcpy(show_path, rel_path);
if(Sfile_add_to_path(show_path, name, 0)<=0)
goto much_too_long;
if(LIBISO_ISDIR(node)) {
strcpy(path, abs_path);
if(Sfile_add_to_path(path, name, 0)<=0) {
much_too_long:;
Xorriso_much_too_long(xorriso, strlen(path)+strlen(name)+1, 2);
{ret= -1; goto ex;}
}
filec++;
mem+= strlen(rel_path)+1;
if(l % sizeof(char *))
mem+= sizeof(char *)-(l % sizeof(char *));
ret= Xorriso_show_du_subs(xorriso, (struct iso_tree_node_dir *) node,
path, show_path, &sub_size, boss_mem, 0);
if(ret<0)
goto ex;
if(ret==0)
continue;
}
sub_size+= iso_tree_node_get_size(node)+strlen(name)+1;
if(sub_size>0)
(*size)+= sub_size;
Xorriso_process_msg_queues(xorriso,0);
}
if(filec<=0 || !(flag&1))
{ret= 1; goto ex;}
/* Reset iteration */
iso_tree_iter_free(iter);
iter= NULL;
Xorriso_process_msg_queues(xorriso,0);
ret= Xorriso_sorted_node_array(xorriso, dir_node, &filec, &node_array,
boss_mem, 1|2|4);
if(ret<0)
goto ex;
if(ret==0) {
no_sort= 1;
iter= iso_tree_node_children(dir_node);
}
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
if(no_sort) {
node= iso_tree_iter_next(iter);
if(node==NULL)
break;
if(!LIBISO_ISDIR(node))
continue;
} else
node= node_array[i];
sub_size= 0;
name= (char *) iso_tree_node_get_name(node);
strcpy(show_path, rel_path);
if(Sfile_add_to_path(show_path, name, 0)<=0)
goto much_too_long;
strcpy(path, abs_path);
if(Sfile_add_to_path(path, name, 0)<=0)
goto much_too_long;
ret= Xorriso_show_du_subs(xorriso, (struct iso_tree_node_dir *) node,
path, show_path, &sub_size, boss_mem+mem, flag&1);
if(ret<0)
goto ex;
report_size= sub_size/1024;
if(report_size*1024<sub_size)
report_size++;
sprintf(xorriso->result_line, "%7.f ",(double) (report_size));
sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n",
Text_shellsafe(show_path, sfe, 0));
Xorriso_result(xorriso, 0);
}
ret= 1;
ex:;
if(iter!=NULL)
iso_tree_iter_free(iter);
if(node_array!=NULL)
free((char *) node_array);
Xorriso_process_msg_queues(xorriso,0);
return(ret);
}
int Xorriso__mode_to_perms(mode_t st_mode, char perms[10], int flag)
{
strcpy(perms,"---------");
if(st_mode&S_IRUSR) perms[0]= 'r';
if(st_mode&S_IWUSR) perms[1]= 'w';
if(st_mode&S_IXUSR) perms[2]= 'x';
if(st_mode&S_ISUID) {
if(st_mode&S_IXUSR)
perms[2]= 's';
else
perms[2]= 'S';
}
if(st_mode&S_IRGRP) perms[3]= 'r';
if(st_mode&S_IWGRP) perms[4]= 'w';
if(st_mode&S_IXGRP) perms[5]= 'x';
if(st_mode&S_ISGID) {
if(st_mode&S_IXGRP)
perms[5]= 's';
else
perms[5]= 'S';
}
if(st_mode&S_IROTH) perms[6]= 'r';
if(st_mode&S_IWOTH) perms[7]= 'w';
if(st_mode&S_IXOTH) perms[8]= 'x';
if(st_mode&S_ISVTX) {
if(st_mode&S_IXOTH)
perms[8]= 't';
else
perms[8]= 'T';
}
return(1);
}
/* @param flag bit0= long format
bit1= do not print count of nodes
bit2= du format
*/
int Xorriso_ls_filev(struct XorrisO *xorriso, int filec, char **filev,
off_t boss_mem, int flag)
{
int i, ret, was_error= 0;
struct iso_tree_node *node;
struct iso_volume *volume;
char sfe[4*SfileadrL], path[SfileadrL], *rpt, perms[10];
mode_t st_mode;
off_t size;
time_t mtime;
struct tm tms, *tmpt;
static char months[12][4]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
rpt= xorriso->result_line;
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
Sort_argv(filec, filev, 0);
/* Count valid nodes, warn of invalid ones */
for(i= 0; i<filec; i++) {
if(filev[i][0]!='/') {
strcpy(path, xorriso->wdi);
if(Sfile_add_to_path(path, filev[i], 0)<=0) {
much_too_long:;
sprintf(xorriso->info_text,
"Path for file listing gets much too long (%d)",
strlen(path)+strlen(filev[i])+1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
was_error++;
continue;
}
} else
if(Sfile_str(path, filev[i], 0)<=0)
goto much_too_long;
node= iso_tree_volume_path_to_node(volume,path);
if(node==NULL) {
sprintf(xorriso->info_text, "Not found in ISO image: %s",
Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
was_error++;
continue;
}
}
if(!(flag&(2|4))) {
sprintf(xorriso->info_text, "Valid ISO nodes found: %d\n", filec-was_error);
Xorriso_info(xorriso,0);
if(filec-was_error<=0)
return(!was_error);
}
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
if(filev[i][0]!='/') {
strcpy(path, xorriso->wdi);
if(Sfile_add_to_path(path, filev[i], 0)<=0)
continue;
} else
if(Sfile_str(path, filev[i], 0)<=0)
continue;
node= iso_tree_volume_path_to_node(volume,path);
if(node==NULL)
continue;
rpt[0]= 0;
if((flag&5)==1) { /* -ls_l */
if(LIBISO_ISDIR(node))
strcat(rpt, "d");
else if(LIBISO_ISREG(node))
strcat(rpt, "-");
else if(LIBISO_ISLNK(node))
strcat(rpt, "l");
else
strcat(rpt, "?");
st_mode= iso_tree_node_get_permissions(node);
Xorriso__mode_to_perms(st_mode, perms, 0);
strcat(rpt, perms);
/* >>> With directories this should be : number of subdirs + 2 */
/* >>> ??? How to obtain RR hardlink number for other types ? */
strcat(rpt," 1 ");
sprintf(rpt+strlen(rpt), "%-8lu ",
(unsigned long) iso_tree_node_get_uid(node));
sprintf(rpt+strlen(rpt), "%-8lu ",
(unsigned long) iso_tree_node_get_gid(node));
size= iso_tree_node_get_size(node);
sprintf(rpt+strlen(rpt), "%8.f ",(double) size);
mtime= iso_tree_node_get_mtime(node);
tmpt= localtime_r(&mtime, &tms);
if(tmpt==0)
sprintf(rpt+strlen(rpt), "%12.f ",(double) mtime);
else if(time(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
sprintf(rpt+strlen(rpt), "%3s %2d %2.2d:%2.2d ",
months[tms.tm_mon], tms.tm_mday, tms.tm_hour, tms.tm_min);
else
sprintf(rpt+strlen(rpt), "%3s %2d %4.4d ",
months[tms.tm_mon], tms.tm_mday, 1900+tms.tm_year);
} else if(flag&4) { /* -du or -du_s */
if(LIBISO_ISDIR(node)) {
ret= Xorriso_show_du_subs(xorriso, (struct iso_tree_node_dir *) node,
path, filev[i], &size, boss_mem, flag&1);
if(ret<0)
return(-1);
if(ret==0)
continue;
}
sprintf(rpt, "%7.f ",(double) (size/1024));
}
sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n",
Text_shellsafe(filev[i], sfe, 0));
Xorriso_result(xorriso, 0);
}
return(!was_error);
}
/* This function needs less buffer memory than Xorriso_ls_filev() but cannot
perform structured pattern matching.
@param flag bit0= long format
bit1= only check for directory existence
bit2= do not apply search pattern but accept any file
*/
int Xorriso_ls(struct XorrisO *xorriso, int flag)
{
int ret, is_dir= 0, i, filec= 0, failed_at, no_sort= 0;
struct iso_tree_node *node, **node_array= NULL;
struct iso_tree_node_dir *dir_node;
struct iso_volume *volume;
struct iso_tree_iter *iter= NULL;
char sfe[4*SfileadrL], *npt, *rpt, perms[10];
mode_t st_mode;
off_t size;
time_t mtime;
struct tm tms, *tmpt;
static char months[12][4]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
rpt= xorriso->result_line;
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
ret= Xorriso_node_from_path(xorriso, volume, xorriso->wdi, &node, 0);
if(ret<=0)
goto wdi_is_not_a_dir;
if(LIBISO_ISDIR(node))
is_dir= 1;
if(!is_dir) {
wdi_is_not_a_dir:;
sprintf(xorriso->info_text,
"Working directory path does not lead to a directory in ISO image");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
if(flag&2)
{ret= 1; goto ex;}
dir_node= (struct iso_tree_node_dir *) node;
iter= iso_tree_node_children(dir_node);
Xorriso_process_msg_queues(xorriso,0);
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL; ) {
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
filec++;
}
/* Reset iteration */
iso_tree_iter_free(iter);
iter= NULL;
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Valid ISO nodes found: %d\n", filec);
Xorriso_info(xorriso,0);
ret= Xorriso_sorted_node_array(xorriso, dir_node, &filec, &node_array, 0,
flag&4);
if(ret<0)
goto ex;
if(ret==0) {
no_sort= 1;
iter= iso_tree_node_children(dir_node);
}
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
if(no_sort) {
node= iso_tree_iter_next(iter);
if(node==NULL)
break;
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
} else
node= node_array[i];
npt= (char *) iso_tree_node_get_name(node);
rpt[0]= 0;
if(flag&1) {
if(LIBISO_ISDIR(node))
strcat(rpt, "d");
else if(LIBISO_ISREG(node))
strcat(rpt, "-");
else if(LIBISO_ISLNK(node))
strcat(rpt, "l");
else
strcat(rpt, "?");
st_mode= iso_tree_node_get_permissions(node);
Xorriso__mode_to_perms(st_mode, perms, 0);
strcat(rpt, perms);
/* >>> With directories this should be : number of subdirs + 2 */
/* >>> ??? How to obtain RR hardlink number for other types ? */
strcat(rpt," 1 ");
sprintf(rpt+strlen(rpt), "%-8lu ",
(unsigned long) iso_tree_node_get_uid(node));
sprintf(rpt+strlen(rpt), "%-8lu ",
(unsigned long) iso_tree_node_get_gid(node));
size= iso_tree_node_get_size(node);
sprintf(rpt+strlen(rpt), "%8.f ",(double) size);
mtime= iso_tree_node_get_mtime(node);
tmpt= localtime_r(&mtime, &tms);
if(tmpt==0)
sprintf(rpt+strlen(rpt), "%12.f ",(double) mtime);
else if(time(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
sprintf(rpt+strlen(rpt), "%3s %2d %2.2d:%2.2d ",
months[tms.tm_mon], tms.tm_mday, tms.tm_hour, tms.tm_min);
else
sprintf(rpt+strlen(rpt), "%3s %2d %4.4d ",
months[tms.tm_mon], tms.tm_mday, 1900+tms.tm_year);
}
sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n",
Text_shellsafe(npt, sfe, 0));
Xorriso_result(xorriso, 0);
}
ret= 1;
ex:;
if(iter!=NULL)
iso_tree_iter_free(iter);
Xorriso_process_msg_queues(xorriso,0);
if(node_array!=NULL)
free((char *) node_array);
return(1);
}
int Xorriso_rename(struct XorrisO *xorriso, char *origin, char *dest, int flag)
{
int ret, ol, dest_ret;
char sfe[4*SfileadrL], eff_dest[SfileadrL], dir_adr[SfileadrL], *cpt;
char *leafname, eff_origin[SfileadrL], sfe2[4*SfileadrL];
struct iso_volume *volume;
struct iso_tree_node_dir *origin_dir, *dest_dir;
struct iso_tree_node *node;
ret= Xorriso_normalize_img_path(xorriso, origin, eff_origin, 0);
if(ret<=0)
return(ret);
dest_ret= Xorriso_normalize_img_path(xorriso, dest, eff_dest, 1);
if(dest_ret<0)
return(dest_ret);
if(dest_ret==0) { /* obtain eff_dest address despite it does not exist */
ret= Xorriso_normalize_img_path(xorriso, dest, eff_dest, 2);
if(ret<=0)
return(ret);
}
/* Prevent that destination is a subordinate of origin
(that would be a black hole plopping out of the universe) */
ol= strlen(eff_origin);
if(ol==0) {
sprintf(xorriso->info_text, "May not rename root directory");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if(strcmp(eff_origin, eff_dest)==0) {
sprintf(xorriso->info_text, "Ignored attempt to rename %s to itself",
Text_shellsafe(eff_origin,sfe,0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
return(0);
} else if(strncmp(eff_origin, eff_dest, ol)==0 &&
(eff_dest[ol]==0 || eff_dest[ol]=='/')) {
sprintf(xorriso->info_text,
"May not rename %s to its own sub address %s",
Text_shellsafe(eff_origin,sfe,0), Text_shellsafe(eff_dest,sfe2,0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
/* Check whether destination exists and may be not overwriteable */
if(dest_ret==2) {
sprintf(xorriso->info_text, "Renaming refuses to unlink directory: %s",
Text_shellsafe(eff_dest, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if (dest_ret==1 && !xorriso->do_overwrite) {
sprintf(xorriso->info_text, "Renaming may not overwite: %s",
Text_shellsafe(eff_dest, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if(dest_ret>0) {
ret= Xorriso_rmi(xorriso, NULL, eff_dest, 8);
if(ret<=0)
return(0);
if(ret==3) {
sprintf(xorriso->info_text, "User revoked renaming of: %s",
Text_shellsafe(eff_origin, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
return(0);
}
}
/* Ensure existence of destination directory */
strcpy(dir_adr, eff_dest);
cpt= strrchr(dir_adr, '/');
if(cpt==NULL)
cpt= dir_adr+strlen(dir_adr);
*cpt= 0;
if(dir_adr[0]!=0) {
ret= Xorriso_graft_in(xorriso, NULL, dir_adr, 1);
if(ret<=0)
return(ret);
}
/* Move node */
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
dest_dir= (struct iso_tree_node_dir *)
iso_tree_volume_path_to_node(volume,dir_adr);
strcpy(dir_adr, eff_origin);
cpt= strrchr(dir_adr, '/');
if(cpt==NULL)
cpt= dir_adr+strlen(dir_adr);
*cpt= 0;
origin_dir= (struct iso_tree_node_dir *)
iso_tree_volume_path_to_node(volume,dir_adr);
node= iso_tree_volume_path_to_node(volume,eff_origin);
if(dest_dir==NULL || origin_dir==NULL || node==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Internal error on rename: confirmed node turns out as NULL");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
ret= iso_tree_node_take(origin_dir, node);
if(ret==-1) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Internal error on rename: failed to take node");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
iso_tree_add_child(dest_dir, node);
leafname= strrchr(eff_dest, '/');
if(leafname==NULL)
leafname= eff_dest;
else
leafname++;
iso_tree_node_set_name(node, leafname);
Xorriso_process_msg_queues(xorriso,0);
return(1);
}
/* @param flag bit0= do not produce info message on success
@return 1=success,
0=was already directory, -1=was other type, -2=other error
*/
int Xorriso_mkdir(struct XorrisO *xorriso, char *path, int flag)
{
int ret;
char eff_path[SfileadrL], sfe[4*SfileadrL];
ret= Xorriso_normalize_img_path(xorriso, path, eff_path, 1);
if(ret<0)
return(-2);
if(ret>0) {
sprintf(xorriso->info_text,"-mkdir: Address already existing %s",
Text_shellsafe(eff_path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
(ret==2 ? "WARNING" : "SORRY"), 0);
return(-1+(ret==2));
}
ret= Xorriso_normalize_img_path(xorriso, path, eff_path, 2);
if(ret<0)
return(-2);
ret= Xorriso_graft_in(xorriso, NULL, eff_path, 1);
if(ret<=0)
return(-2);
if(!(flag&1)) {
sprintf(xorriso->info_text, "Created directory in ISO image: %s\n",
Text_shellsafe(eff_path,sfe,0));
Xorriso_info(xorriso, 0);
}
return(1);
}
/* @param flag bit0= count results rather than storing them
bit1= this is a recursion
bit2= prepend /
*/
int Xorriso_obtain_pattern_files(
struct XorrisO *xorriso, char *wd, struct iso_tree_node_dir *dir,
int *filec, char **filev, int count_limit, off_t *mem, int flag)
{
int i, ret, failed_at, l;
struct iso_tree_iter *iter= NULL;
struct iso_tree_node *node;
char adr[SfileadrL], *name;
if(xorriso->re_fill==0) { /* This is the empty pattern representing root */
if(flag&1) {
*filec++;
(*mem)+= 8;
return(1);
} else {
if(*filec >= count_limit)
goto unexpected_change;
filev[*filec]= strdup("/");
if(filev[*filec]==NULL)
goto out_of_memory;
(*filec)++;
return(1);
}
}
iter= iso_tree_node_children(dir);
if(iter==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text, "Cannot obtain ISO directory iterator");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL; ) {
name= (char *) iso_tree_node_get_name(node);
if(wd[0]!=0 || (flag&4)) {
if(strlen(wd)+1>=SfileadrL)
goto much_too_long;
strcpy(adr, wd);
if(Sfile_add_to_path(adr, name, 0)<=0) {
much_too_long:;
sprintf(xorriso->info_text,
"Path for pattern matching gets much too long (%d)",
strlen(adr)+strlen(name)+1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
} else {
if(strlen(name)+1>=SfileadrL)
goto much_too_long;
strcpy(adr, name);
}
ret= Xorriso_regexec(xorriso, adr, &failed_at, 0);
if(ret) { /* no match */
if(!LIBISO_ISDIR(node))
continue;
/* dive deeper */
ret= Xorriso_obtain_pattern_files(
xorriso, adr, (struct iso_tree_node_dir *) node,
filec, filev, count_limit, mem, flag|2);
if(ret<=0)
return(ret);
} else {
if(flag&1) {
(*filec)++;
l= strlen(adr)+1;
(*mem)+= sizeof(char *)+l;
if(l % sizeof(char *))
(*mem)+= sizeof(char *)-(l % sizeof(char *));
} else {
if(*filec >= count_limit) {
unexpected_change:;
sprintf(xorriso->info_text,
"Number of matching files changed unexpectedly (> %d)",
count_limit);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
filev[*filec]= strdup(adr);
if(filev[*filec]==NULL) {
out_of_memory:;
sprintf(xorriso->info_text,
"Cannot allocate enough memory (%d bytes) for pattern expansion",
strlen(adr)+1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
return(-1);
}
(*filec)++;
}
}
}
return(1);
}
int Xorriso_expand_pattern(struct XorrisO *xorriso,
int num_patterns, char **patterns,
int *filec, char ***filev, off_t *mem, int flag)
{
int ret, count= 0, abs_adr= 0, i, l, was_count, was_filec;
char mem_text[80], limit_text[80], sfe[4*SfileadrL];
struct iso_volume *volume;
struct iso_tree_node_dir *dir, *root_dir;
*filec= 0;
*filev= NULL;
xorriso->search_mode= 3;
xorriso->structured_search= 1;
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(ret);
root_dir= iso_volume_get_root(volume);
if(root_dir==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"While expanding pattern : Cannot obtain root node of ISO image");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
ret= -1; goto ex;
}
for(i= 0; i<num_patterns; i++) {
ret= Xorriso_prepare_regex(xorriso, patterns[i], 1|2);
if(ret==2) {
ret= Xorriso_prepare_regex(xorriso, patterns[i], 0);
abs_adr= 4;
}
if(ret<=0) {
cannot_compile:;
sprintf(xorriso->info_text,
"Cannot compile pattern to regular expression: %s", patterns[i]);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
if(patterns[i][0]=='/' || abs_adr) {
dir= root_dir;
abs_adr= 4;
} else {
/* This is done so late ito allow the following:
It is not an error if xorriso->wdi does not exist yet, but one may
not use it as base for relative address searches.
*/
dir= (struct iso_tree_node_dir *)
iso_tree_volume_path_to_node(volume,xorriso->wdi);
if(dir==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"While expanding pattern %s : Working directory does not exist in ISO image",
Text_shellsafe(patterns[i], sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
if(!LIBISO_ISDIR((struct iso_tree_node *) dir)) {
sprintf(xorriso->info_text,
"Working directory path does not lead to a directory in ISO image");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
}
/* count the matches */
was_count= count;
ret= Xorriso_obtain_pattern_files(xorriso, "", dir, &count, NULL, 0, mem,
1 | abs_adr);
if(ret<=0)
goto ex;
if(was_count==count && strcmp(patterns[i],"*")!=0) {
count++;
l= strlen(patterns[i])+1;
(*mem)+= sizeof(char *)+l;
if(l % sizeof(char *))
(*mem)+= sizeof(char *)-(l % sizeof(char *));
}
}
if(count<=0)
{ret= 0; goto ex;}
Sfile_scale((double) *mem, mem_text,5,1e4,0);
sprintf(xorriso->info_text,
"Temporary memory needed for pattern expansion : %s", mem_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
if(*mem > xorriso->temp_mem_limit) {
Sfile_scale((double) xorriso->temp_mem_limit, limit_text,5,1e4,1);
sprintf(xorriso->info_text,
"List of matching file addresses exceeds -temp_mem_limit (%s > %s)",
mem_text, limit_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
ret= 0; goto ex;
}
(*filev)= (char **) calloc(sizeof(char *), count);
if(*filev==NULL) {
no_memory:;
Sfile_scale((double) *mem, mem_text,5,1e4,1);
sprintf(xorriso->info_text,
"Cannot allocate enough memory (%s) for pattern expansion",
mem_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
ret= -1; goto ex;
}
/* now store addresses */
for(i= 0; i<num_patterns; i++) {
ret= Xorriso_prepare_regex(xorriso, patterns[i], 1|2);
if(ret==2) {
ret= Xorriso_prepare_regex(xorriso, patterns[i], 0);
abs_adr= 4;
}
if(ret<=0)
goto cannot_compile;
was_filec= *filec;
ret= Xorriso_obtain_pattern_files(xorriso, "", dir, filec, *filev, count,
mem, abs_adr);
if(ret<=0)
goto ex;
if(was_filec == *filec && strcmp(patterns[i],"*")!=0) {
(*filev)[*filec]= strdup(patterns[i]);
if((*filev)[*filec]==NULL) {
(*mem)= strlen(patterns[i])+1;
goto no_memory;
}
(*filec)++;
}
}
ret= 1;
ex:;
if(ret<=0) {
if(filev!=NULL)
Sfile_destroy_argv(&count, filev, 0);
*filec= 0;
}
return(ret);
}
int Xorriso_set_st_mode(struct XorrisO *xorriso, char *in_path,
mode_t mode_and, mode_t mode_or, int flag)
{
mode_t mode= 0;
int ret;
struct iso_tree_node *node;
char sfe[4*SfileadrL], path[SfileadrL];
ret= Xorriso_get_node_by_path(xorriso, in_path, path, &node, 0);
if(ret<=0)
return(ret);
mode= iso_tree_node_get_permissions(node);
mode= (mode & mode_and) | mode_or;
iso_tree_node_set_permissions(node, mode);
iso_tree_node_set_ctime(node, time(NULL));
sprintf(xorriso->info_text,"Permissions now: %-5.5o %s",
mode, Text_shellsafe(path, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
xorriso->volset_change_pending= 1;
return(1);
}
int Xorriso_set_uid(struct XorrisO *xorriso, char *in_path, uid_t uid,
int flag)
{
int ret;
struct iso_tree_node *node;
ret= Xorriso_get_node_by_path(xorriso, in_path, NULL, &node, 0);
if(ret<=0)
return(ret);
iso_tree_node_set_uid(node, uid);
iso_tree_node_set_ctime(node, time(NULL));
xorriso->volset_change_pending= 1;
return(1);
}
int Xorriso_set_gid(struct XorrisO *xorriso, char *in_path, gid_t gid,
int flag)
{
int ret;
struct iso_tree_node *node;
ret= Xorriso_get_node_by_path(xorriso, in_path, NULL, &node, 0);
if(ret<=0)
return(ret);
iso_tree_node_set_gid(node, gid);
iso_tree_node_set_ctime(node, time(NULL));
xorriso->volset_change_pending= 1;
return(1);
}
/* @parm flag bit0= atime, bit1= ctime, bit2= mtime, bit8=no auto ctime */
int Xorriso_set_time(struct XorrisO *xorriso, char *in_path, time_t t,
int flag)
{
int ret;
struct iso_tree_node *node;
ret= Xorriso_get_node_by_path(xorriso, in_path, NULL, &node, 0);
if(ret<=0)
return(ret);
if(flag&1)
iso_tree_node_set_atime(node, t);
if(flag&2)
iso_tree_node_set_ctime(node, t);
if(flag&4)
iso_tree_node_set_mtime(node, t);
if(!(flag&(2|256)))
iso_tree_node_set_ctime(node, time(NULL));
xorriso->volset_change_pending= 1;
return(1);
}