legacy/libisofs/trunk/test/iso_grow.c
2007-08-28 10:29:55 +00:00

172 lines
3.5 KiB
C

/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
/* vim: set ts=8 sts=8 sw=8 noet : */
#define _GNU_SOURCE
#include "libisofs.h"
#include "libburn/libburn.h"
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <err.h>
#define SECSIZE 2048
const char * const optstring = "JRL:h";
extern char *optarg;
extern int optind;
void usage()
{
printf("test [OPTIONS] DISC DIRECTORY\n");
}
void help()
{
printf(
"Options:\n"
" -J Add Joliet support\n"
" -R Add Rock Ridge support\n"
" -L <num> Set the ISO level (1 or 2)\n"
" -h Print this message\n"
);
}
int main(int argc, char **argv)
{
struct ecma119_source_opts wopts;
struct ecma119_read_opts ropts;
struct data_source *rsrc;
struct iso_volset *volset;
struct iso_volume *volume;
struct iso_tree_node_dir *root;
struct burn_source *wsrc;
unsigned char buf[2048];
int fd;
int c;
int constraints;
struct iso_tree_radd_dir_behavior behav = {0,0,0};
int level=1, flags=0;
char *boot_img = NULL;
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 'L':
level = atoi(optarg);
break;
case '?':
usage();
exit(1);
break;
}
}
if (argc < optind + 1) {
printf ("Please supply device name\n");
usage();
return 1;
}
if (argc < optind + 2) {
printf ("Please supply directory to add to disc\n");
usage();
return 1;
}
if (!iso_init()) {
err(1, "Can't init libisofs");
}
iso_msgs_set_severities("NEVER", "ALL", "");
printf("Reading from %s\n", argv[optind]);
rsrc = data_source_from_file(argv[optind]);
if (rsrc == NULL) {
printf ("Can't open device\n");
return 1;
}
ropts.block = 0; /* image always start on first block */
ropts.norock = 0;
ropts.nojoliet = 0;
ropts.preferjoliet = 0;
ropts.mode = 0555;
ropts.uid = 0;
ropts.gid = 0;
volset = iso_volset_read(rsrc, &ropts);
if (volset == NULL) {
printf ("Error reading image\n");
return 1;
}
printf("Image size: %d blocks.\n", ropts.size);
/* close source, no more needed */
data_source_free(rsrc);
root = iso_volume_get_root(iso_volset_get_volume(volset, 0));
/* add a new dir */
iso_tree_radd_dir(root, argv[optind+1], &behav);
memset(&wopts, 0, sizeof(struct ecma119_source_opts));
wopts.level = level;
wopts.flags = flags;
wopts.relaxed_constraints = 0;
wopts.input_charset = "UTF-8";
wopts.ouput_charset = "UTF-8";
wopts.ms_block = ropts.size;
wopts.dvd_plus_rw = 1;
wsrc = iso_source_new_ecma119(volset, &wopts);
fd = open(argv[optind], O_WRONLY);
if ( fd == -1 ) {
printf ("Can't write to disc\n");
return 1;
}
printf("HERE %d\n", wopts.vol_desc_count);
/* a. write the new image */
printf("Adding new data...\n");
if ( lseek(fd, ropts.size * 2048, SEEK_SET) == (off_t) -1) {
printf ("Can't seek to new location\n");
return 1;
}
while (wsrc->read(wsrc, buf, 2048) == 2048) {
write(fd, buf, 2048);
}
/* b. write the new vol desc */
printf("Writing the new vol desc...\n");
if ( lseek(fd, 16 * 2048, SEEK_SET) == (off_t) -1) {
printf ("Can't seek to 16th sector\n");
return 1;
}
write(fd, wopts.vol_desc, 2048 * wopts.vol_desc_count);
fsync(fd);
close(fd);
iso_finish();
return 0;
}