util_posix.cc (fstransform-0.9.3-src) | : | util_posix.cc (fstransform-0.9.4) | ||
---|---|---|---|---|
skipping to change at line 45 | skipping to change at line 45 | |||
#elif defined(FT_HAVE_CSTDLIB) | #elif defined(FT_HAVE_CSTDLIB) | |||
# include <cstdlib> // for exit(), posix_fallocate() | # include <cstdlib> // for exit(), posix_fallocate() | |||
#endif | #endif | |||
#ifdef FT_HAVE_FCNTL_H | #ifdef FT_HAVE_FCNTL_H | |||
# include <fcntl.h> // for fallocate() | # include <fcntl.h> // for fallocate() | |||
#endif | #endif | |||
#ifdef FT_HAVE_UNISTD_H | #ifdef FT_HAVE_UNISTD_H | |||
# include <unistd.h> // for fork(), execvp() | # include <unistd.h> // for fork(), execvp() | |||
#endif | #endif | |||
#ifdef FT_HAVE_SYS_STAT_H | #ifdef FT_HAVE_SYS_IOCTL_H | |||
# include <sys/ioctl.h> // for ioctl() | # include <sys/ioctl.h> // for ioctl() | |||
#endif | #endif | |||
#ifdef FT_HAVE_SYS_STAT_H | ||||
# include <sys/stat.h> // for stat() | ||||
#endif | ||||
#ifdef FT_HAVE_SYS_TYPES_H | #ifdef FT_HAVE_SYS_TYPES_H | |||
# include <sys/types.h> // for waitpid() | # include <sys/types.h> // for waitpid() | |||
#endif | #endif | |||
#ifdef FT_HAVE_SYS_WAIT_H | #ifdef FT_HAVE_SYS_WAIT_H | |||
# include <sys/wait.h> // for " | # include <sys/wait.h> // for " | |||
#endif | #endif | |||
#ifdef FT_HAVE_SYS_DISKLABEL_H | ||||
# include <sys/disklabel.h> // for struct disklabel on *BSD | ||||
#endif | ||||
#ifdef FT_HAVE_LINUX_FS_H | #ifdef FT_HAVE_LINUX_FS_H | |||
# include <linux/fs.h> // for BLKGETSIZE64 | # include <linux/fs.h> // for BLKGETSIZE64 on Linux | |||
#endif | #endif | |||
#include "../types.hh" // for ft_u64, ft_stat | #include "../types.hh" // for ft_u64, ft_stat | |||
#include "../log.hh" // for ff_log() | #include "../log.hh" // for ff_log() | |||
#include "../misc.hh" // for ff_min2<T>() | #include "../misc.hh" // for ff_min2<T>() | |||
#include "util_posix.hh" // for ff_posix_ioctl(), ff_posix_stat(), ff_posix_siz e(), ff_filedev() | #include "util_posix.hh" // for ff_posix_ioctl(), ff_posix_stat(), ff_posix_siz e(), ff_filedev() | |||
FT_IO_NAMESPACE_BEGIN | FT_IO_NAMESPACE_BEGIN | |||
/** invoke ioctl() */ | /** invoke ioctl() */ | |||
skipping to change at line 80 | skipping to change at line 86 | |||
/** return file stats in (*ret_stat) */ | /** return file stats in (*ret_stat) */ | |||
int ff_posix_stat(int fd, ft_stat * ret_stat) | int ff_posix_stat(int fd, ft_stat * ret_stat) | |||
{ | { | |||
int err = fstat(fd, ret_stat); | int err = fstat(fd, ret_stat); | |||
if (err != 0) | if (err != 0) | |||
err = errno; | err = errno; | |||
return err; | return err; | |||
} | } | |||
/** return file stats in (*ret_stat) */ | ||||
int ff_posix_stat(const char * path, ft_stat * ret_stat) | ||||
{ | ||||
int err = lstat(path, ret_stat); | ||||
if (err != 0) | ||||
err = ff_log(FC_ERROR, errno, "error in lstat(%s)", path); | ||||
return err; | ||||
} | ||||
/** return block size of file-system containing file */ | /** return block size of file-system containing file */ | |||
int ff_posix_blocksize(int fd, ft_uoff * ret_block_size) | int ff_posix_blocksize(int fd, ft_uoff * ret_block_size) | |||
{ | { | |||
ft_stat st_buf; | ft_stat st_buf; | |||
int err = ff_posix_stat(fd, & st_buf); | int err = ff_posix_stat(fd, & st_buf); | |||
if (err == 0) { | if (err == 0) { | |||
ft_uoff block_size; | ft_uoff block_size; | |||
if ((err = ff_narrow(st_buf.st_blksize, & block_size)) == 0) | if ((err = ff_narrow(st_buf.st_blksize, & block_size)) == 0) | |||
* ret_block_size = block_size; | * ret_block_size = block_size; | |||
} | } | |||
skipping to change at line 133 | skipping to change at line 148 | |||
* ret_dev = st_buf.st_rdev; | * ret_dev = st_buf.st_rdev; | |||
else | else | |||
err = ENOTBLK; | err = ENOTBLK; | |||
} | } | |||
return err; | return err; | |||
} | } | |||
/** if file is special block device, return its length in (*ret_size) */ | /** if file is special block device, return its length in (*ret_size) */ | |||
int ff_posix_blkdev_size(int fd, ft_uoff * ret_size) | int ff_posix_blkdev_size(int fd, ft_uoff * ret_size) | |||
{ | { | |||
ft_u64 size_buf; | #if defined(DIOCGDINFO) && defined(FT_HAVE_STRUCT_DISKLABEL_D_SECSIZE) && define | |||
int err = ff_posix_ioctl(fd, BLKGETSIZE64, & size_buf); | d(FT_HAVE_STRUCT_DISKLABEL_D_SECPERUNIT) | |||
// *BSD | ||||
struct disklabel dl; | ||||
int err = ff_posix_ioctl(fd, DIOCGDINFO, & dl); | ||||
if (err == 0) { | if (err == 0) { | |||
ft_uoff dev_size = (ft_uoff) size_buf; | if (dl.d_secsize <= 0 || dl.d_secperunit <= 0) | |||
if ((ft_u64) dev_size == size_buf) | err = EINVAL; // invalid size | |||
* ret_size = dev_size; | else { | |||
ft_uoff dev_sector_size = (ft_uoff) dl.d_secsize; | ||||
ft_uoff dev_sector_count = (ft_uoff) dl.d_secperunit; | ||||
if (dev_sector_size != dl.d_secsize || dev_sector_count != dl.d_secp | ||||
erunit) | ||||
err = EOVERFLOW; // sector size or sector count cannot be repres | ||||
ented by ft_uoff! | ||||
else { | ||||
ft_uoff dev_size = dev_sector_size * dev_sector_count; | ||||
// check for multiplication overflow | ||||
if (dev_size / dev_sector_size != dev_sector_count) | ||||
err = EOVERFLOW; // device size cannot be represented by ft | ||||
_uoff! | ||||
else | ||||
* ret_size = dev_size; | ||||
} | ||||
} | ||||
} | ||||
#elif defined(BLKGETSIZE64) | ||||
// Linux | ||||
ft_u64 size_u64 = 0; | ||||
int err = ff_posix_ioctl(fd, BLKGETSIZE64, & size_u64); | ||||
if (err == 0) { | ||||
if (size_u64 <= 0) | ||||
err = EINVAL; // invalid size | ||||
else if (size_u64 > (ft_uoff)-1) | ||||
err = EOVERFLOW; // device size cannot be represented by ft_uoff! | ||||
else | else | |||
* ret_size = (ft_uoff) size_u64; | ||||
} | ||||
#else | ||||
// Linux, obsolete: BLKGETSIZE returns device size DIVIDED 512 | ||||
unsigned long size_div_512 = 0; | ||||
int err = ff_posix_ioctl(fd, BLKGETSIZE, & size_div_512); | ||||
if (err == 0) { | ||||
if (size_div_512 <= 0) | ||||
err = EINVAL; // invalid size | ||||
else if (size_div_512 > ((ft_uoff)-1 >> 9)) | ||||
err = EOVERFLOW; // device size cannot be represented by ft_uoff! | err = EOVERFLOW; // device size cannot be represented by ft_uoff! | |||
else | ||||
* ret_size = (ft_uoff) size_div_512 << 9; | ||||
} | } | |||
#endif | ||||
return err; | return err; | |||
} | } | |||
/** create a directory */ | ||||
int ff_posix_mkdir(const char * path, ft_mode mode) | ||||
{ | ||||
int err = mkdir(path, mode); | ||||
return err == 0 ? err : errno; | ||||
} | ||||
/** | /** | |||
* seek file descriptor to specified position from file beginning. | * seek file descriptor to specified position from file beginning. | |||
* note: if an error is returned, file descriptor position will be undefined! | * note: if an error is returned, file descriptor position will be undefined! | |||
*/ | */ | |||
int ff_posix_lseek(int fd, ft_uoff pos) | int ff_posix_lseek(int fd, ft_uoff pos) | |||
{ | { | |||
off_t pos_s = (off_t)pos; | off_t pos_s = (off_t)pos; | |||
if (pos_s < 0 || pos != (ft_uoff) pos_s) | if (pos_s < 0 || pos != (ft_uoff) pos_s) | |||
return EOVERFLOW; | return EOVERFLOW; | |||
End of changes. 11 change blocks. | ||||
14 lines changed or deleted | 64 lines changed or added |