Belle II Software development
File Class Reference
Inheritance diagram for File:
FileDescriptor

Public Member Functions

 File (int fd)
 
 File (const std::string &path, const std::string &mode="r")
 
void open (const std::string &path, const std::string &mode="r")
 
void unlink (const std::string &path)
 
virtual size_t write (const void *v, size_t count)
 
virtual size_t read (void *v, size_t count)
 
int get_fd () const
 
bool select (int sec=-1, int usec=-1)
 
bool select2 (int sec=-1, int usec=-1)
 
bool close ()
 

Static Public Member Functions

static bool exist (const std::string &filename)
 

Protected Attributes

int m_fd
 

Detailed Description

Definition at line 21 of file File.h.

Constructor & Destructor Documentation

◆ File() [1/3]

File ( )
inline

Definition at line 27 of file File.h.

27{}

◆ File() [2/3]

File ( int  fd)
inline

Definition at line 28 of file File.h.

28: FileDescriptor(fd) {}

◆ File() [3/3]

File ( const std::string &  path,
const std::string &  mode = "r" 
)
inline

Definition at line 29 of file File.h.

30 { open(path, mode); }

◆ ~File()

virtual ~File ( )
inlinevirtual

Definition at line 31 of file File.h.

31{}

Member Function Documentation

◆ close()

bool close ( )
inherited

Definition at line 90 of file FileDescriptor.cc.

91{
92 if (m_fd > 0) {
93 if (::close(m_fd) != 0) {
94 return false;
95 }
96 }
97 m_fd = -1;
98 return true;
99}

◆ exist()

bool exist ( const std::string &  filename)
static

Definition at line 88 of file File.cc.

89{
90 struct stat st;
91 if (stat(filename.c_str(), &st) != 0) {
92 return false;
93 } else {
94 mode_t m = st.st_mode;
95 if (S_ISDIR(m)) {
96 return false;
97 } else {
98 return true;
99 }
100 }
101 return false;
102}

◆ get_fd()

int get_fd ( ) const
inherited

Definition at line 33 of file FileDescriptor.cc.

34{
35 return m_fd;
36}

◆ open()

void open ( const std::string &  path,
const std::string &  mode = "r" 
)

Definition at line 21 of file File.cc.

22{
23 int mode = O_RDONLY;
24 if (mode_s.find("w") != std::string::npos) {
25 mode = O_WRONLY;
26 if (mode_s.find("r") != std::string::npos) {
27 mode = O_RDWR;
28 } else {
29 mode |= O_CREAT;
30 }
31 }
32 if ((m_fd = ::open(path.c_str(), mode, 0666)) < 0) {
33 perror("open");
34 throw (IOException("Failed to open file : %s", path.c_str()));
35 }
36}

◆ read()

size_t read ( void *  v,
size_t  count 
)
virtual

Definition at line 69 of file File.cc.

70{
71 size_t c = 0;
72 while (c < count) {
73 errno = 0;
74 int ret = ::read(m_fd, ((unsigned char*)buf + c), (count - c));
75 if (ret <= 0) {
76 switch (errno) {
77 case EINTR: continue;
78 case EAGAIN: continue;
79 default:
80 throw (IOException("Error while reading. %d", errno));
81 }
82 }
83 c += ret;
84 }
85 return c;
86}

◆ select()

bool select ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 38 of file FileDescriptor.cc.

39{
40 if (m_fd <= 0) {
41 return false;
42 }
43 fd_set fds;
44 FD_ZERO(&fds);
45 FD_SET(m_fd, &fds);
46 int ret;
47 if (sec >= 0 && usec >= 0) {
48 timeval t = {sec, usec};
49 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
50 } else {
51 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
52 }
53 if (ret < 0) {
54 perror("select");
55 throw (IOException("Failed to select"));
56 }
57 if (FD_ISSET(m_fd, &fds)) {
58 return true;
59 } else {
60 return false;
61 }
62}

◆ select2()

bool select2 ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 64 of file FileDescriptor.cc.

65{
66 if (m_fd <= 0) {
67 return false;
68 }
69 fd_set fds;
70 FD_ZERO(&fds);
71 FD_SET(m_fd, &fds);
72 int ret;
73 if (sec >= 0 && usec >= 0) {
74 timeval t = {sec, usec};
75 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
76 } else {
77 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
78 }
79 if (ret < 0) {
80 perror("select");
81 throw (IOException("Failed to select"));
82 }
83 if (FD_ISSET(m_fd, &fds)) {
84 return true;
85 } else {
86 return false;
87 }
88}

◆ unlink()

void unlink ( const std::string &  path)

Definition at line 38 of file File.cc.

39{
40 if ((::unlink(path.c_str())) < 0) {
41 perror("unlink");
42 }
43 close();
44}

◆ write()

size_t write ( const void *  v,
size_t  count 
)
virtual

Definition at line 46 of file File.cc.

47{
48 size_t c = 0;
49 while (c < count) {
50 errno = 0;
51 int ret = ::write(m_fd, ((unsigned char*)buf + c), (count - c));
52 if (ret <= 0) {
53 switch (errno) {
54 case EINTR: continue;
55 case ENETUNREACH:
56 case EHOSTUNREACH:
57 case ETIMEDOUT:
58 usleep(500);
59 continue;
60 default:
61 throw (IOException("Error while writing"));
62 }
63 }
64 c += ret;
65 }
66 return c;
67}

Member Data Documentation

◆ m_fd

int m_fd
protectedinherited

Definition at line 31 of file FileDescriptor.h.


The documentation for this class was generated from the following files: