Use int64_t as offset to ext4_fseek.
authorFan Deng <enetor@gmail.com>
Wed, 18 Oct 2017 17:54:54 +0000 (10:54 -0700)
committerFan Deng <enetor@gmail.com>
Wed, 18 Oct 2017 17:54:54 +0000 (10:54 -0700)
This change makes it possible to fseek backwards in fseek.

Tested:
  make test_all

include/ext4.h
src/ext4.c

index 36944100c0e9d9b11df208012f7fd3a8d8328a1e..516bbdd3905c533f915240da0401dece08f19cdc 100644 (file)
@@ -368,7 +368,7 @@ int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt);
  *              @ref SEEK_END
  *
  * @return  Standard error code.*/
-int ext4_fseek(ext4_file *file, uint64_t offset, uint32_t origin);
+int ext4_fseek(ext4_file *file, int64_t offset, uint32_t origin);
 
 /**@brief   Get file position.
  *
index b27ae6b758aef07e8dbf71ea7597ddb87c3abdc7..a6d3e2371da22427267a821c9d8b89511b0979d7 100644 (file)
@@ -2016,23 +2016,25 @@ Finish:
        return r;
 }
 
-int ext4_fseek(ext4_file *file, uint64_t offset, uint32_t origin)
+int ext4_fseek(ext4_file *file, int64_t offset, uint32_t origin)
 {
        switch (origin) {
        case SEEK_SET:
-               if (offset > file->fsize)
+               if (offset < 0 || (uint64_t)offset > file->fsize)
                        return EINVAL;
 
                file->fpos = offset;
                return EOK;
        case SEEK_CUR:
-               if ((offset + file->fpos) > file->fsize)
+               if ((offset < 0 && (uint64_t)(-offset) > file->fpos) ||
+                   (offset > 0 &&
+                    (uint64_t)offset > (file->fsize - file->fpos)))
                        return EINVAL;
 
                file->fpos += offset;
                return EOK;
        case SEEK_END:
-               if (offset > file->fsize)
+               if (offset < 0 || (uint64_t)offset > file->fsize)
                        return EINVAL;
 
                file->fpos = file->fsize - offset;