From 1f896b10e6e3417316e27cb573e3ab9be0eefdd4 Mon Sep 17 00:00:00 2001 From: Konstantin Germanov Date: Wed, 29 Jun 2022 10:33:26 -0400 Subject: [PATCH 1/3] Improve ntfs_device_size_get for file For avoid random read we can use IO callback for get stat. --- libntfs-3g/device.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libntfs-3g/device.c b/libntfs-3g/device.c index a5c32f42..6c478a2d 100644 --- a/libntfs-3g/device.c +++ b/libntfs-3g/device.c @@ -529,6 +529,7 @@ static int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs) s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) { s64 high, low; + struct stat sbuf; if (!dev || block_size <= 0 || (block_size - 1) & block_size) { errno = EINVAL; @@ -596,6 +597,18 @@ s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) } } #endif + /* + * We couldn't figure it out by using a specialized ioctl, + * so do lstat on device. + */ + if (dev->d_ops->stat(dev, &sbuf) == 0) { + ntfs_log_debug("STAT nr bytes = %llu (0x%llx)\n", + (unsigned long long)sbuf.st_size, + (unsigned long long)sbuf.st_size); + + return sbuf.st_size / block_size; + } + /* * We couldn't figure it out by using a specialized ioctl, * so do binary search to find the size of the device. From 48443a6fbd8aa799cf43b13a45afebaffc3c704c Mon Sep 17 00:00:00 2001 From: Konstantin Germanov Date: Mon, 4 Jul 2022 03:13:46 -0400 Subject: [PATCH 2/3] Check if it is regular file --- libntfs-3g/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libntfs-3g/device.c b/libntfs-3g/device.c index 6c478a2d..c8af5fb7 100644 --- a/libntfs-3g/device.c +++ b/libntfs-3g/device.c @@ -601,7 +601,7 @@ s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) * We couldn't figure it out by using a specialized ioctl, * so do lstat on device. */ - if (dev->d_ops->stat(dev, &sbuf) == 0) { + if (dev->d_ops->stat(dev, &sbuf) == 0 && S_ISREG(sbuf.st_mode)) { ntfs_log_debug("STAT nr bytes = %llu (0x%llx)\n", (unsigned long long)sbuf.st_size, (unsigned long long)sbuf.st_size); From a302a19cd2b54d5169b71fd55bc8b2a747cf440a Mon Sep 17 00:00:00 2001 From: Konstantin Germanov Date: Tue, 12 Jul 2022 08:57:41 -0400 Subject: [PATCH 3/3] Check if lstat size more that zero --- libntfs-3g/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libntfs-3g/device.c b/libntfs-3g/device.c index c8af5fb7..dbdd039f 100644 --- a/libntfs-3g/device.c +++ b/libntfs-3g/device.c @@ -601,7 +601,7 @@ s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) * We couldn't figure it out by using a specialized ioctl, * so do lstat on device. */ - if (dev->d_ops->stat(dev, &sbuf) == 0 && S_ISREG(sbuf.st_mode)) { + if (dev->d_ops->stat(dev, &sbuf) == 0 && S_ISREG(sbuf.st_mode) && sbuf.st_size > 512) { ntfs_log_debug("STAT nr bytes = %llu (0x%llx)\n", (unsigned long long)sbuf.st_size, (unsigned long long)sbuf.st_size);