Removed more code redundancies and fd leak with Xorriso_obtain_pattern_files_x()

This commit is contained in:
Thomas Schmitt 2007-12-08 17:52:32 +00:00
parent d50079825e
commit 5a7e9ca843
4 changed files with 160 additions and 190 deletions

View File

@ -2013,10 +2013,10 @@ int Dirseq_next_adrblock(struct DirseQ *o, char *replies[], int *reply_count,
int Dirseq_new(struct DirseQ **o, char *adr, int flag) int Dirseq_new(struct DirseQ **o, char *adr, int flag)
/* /*
bit0= do not complain about failed opendir() bit0= with non-fatal errors do not complain about failed opendir()
*/ */
{ {
int ret,i; int ret,i,severe_error;
struct DirseQ *m; struct DirseQ *m;
m= *o= TSOB_FELD(struct DirseQ,1); m= *o= TSOB_FELD(struct DirseQ,1);
@ -2043,9 +2043,11 @@ int Dirseq_new(struct DirseQ **o, char *adr, int flag)
else else
m->dirpt= opendir(adr); m->dirpt= opendir(adr);
if(m->dirpt==NULL) { if(m->dirpt==NULL) {
if(!(flag&1)) severe_error= (errno && errno!=ENOENT && errno!=EACCES && errno!=ENOTDIR);
if(severe_error || !(flag&1))
fprintf(stderr,"opendir(%s) failed : %s\n",adr,strerror(errno)); fprintf(stderr,"opendir(%s) failed : %s\n",adr,strerror(errno));
ret= 0; goto failed; ret= -severe_error;
goto failed;
} }
return(1); return(1);
failed:; failed:;
@ -2679,7 +2681,7 @@ klammer_affe:;
} else { } else {
/* >>> unknown input */ /* >>> unknown input */
sprintf(xorriso->info_text, sprintf(xorriso->info_text,
"--- Please enter one of : empty line, i,n, r,y, q,x, @, @@, @@@\n"); "--- Please enter one of : empty line, i,n, r,y, q,x, @, @@@\n");
Xorriso_info(xorriso,0); Xorriso_info(xorriso,0);
return(3); return(3);
} }
@ -2696,7 +2698,7 @@ klammer_affe:;
} else if(*cpt!=0) { } else if(*cpt!=0) {
Xorriso_dialog_input(xorriso,cpt,strlen(line)+1,2); /* write to history */ Xorriso_dialog_input(xorriso,cpt,strlen(line)+1,2); /* write to history */
sprintf(xorriso->info_text, sprintf(xorriso->info_text,
"--- Please enter one of : empty line, @, @@, @@@\n"); "--- Please enter one of : empty line, @, @@@\n");
Xorriso_info(xorriso,0); Xorriso_info(xorriso,0);
return(3); return(3);
} }
@ -3669,12 +3671,13 @@ int Xorriso_check_for_root_pattern(struct XorrisO *xorriso,
} }
/* @param flag bit2= prepend wd (automatically done if wd[0]!=0) /* @param flag bit0= prepend wd only if name does not begin by '/'
bit2= prepend wd (automatically done if wd[0]!=0)
*/ */
int Xorriso_make_pattern_adr(struct XorrisO *xorriso, char *wd, char *name, int Xorriso_make_pattern_adr(struct XorrisO *xorriso, char *wd, char *name,
char adr[], int flag) char adr[], int flag)
{ {
if(wd[0]!=0 || (flag&4)) { if((wd[0]!=0 || (flag&4)) && !((flag&1) && name[0]=='/')) {
if(strlen(wd)+1>=SfileadrL) if(strlen(wd)+1>=SfileadrL)
goto much_too_long; goto much_too_long;
strcpy(adr, wd); strcpy(adr, wd);
@ -3798,6 +3801,7 @@ int Xorriso_obtain_pattern_files_x(
} }
ret= 1; ret= 1;
ex:; ex:;
Dirseq_destroy(&dirseq,0);
if(flag&2) if(flag&2)
(*dive_count)--; (*dive_count)--;
return(ret); return(ret);
@ -4588,6 +4592,58 @@ int Xorriso__mode_to_perms(mode_t st_mode, char perms[10], int flag)
} }
int Xorriso_format_ls_l(struct XorrisO *xorriso, struct stat *stbuf, int flag)
{
char *rpt, perms[10];
mode_t st_mode;
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;
rpt[0]= 0;
st_mode= stbuf->st_mode;
if(S_ISDIR(st_mode))
strcat(rpt, "d");
else if(S_ISREG(st_mode))
strcat(rpt, "-");
else if(S_ISLNK(st_mode))
strcat(rpt, "l");
else if(S_ISBLK(st_mode))
strcat(rpt, "b");
else if(S_ISCHR(st_mode))
strcat(rpt, "c");
else if(S_ISFIFO(st_mode))
strcat(rpt, "p");
else if(S_ISSOCK(st_mode))
strcat(rpt, "s");
else
strcat(rpt, "?");
Xorriso__mode_to_perms(st_mode, perms, 0);
strcat(rpt, perms);
sprintf(rpt+strlen(rpt),"%4u ",(unsigned int) stbuf->st_nlink);
sprintf(rpt+strlen(rpt), "%-8lu ", (unsigned long) stbuf->st_uid);
sprintf(rpt+strlen(rpt), "%-8lu ", (unsigned long) stbuf->st_gid);
sprintf(rpt+strlen(rpt), "%8.f ", (double) stbuf->st_size);
mtime= stbuf->st_mtime;
tmpt= localtime_r(&mtime, &tms);
if(tmpt==0)
sprintf(rpt+strlen(rpt), "%12.f ",(double) mtime);
else if(time(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
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);
return(1);
}
/* @param flag bit0= long format /* @param flag bit0= long format
bit1= do not print count of nodes bit1= do not print count of nodes
bit2= du format bit2= du format
@ -4596,14 +4652,9 @@ int Xorriso_lsx_filev(struct XorrisO *xorriso, int filec, char **filev,
off_t boss_mem, int flag) off_t boss_mem, int flag)
{ {
int i, ret, was_error= 0; int i, ret, was_error= 0;
char sfe[5*SfileadrL], path[SfileadrL], *rpt, perms[10]; char sfe[5*SfileadrL], path[SfileadrL], *rpt;
mode_t st_mode;
off_t size; off_t size;
time_t mtime;
struct tm tms, *tmpt;
struct stat stbuf; struct stat stbuf;
static char months[12][4]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
rpt= xorriso->result_line; rpt= xorriso->result_line;
@ -4611,21 +4662,11 @@ int Xorriso_lsx_filev(struct XorrisO *xorriso, int filec, char **filev,
/* Count valid nodes, warn of invalid ones */ /* Count valid nodes, warn of invalid ones */
for(i= 0; i<filec; i++) { for(i= 0; i<filec; i++) {
if(filev[i][0]!='/') { ret= Xorriso_make_pattern_adr(xorriso, xorriso->wdx, filev[i], path, 1|4);
strcpy(path, xorriso->wdx); if(ret<=0) {
if(Sfile_add_to_path(path, filev[i], 0)<=0) { was_error++;
much_too_long:;
sprintf(xorriso->info_text,
"Path for file listing gets much too long (%d)",
(int) (strlen(path)+strlen(filev[i])+1));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
was_error++;
continue; continue;
} }
} else
if(Sfile_str(path, filev[i], 0)<=0)
goto much_too_long;
if(xorriso->do_follow_links) if(xorriso->do_follow_links)
ret= stat(path, &stbuf); ret= stat(path, &stbuf);
else else
@ -4647,64 +4688,23 @@ much_too_long:;
} }
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) { for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
if(filev[i][0]!='/') { ret= Xorriso_make_pattern_adr(xorriso, xorriso->wdx, filev[i], path, 1|4);
strcpy(path, xorriso->wdx); if(ret<=0)
if(Sfile_add_to_path(path, filev[i], 0)<=0)
continue; continue;
} else
if(Sfile_str(path, filev[i], 0)<=0)
continue;
if(xorriso->do_follow_links) if(xorriso->do_follow_links)
ret= stat(path, &stbuf); ret= stat(path, &stbuf);
else else
ret= lstat(path, &stbuf); ret= lstat(path, &stbuf);
if(ret==-1) if(ret==-1)
continue; continue;
size= stbuf.st_size;
rpt[0]= 0; rpt[0]= 0;
st_mode= stbuf.st_mode; if((flag&5)==1) {
if((flag&5)==1) { /* -ls_lx */ ret= Xorriso_format_ls_l(xorriso, &stbuf, 0);
if(S_ISDIR(st_mode)) if(ret<=0)
strcat(rpt, "d"); continue;
else if(S_ISREG(st_mode))
strcat(rpt, "-");
else if(S_ISLNK(st_mode))
strcat(rpt, "l");
else if(S_ISBLK(st_mode))
strcat(rpt, "b");
else if(S_ISCHR(st_mode))
strcat(rpt, "c");
else if(S_ISFIFO(st_mode))
strcat(rpt, "p");
else if(S_ISSOCK(st_mode))
strcat(rpt, "s");
else
strcat(rpt, "?");
Xorriso__mode_to_perms(st_mode, perms, 0);
strcat(rpt, perms);
sprintf(rpt+strlen(rpt),"%4u ",(unsigned int) stbuf.st_nlink);
sprintf(rpt+strlen(rpt), "%-8lu ", (unsigned long) stbuf.st_uid);
sprintf(rpt+strlen(rpt), "%-8lu ", (unsigned long) stbuf.st_gid);
sprintf(rpt+strlen(rpt), "%8.f ",(double) size);
mtime= stbuf.st_mtime;
tmpt= localtime_r(&mtime, &tms);
if(tmpt==0)
sprintf(rpt+strlen(rpt), "%12.f ",(double) mtime);
else if(time(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
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);
} else if(flag&4) { /* -du or -du_s */ } else if(flag&4) { /* -du or -du_s */
size= stbuf.st_size;
if(S_ISDIR(st_mode)) { if(S_ISDIR(stbuf.st_mode)) {
ret= Xorriso_show_dux_subs(xorriso, ret= Xorriso_show_dux_subs(xorriso,
path, filev[i], &size, boss_mem, flag&1); path, filev[i], &size, boss_mem, flag&1);
if(ret<0) if(ret<0)
@ -4713,7 +4713,6 @@ much_too_long:;
continue; continue;
} }
sprintf(rpt, "%7.f ",(double) (size/1024)); sprintf(rpt, "%7.f ",(double) (size/1024));
} }
sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n", sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n",
Text_shellsafe(filev[i], sfe, 0)); Text_shellsafe(filev[i], sfe, 0));

View File

@ -6,7 +6,8 @@
Provided under GPL version 2. Provided under GPL version 2.
This file contains the inner description of xorriso. This file contains inner declarations of xorriso.
The public interface is in xorriso.h
*/ */
@ -239,6 +240,7 @@ int Xorriso_register_matched_adr(struct XorrisO *xorriso,
char *adr, int count_limit, char *adr, int count_limit,
int *filec, char **filev, off_t *mem, int flag); int *filec, char **filev, off_t *mem, int flag);
int Xorriso_format_ls_l(struct XorrisO *xorriso, struct stat *stbuf, int flag);
int Sfile_str(char target[SfileadrL], char *source, int flag); int Sfile_str(char target[SfileadrL], char *source, int flag);

View File

@ -1 +1 @@
#define Xorriso_timestamP "2007.12.06.192437" #define Xorriso_timestamP "2007.12.08.175117"

View File

@ -1246,7 +1246,7 @@ int Xorriso_toc(struct XorrisO *xorriso, int flag)
if(xorriso->request_to_abort) if(xorriso->request_to_abort)
return(1); return(1);
Sfile_scale(((double) num_data) * 2048.0, mem_text,5,1e4,1); Sfile_scale(((double) num_data) * 2048.0, mem_text,5,1e4,1);
sprintf(respt, "Media summary: %d session%s, %d data blocks (%s)\n", sprintf(respt, "Media summary: %d session%s, %d data blocks, %s\n",
num_sessions, (num_sessions==1 ? "" : "s"), num_data, mem_text); num_sessions, (num_sessions==1 ? "" : "s"), num_data, mem_text);
Xorriso_result(xorriso,0); Xorriso_result(xorriso,0);
@ -1862,6 +1862,57 @@ ex:;
} }
/* @param flag bit0= *node is already valid */
int Xorriso_fake_stbuf(struct XorrisO *xorriso, char *path, struct stat *stbuf,
struct iso_tree_node **node, int flag)
{
int ret;
struct iso_volume *volume;
if(!(flag&1)) {
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
return(-1);
memset((char *) stbuf, 0, sizeof(struct stat));
*node= iso_tree_volume_path_to_node(volume,path);
}
if(*node==NULL)
return(0);
/* >>> stbuf->st_dev */
/* >>> stbuf->st_ino */
stbuf->st_mode= iso_tree_node_get_permissions(*node);
if(LIBISO_ISDIR(*node))
stbuf->st_mode|= S_IFDIR;
else if(LIBISO_ISREG(*node))
stbuf->st_mode|= S_IFREG;
else if(LIBISO_ISLNK(*node))
stbuf->st_mode|= S_IFLNK;
/* >>> With directories this should be : number of subdirs + 2 */
/* >>> ??? How to obtain RR hardlink number for other types ? */
stbuf->st_nlink= 1;
stbuf->st_uid= iso_tree_node_get_uid(*node);
stbuf->st_gid= iso_tree_node_get_gid(*node);
/* >>> stbuf->st_rdev */
stbuf->st_size= iso_tree_node_get_size(*node);
stbuf->st_blksize= 2048;
stbuf->st_blocks= stbuf->st_size / (off_t) 2048;
if(stbuf->st_blocks * (off_t) 2048 != stbuf->st_size)
stbuf->st_blocks++;
stbuf->st_atime= iso_tree_node_get_atime(*node);
stbuf->st_mtime= iso_tree_node_get_mtime(*node);
stbuf->st_ctime= iso_tree_node_get_ctime(*node);
return(1);
}
/* @param flag bit0= long format /* @param flag bit0= long format
bit1= do not print count of nodes bit1= do not print count of nodes
bit2= du format bit2= du format
@ -1872,13 +1923,9 @@ int Xorriso_ls_filev(struct XorrisO *xorriso, int filec, char **filev,
int i, ret, was_error= 0; int i, ret, was_error= 0;
struct iso_tree_node *node; struct iso_tree_node *node;
struct iso_volume *volume; struct iso_volume *volume;
char sfe[5*SfileadrL], path[SfileadrL], *rpt, perms[10]; char sfe[5*SfileadrL], path[SfileadrL], *rpt;
mode_t st_mode;
off_t size; off_t size;
time_t mtime; struct stat stbuf;
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; rpt= xorriso->result_line;
@ -1890,21 +1937,11 @@ int Xorriso_ls_filev(struct XorrisO *xorriso, int filec, char **filev,
/* Count valid nodes, warn of invalid ones */ /* Count valid nodes, warn of invalid ones */
for(i= 0; i<filec; i++) { for(i= 0; i<filec; i++) {
if(filev[i][0]!='/') { ret= Xorriso_make_pattern_adr(xorriso, xorriso->wdi, filev[i], path, 1|4);
strcpy(path, xorriso->wdi); if(ret<=0) {
if(Sfile_add_to_path(path, filev[i], 0)<=0) { was_error++;
much_too_long:;
sprintf(xorriso->info_text,
"Path for file listing gets much too long (%d)",
(int) (strlen(path)+strlen(filev[i])+1));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
was_error++;
continue; continue;
} }
} else
if(Sfile_str(path, filev[i], 0)<=0)
goto much_too_long;
node= iso_tree_volume_path_to_node(volume,path); node= iso_tree_volume_path_to_node(volume,path);
if(node==NULL) { if(node==NULL) {
sprintf(xorriso->info_text, "Not found in ISO image: %s", sprintf(xorriso->info_text, "Not found in ISO image: %s",
@ -1923,55 +1960,20 @@ much_too_long:;
} }
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) { for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
if(filev[i][0]!='/') {
strcpy(path, xorriso->wdi);
if(Sfile_add_to_path(path, filev[i], 0)<=0)
continue;
} else
if(Sfile_str(path, filev[i], 0)<=0)
continue;
node= iso_tree_volume_path_to_node(volume,path);
if(node==NULL)
continue;
size= iso_tree_node_get_size(node);
rpt[0]= 0; rpt[0]= 0;
ret= Xorriso_make_pattern_adr(xorriso, xorriso->wdi, filev[i], path, 1|4);
if(ret<=0)
continue;
ret= Xorriso_fake_stbuf(xorriso, path, &stbuf, &node, 0);
if(ret<=0)
continue;
if((flag&5)==1) { /* -ls_l */ if((flag&5)==1) { /* -ls_l */
if(LIBISO_ISDIR(node)) ret= Xorriso_format_ls_l(xorriso, &stbuf, 0);
strcat(rpt, "d"); if(ret<=0)
else if(LIBISO_ISREG(node)) continue;
strcat(rpt, "-");
else if(LIBISO_ISLNK(node))
strcat(rpt, "l");
else
strcat(rpt, "?");
st_mode= iso_tree_node_get_permissions(node);
Xorriso__mode_to_perms(st_mode, perms, 0);
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));
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(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
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);
} else if(flag&4) { /* -du or -du_s */ } else if(flag&4) { /* -du or -du_s */
if(LIBISO_ISDIR(node)) { size= stbuf.st_size;
if(S_ISDIR(stbuf.st_mode)) {
ret= Xorriso_show_du_subs(xorriso, (struct iso_tree_node_dir *) node, ret= Xorriso_show_du_subs(xorriso, (struct iso_tree_node_dir *) node,
path, filev[i], &size, boss_mem, flag&1); path, filev[i], &size, boss_mem, flag&1);
if(ret<0) if(ret<0)
@ -1990,7 +1992,8 @@ much_too_long:;
/* This function needs less buffer memory than Xorriso_ls_filev() but cannot /* This function needs less buffer memory than Xorriso_ls_filev() but cannot
perform structured pattern matching. perform structured pattern matching as done by Xorriso_expand_pattern()
for subsequent Xorriso_ls_filev().
@param flag bit0= long format @param flag bit0= long format
bit1= only check for directory existence bit1= only check for directory existence
bit2= do not apply search pattern but accept any file bit2= do not apply search pattern but accept any file
@ -2002,13 +2005,8 @@ int Xorriso_ls(struct XorrisO *xorriso, int flag)
struct iso_tree_node_dir *dir_node; struct iso_tree_node_dir *dir_node;
struct iso_volume *volume; struct iso_volume *volume;
struct iso_tree_iter *iter= NULL; struct iso_tree_iter *iter= NULL;
char sfe[5*SfileadrL], *npt, *rpt, perms[10]; char sfe[5*SfileadrL], *npt, *rpt;
mode_t st_mode; struct stat stbuf;
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; rpt= xorriso->result_line;
@ -2077,42 +2075,13 @@ wdi_is_not_a_dir:;
npt= (char *) iso_tree_node_get_name(node); npt= (char *) iso_tree_node_get_name(node);
rpt[0]= 0; rpt[0]= 0;
if(flag&1) { if(flag&1) {
if(LIBISO_ISDIR(node)) ret= Xorriso_fake_stbuf(xorriso, "", &stbuf, &node, 1);
strcat(rpt, "d"); if(ret<=0)
else if(LIBISO_ISREG(node)) continue;
strcat(rpt, "-"); ret= Xorriso_format_ls_l(xorriso, &stbuf, 0);
else if(LIBISO_ISLNK(node)) if(ret<=0)
strcat(rpt, "l"); continue;
else
strcat(rpt, "?");
st_mode= iso_tree_node_get_permissions(node);
Xorriso__mode_to_perms(st_mode, perms, 0);
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(NULL)-mtime < 180*86400 && time(NULL)-mtime >= 0)
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", sprintf(xorriso->result_line+strlen(xorriso->result_line), "%s\n",
Text_shellsafe(npt, sfe, 0)); Text_shellsafe(npt, sfe, 0));