root/trunk/whisperlib/common/io/file/fd.cc

Revision 7, 3.7 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <errno.h>
37
38 #include "common/base/common.h"
39 #include "common/base/errno.h"
40 #include "common/base/log.h"
41 #include "common/io/file/fd.h"
42
43 namespace io {
44
45 FileDescriptor::FileDescriptor()
46   : fd_(INVALID_FD_VALUE),
47     is_live_(false),
48     is_eof_(true),
49     close_on_exit_(true) {
50 }
51
52 FileDescriptor::~FileDescriptor() {
53   Close();
54 }
55
56 bool FileDescriptor::OpenFD(int fd,
57                             bool is_live,       // = false
58                             bool duplicate) {   // = false
59   CHECK(!is_open());
60   if ( duplicate ) {
61     fd_ = dup(fd);
62     if ( fd_ == INVALID_FD_VALUE ) {
63       LOG_ERROR << " dup (fd=" << fd << ") failed with error: "
64                 << GetLastSystemErrorDescription();
65       return false;
66     }
67   } else {
68     fd_ = fd;
69   }
70   close_on_exit_ = duplicate;
71   is_live_ = is_live;
72   is_eof_ = false;
73   DCHECK(is_open());
74
75   return true;
76 }
77
78 void FileDescriptor::Close() {
79   if ( close_on_exit_ ) {
80     ::close(fd_);
81   }
82   fd_ = INVALID_FD_VALUE;
83   is_live_ = false;
84   is_eof_ = true;
85   close_on_exit_ = false;
86 }
87
88 int32 FileDescriptor::Read(void* buf, int32 len) {
89   DCHECK(is_open());
90
91   int32 cb = ::read(fd_, buf, len);
92   if ( cb < 0 ) {
93     if ( GetLastSystemError() == EAGAIN ) {
94       cb = 0;
95       SetLastSystemError(0);
96     } else {
97       LOG_ERROR << "::read failed: " << GetLastSystemErrorDescription();
98       return -1;
99     }
100   }
101   if ( cb < len && !is_live_ ) {
102     LOG_DEBUG <<  "Reached EOF for fd: " << fd_;
103     is_eof_ = true;
104   }
105
106   return cb;
107 }
108
109 int32 FileDescriptor::Write(const void* buf, int32 len) {
110   DCHECK(is_open());
111   const int32 cb = ::write(fd_, buf, len);
112   if ( cb < 0 ) {
113     LOG_ERROR << "::write failed: " << GetLastSystemErrorDescription();
114     return cb;
115   }
116   return cb;
117 }
118
119 int32 FileDescriptor::Skip(int32 len) {
120   DCHECK(is_open());
121
122   uint8 tmp_buf[256] = { 0, };
123   int32 total_read = 0;
124   int32 crt_read = 0;
125   while ( total_read < len ) {
126     const int32 to_read = min(static_cast<int32>(sizeof(tmp_buf)), len);
127     if ( (crt_read = Read(tmp_buf, to_read) <= 0 ) ) {
128       break;
129     }
130     total_read += crt_read;
131   }
132   return total_read;
133 }
134 }
Note: See TracBrowser for help on using the browser.