Now has the possibility of opening a file directly, instead of only
accepting already opened files. Hence, it now also has an InitCheck() method, because opening doesn't have to succeed. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4635 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
fa89d1f6f3
commit
fe281d90df
@ -11,6 +11,9 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#ifndef HAVE_READ_POS
|
||||
@ -22,11 +25,25 @@
|
||||
Handle::Handle(int handle, bool takeOwnership)
|
||||
:
|
||||
fHandle(handle),
|
||||
fOwnHandle(takeOwnership)
|
||||
fOwnHandle(takeOwnership),
|
||||
fPath(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Handle::Handle(const char *path)
|
||||
:
|
||||
fOwnHandle(true),
|
||||
fPath(NULL)
|
||||
{
|
||||
fHandle = open(path, O_RDONLY);
|
||||
if (fHandle < B_OK)
|
||||
return;
|
||||
|
||||
fPath = strdup(path);
|
||||
}
|
||||
|
||||
|
||||
Handle::Handle(void)
|
||||
:
|
||||
fHandle(0)
|
||||
@ -38,11 +55,20 @@ Handle::~Handle()
|
||||
{
|
||||
if (fOwnHandle)
|
||||
close(fHandle);
|
||||
|
||||
free(fPath);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
Handle::InitCheck()
|
||||
{
|
||||
return fHandle < B_OK ? fHandle : B_OK;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Handle::SetHandle(int handle, bool takeOwnership)
|
||||
Handle::SetTo(int handle, bool takeOwnership)
|
||||
{
|
||||
if (fHandle && fOwnHandle)
|
||||
close(fHandle);
|
||||
@ -66,6 +92,18 @@ Handle::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize)
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
Handle::GetName(char *nameBuffer, size_t bufferSize) const
|
||||
{
|
||||
if (fPath == NULL)
|
||||
return B_ERROR;
|
||||
|
||||
strncpy(nameBuffer, fPath, bufferSize - 1);
|
||||
nameBuffer[bufferSize - 1] = '\0';
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
Handle::Size() const
|
||||
{
|
||||
|
@ -14,19 +14,24 @@
|
||||
class Handle : public Node {
|
||||
public:
|
||||
Handle(int handle, bool takeOwnership = true);
|
||||
Handle(const char *path);
|
||||
Handle();
|
||||
virtual ~Handle();
|
||||
|
||||
void SetHandle(int handle, bool takeOwnership = true);
|
||||
status_t InitCheck();
|
||||
|
||||
void SetTo(int handle, bool takeOwnership = true);
|
||||
|
||||
virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize);
|
||||
virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize);
|
||||
|
||||
virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
|
||||
virtual off_t Size() const;
|
||||
|
||||
protected:
|
||||
int fHandle;
|
||||
bool fOwnHandle;
|
||||
char *fPath;
|
||||
};
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
Loading…
Reference in New Issue
Block a user