/
RawFileIO.cpp
289 lines (237 loc) · 7.38 KB
/
RawFileIO.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*****************************************************************************
* Author: Valient Gough <vgough@pobox.com>
*
*****************************************************************************
* Copyright (c) 2004, Valient Gough
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef linux
#define _XOPEN_SOURCE 500 // pick up pread , pwrite
#endif
#include "easylogging++.h"
#include <cerrno>
#include <cinttypes>
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utility>
#include "Error.h"
#include "FileIO.h"
#include "RawFileIO.h"
using namespace std;
namespace encfs {
static Interface RawFileIO_iface("FileIO/Raw", 1, 0, 0);
FileIO *NewRawFileIO(const Interface &iface) {
(void)iface;
return new RawFileIO();
}
inline void swap(int &x, int &y) {
int tmp = x;
x = y;
y = tmp;
}
RawFileIO::RawFileIO()
: knownSize(false), fileSize(0), fd(-1), oldfd(-1), canWrite(false) {}
RawFileIO::RawFileIO(std::string fileName)
: name(std::move(fileName)),
knownSize(false),
fileSize(0),
fd(-1),
oldfd(-1),
canWrite(false) {}
RawFileIO::~RawFileIO() {
int _fd = -1;
int _oldfd = -1;
swap(_fd, fd);
swap(_oldfd, oldfd);
if (_oldfd != -1) {
close(_oldfd);
}
if (_fd != -1) {
close(_fd);
}
}
Interface RawFileIO::interface() const { return RawFileIO_iface; }
/*
Workaround for opening a file for write when permissions don't allow.
Since the kernel has already checked permissions, we can assume it is ok to
provide access. So force it by changing permissions temporarily. Should
be called with a lock around it so that there won't be a race condition
with calls to lstat picking up the wrong permissions.
This works around the problem described in
https://github.com/vgough/encfs/issues/181
Without this, "umask 0777 ; echo foo > bar" fails.
*/
static int open_readonly_workaround(const char *path, int flags) {
int fd = -1;
struct stat stbuf;
memset(&stbuf, 0, sizeof(struct stat));
if (lstat(path, &stbuf) != -1) {
// make sure user has read/write permission..
chmod(path, stbuf.st_mode | 0600);
fd = ::open(path, flags);
chmod(path, stbuf.st_mode);
} else {
RLOG(INFO) << "can't stat file " << path;
}
return fd;
}
/*
We shouldn't have to support all possible open flags, so untaint the flags
argument by only taking ones we understand and accept.
- Since the kernel has already done permission tests before calling us, we
shouldn't have to worry about access control.
- Basically we just need to distinguish between read and write flags
- Also keep the O_LARGEFILE flag, in case the underlying filesystem needs
it..
*/
int RawFileIO::open(int flags) {
bool requestWrite = (((flags & O_RDWR) != 0) || ((flags & O_WRONLY) != 0));
VLOG(1) << "open call, requestWrite = " << requestWrite;
int result = 0;
// if we have a descriptor and it is writable, or we don't need writable..
if ((fd >= 0) && (canWrite || !requestWrite)) {
VLOG(1) << "using existing file descriptor";
result = fd; // success
} else {
int finalFlags = requestWrite ? O_RDWR : O_RDONLY;
#if defined(O_LARGEFILE)
if ((flags & O_LARGEFILE) != 0) {
finalFlags |= O_LARGEFILE;
}
#else
#warning O_LARGEFILE not supported
#endif
int newFd = ::open(name.c_str(), finalFlags);
VLOG(1) << "open file with flags " << finalFlags << ", result = " << newFd;
if ((newFd == -1) && (errno == EACCES)) {
VLOG(1) << "using readonly workaround for open";
newFd = open_readonly_workaround(name.c_str(), finalFlags);
}
if (newFd >= 0) {
if (oldfd >= 0) {
RLOG(ERROR) << "leaking FD?: oldfd = " << oldfd << ", fd = " << fd
<< ", newfd = " << newFd;
}
// the old fd might still be in use, so just keep it around for
// now.
canWrite = requestWrite;
oldfd = fd;
result = fd = newFd;
} else {
result = -errno;
RLOG(DEBUG) << "::open error: " << strerror(errno);
}
}
return result;
}
int RawFileIO::getAttr(struct stat *stbuf) const {
int res = lstat(name.c_str(), stbuf);
int eno = errno;
if (res < 0) {
RLOG(DEBUG) << "getAttr error on " << name << ": " << strerror(eno);
}
return (res < 0) ? -eno : 0;
}
void RawFileIO::setFileName(const char *fileName) { name = fileName; }
const char *RawFileIO::getFileName() const { return name.c_str(); }
off_t RawFileIO::getSize() const {
if (!knownSize) {
struct stat stbuf;
memset(&stbuf, 0, sizeof(struct stat));
int res = lstat(name.c_str(), &stbuf);
if (res == 0) {
const_cast<RawFileIO *>(this)->fileSize = stbuf.st_size;
const_cast<RawFileIO *>(this)->knownSize = true;
return fileSize;
}
RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(errno);
return -1;
}
return fileSize;
}
ssize_t RawFileIO::read(const IORequest &req) const {
rAssert(fd >= 0);
ssize_t readSize = pread(fd, req.data, req.dataLen, req.offset);
if (readSize < 0) {
RLOG(WARNING) << "read failed at offset " << req.offset << " for "
<< req.dataLen << " bytes: " << strerror(errno);
}
return readSize;
}
bool RawFileIO::write(const IORequest &req) {
rAssert(fd >= 0);
rAssert(canWrite);
int retrys = 10;
void *buf = req.data;
ssize_t bytes = req.dataLen;
off_t offset = req.offset;
while ((bytes != 0) && retrys > 0) {
ssize_t writeSize = ::pwrite(fd, buf, bytes, offset);
if (writeSize < 0) {
knownSize = false;
RLOG(WARNING) << "write failed at offset " << offset << " for " << bytes
<< " bytes: " << strerror(errno);
return false;
}
bytes -= writeSize;
offset += writeSize;
buf = (void *)((char *)buf + writeSize);
--retrys;
}
if (bytes != 0) {
RLOG(ERROR) << "Write error: wrote " << req.dataLen - bytes << " bytes of "
<< req.dataLen << ", max retries reached";
knownSize = false;
return false;
}
if (knownSize) {
off_t last = req.offset + req.dataLen;
if (last > fileSize) {
fileSize = last;
}
}
return true;
}
int RawFileIO::truncate(off_t size) {
int res;
if (fd >= 0 && canWrite) {
res = ::ftruncate(fd, size);
} else {
res = ::truncate(name.c_str(), size);
}
if (res < 0) {
int eno = errno;
RLOG(WARNING) << "truncate failed for " << name << " (" << fd << ") size "
<< size << ", error " << strerror(eno);
res = -eno;
knownSize = false;
} else {
res = 0;
fileSize = size;
knownSize = true;
}
if (fd >= 0 && canWrite) {
#if defined(HAVE_FDATASYNC)
::fdatasync(fd);
#else
::fsync(fd);
#endif
}
return res;
}
bool RawFileIO::isWritable() const { return canWrite; }
} // namespace encfs