2018-05-25 20:07:45 -05:00
|
|
|
// 25 may 2018
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2018-05-25 20:17:04 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2018-05-25 20:07:45 -05:00
|
|
|
#include "lib.hpp"
|
|
|
|
|
|
|
|
class posixError : public Error {
|
|
|
|
int error;
|
|
|
|
public:
|
|
|
|
posixError(int error);
|
|
|
|
virtual ~posixError(void);
|
|
|
|
|
|
|
|
virtual const char *String(void) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
posixError::posixError(int error)
|
|
|
|
{
|
|
|
|
this->error = error;
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
posixError::~posixError(void)
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
const char *posixError::String(void) const
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
return strerror(this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
class posixReadCloser : public ReadCloser {
|
|
|
|
int fd;
|
|
|
|
public:
|
|
|
|
posixReadCloser(int fd);
|
|
|
|
virtual ~posixReadCloser(void);
|
|
|
|
|
|
|
|
virtual Error *Read(void *buf, size_t n, size_t *actual);
|
|
|
|
};
|
|
|
|
|
|
|
|
posixReadCloser::posixReadCloser(int fd)
|
|
|
|
{
|
|
|
|
this->fd = fd;
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
posixReadCloser::~posixReadCloser(void)
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
close(this->fd);
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
Error *posixReadCloser::Read(void *buf, size_t n, size_t *actual)
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
*actual = 0;
|
|
|
|
ret = read(this->fd, buf, n);
|
|
|
|
if (ret < 0)
|
|
|
|
return new posixError(errno);
|
|
|
|
if (ret == 0)
|
2018-05-26 15:56:53 -05:00
|
|
|
return NewEOF();
|
2018-05-25 20:07:45 -05:00
|
|
|
*actual = ret;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
class posixWriteCloser : public WriteCloser {
|
|
|
|
int fd;
|
|
|
|
public:
|
|
|
|
posixWriteCloser(int fd);
|
|
|
|
virtual ~posixWriteCloser(void);
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
virtual Error *Write(void *buf, size_t n);
|
2018-05-25 20:07:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
posixWriteCloser::posixWriteCloser(int fd)
|
|
|
|
{
|
|
|
|
this->fd = fd;
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
posixWriteCloser::~posixWriteCloser(void)
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
close(this->fd);
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:56:53 -05:00
|
|
|
Error *posixWriteCloser::Write(void *buf, size_t n)
|
2018-05-25 20:07:45 -05:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = write(this->fd, buf, n);
|
|
|
|
if (ret < 0)
|
|
|
|
return new posixError(errno);
|
2018-05-26 15:56:53 -05:00
|
|
|
if (((size_t) ret) != n)
|
|
|
|
return NewErrShortWrite();
|
2018-05-25 20:07:45 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error *OpenRead(const char *filename, ReadCloser **r)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
*r = NULL;
|
|
|
|
fd = open(filename, O_RDONLY, 0644);
|
|
|
|
if (fd < 0)
|
|
|
|
return new posixError(errno);
|
|
|
|
*r = new posixReadCloser(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-26 16:42:11 -05:00
|
|
|
|
|
|
|
Error *CreateWrite(const char *filename, WriteCloser **w)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
*w = NULL;
|
|
|
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
|
|
if (fd < 0)
|
|
|
|
return new posixError(errno);
|
|
|
|
*w = new posixWriteCloser(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|