-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfile_descriptor.cc
85 lines (68 loc) · 1.88 KB
/
file_descriptor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "file_descriptor.hh"
#include "util.hh"
#include <unistd.h>
using namespace std;
/* construct from fd number */
FileDescriptor::FileDescriptor( const int fd )
: fd_( fd ),
eof_( false ),
read_count_( 0 ),
write_count_( 0 )
{}
/* move constructor */
FileDescriptor::FileDescriptor( FileDescriptor && other )
: fd_( other.fd_ ),
eof_( other.eof_ ),
read_count_( other.read_count_ ),
write_count_( other.write_count_ )
{
/* mark other file descriptor as inactive */
other.fd_ = -1;
}
/* destructor */
FileDescriptor::~FileDescriptor()
{
if ( fd_ < 0 ) { /* has already been moved away */
return;
}
try {
SystemCall( "close", close( fd_ ) );
} catch ( const exception & e ) { /* don't throw from destructor */
print_exception( e );
}
}
/* attempt to write a portion of a string */
string::const_iterator FileDescriptor::write( const string::const_iterator & begin,
const string::const_iterator & end )
{
if ( begin >= end ) {
throw runtime_error( "nothing to write" );
}
ssize_t bytes_written = SystemCall( "write", ::write( fd_, &*begin, end - begin ) );
if ( bytes_written == 0 ) {
throw runtime_error( "write returned 0" );
}
register_write();
return begin + bytes_written;
}
/* read method */
string FileDescriptor::read( const size_t limit )
{
constexpr size_t BUFFER_SIZE = 1024 * 1024; /* maximum size of a read */
char buffer[ BUFFER_SIZE ];
ssize_t bytes_read = SystemCall( "read", ::read( fd_, buffer, min( BUFFER_SIZE, limit ) ) );
if ( bytes_read == 0 ) {
set_eof();
}
register_read();
return string( buffer, bytes_read );
}
/* write method */
string::const_iterator FileDescriptor::write( const std::string & buffer, const bool write_all )
{
auto it = buffer.begin();
do {
it = write( it, buffer.end() );
} while ( write_all and (it != buffer.end()) );
return it;
}