Implemented -find option -exec echo, chown, chgrp, chmod, alter_date, lsdl

This commit is contained in:
2007-12-30 19:02:44 +00:00
parent 81a5121d73
commit bc1d8d4092
6 changed files with 451 additions and 17 deletions

View File

@ -2450,6 +2450,24 @@ struct FindjoB {
regmatch_t name_match;
#endif
/* 0= echo
1= rm
2= rm_r
3= mv target
4= chown user
5= chgrp group
6= chmod mode
7= alter_date type date
8= lsdl
*/
int action;
char *target;
uid_t user;
gid_t group;
mode_t mode_and, mode_or;
int type; /* see Xorriso_set_time flag */
time_t date;
};
int Findjob_destroy(struct FindjoB **job, int flag);
@ -2462,7 +2480,14 @@ int Findjob_new(struct FindjoB **o, char *start_path, int flag)
m= *o= TSOB_FELD(struct FindjoB,1);
if(m==NULL)
return(-1);
m->start_path= NULL;
m->name_expr= NULL;
m->action= 0; /* print */
m->target= NULL; /* a mere pointer, not managed memory */
m->user= 0;
m->group= 0;
m->type= 0;
m->date= 0;
m->start_path= strdup(start_path);
if(m->start_path==NULL)
goto failed;
@ -2541,6 +2566,74 @@ int Findjob_test(struct FindjoB *o, char *name,
}
int Findjob_get_action(struct FindjoB *o, int flag)
{
return(o->action);
}
/* @return <0 error, >=0 see above struct FindjoB.action
*/
int Findjob_get_action_parms(struct FindjoB *o, char **target,
uid_t *user, gid_t *group,
mode_t *mode_and, mode_t *mode_or,
int *type, time_t *date, int flag)
{
*target= o->target;
*user= o->user;
*group= o->group;
*mode_and= o->mode_and;
*mode_or= o->mode_or;
*type= o->type;
*date= o->date;
return(o->action);
}
int Findjob_set_action_target(struct FindjoB *o, int action, char *target,
int flag)
{
o->action= action;
o->target= target;
return(1);
}
int Findjob_set_action_chown(struct FindjoB *o, uid_t user,int flag)
{
o->action= 4;
o->user= user;
return(1);
}
int Findjob_set_action_chgrp(struct FindjoB *o, gid_t group, int flag)
{
o->action= 5;
o->group= group;
return(1);
}
int Findjob_set_action_chmod(struct FindjoB *o,
mode_t mode_and, mode_t mode_or, int flag)
{
o->action= 6;
o->mode_and= mode_and;
o->mode_or= mode_or;
return(1);
}
int Findjob_set_action_ad(struct FindjoB *o, int type, time_t date, int flag)
{
o->action= 7;
o->type= type;
o->date= date;
return(1);
}
/* ------------------------------- Xorriso -------------------------------- */
/** The list of startup file names */
@ -4695,6 +4788,136 @@ int Xorriso_convert_gidstring(struct XorrisO *xorriso, char *gid_string,
}
int Xorriso_convert_modstring(struct XorrisO *xorriso, char *cmd, char *mode,
mode_t *mode_and, mode_t *mode_or, int flag)
{
int who_val= 0;
char sfe[5*SfileadrL], *mpt, *vpt, *opt;
unsigned int num= 0;
mode_t mode_val,mask;
*mode_and= ~1;
*mode_or= 0;
if(mode[0]=='0') {
*mode_and= 0;
sscanf(mode,"%o",&num);
*mode_or= num;
} else if(strchr(mode,'+')!=NULL || strchr(mode,'-')!=NULL
|| strchr(mode,'=')!=NULL) {
/* [ugoa][+-][rwxst] */;
for(mpt= mode; mpt!=NULL; mpt= strchr(mpt, ',')) {
if(*mpt==',')
mpt++;
if(strlen(mpt)<3)
goto unrecognizable;
who_val= 0;
for(vpt= mpt; *vpt!='+' && *vpt!='-' && *vpt!='='; vpt++) {
if(*vpt=='u')
who_val|= 4;
else if(*vpt=='g')
who_val|= 2;
else if(*vpt=='o')
who_val|= 1;
else if(*vpt=='a')
who_val|= 7;
else
goto unrecognizable;
}
opt= vpt;
mode_val= 0;
for(vpt= opt+1; *vpt!=0 && *vpt!=','; vpt++) {
if(*vpt=='r') {
if(who_val&4)
mode_val|= S_IRUSR;
if(who_val&2)
mode_val|= S_IRGRP;
if(who_val&1)
mode_val|= S_IROTH;
} else if(*vpt=='w') {
if(who_val&4)
mode_val|= S_IWUSR;
if(who_val&2)
mode_val|= S_IWGRP;
if(who_val&1)
mode_val|= S_IWOTH;
} else if(*vpt=='x') {
if(who_val&4)
mode_val|= S_IXUSR;
if(who_val&2)
mode_val|= S_IXGRP;
if(who_val&1)
mode_val|= S_IXOTH;
} else if(*vpt=='s') {
if(who_val&4)
mode_val|= S_ISUID;
if(who_val&2)
mode_val|= S_ISGID;
} else if(*vpt=='t') {
if(who_val&1)
mode_val|= S_ISVTX;
} else
goto unrecognizable;
}
if(*opt=='+') {
(*mode_or)|= mode_val;
} else if(*opt=='=') {
mask= 0;
if(who_val&1)
mask|= S_IRWXO|S_ISVTX;
if(who_val&2)
mask|= S_IRWXG|S_ISGID;
if(who_val&4)
mask|= S_IRWXU|S_ISUID;
(*mode_and)&= ~(mask);
(*mode_or)= ((*mode_or) & ~mask) | mode_val;
} else if(*opt=='-') {
(*mode_or)&= ~mode_val;
(*mode_and)&= ~mode_val;
}
}
} else {
unrecognizable:;
sprintf(xorriso->info_text,
"%s: Unrecognizable or faulty permission mode %s\n", cmd,
Text_shellsafe(mode, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
return(1);
}
int Xorriso_convert_datestring(struct XorrisO *xorriso, char *cmd,
char *time_type, char *timestring,
int *t_type, time_t *t, int flag)
{
int ret;
if(strcmp(time_type, "a")==0)
(*t_type)|= 1;
else if(strcmp(time_type, "m")==0)
(*t_type)|= 4;
else if(strcmp(time_type, "b")==0)
(*t_type)|= 5;
else {
sprintf(xorriso->info_text, "%s: Unrecognized type '%s'", cmd, time_type);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
ret= Decode_timestring(timestring, t, 0);
if(ret<=0) {
sprintf(xorriso->info_text, "%s: Cannot decode timestring '%s'", cmd,
timestring);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
sprintf(xorriso->info_text, "Understanding timestring '%s' as: %s",
timestring, ctime(t));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
return(1);
}
/* @param flag bit0=path is in source filesystem , bit1= unconditionally */
int Xorriso_much_too_long(struct XorrisO *xorriso, int len, int flag)
{
@ -4708,6 +4931,7 @@ int Xorriso_much_too_long(struct XorrisO *xorriso, int len, int flag)
return(1);
}
/* @param flag bit1= do not report memory usage as DEBUG
*/
int Xorriso_check_temp_mem_limit(struct XorrisO *xorriso, off_t mem, int flag)
@ -5638,6 +5862,8 @@ int Xorriso_option_alter_date(struct XorrisO *xorriso,
int optc= 0;
char **optv= NULL;
#ifdef NIX
if(strcmp(time_type, "a")==0)
t_type|= 1;
else if(strcmp(time_type, "m")==0)
@ -5661,6 +5887,15 @@ int Xorriso_option_alter_date(struct XorrisO *xorriso,
timestring, ctime(&t));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
#else /* NIX */
ret= Xorriso_convert_datestring(xorriso, "-alter_date", time_type, timestring,
&t_type, &t, 0);
if(ret<=0)
goto ex;
#endif /* ! NIX */
ret= Xorriso_opt_args(xorriso, argc, argv, *idx, &end_idx, &optc, &optv, 0);
if(ret<=0)
goto ex;
@ -5911,13 +6146,19 @@ ex:;
int Xorriso_option_chmodi(struct XorrisO *xorriso, char *mode,
int argc, char **argv, int *idx, int flag)
{
int i, ret, who_val= 0, was_failure= 0, end_idx, fret;
unsigned int num;
mode_t mode_and= ~0, mode_or= 0, mode_val, mask;
char sfe[5*SfileadrL], *mpt, *opt, *vpt;
int i, ret, was_failure= 0, end_idx, fret;
mode_t mode_and= ~0, mode_or= 0;
int optc= 0;
char **optv= NULL;
#ifdef NIX
char sfe[5*SfileadrL], *mpt, *opt, *vpt;
unsigned int num;
int who_val= 0;
mode_t mode_val, mask;
if(mode[0]=='0') {
mode_and= 0;
sscanf(mode,"%o",&num);
@ -6004,6 +6245,15 @@ unrecognizable:;
ret= 0; goto ex;
}
#else /* NIX */
ret= Xorriso_convert_modstring(xorriso, "-chmodi",
mode, &mode_and, &mode_or, 0);
if(ret<=0)
goto ex;
#endif /* ! NIX */
ret= Xorriso_opt_args(xorriso, argc, argv, *idx, &end_idx, &optc, &optv, 0);
if(ret<=0)
goto ex;
@ -6435,10 +6685,14 @@ sorry_ex:
int Xorriso_option_find(struct XorrisO *xorriso, int argc, char **argv,
int *idx, int flag)
{
int ret, i, end_idx;
int ret, i, end_idx, type= 0;
struct FindjoB *job= NULL;
char *start_path, sfe[5*SfileadrL];
char *start_path, sfe[5*SfileadrL], *cpt;
struct stat dir_stbuf;
uid_t user= 0;
gid_t group= 0;
time_t date= 0;
mode_t mode_or= 0, mode_and= ~1;
end_idx= Xorriso_end_idx(xorriso, argc, argv, *idx, 1);
start_path= ".";
@ -6453,6 +6707,13 @@ int Xorriso_option_find(struct XorrisO *xorriso, int argc, char **argv,
for(i= *idx+1; i<end_idx; i++) {
if(strcmp(argv[i], "-name")==0) {
if(i+1>=end_idx) {
not_enough_arguments:;
sprintf(xorriso->info_text,
"-find[ix]: not enough arguments with -exec %s",
Text_shellsafe(argv[i], sfe, 0));
goto sorry_ex;
}
i++;
ret= Findjob_set_name_expr(job, argv[i], 0);
if(ret<=0) {
@ -6460,6 +6721,70 @@ int Xorriso_option_find(struct XorrisO *xorriso, int argc, char **argv,
Text_shellsafe(argv[i], sfe, 0));
goto sorry_ex;
}
} else if(strcmp(argv[i], "-exec")==0) {
if(i+1>=end_idx)
goto not_enough_arguments;
i++;
cpt= argv[i];
if(*cpt=='-')
cpt++;
if(strcmp(cpt, "echo")==0) {
Findjob_set_action_target(job, 0, NULL, 0);
#ifdef NIX
/* >>> not implemented yet */;
} else if(strcmp(cpt, "rm")==0) {
Findjob_set_action_target(job, 1, NULL, 0);
} else if(strcmp(cpt, "rm_r")==0) {
Findjob_set_action_target(job, 2, NULL, 0);
} else if(strcmp(cpt, "mv")==0) {
if(i+1>=end_idx)
goto not_enough_arguments;
i++;
Findjob_set_action_target(job, 3, argv[i], 0);
#endif
} else if(strcmp(cpt, "chown")==0) {
if(i+1>=end_idx)
goto not_enough_arguments;
i++;
ret= Xorriso_convert_uidstring(xorriso, argv[i], &user, 0);
if(ret<=0)
goto ex;
Findjob_set_action_chown(job, user, 0);
} else if(strcmp(cpt, "chgrp")==0) {
if(i+1>=end_idx)
goto not_enough_arguments;
i++;
ret= Xorriso_convert_gidstring(xorriso, argv[i], &group, 0);
if(ret<=0)
goto ex;
Findjob_set_action_chgrp(job, group, 0);
} else if(strcmp(cpt, "chmod")==0) {
if(i+1>=end_idx)
goto not_enough_arguments;
i++;
ret= Xorriso_convert_modstring(xorriso, "-find -exec chmod",
argv[i], &mode_and, &mode_or,0);
if(ret<=0)
goto ex;
Findjob_set_action_chmod(job, mode_and, mode_or, 0);
} else if(strcmp(cpt, "alter_date")==0) {
if(i+2>=end_idx)
goto not_enough_arguments;
i+= 2;
ret= Xorriso_convert_datestring(xorriso, "-find -exec alter_date",
argv[i-1], argv[i], &type, &date, 0);
if(ret<=0)
goto ex;
Findjob_set_action_ad(job, type, date, 0);
} else if(strcmp(cpt, "lsdl")==0) {
Findjob_set_action_target(job, 8, NULL, 0);
} else {
sprintf(xorriso->info_text, "-find -exec: unknown action %s",
Text_shellsafe(argv[i], sfe, 0));
goto sorry_ex;
}
} else {
sprintf(xorriso->info_text, "-find[ix]: unknown option %s",
Text_shellsafe(argv[i], sfe, 0));