00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <errno.h>
00040 #include "FileIOClient.h"
00041 #include "IO.h"
00042
00043 namespace oasys {
00044
00045
00046 FileIOClient::FileIOClient()
00047 : FdIOClient(-1)
00048 {
00049 }
00050
00051
00052 FileIOClient::~FileIOClient()
00053 {
00054 if (fd_ != -1)
00055 close();
00056 }
00057
00058
00059 int
00060 FileIOClient::open(const char* path, int flags, int* errnop)
00061 {
00062 path_.assign(path);
00063 fd_ = IO::open(path, flags, errnop, logpath_);
00064 return fd_;
00065 }
00066
00067
00068 int
00069 FileIOClient::open(const char* path, int flags, mode_t mode, int* errnop)
00070 {
00071 path_.assign(path);
00072 fd_ = IO::open(path, flags, mode, errnop, logpath_);
00073 return fd_;
00074 }
00075
00076
00077 int
00078 FileIOClient::close()
00079 {
00080 int ret = IO::close(fd_, logpath_, path_.c_str());
00081 fd_ = -1;
00082 return ret;
00083 }
00084
00085
00086 int
00087 FileIOClient::reopen(int flags)
00088 {
00089 ASSERT(path_.length() != 0);
00090 fd_ = IO::open(path_.c_str(), flags, NULL, logpath_);
00091 return fd_;
00092 }
00093
00094
00095 int
00096 FileIOClient::unlink()
00097 {
00098 if (path_.length() == 0)
00099 return 0;
00100
00101 int ret = 0;
00102 ret = IO::unlink(path_.c_str(), logpath_);
00103 path_.assign("");
00104 return ret;
00105 }
00106
00107
00108 int
00109 FileIOClient::lseek(off_t offset, int whence)
00110 {
00111 return IO::lseek(fd_, offset, whence, logpath_);
00112 }
00113
00114
00115 int
00116 FileIOClient::truncate(off_t length)
00117 {
00118 return IO::truncate(fd_, length, logpath_);
00119 }
00120
00121
00122 int
00123 FileIOClient::mkstemp(char* temp)
00124 {
00125 if (fd_ != -1) {
00126 log_err("can't call mkstemp on open file");
00127 return -1;
00128 }
00129
00130 fd_ = IO::mkstemp(temp, logpath_);
00131
00132 path_.assign(temp);
00133 return fd_;
00134 }
00135
00136
00137 int
00138 FileIOClient::stat(struct stat* buf)
00139 {
00140 return IO::stat(path_.c_str(), buf, logpath_);
00141 }
00142
00143
00144 int
00145 FileIOClient::lstat(struct stat* buf)
00146 {
00147 return IO::lstat(path_.c_str(), buf, logpath_);
00148 }
00149
00150
00151 int
00152 FileIOClient::copy_contents(size_t len, FileIOClient* dest)
00153 {
00154 char buf[1024];
00155 int cnt;
00156 int origlen = len;
00157
00158 while (len > 0) {
00159 cnt = (len < sizeof(buf)) ? len : sizeof(buf);
00160
00161 if (readall(buf, cnt) != cnt) {
00162 log_err("copy_contents: error reading %d bytes: %s",
00163 cnt, strerror(errno));
00164 return -1;
00165 }
00166
00167 if (dest->writeall(buf, cnt) != cnt) {
00168 log_err("copy_contents: error writing %d bytes: %s",
00169 cnt, strerror(errno));
00170 return -1;
00171 }
00172
00173 len -= cnt;
00174 }
00175
00176 return origlen;
00177 }
00178
00179 }