Rename io_raw module to more appropriate file_windows
[lwext4.git] / blockdev / linux / ext4_filedev.c
index 14fae75951242d325330a474d80436c47dad89e2..d4417996994ac4e5781fcc6030a358cfbb4c33a1 100644 (file)
@@ -26,6 +26,7 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define _LARGEFILE64_SOURCE
 #define _FILE_OFFSET_BITS 64
 
 #include <ext4_config.h>
@@ -34,7 +35,6 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <string.h>
-#include <fcntl.h>
 
 /**@brief   Default filename.*/
 static const char *fname = "ext2";
@@ -57,7 +57,7 @@ static int filedev_close(struct ext4_blockdev *bdev);
 
 /******************************************************************************/
 EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_FILEDEV_BSIZE, 0, filedev_open,
-                             filedev_bread, filedev_bwrite, filedev_close);
+               filedev_bread, filedev_bwrite, filedev_close, 0, 0);
 
 /******************************************************************************/
 static int filedev_open(struct ext4_blockdev *bdev)
@@ -70,10 +70,12 @@ static int filedev_open(struct ext4_blockdev *bdev)
        /*No buffering at file.*/
        setbuf(dev_file, 0);
 
-       if (fseek(dev_file, 0, SEEK_END))
+       if (fseeko(dev_file, 0, SEEK_END))
                return EFAULT;
 
-       _filedev.ph_bcnt = ftell(dev_file) / _filedev.ph_bsize;
+       _filedev.part_offset = 0;
+       _filedev.part_size = ftello(dev_file);
+       _filedev.bdif->ph_bcnt = _filedev.part_size / _filedev.bdif->ph_bsize;
 
        return EOK;
 }
@@ -83,10 +85,11 @@ static int filedev_open(struct ext4_blockdev *bdev)
 static int filedev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
                         uint32_t blk_cnt)
 {
-       if (fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
+       if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
                return EIO;
-
-       if (!fread(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
+       if (!blk_cnt)
+               return EOK;
+       if (!fread(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
                return EIO;
 
        return EOK;
@@ -109,10 +112,11 @@ static void drop_cache(void)
 static int filedev_bwrite(struct ext4_blockdev *bdev, const void *buf,
                          uint64_t blk_id, uint32_t blk_cnt)
 {
-       if (fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
+       if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
                return EIO;
-
-       if (!fwrite(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
+       if (!blk_cnt)
+               return EOK;
+       if (!fwrite(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
                return EIO;
 
        drop_cache();