diff --git a/xorriso/disk_ops.c b/xorriso/disk_ops.c index c4d237bc..ba5bb994 100644 --- a/xorriso/disk_ops.c +++ b/xorriso/disk_ops.c @@ -1,7 +1,7 @@ /* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images. - Copyright 2007-2019 Thomas Schmitt, + Copyright 2007-2022 Thomas Schmitt, Provided under GPL version 2 or later. @@ -2172,3 +2172,108 @@ ex:; Xorriso_wait_child_end(xorriso, forked_pid, &status, 0); return(ret); } + + +/* flag: bit0= *capacity is a desired value. + Try SEEK_SET with *capacity + bit1= open for writing + return: 0= no random access , -1= cannot open path + 1= *capacity is valid +*/ +int Xorriso_lseek_capacity(struct XorrisO *xorriso, char *path, + off_t *capacity, int flag) +{ + int fd; + off_t seek_result; + + if(flag & 2) + fd= open(path, O_WRONLY); + else + fd= open(path, O_RDONLY); + if(fd < 0) { + sprintf(xorriso->info_text, + "Cannot open for determination of random-access capacity: "); + Text_shellsafe(path, xorriso->info_text, 1); + Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "FAILURE", 0); + return(-1); + } + if(flag & 1) { + seek_result= lseek(fd, *capacity, SEEK_SET); + } else { + seek_result= lseek(fd, 0, SEEK_END); + } + close(fd); + if(seek_result < 0) + return(0); + *capacity= seek_result; + return(1); +} + + +/* flag: bit0= *capacity is a desired value. + If SEEK_END fails, try SEEK_SET with *capacity + bit1= open for writing + return: 0= no random access , -1= path does not exist , -2= wrong type + -3= cannot open path + 1= *capacity is valid +*/ +int Xorriso_determine_capacity(struct XorrisO *xorriso, char *path, + off_t *capacity, char **reason, int flag) +{ + int ret; + off_t src_size, src_seek_size= -1; + struct stat stbuf; + + if(reason != NULL) + *reason= "offers no random access"; + if(lstat(path, &stbuf) == -1) { + *capacity= 0; + if(reason != NULL) + *reason= "does not exist"; + return(-1); + } + if(S_ISREG(stbuf.st_mode)) { + src_size= stbuf.st_size; + } else if(!(S_ISDIR(stbuf.st_mode) || S_ISLNK(stbuf.st_mode) || + S_ISFIFO(stbuf.st_mode) || S_ISSOCK(stbuf.st_mode))) { + ret= Xorriso_lseek_capacity(xorriso, path, &src_size, flag & 2); + if(ret <= 0 || src_size <= 0) { + if(ret == -1 || !(flag & 1)) { + *capacity= 0; + if(ret == -1) { + if(reason != NULL) + *reason= "cannot be opened"; + return(-3); + } + return(ret > 0); + } + if(ret > 0) + src_seek_size= 0; + src_size= *capacity; + ret= Xorriso_lseek_capacity(xorriso, path, &src_size, flag & 3); + if(ret <= 0 || src_size < src_seek_size) { + if(src_seek_size == 0) { + src_size= src_seek_size; + } else { + *capacity= 0; + if(ret == -1) { + if(reason != NULL) + *reason= "cannot be opened"; + return(-3); + } + return(0); + } + } + } + } else { + *capacity= 0; + if(reason != NULL) + *reason= "is of wrong type"; + return(-2); + } + *capacity= src_size; + if(reason != NULL) + *reason= ""; + return(1); +} + diff --git a/xorriso/disk_ops.h b/xorriso/disk_ops.h index 90a9ddcd..b7244d8b 100644 --- a/xorriso/disk_ops.h +++ b/xorriso/disk_ops.h @@ -1,7 +1,7 @@ /* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images. - Copyright 2007-2019 Thomas Schmitt, + Copyright 2007-2022 Thomas Schmitt, Provided under GPL version 2 or later. @@ -127,5 +127,8 @@ int Xorriso_concat(struct XorrisO *xorriso, char *mode, char *target, int progc, char **progv, int filec, char **filev, int flag); +int Xorriso_determine_capacity(struct XorrisO *xorriso, char *path, + off_t *capacity, char **reason, int flag); + #endif /* ! Xorriso_pvt_diskop_includeD */ diff --git a/xorriso/iso_manip.c b/xorriso/iso_manip.c index deb3d859..fe171405 100644 --- a/xorriso/iso_manip.c +++ b/xorriso/iso_manip.c @@ -36,7 +36,7 @@ #include "sort_cmp.h" #include "parse_exec.h" #include "write_run.h" - +#include "disk_ops.h" /* @param flag bit0= give directory x-permission where is r-permission @@ -1225,27 +1225,6 @@ ex:; } -int Xorriso_lseek_capacity(struct XorrisO *xorriso, char *path, - off_t *capacity, int flag) -{ - int fd; - - fd= open(path, O_RDONLY); - if(fd < 0) { - sprintf(xorriso->info_text, - "Determination of random-access readable capacity failed: "); - Text_shellsafe(path, xorriso->info_text, 1); - Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "FAILURE", 0); - return -1; - } - *capacity= lseek(fd, 0, SEEK_END); - close(fd); - if(*capacity < 0) - return(-1); - return(1); -} - - /* @param flag bit0= -follow: disk_path is not a command parameter */ int Xorriso_cut_out(struct XorrisO *xorriso, char *disk_path, @@ -1292,7 +1271,8 @@ int Xorriso_cut_out(struct XorrisO *xorriso, char *disk_path, src_size= stbuf.st_size; } else if(!(S_ISDIR(stbuf.st_mode) || S_ISLNK(stbuf.st_mode) || S_ISFIFO(stbuf.st_mode) || S_ISSOCK(stbuf.st_mode))) { - ret= Xorriso_lseek_capacity(xorriso, eff_source, &src_size, 0); + src_size= startbyte + bytecount; + ret= Xorriso_determine_capacity(xorriso, eff_source, &src_size, NULL, 1); if(ret <= 0) goto unsupported_type; if(src_size <= 0) { @@ -1311,7 +1291,7 @@ unsupported_type:; Ftypetxt(stbuf.st_mode, 0)); } else { sprintf(xorriso->info_text, - "-cut_out: File (%s) does not support random read access: ", + "-cut_out: File (%s) does not support or permit random read access: ", Ftypetxt(stbuf.st_mode, 0)); } Text_shellsafe(eff_source, xorriso->info_text, 1); diff --git a/xorriso/xorriso_timestamp.h b/xorriso/xorriso_timestamp.h index 3b2aaaa8..2c808517 100644 --- a/xorriso/xorriso_timestamp.h +++ b/xorriso/xorriso_timestamp.h @@ -1 +1 @@ -#define Xorriso_timestamP "2022.04.26.101633" +#define Xorriso_timestamP "2022.05.30.164422"