mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
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:
parent
805e87a21c
commit
c8fda23a03
3 changed files with 143 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,7 +37,13 @@ public:
|
|||
|
||||
bool can_read() const;
|
||||
|
||||
bool seek(signed_qword);
|
||||
enum class SeekMode {
|
||||
SetPosition,
|
||||
FromCurrentPosition,
|
||||
FromEndPosition,
|
||||
};
|
||||
|
||||
bool seek(signed_qword, SeekMode = SeekMode::SetPosition, off_t* = nullptr);
|
||||
|
||||
virtual bool open(CIODevice::OpenMode) = 0;
|
||||
virtual bool close();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue