file abstraction: actually follow lseek semantics

Signed-off-by: Tianhao Chai <cth451@aosc.io>
pull/7/head
Tianhao Chai 3 years ago committed by liushuyu
parent e8ad2c11ae
commit 0b60970d5a

@ -76,7 +76,7 @@ public:
* Seeks to the position. * Seeks to the position.
* Behaves semantically as lseek. * 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. * Returns current position in file, or negative if unknown.
*/ */

@ -115,11 +115,11 @@ long LocalFile::write(const char* ptr, long num) {
return n; return n;
} }
bool LocalFile::seek(long to, int whence) { ssize_t LocalFile::seek(long to, int whence) {
if(_fd == -1) return false; if(_fd == -1) return -1;
int s = ::lseek(_fd, to, whence); ssize_t s = ::lseek(_fd, to, whence);
if (s >= 0) pos = s; if (s >= 0) pos = s;
return (s >= 0); return s;
} }
long LocalFile::position() const { long LocalFile::position() const {

@ -50,7 +50,7 @@ public:
long read(char* ptr, long num); long read(char* ptr, long num);
long write(const 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 position() const;
long length() const; long length() const;

@ -94,7 +94,7 @@ long MMapFile::write(const char*, long) {
return false; return false;
} }
bool MMapFile::seek(long to, int whence) { ssize_t MMapFile::seek(long to, int whence) {
if(!handle) return false; if(!handle) return false;
long newpos = 0; long newpos = 0;
@ -109,12 +109,12 @@ bool MMapFile::seek(long to, int whence) {
newpos = len + to; newpos = len + to;
break; break;
default: default:
return false; return length();
} }
if (newpos > len || newpos < 0) if (newpos > len || newpos < 0)
return false; return -1;
pos = newpos; pos = newpos;
return true; return newpos;
} }
long MMapFile::position() const { long MMapFile::position() const {

@ -47,7 +47,7 @@ public:
long read(char* ptr, long num); long read(char* ptr, long num);
long write(const char*, long); 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 position() const;
long length() const; long length() const;

Loading…
Cancel
Save