libisoburn/test/test.c

278 lines
6.1 KiB
C

/*
Little test program for libisoburn.
It grows an iso filesystem on a disc.
Copyright 2007 Vreixo Formoso Lopes <metalpain2002@yahoo.es>
and Thomas Schmitt <scdbackup@gmx.net>
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <string.h>
#include <libisofs/libisofs.h>
#include <libburn/libburn.h>
#include "../src/libisoburn.h"
const char * const optstring = "JRh";
extern char *optarg;
extern int optind;
/** Activates the usage of function graft_point() rather than
plain iso_tree_radd_dir() from libisofs
*/
#define With_graft_poinT 1
static int graft_point(struct iso_volume *volume, const char *disc_path,
const char *img_path, struct iso_tree_radd_dir_behavior *behav)
{
char path[4096], *apt, *npt;
struct iso_tree_node_dir *dir;
struct iso_tree_node *node;
int done= 0;
strncpy(path, img_path, sizeof(path)-1);
path[sizeof(path)-1]= 0;
apt= npt= path;
dir= iso_volume_get_root(volume);
if(dir==NULL) {
fprintf(stderr, "While grafting '%s' : no root node available\n", img_path);
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++;
continue;
}
node= iso_tree_volume_path_to_node(volume,path);
if(node!=NULL) {
if(iso_tree_node_get_type(node)!=LIBISO_NODE_DIR) {
fprintf(stderr, "While grafting '%s' : '%s' is not a directory\n",
img_path, path);
return(0);
}
dir= (struct iso_tree_node_dir *) node;
} else {
dir= iso_tree_add_dir(dir, apt);
if(dir==NULL) {
fprintf(stderr, "While grafting '%s' : could not insert '%s'\n",
img_path, path);
return(0);
}
}
if(done)
iso_tree_radd_dir(dir, path, behav);
else
*npt= '/';
}
return(1);
}
static
void usage()
{
printf("test [OPTIONS] DRIVE DIRECTORY\n");
}
static
void help()
{
printf(
"Options:\n"
" -J Add Joliet support\n"
" -R Add Rock Ridge support\n"
" -h Print this message\n"
);
}
int main(int argc, char **argv)
{
struct burn_drive_info *drives;
struct iso_volset *volset;
struct burn_drive *drive;
struct iso_tree_node_dir *root;
struct burn_disc *disc;
enum burn_disc_status state;
struct isoburn_read_opts ropts;
struct isoburn_source_opts sopts;
int c;
struct iso_tree_radd_dir_behavior behav = {0,0,0};
int flags=0;
int ret=0;
while ((c = getopt(argc, argv, optstring)) != -1) {
switch(c) {
case 'h':
usage();
help();
exit(0);
break;
case 'J':
flags |= ECMA119_JOLIET;
break;
case 'R':
flags |= ECMA119_ROCKRIDGE;
break;
case '?':
usage();
exit(1);
break;
}
}
if (argc < optind + 1) {
fprintf(stderr, "Please supply device name\n");
usage();
exit(1);
}
if (argc < optind + 2) {
fprintf(stderr, "Please supply directory to add to disc\n");
usage();
exit(1);
}
if (!isoburn_initialize()) {
fprintf(stderr, "Can't init libisoburn\n");
exit(1);
}
/* TODO change this. maybe we can add wrapp in libisoburn */
iso_msgs_set_severities("NEVER", "DEBUG", "libisofs : ");
burn_msgs_set_severities("NEVER", "DEBUG", "libburn : ");
burn_set_signal_handling("libisoburn/test/test : ", NULL, 0);
printf("Growing drive %s\n", argv[optind]);
if (isoburn_drive_scan_and_grab(&drives, argv[optind], 1) <= 0) {
fprintf(stderr,
"Can't open device. Are you sure it is a valid drive?\n");
exit(1);
}
drive = drives[0].drive;
/* check for invalid state */
state = isoburn_disc_get_status(drive);
if (state != BURN_DISC_BLANK && state != BURN_DISC_APPENDABLE) {
fprintf(stderr, "Unsuitable disc status\n");
goto exit_cleanup;
}
/* 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) {
fprintf(stderr, "Can't read volset\n");
goto exit_cleanup;
}
#ifdef With_graft_poinT
if (graft_point(iso_volset_get_volume(volset, 0),
argv[optind+1], argv[optind+1], &behav) <= 0) {
fprintf(stderr, "Canot graft '%s'\n", argv[optind+1]);
goto exit_cleanup;
}
#else
root = iso_volume_get_root(iso_volset_get_volume(volset, 0));
/* add a new dir */
iso_tree_radd_dir(root, argv[optind+1], &behav);
#endif /* ! With_graft_poinT */
sopts.level = 2;
sopts.flags = flags;
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.replace_file_mode = 0;
sopts.replace_uid = 0;
sopts.replace_gid = 0;
sopts.dir_mode = 0555;
sopts.file_mode = 0444;
sopts.gid = 0;
sopts.uid = 0;
sopts.input_charset = NULL;
sopts.ouput_charset = NULL;
if (isoburn_prepare_disc(drive, &disc, &sopts) <= 0) {
fprintf(stderr, "Can't prepare disc\n");
goto volset_cleanup;
}
/* a. write the new image */
printf("Adding new data...\n");
{
struct burn_write_opts *burn_options;
struct burn_progress progress;
char reasons[BURN_REASONS_LEN];
burn_options = burn_write_opts_new(drive);
burn_drive_set_speed(drive, 0, 0);
burn_write_opts_set_underrun_proof(burn_options, 1);
if (burn_write_opts_auto_write_type(burn_options, disc,
reasons, 0) == BURN_WRITE_NONE) {
fprintf(stderr,
"Failed to find a suitable write mode:\n%s\n", reasons);
ret = 1;
goto volset_cleanup;
}
/* ok, write the new track */
isoburn_disc_write(burn_options, disc);
burn_write_opts_free(burn_options);
while (burn_drive_get_status(drive, NULL) == BURN_DRIVE_SPAWNING)
usleep(100002);
while (burn_drive_get_status(drive, &progress) != BURN_DRIVE_IDLE) {
printf("Writing: sector %d of %d\n", progress.sector, progress.sectors);
sleep(1);
}
}
/* b. write the new vol desc */
printf("Writing the new vol desc...\n");
if (isoburn_activate_session(drive) <= 0) {
fprintf(stderr, "Ups, new vol desc write failed\n");
}
volset_cleanup:;
/*
iso_volset_free(volset);
*/
exit_cleanup:;
isoburn_drive_release(drive, 0);
isoburn_finish();
exit(ret);
}