Widened the lseek capacity determination to SEEK_SET with wanted size
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user