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:
@@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
#ifndef HAVE_READ_POS
|
#ifndef HAVE_READ_POS
|
||||||
@@ -22,11 +25,25 @@
|
|||||||
Handle::Handle(int handle, bool takeOwnership)
|
Handle::Handle(int handle, bool takeOwnership)
|
||||||
:
|
:
|
||||||
fHandle(handle),
|
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)
|
Handle::Handle(void)
|
||||||
:
|
:
|
||||||
fHandle(0)
|
fHandle(0)
|
||||||
@@ -38,11 +55,20 @@ Handle::~Handle()
|
|||||||
{
|
{
|
||||||
if (fOwnHandle)
|
if (fOwnHandle)
|
||||||
close(fHandle);
|
close(fHandle);
|
||||||
|
|
||||||
|
free(fPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
Handle::InitCheck()
|
||||||
|
{
|
||||||
|
return fHandle < B_OK ? fHandle : B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Handle::SetHandle(int handle, bool takeOwnership)
|
Handle::SetTo(int handle, bool takeOwnership)
|
||||||
{
|
{
|
||||||
if (fHandle && fOwnHandle)
|
if (fHandle && fOwnHandle)
|
||||||
close(fHandle);
|
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
|
off_t
|
||||||
Handle::Size() const
|
Handle::Size() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,19 +14,24 @@
|
|||||||
class Handle : public Node {
|
class Handle : public Node {
|
||||||
public:
|
public:
|
||||||
Handle(int handle, bool takeOwnership = true);
|
Handle(int handle, bool takeOwnership = true);
|
||||||
|
Handle(const char *path);
|
||||||
Handle();
|
Handle();
|
||||||
virtual ~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 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 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;
|
virtual off_t Size() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int fHandle;
|
int fHandle;
|
||||||
bool fOwnHandle;
|
bool fOwnHandle;
|
||||||
|
char *fPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|||||||
Reference in New Issue
Block a user