Structured patterns for options -ls and -ls_l
This commit is contained in:
@ -1237,6 +1237,111 @@ int Xorriso_rmi(struct XorrisO *xorriso, char *path, int flag)
|
||||
}
|
||||
|
||||
|
||||
#ifdef Xorriso_lsi_structured_patterN
|
||||
|
||||
/* @param flag bit0= long format */
|
||||
int Xorriso_ls_filev(struct XorrisO *xorriso, int filec, char **filev,
|
||||
int flag)
|
||||
{
|
||||
int i, ret;
|
||||
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);
|
||||
|
||||
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);
|
||||
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,
|
||||
"Internal error: Unexpectedly missing node %s",
|
||||
Text_shellsafe(path, sfe, 0));
|
||||
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
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);
|
||||
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_IRGRP) perms[3]= 'r';
|
||||
if(st_mode&S_IWGRP) perms[4]= 'w';
|
||||
if(st_mode&S_IXGRP) perms[5]= 'x';
|
||||
if(st_mode&S_IROTH) perms[6]= 'r';
|
||||
if(st_mode&S_IWOTH) perms[7]= 'w';
|
||||
if(st_mode&S_IXOTH) perms[8]= 'x';
|
||||
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(0)-mtime < 180*86400)
|
||||
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(filev[i], sfe, 0));
|
||||
Xorriso_result(xorriso, 0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
#else /* Xorriso_lsi_structured_patterN */
|
||||
|
||||
int Xorriso__node_name_cmp(const void *node1, const void *node2)
|
||||
{
|
||||
char *name1, *name2;
|
||||
@ -1288,7 +1393,7 @@ wdi_is_not_a_dir:;
|
||||
iter= iso_tree_node_children(dir_node);
|
||||
Xorriso_process_msg_queues(xorriso,0);
|
||||
|
||||
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL && i<filec; ) {
|
||||
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL; ) {
|
||||
npt= (char *) iso_tree_node_get_name(node);
|
||||
|
||||
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
|
||||
@ -1393,6 +1498,8 @@ ex:;
|
||||
return(1);
|
||||
}
|
||||
|
||||
#endif /* ! Xorriso_lsi_structured_patterN */
|
||||
|
||||
|
||||
int Xorriso_rename(struct XorrisO *xorriso, char *origin, char *dest, int flag)
|
||||
{
|
||||
@ -1541,3 +1648,208 @@ int Xorriso_mkdir(struct XorrisO *xorriso, char *path, int flag)
|
||||
}
|
||||
|
||||
|
||||
/* @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, char *pattern,
|
||||
int *filec, char ***filev, int flag)
|
||||
{
|
||||
int ret, count= 0, abs_adr= 0;
|
||||
off_t mem= 0;
|
||||
char mem_text[80], limit_text[80], sfe[4*SfileadrL];
|
||||
struct iso_volume *volume;
|
||||
struct iso_tree_node_dir *dir;
|
||||
|
||||
*filec= 0;
|
||||
*filev= NULL;
|
||||
|
||||
xorriso->search_mode= 3;
|
||||
xorriso->structured_search= 1;
|
||||
ret= Xorriso_prepare_regex(xorriso, pattern, 1|2);
|
||||
if(ret==2) {
|
||||
ret= Xorriso_prepare_regex(xorriso, pattern, 0);
|
||||
abs_adr= 4;
|
||||
}
|
||||
if(ret<=0) {
|
||||
sprintf(xorriso->info_text,
|
||||
"Cannot compile pattern to regular expression: %s", pattern);
|
||||
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
|
||||
return(0);
|
||||
}
|
||||
|
||||
ret= Xorriso_get_volume(xorriso, &volume, 0);
|
||||
if(ret<=0)
|
||||
return(ret);
|
||||
|
||||
if(pattern[0]=='/' || abs_adr) {
|
||||
dir= iso_volume_get_root(volume);
|
||||
if(dir==NULL) {
|
||||
Xorriso_process_msg_queues(xorriso,0);
|
||||
sprintf(xorriso->info_text,
|
||||
"While expanding pattern %s : Cannot obtain root node of ISO image",
|
||||
Text_shellsafe(pattern, sfe, 0));
|
||||
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
|
||||
ret= -1; goto ex;
|
||||
}
|
||||
abs_adr= 4;
|
||||
} else {
|
||||
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(pattern, 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 matches */
|
||||
ret= Xorriso_obtain_pattern_files(xorriso, "", dir, &count, NULL, 0, &mem,
|
||||
1 | abs_adr);
|
||||
if(ret<=0)
|
||||
goto ex;
|
||||
if(count<=0)
|
||||
{ret= 0; goto ex;}
|
||||
|
||||
Sfile_scale((double) mem, mem_text,5,1e4,1);
|
||||
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(count*sizeof(char *)+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) {
|
||||
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 store addresses */
|
||||
ret= Xorriso_obtain_pattern_files(xorriso, "", dir, filec, *filev, count,
|
||||
&mem, abs_adr);
|
||||
if(ret<=0)
|
||||
goto ex;
|
||||
|
||||
ret= 1;
|
||||
ex:;
|
||||
if(ret<=0) {
|
||||
if(filev!=NULL)
|
||||
Sfile_destroy_argv(&count, filev, 0);
|
||||
*filec= 0;
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user