Gave simple -ls an implemention with minimal memory consumption

This commit is contained in:
Thomas Schmitt 2007-10-31 16:55:55 +00:00
parent 43ffc8f19f
commit 44a3b060ec
4 changed files with 80 additions and 42 deletions

View File

@ -4068,16 +4068,17 @@ no_memory:;
}
if(flag&2) {
ret= Xorriso_ls_filev(xorriso, nump, argv + (*idx), flag&1);
if(ret<=0)
{ret= 0; goto ex;}
} else if(nump==1 && strcmp(patterns[0],"*")==0){
/* save temporary memory by calling simpler function */
ret= Xorriso_ls(xorriso, (flag&1)|4);
} else {
ret= Xorriso_expand_pattern(xorriso, nump, patterns, &filec, &filev, 0);
if(ret<=0)
{ret= 0; goto ex;}
ret= Xorriso_ls_filev(xorriso, filec, filev, flag&1);
}
if(ret<=0)
{ret= 0; goto ex;}
}
ret= 1;
ex:;

View File

@ -1 +1 @@
#define Xorriso_timestamP "2007.10.31.103338"
#define Xorriso_timestamP "2007.10.31.165413"

View File

@ -1358,8 +1358,6 @@ much_too_long:;
}
#ifdef Xorriso_lsi_outdated_unstructured_patterN
int Xorriso__node_name_cmp(const void *node1, const void *node2)
{
char *name1, *name2;
@ -1370,17 +1368,22 @@ int Xorriso__node_name_cmp(const void *node1, const void *node2)
}
/* @param flag bit0= long format , bit1= only check for directory existence */
/* This function needs less buffer memory than Xorriso_ls_filev() but cannot
perform structured pattern matching.
@param flag bit0= long format
bit1= only check for directory existence
bit2= do not apply search pattern but accept any file
*/
int Xorriso_ls(struct XorrisO *xorriso, int flag)
{
int ret, is_dir= 0, i, filec, failed_at;
int ret, is_dir= 0, i, filec= 0, failed_at, no_sort= 0;
struct iso_tree_node *node, **node_array= NULL;
struct iso_tree_node_dir *dir_node;
struct iso_volume *volume;
struct iso_tree_iter *iter= NULL;
char sfe[4*SfileadrL], *npt, *rpt, perms[10];
char sfe[4*SfileadrL], *npt, *rpt, perms[10], mem_text[80], limit_text[80];
mode_t st_mode;
off_t size;
off_t size, mem= 0;
time_t mtime;
struct tm tms, *tmpt;
static char months[12][4]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
@ -1413,17 +1416,38 @@ wdi_is_not_a_dir:;
for(i= 0; (node= iso_tree_iter_next(iter)) != NULL; ) {
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
filec++;
}
/* Reset iteration */
iso_tree_iter_free(iter);
iter= NULL;
iter= iso_tree_node_children(dir_node);
Xorriso_process_msg_queues(xorriso,0);
node_array= calloc(sizeof(struct iso_tree *), filec+1);
mem= (filec+1)*sizeof(struct iso_tree_node *);
Sfile_scale((double) mem, mem_text,5,1e4,0);
sprintf(xorriso->info_text,
"Temporary memory needed for result sorting : %s", mem_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
if(mem > xorriso->temp_mem_limit) {
Sfile_scale((double) xorriso->temp_mem_limit, limit_text,5,1e4,1);
sprintf(xorriso->info_text,
"Cannot sort. List of matching files exceeds -temp_mem_limit (%s > %s)",
mem_text, limit_text);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
no_sort= 1;
}
sprintf(xorriso->info_text, "Valid ISO nodes found: %d\n", filec);
Xorriso_info(xorriso,0);
if(!no_sort) {
node_array= calloc(sizeof(struct iso_tree_node *), filec+1);
if(node_array==NULL) {
sprintf(xorriso->info_text,
"Cannot allocate memory for %d directory entries", filec);
@ -1431,28 +1455,35 @@ wdi_is_not_a_dir:;
ret= -1; goto ex;
}
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; ) {
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
node_array[i++]= node;
}
iso_tree_iter_free(iter);
iter= NULL;
Xorriso_process_msg_queues(xorriso,0);
filec= i;
if(filec<=0)
{ret= 1; goto ex;}
qsort(node_array, filec, sizeof(struct iso_tree *), Xorriso__node_name_cmp);
}
for(i= 0; i<filec; i++) {
if(no_sort) {
node= iso_tree_iter_next(iter);
if(node==NULL)
break;
npt= (char *) iso_tree_node_get_name(node);
if(!(flag&4)) {
ret= Xorriso_regexec(xorriso, npt, &failed_at, 0);
if(ret)
continue; /* no match */
}
} else
node= node_array[i];
npt= (char *) iso_tree_node_get_name(node);
rpt[0]= 0;
@ -1516,8 +1547,6 @@ ex:;
return(1);
}
#endif /* Xorriso_lsi_outdated_unstructured_patterN */
int Xorriso_rename(struct XorrisO *xorriso, char *origin, char *dest, int flag)
{
@ -1856,7 +1885,7 @@ cannot_compile:;
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) {
if(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)",

View File

@ -59,6 +59,14 @@ int Xorriso_rmi(struct XorrisO *xorriso, char *path, int flag);
int Xorriso_ls_filev(struct XorrisO *xorriso, int filec, char **filev,
int flag);
/* This function needs less buffer memory than Xorriso_ls_filev() but cannot
perform structured pattern matching.
@param flag bit0= long format
bit1= only check for directory existence
bit2= do not apply search pattern but accept any file
*/
int Xorriso_ls(struct XorrisO *xorriso, int flag);
/* @param eff_path returns resulting effective path.
Must provide at least SfileadrL bytes of storage.
@param flag bit0= do not produce problem events (unless faulty path format)