New options -external_filter , -unregister_filter, -set_filter , -set_filter_r

This commit is contained in:
2009-04-02 16:25:33 +00:00
parent 25bc0d4101
commit e3eaea1a4e
8 changed files with 609 additions and 102 deletions

View File

@ -490,6 +490,7 @@ int Xorriso_assert_volid(struct XorrisO *xorriso, int msc1, int flag)
"-assert_volid: Cannot determine Volume Id at LBA %d.", msc1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
xorriso->assert_volid_sev, 0);
return(0);
}
ret= Sregex_match(xorriso->assert_volid, volid, 0);
if(ret < 0)
@ -6127,6 +6128,8 @@ ex:;
free(adr);
if(flag&2)
(*dive_count)--;
if(iter != NULL)
iso_dir_iter_free(iter);
return(ret);
}
@ -6600,8 +6603,7 @@ int Xorriso_findi_action(struct XorrisO *xorriso, struct FindjoB *job,
ret= Xorriso_path_setfattr(xorriso, (void *) node, show_path,
target, strlen(text_2), text_2, 0);
} else if(action == 28) {
ret= Xorriso_set_filter(xorriso, (void *) node, show_path,
target, text_2, 1 | 2);
ret= Xorriso_set_filter(xorriso, (void *) node, show_path, target, 1 | 2);
} else { /* includes : 15 in_iso */
sprintf(xorriso->result_line, "%s\n", Text_shellsafe(show_path, sfe, 0));
Xorriso_result(xorriso, 0);
@ -9612,48 +9614,150 @@ ex:;
}
struct Xorriso_extF {
int flag; /* unused yet */
IsoExternalFilterCommand *cmd;
};
int Xorriso_extf_destroy(struct XorrisO *xorriso, struct Xorriso_extF **filter,
int flag);
/* @param flag see struct Xorriso_extF.flag */
int Xorriso_extf_new(struct XorrisO *xorriso, struct Xorriso_extF **filter,
char *path, int argc, char **argv, int behavior,
char *suffix, char *name, int flag)
{
int i;
struct Xorriso_extF *o= NULL;
IsoExternalFilterCommand *cmd;
*filter= o= calloc(sizeof(struct Xorriso_extF), 1);
if(o == NULL)
goto failure;
o->flag= flag;
o->cmd= NULL;
o->cmd= cmd= calloc(sizeof(IsoExternalFilterCommand), 1);
if(cmd == NULL)
goto failure;
cmd->version= 0;
cmd->refcount= 0;
cmd->name= NULL;
cmd->path= NULL;
cmd->argv= NULL;
cmd->argc= argc + 1;
cmd->behavior= behavior;
cmd->suffix= NULL;
cmd->suffix= strdup(suffix);
if(cmd->suffix == NULL)
goto failure;
cmd->path= strdup(path);
if(cmd->path == NULL)
goto failure;
cmd->argv= calloc(sizeof(char *), argc + 2);
if(cmd->argv == NULL)
goto failure;
for(i= 0; i < argc + 2; i++)
cmd->argv[i]= NULL;
cmd->argv[0]= strdup(path);
if(cmd->argv[0] == NULL)
goto failure;
for(i= 0; i < argc; i++) {
cmd->argv[i + 1]= strdup(argv[i]);
if(cmd->argv[i] == NULL)
goto failure;
}
cmd->name= strdup(name);
if(cmd->name == NULL)
goto failure;
return(1);
failure:;
Xorriso_extf_destroy(xorriso, filter, 0);
return(-1);
}
int Xorriso_extf_destroy(struct XorrisO *xorriso, struct Xorriso_extF **filter,
int flag)
{
int i;
IsoExternalFilterCommand *cmd;
if(*filter == NULL)
return(0);
cmd= (*filter)->cmd;
if(cmd != NULL) {
if(cmd->refcount > 0)
return(0);
if(cmd->suffix != NULL)
free(cmd->suffix);
if(cmd->argv != NULL) {
for(i= 0; i < cmd->argc; i++)
if(cmd->argv[i] != NULL)
free(cmd->argv[i]);
free((char *) cmd->argv);
}
if(cmd->name != NULL)
free(cmd->name);
}
free((char *) *filter);
*filter= NULL;
return(1);
}
int Xorriso_lookup_extf(struct XorrisO *xorriso, char *name,
struct Xorriso_lsT **found_lst, int flag)
{
struct Xorriso_extF *filter;
struct Xorriso_lsT *lst;
for(lst= xorriso->filters; lst != NULL; lst= Xorriso_lst_get_next(lst, 0)) {
filter= (struct Xorriso_extF *) Xorriso_lst_get_text(lst, 0);
if(strcmp(filter->cmd->name, name) == 0) {
*found_lst= lst;
return(1);
}
}
return(0);
}
int Xorriso_destroy_all_extf(struct XorrisO *xorriso, int flag)
{
struct Xorriso_extF *filter;
struct Xorriso_lsT *lst, *next_lst;
for(lst= xorriso->filters; lst != NULL; lst= next_lst) {
filter= (struct Xorriso_extF *) Xorriso_lst_get_text(lst, 0);
Xorriso_lst_detach_text(lst, 0);
next_lst= Xorriso_lst_get_next(lst, 0);
Xorriso_lst_destroy(&lst, 0);
Xorriso_extf_destroy(xorriso, &filter, 0);
}
xorriso->filters= NULL;
return(1);
}
/*
@pram suffix to be added or stripped if not empty or "-no-suffix-"
@param flag bit0= return 2 if renaming is not possible
bit1= print pacifier messages
*/
int Xorriso_set_filter(struct XorrisO *xorriso, void *in_node,
char *path, char *filter_name, char *suffix, int flag)
char *path, char *filter_name, int flag)
{
int ret, lo= 0, ls= 0, strip_suffix= 0, strip_filter= 0;
IsoNode *node;
IsoFile *file;
struct Xorriso_lsT *found_lst;
struct Xorriso_extF *found_filter;
IsoExternalFilterCommand *cmd = NULL;
char *old_name= NULL, new_name[SfileadrL];
static char *argv_cat[2] = {"cat", NULL};
static IsoExternalFilterCommand cmd_cat =
{0, 0, "/bin/cat", 1, argv_cat, 1};
static char *argv_gzip[2] = {"gzip", NULL};
static IsoExternalFilterCommand cmd_gzip =
{0, 0, "/usr/bin/gzip", 1, argv_gzip, 4};
static char *argv_gunzip[2] = {"gunzip", NULL};
static IsoExternalFilterCommand cmd_gunzip =
{0, 0, "/usr/bin/gunzip", 1, argv_gunzip, 1};
static char *argv_sencrypt[6] =
{"sencrypt", "-e", "-g", "-k", "1QwErTy7", NULL};
static IsoExternalFilterCommand cmd_sencrypt =
{0, 0, "/usr/bin/sencrypt", 5, argv_sencrypt, 1};
static char *argv_sencrypt_d[6] =
{"sencrypt", "-d", "-g", "-k", "1QwErTy7", NULL};
static IsoExternalFilterCommand cmd_sencrypt_d =
{0, 0, "/usr/bin/sencrypt", 5, argv_sencrypt_d, 1};
static char *argv_sencrypt_f[6] =
{"sencrypt", "-e", "-g", "-f", "/var/opt/xorriso/keys/testkey", NULL};
static IsoExternalFilterCommand cmd_sencrypt_f =
{0, 0, "/usr/bin/sencrypt", 5, argv_sencrypt_f, 1};
static char *argv_sencrypt_f_d[6] =
{"sencrypt", "-d", "-g", "-f", "/var/opt/xorriso/keys/testkey", NULL};
static IsoExternalFilterCommand cmd_sencrypt_f_d =
{0, 0, "/usr/bin/sencrypt", 5, argv_sencrypt_f_d, 1};
char *old_name= NULL, new_name[SfileadrL], *suffix= "";
new_name[0]= 0;
@ -9678,36 +9782,30 @@ int Xorriso_set_filter(struct XorrisO *xorriso, void *in_node,
}
file= (IsoFile *) node;
/* >>> lookup cmd by filter_name */
/* <<< this is a dummy for testing */
if(strcmp(filter_name, "-cat") == 0) {
cmd= &cmd_cat;
} else if(strcmp(filter_name, "-gzip") == 0) {
cmd= &cmd_gzip;
} else if(strcmp(filter_name, "-gunzip") == 0) {
cmd= &cmd_gunzip;
strip_suffix= 1;
} else if(strcmp(filter_name, "-sencrypt") == 0) {
cmd= &cmd_sencrypt;
} else if(strcmp(filter_name, "-sencrypt_d") == 0) {
cmd= &cmd_sencrypt_d;
strip_suffix= 1;
} else if(strcmp(filter_name, "-sencrypt_f") == 0) {
cmd= &cmd_sencrypt_f;
} else if(strcmp(filter_name, "-sencrypt_f_d") == 0) {
cmd= &cmd_sencrypt_f_d;
strip_suffix= 1;
} else if(strcmp(filter_name, "--remove-all-filters") == 0) {
if(strncmp(filter_name, "--remove-all-filters", 20) == 0) {
strip_filter= 1;
strip_suffix= 1;
if(strlen(filter_name) > 21) {
strip_suffix= (filter_name[20] != '+');
suffix= filter_name + 21;
}
} else {
strcpy(xorriso->info_text, "-set_filter: Not a registered filter name ");
Text_shellsafe(filter_name, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
ret= Xorriso_lookup_extf(xorriso, filter_name, &found_lst, 0);
if(ret < 0)
goto ex;
if(ret == 0) {
strcpy(xorriso->info_text, "-set_filter: Not a registered filter name ");
Text_shellsafe(filter_name, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
found_filter= (struct Xorriso_extF *) Xorriso_lst_get_text(found_lst, 0);
cmd= found_filter->cmd;
suffix= cmd->suffix;
strip_suffix= cmd->behavior & 8;
}
if(suffix[0] && strcmp(suffix, "--no-suffix") != 0) {
if(suffix[0]) {
old_name= strdup((char *) iso_node_get_name(node));
lo= strlen(old_name);
ls= strlen(suffix);
@ -9753,6 +9851,7 @@ cannot_append_suffix:;
strcpy(xorriso->info_text, "-set_filter: Cannot append suffix to ");
Text_shellsafe(old_name, xorriso->info_text, 1);
strcat(xorriso->info_text, ". Left unfiltered.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
(flag & 1) ? "WARNING" : "FAILURE", 0);
ret= 2 * (flag & 1); goto ex;
@ -9775,6 +9874,7 @@ cannot_append_suffix:;
if(ret != 1)
break;
}
ret= 1;
} else {
ret = iso_file_add_external_filter(file, cmd, 0);
}
@ -9812,3 +9912,153 @@ ex:;
}
/* @param flag bit0= delete filter with the given name
*/
int Xorriso_external_filter(struct XorrisO *xorriso,
char *name, char *options, char *path,
int argc, char **argv, int flag)
{
int ret, delete= 0, behavior= 0, extf_flag= 0;
char *what, *what_next, *suffix= "";
struct Xorriso_lsT *lst;
struct Xorriso_extF *found_filter, *new_filter= NULL;
delete= flag & 1;
ret= Xorriso_lookup_extf(xorriso, name, &lst, 0);
if(ret < 0)
return(ret);
if(ret > 0) {
if(delete) {
found_filter= (struct Xorriso_extF *) Xorriso_lst_get_text(lst, 0);
if(found_filter->cmd->refcount > 0) {
sprintf(xorriso->info_text,
"-external_filter: Cannot remove filter because it is in use by %.f nodes : ",
(double) found_filter->cmd->refcount);
Text_shellsafe(name, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
Xorriso_lst_detach_text(lst, 0);
if(xorriso->filters == lst)
xorriso->filters= Xorriso_lst_get_next(lst, 0);
Xorriso_lst_destroy(&lst, 0);
Xorriso_extf_destroy(xorriso, &found_filter, 0);
ret= 1; goto ex;
}
strcpy(xorriso->info_text,
"-external_filter: filter with given name already existing: ");
Text_shellsafe(name, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
if(delete) {
strcpy(xorriso->info_text,
"-external_filter: filter with given name does not exist: ");
Text_shellsafe(name, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
for(what= options; what!=NULL; what= what_next) {
what_next= strchr(what, ':');
if(what_next!=NULL) {
*what_next= 0;
what_next++;
}
if(strncmp(what, "suffix=", 7) == 0) {
suffix= what + 7;
} else if(strcmp(what, "remove_suffix") == 0) {
behavior|= 8;
} else if(strcmp(what, "if_nonempty") == 0) {
behavior|= 1;
} else if(strcmp(what, "if_reduction") == 0) {
behavior|= 2;
} else if(strcmp(what, "if_block_reduction") == 0) {
behavior|= 4;
} else if(strncmp(what, "used=", 5) == 0) {
; /* this is informational output from -status */
} else if(what[0]) {
strcpy(xorriso->info_text,
"-external_filter: unknown option ");
Text_shellsafe(what, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
}
ret= Xorriso_extf_new(xorriso, &new_filter, path, argc, argv, behavior,
suffix, name, extf_flag);
if(ret <= 0) {
could_not_create:;
strcpy(xorriso->info_text,
"-external_filter: Could not create filter object");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
goto ex;
}
ret= Xorriso_lst_append_binary(&(xorriso->filters), (char *) new_filter,0, 4);
if(ret <= 0)
goto could_not_create;
ret= 1;
ex:;
if(ret <= 0) {
if(new_filter != NULL)
Xorriso_extf_destroy(xorriso, &new_filter, 0);
}
return(ret);
}
int Xorriso_status_extf(struct XorrisO *xorriso, char *filter, FILE *fp,
int flag)
/*
bit1= do only report to fp
*/
{
int i, maxl= 4 * SfileadrL;
struct Xorriso_extF *extf;
struct Xorriso_lsT *lst;
char *line;
line= xorriso->result_line;
for(lst= xorriso->filters; lst != NULL; lst= Xorriso_lst_get_next(lst, 0)) {
extf= (struct Xorriso_extF *) Xorriso_lst_get_text(lst, 0);
strcpy(xorriso->result_line, "-external_filter ");
Text_shellsafe(extf->cmd->name, line, 1);
if(strlen(line) > maxl)
continue;
strcat(line, " ");
if(extf->cmd->suffix[0]) {
strcat(line, "suffix=");
Text_shellsafe(extf->cmd->suffix, line, 1);
if(strlen(line) > maxl)
continue;
strcat(line, ":");
}
if(extf->cmd->behavior & 8)
strcat(line, "remove_suffix:");
if(extf->cmd->behavior & 1)
strcat(line, "if_nonempty:");
if(extf->cmd->behavior & 2)
strcat(line, "if_reduction:");
if(extf->cmd->behavior & 4)
strcat(line, "if_block_reduction:");
sprintf(line + strlen(line), "used=%.f ", (double) extf->cmd->refcount);
if(strlen(line) > maxl)
continue;
Text_shellsafe(extf->cmd->path, line, 1);
if(strlen(line) > maxl)
continue;
for(i= 1; i < extf->cmd->argc; i++) {
strcat(line, " ");
Text_shellsafe(extf->cmd->argv[i], line, 1);
if(strlen(line) > maxl)
break;
}
if(i < extf->cmd->argc)
continue;
strcat(line, " --\n");
Xorriso_status_result(xorriso, filter, fp, flag&2);
}
return(1);
}