From 3904afa1ff696c64e13cf4c9c2b0b355bab8ec36 Mon Sep 17 00:00:00 2001 From: Tianhao Chai Date: Sat, 21 Aug 2021 21:46:35 -0500 Subject: [PATCH] file abstraction: actually follow lseek semantics Signed-off-by: Tianhao Chai (cherry picked from commit 0b60970d5a332a2af569f26fd3b8fb5cff34e036) --- akode/lib/file.h | 2 +- akode/lib/localfile.cpp | 8 ++++---- akode/lib/localfile.h | 2 +- akode/lib/mmapfile.cpp | 8 ++++---- akode/lib/mmapfile.h | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/akode/lib/file.h b/akode/lib/file.h index 11a4fef..3685bcc 100644 --- a/akode/lib/file.h +++ b/akode/lib/file.h @@ -76,7 +76,7 @@ public: * Seeks to the position. * Behaves semantically as lseek. */ - virtual bool seek(long to, int whence = SEEK_SET) = 0; + virtual ssize_t seek(long to, int whence = SEEK_SET) = 0; /*! * Returns current position in file, or negative if unknown. */ diff --git a/akode/lib/localfile.cpp b/akode/lib/localfile.cpp index ff92760..35ce2f3 100644 --- a/akode/lib/localfile.cpp +++ b/akode/lib/localfile.cpp @@ -115,11 +115,11 @@ long LocalFile::write(const char* ptr, long num) { return n; } -bool LocalFile::seek(long to, int whence) { - if(_fd == -1) return false; - int s = ::lseek(_fd, to, whence); +ssize_t LocalFile::seek(long to, int whence) { + if(_fd == -1) return -1; + ssize_t s = ::lseek(_fd, to, whence); if (s >= 0) pos = s; - return (s >= 0); + return s; } long LocalFile::position() const { diff --git a/akode/lib/localfile.h b/akode/lib/localfile.h index 7f7c2c2..9003e43 100644 --- a/akode/lib/localfile.h +++ b/akode/lib/localfile.h @@ -50,7 +50,7 @@ public: long read(char* ptr, long num); long write(const char* ptr, long num); - bool seek(long to, int whence = SEEK_SET); + ssize_t seek(long to, int whence = SEEK_SET); long position() const; long length() const; diff --git a/akode/lib/mmapfile.cpp b/akode/lib/mmapfile.cpp index 4c45603..ec45f98 100644 --- a/akode/lib/mmapfile.cpp +++ b/akode/lib/mmapfile.cpp @@ -94,7 +94,7 @@ long MMapFile::write(const char*, long) { return false; } -bool MMapFile::seek(long to, int whence) { +ssize_t MMapFile::seek(long to, int whence) { if(!handle) return false; long newpos = 0; @@ -109,12 +109,12 @@ bool MMapFile::seek(long to, int whence) { newpos = len + to; break; default: - return false; + return length(); } if (newpos > len || newpos < 0) - return false; + return -1; pos = newpos; - return true; + return newpos; } long MMapFile::position() const { diff --git a/akode/lib/mmapfile.h b/akode/lib/mmapfile.h index 80c5913..c2de99f 100644 --- a/akode/lib/mmapfile.h +++ b/akode/lib/mmapfile.h @@ -47,7 +47,7 @@ public: long read(char* ptr, long num); long write(const char*, long); - bool seek(long to, int whence = SEEK_SET); + ssize_t seek(long to, int whence = SEEK_SET); long position() const; long length() const;