Prevented some interesting pitfalls with -mv

This commit is contained in:
2007-10-28 12:54:22 +00:00
parent c77e00f9f5
commit 9e50ac35da
3 changed files with 93 additions and 32 deletions

View File

@ -445,6 +445,10 @@ int Xorriso_normalize_img_path(struct XorrisO *xorriso, char *img_path,
struct iso_tree_node *node= NULL;
char path[SfileadrL], *apt, *npt, sfe[4*SfileadrL], *cpt;
eff_path[0]= 0;
if(img_path[0]==0)
return(2); /* root directory */
if(!(flag&2)) {
ret= Xorriso_get_volume(xorriso, &volume, 0);
if(ret<=0)
@ -463,7 +467,6 @@ int Xorriso_normalize_img_path(struct XorrisO *xorriso, char *img_path,
if(Sfile_str(path, img_path, 0)<=0)
return(-1);
apt= npt= path;
eff_path[0]= 0;
if(path[0]!='/') {
sprintf(xorriso->info_text,
@ -471,7 +474,8 @@ int Xorriso_normalize_img_path(struct XorrisO *xorriso, char *img_path,
img_path);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "FATAL", 0);
return(-1);
}
} else if(path[1]==0)
return(2); /* root directory */
for(npt= apt; !done; apt= npt+1) {
npt= strchr(apt, '/');
@ -1259,7 +1263,6 @@ int Xorriso_ls(struct XorrisO *xorriso, int flag)
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);
@ -1394,28 +1397,58 @@ ex:;
int Xorriso_rename(struct XorrisO *xorriso,
char origin[], char dest[], int flag)
{
int ret;
int ret, ol, dest_ret;
char sfe[4*SfileadrL], eff_dest[SfileadrL], dir_adr[SfileadrL], *cpt;
char *leafname;
char *leafname, eff_origin[SfileadrL], sfe2[4*SfileadrL];
struct iso_volume *volume;
struct iso_tree_node_dir *origin_dir, *dest_dir;
struct iso_tree_node *node;
/* Check whether destination exists and may be not overwriteable */
ret= Xorriso_normalize_img_path(xorriso, dest, eff_dest, 1);
if(ret<0)
ret= Xorriso_normalize_img_path(xorriso, origin, eff_origin, 0);
if(ret<=0)
return(ret);
if(ret==2) {
dest_ret= Xorriso_normalize_img_path(xorriso, dest, eff_dest, 1);
if(dest_ret<0)
return(dest_ret);
if(dest_ret==0) { /* obtain eff_dest address despite it does not exist */
ret= Xorriso_normalize_img_path(xorriso, dest, eff_dest, 2);
if(ret<=0)
return(ret);
}
/* Prevent that destination is a subordinate of origin
(that would be a black hole plopping out of the universe) */
ol= strlen(eff_origin);
if(ol==0) {
sprintf(xorriso->info_text, "May not rename root directory");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if(strcmp(eff_origin, eff_dest)==0) {
sprintf(xorriso->info_text, "Ignored attempt to rename %s to itself",
Text_shellsafe(eff_origin,sfe,0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
return(0);
} else if(strncmp(eff_origin, eff_dest, ol)==0 &&
(eff_dest[ol]==0 || eff_dest[ol]=='/')) {
sprintf(xorriso->info_text,
"May not rename %s to its own sub address %s",
Text_shellsafe(eff_origin,sfe,0), Text_shellsafe(eff_dest,sfe2,0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
}
/* Check whether destination exists and may be not overwriteable */
if(dest_ret==2) {
sprintf(xorriso->info_text, "Renaming refuses to unlink directory: %s",
Text_shellsafe(eff_dest, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if (ret==1 && !xorriso->do_overwrite) {
} else if (dest_ret==1 && !xorriso->do_overwrite) {
sprintf(xorriso->info_text, "Renaming may not overwite: %s",
Text_shellsafe(eff_dest, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
return(0);
} else if(ret>0) {
} else if(dest_ret>0) {
ret= Xorriso_rmi(xorriso, eff_dest, 0);
if(ret<=0)
return(0);
@ -1427,9 +1460,11 @@ int Xorriso_rename(struct XorrisO *xorriso,
if(cpt==NULL)
cpt= dir_adr+strlen(dir_adr);
*cpt= 0;
ret= Xorriso_graft_in(xorriso, NULL, dir_adr, 1);
if(ret<=0)
return(ret);
if(dir_adr[0]!=0) {
ret= Xorriso_graft_in(xorriso, NULL, dir_adr, 1);
if(ret<=0)
return(ret);
}
/* Move node */
ret= Xorriso_get_volume(xorriso, &volume, 0);
@ -1437,15 +1472,16 @@ int Xorriso_rename(struct XorrisO *xorriso,
return(ret);
dest_dir= (struct iso_tree_node_dir *)
iso_tree_volume_path_to_node(volume,dir_adr);
strcpy(dir_adr, origin);
strcpy(dir_adr, eff_origin);
cpt= strrchr(dir_adr, '/');
if(cpt==NULL)
cpt= dir_adr+strlen(dir_adr);
*cpt= 0;
origin_dir= (struct iso_tree_node_dir *)
iso_tree_volume_path_to_node(volume,dir_adr);
node= iso_tree_volume_path_to_node(volume,origin);
node= iso_tree_volume_path_to_node(volume,eff_origin);
if(dest_dir==NULL || origin_dir==NULL || node==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Internal error on rename: confirmed node turns out as NULL");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
@ -1453,6 +1489,7 @@ int Xorriso_rename(struct XorrisO *xorriso,
}
ret= iso_tree_node_take(origin_dir, node);
if(ret==-1) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,
"Internal error on rename: failed to take node");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
@ -1465,6 +1502,8 @@ int Xorriso_rename(struct XorrisO *xorriso,
else
leafname++;
iso_tree_node_set_name(node, leafname);
Xorriso_process_msg_queues(xorriso,0);
return(1);
}