25 #ifndef DOZERG_FILE_H_20120119 26 #define DOZERG_FILE_H_20120119 29 #include <sys/types.h> 36 #include "tools/system.hh" 46 static const int kInvalidFd = -1;
51 static std::string
ErrMsg(){
return tools::ErrorMsg(errno);}
65 bool valid()
const{
return (fd_ >= 0);}
70 int fd()
const{
return fd_;}
87 virtual int fdType()
const = 0;
99 std::string
filename()
const{
return tools::GetFilenameByFd(fd_);}
111 if(0 != ::fstat(
fd(), &st))
125 if(0 != ::fstat(
fd(), &st))
127 return (0 == st.st_nlink);
140 const int oldflag = ::fcntl(fd_, F_GETFL);
143 const int newflag = (on ? oldflag & ~O_NONBLOCK : oldflag | O_NONBLOCK);
144 if(oldflag == newflag)
146 if(::fcntl(fd_, F_SETFL, newflag) < 0)
185 static const int kFlagsDefault = O_RDONLY;
186 static const int kModeDefault = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
188 static const int kFdType = 1;
195 static bool Unlink(
const std::string & pathname){
198 return (0 == ::unlink(pathname.c_str()));
207 static bool Rename(
const std::string & oldfile,
const std::string & newfile){
208 if(oldfile.empty() || newfile.empty())
210 return (0 == ::rename(oldfile.c_str(), newfile.c_str()));
224 explicit CFile(
const char * pathname,
int flags = kFlagsDefault, mode_t mode = kModeDefault){
225 this->open(pathname, flags, mode);
227 explicit CFile(
const std::string & pathname,
int flags = kFlagsDefault, mode_t mode = kModeDefault){
228 this->open(pathname, flags, mode);
231 virtual bool open(
const char * pathname,
int flags = kFlagsDefault, mode_t mode = kModeDefault){
236 fd_ = ::open(pathname, flags, mode);
239 virtual bool open(
const std::string & pathname,
int flags = kFlagsDefault, mode_t mode = kModeDefault){
240 return this->open(pathname.c_str(), flags, mode);
276 ssize_t
read(
char * buf,
size_t size){
279 return ::read(
fd(), buf, size);
284 bool read(std::vector<char> & buf,
size_t size,
bool append){
285 return readData(buf, size, append);
290 bool read(std::string & buf,
size_t size,
bool append){
291 return readData(buf, size, append);
303 ssize_t write(
const char * buf,
size_t size){
306 return ::write(
fd(), buf, size);
308 ssize_t write(
const std::vector<char> & buf){
309 return this->write(&buf[0], buf.size());
311 ssize_t write(
const std::string & buf){
312 return this->write(&buf[0], buf.size());
328 bool seek(off_t offset,
int whence){
331 return ((off_t)-1 != ::lseek(
fd(), offset, whence));
334 bool seek64(off64_t offset,
int whence){
337 return ((off_t)-1 != ::lseek64(
fd(), offset, whence));
352 return ::lseek(
fd(), 0, SEEK_CUR);
355 off64_t tell64()
const{
358 return ::lseek64(
fd(), 0, SEEK_CUR);
362 #ifdef __HAS_FTRUNCATE 370 bool truncate(off_t
length){
373 return (0 == ::ftruncate(
fd(), length));
384 return Unlink(__MyBase::filename());
392 bool rename(
const std::string & newfile){
393 if(!
valid() || newfile.empty())
395 std::string fname = __MyBase::filename();
398 return Rename(fname, newfile);
408 void copyFrom(
const CFile & other){
412 fd_ = ::dup(other.
fd());
414 fd_ = ::dup2(other.
fd(), fd_);
418 bool readData(Buf & buf,
size_t size,
bool append){
421 const size_t from = (append ? buf.size() : 0);
422 buf.resize(from + size);
423 const ssize_t ret = this->read(&buf[from], size);
424 buf.resize(from + (ret > 0 ? ret : 0));
virtual std::string toString() const
Get readable description of this object.
Definition: file.hh:163
int fdType() const
Get type id of this object.
Definition: file.hh:254
std::string toString() const
Get readable description of this object.
Definition: file.hh:400
virtual const char * fdTypeName() const =0
Get type name of this object.
bool valid() const
Test if this object is a valid file descriptor.
Definition: file.hh:65
bool deleted() const
Test if file is deleted.
Definition: file.hh:121
Abstract interface of fd (file descriptor).
Definition: file.hh:43
Definition: to_string.hh:43
static bool Unlink(const std::string &pathname)
Remove a file.
Definition: file.hh:195
IFileDesc()
Construct a default object.
Definition: file.hh:55
static std::string ErrMsg()
Get latest errno and error message.
Definition: file.hh:51
ssize_t read(char *buf, size_t size)
Definition: file.hh:276
virtual ~IFileDesc()
Destroy this object.
Definition: file.hh:60
Regular file operations.
Definition: file.hh:181
static bool Rename(const std::string &oldfile, const std::string &newfile)
Rename a file.
Definition: file.hh:207
void close()
Close this fd.
Definition: file.hh:153
virtual bool unlink()
Remove file.
Definition: file.hh:381
int fd() const
Get fd (file descriptor).
Definition: file.hh:70
bool block(bool on)
Set block/non-block for operations.
Definition: file.hh:137
bool read(std::vector< char > &buf, size_t size, bool append)
Definition: file.hh:284
bool rename(const std::string &newfile)
Rename file.
Definition: file.hh:392
virtual int fdType() const =0
Get type id of this object.
bool read(std::string &buf, size_t size, bool append)
Definition: file.hh:290
CFile()
Default constructor.
Definition: file.hh:215
const char * fdTypeName() const
Get readable description of this object.
Definition: file.hh:259
off_t length() const
Get byte size of file opened by this object.
Definition: file.hh:107
std::string filename() const
Get file name opened by this object.
Definition: file.hh:99