LibCore/Userland: Introduce a simple tail implementation

Also introduce more seek modes on CIODevice, and an out param to find
the current position inside the file -- this means less syscalls (and
less potential races) than requesting it through a separate pos()
accessor or something.
This commit is contained in:
Robin Burchell 2019-05-16 15:02:17 +02:00 committed by Andreas Kling
parent 805e87a21c
commit c8fda23a03
Notes: sideshowbarker 2024-07-19 14:05:01 +09:00
3 changed files with 143 additions and 4 deletions

View file

@ -174,15 +174,31 @@ bool CIODevice::close()
return true;
}
bool CIODevice::seek(signed_qword offset)
bool CIODevice::seek(signed_qword offset, SeekMode mode, off_t *pos)
{
int rc = lseek(m_fd, offset, SEEK_SET);
int m = SEEK_SET;
switch (mode) {
case SeekMode::SetPosition:
m = SEEK_SET;
break;
case SeekMode::FromCurrentPosition:
m = SEEK_CUR;
break;
case SeekMode::FromEndPosition:
m = SEEK_END;
break;
}
off_t rc = lseek(m_fd, offset, m);
if (rc < 0) {
perror("CIODevice::seek: lseek");
set_error(errno);
if (pos)
*pos = -1;
return false;
}
m_buffered_data.clear();
m_eof = false;
if (pos)
*pos = rc;
return true;
}