Add private class BFdIO
Simple BPositionIO implementation using the POSIX API on a FD. In effect similar to BFile, but more easily ported to kernel and boot loader (and the FD is reusable).
This commit is contained in:
parent
8546c4160e
commit
c55a06055f
1
headers/build/private/storage/FdIO.h
Normal file
1
headers/build/private/storage/FdIO.h
Normal file
@ -0,0 +1 @@
|
||||
#include <../private/storage/FdIO.h>
|
45
headers/private/storage/FdIO.h
Normal file
45
headers/private/storage/FdIO.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _FD_IO_H_
|
||||
#define _FD_IO_H_
|
||||
|
||||
|
||||
#include <DataIO.h>
|
||||
|
||||
|
||||
class BFdIO : public BPositionIO {
|
||||
public:
|
||||
BFdIO();
|
||||
BFdIO(int fd, bool keepFd);
|
||||
virtual ~BFdIO();
|
||||
|
||||
void SetTo(int fd, bool keepFd);
|
||||
void Unset();
|
||||
|
||||
virtual ssize_t Read(void* buffer, size_t size);
|
||||
virtual ssize_t Write(const void* buffer, size_t size);
|
||||
|
||||
virtual ssize_t ReadAt(off_t position, void* buffer,
|
||||
size_t size);
|
||||
virtual ssize_t WriteAt(off_t position, const void* buffer,
|
||||
size_t size);
|
||||
|
||||
virtual off_t Seek(off_t position, uint32 seekMode);
|
||||
virtual off_t Position() const;
|
||||
|
||||
virtual status_t SetSize(off_t size);
|
||||
virtual status_t GetSize(off_t* _size) const;
|
||||
|
||||
private:
|
||||
BFdIO(const BFdIO& other);
|
||||
BFdIO& operator=(const BFdIO& other);
|
||||
|
||||
private:
|
||||
int fFd;
|
||||
bool fOwnsFd;
|
||||
};
|
||||
|
||||
|
||||
#endif // _FD_IO_H_
|
@ -14,6 +14,7 @@ BuildPlatformMergeObjectPIC <libbe_build>storage_kit.o :
|
||||
DriverSettings.cpp
|
||||
Entry.cpp
|
||||
EntryList.cpp
|
||||
FdIO.cpp
|
||||
File.cpp
|
||||
FileIO.cpp
|
||||
FindDirectory.cpp
|
||||
|
122
src/kits/storage/FdIO.cpp
Normal file
122
src/kits/storage/FdIO.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include <FdIO.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
BFdIO::BFdIO()
|
||||
:
|
||||
BPositionIO(),
|
||||
fFd(-1),
|
||||
fOwnsFd(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BFdIO::BFdIO(int fd, bool keepFd)
|
||||
:
|
||||
BPositionIO(),
|
||||
fFd(fd),
|
||||
fOwnsFd(keepFd)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BFdIO::~BFdIO()
|
||||
{
|
||||
Unset();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BFdIO::SetTo(int fd, bool keepFd)
|
||||
{
|
||||
Unset();
|
||||
|
||||
fFd = fd;
|
||||
fOwnsFd = keepFd;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BFdIO::Unset()
|
||||
{
|
||||
if (fOwnsFd && fFd >= 0)
|
||||
close(fFd);
|
||||
|
||||
fFd = -1;
|
||||
fOwnsFd = false;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFdIO::Read(void* buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesRead = read(fFd, buffer, size);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFdIO::Write(const void* buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesWritten = write(fFd, buffer, size);
|
||||
return bytesWritten >= 0 ? bytesWritten : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFdIO::ReadAt(off_t position, void* buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesRead = pread(fFd, buffer, size, position);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFdIO::WriteAt(off_t position, const void* buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesWritten = pwrite(fFd, buffer, size, position);
|
||||
return bytesWritten >= 0 ? bytesWritten : errno;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFdIO::Seek(off_t position, uint32 seekMode)
|
||||
{
|
||||
off_t newPosition = lseek(fFd, position, seekMode);
|
||||
return newPosition >= 0 ? newPosition : errno;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFdIO::Position() const
|
||||
{
|
||||
return const_cast<BFdIO*>(this)->BFdIO::Seek(0, SEEK_CUR);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFdIO::SetSize(off_t size)
|
||||
{
|
||||
return ftruncate(fFd, size) == 0 ? B_OK : errno;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFdIO::GetSize(off_t* _size) const
|
||||
{
|
||||
struct stat st;
|
||||
if (fstat(fFd, &st) != 0)
|
||||
return errno;
|
||||
|
||||
*_size = st.st_size;
|
||||
return B_OK;
|
||||
}
|
@ -27,6 +27,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
||||
Entry.cpp
|
||||
EntryList.cpp
|
||||
EntryOperationEngineBase.cpp
|
||||
FdIO.cpp
|
||||
File.cpp
|
||||
FileDescriptorIO.cpp
|
||||
FileIO.cpp
|
||||
|
Loading…
Reference in New Issue
Block a user