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