Widened the lseek capacity determination to SEEK_SET with wanted size

This commit is contained in:
Thomas Schmitt 2022-05-30 18:45:49 +02:00
parent ddfe5098d8
commit 0c0d542591
4 changed files with 115 additions and 27 deletions

View File

@ -1,7 +1,7 @@
/* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images.
Copyright 2007-2019 Thomas Schmitt, <scdbackup@gmx.net>
Copyright 2007-2022 Thomas Schmitt, <scdbackup@gmx.net>
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);
}

View File

@ -1,7 +1,7 @@
/* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images.
Copyright 2007-2019 Thomas Schmitt, <scdbackup@gmx.net>
Copyright 2007-2022 Thomas Schmitt, <scdbackup@gmx.net>
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 */

View File

@ -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);

View File

@ -1 +1 @@
#define Xorriso_timestamP "2022.04.26.101633"
#define Xorriso_timestamP "2022.05.30.164422"