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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "file_io.h"
#include "file_access.h"
#include <sys/stat.h>
#include <fcntl.h> //open
#include <unistd.h> //close, read, write
using namespace zen;
const FileBase::FileHandle FileBase::invalidHandleValue_ = -1;
FileBase::~FileBase()
{
if (fileHandle_ != invalidHandleValue_)
try
{
close(); //throw FileError
}
catch (FileError&) { assert(false); }
}
void FileBase::close() //throw FileError
{
if (fileHandle_ == invalidHandleValue_)
throw FileError(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(getFilePath())), L"Contract error: close() called more than once.");
ZEN_ON_SCOPE_EXIT(fileHandle_ = invalidHandleValue_);
//no need to clean-up on failure here (just like there is no clean on FileOutput::write failure!) => FileOutput is not transactional!
if (::close(fileHandle_) != 0)
THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(getFilePath())), "close");
}
//----------------------------------------------------------------------------------------------------
namespace
{
FileBase::FileHandle openHandleForRead(const Zstring& filePath) //throw FileError, ErrorFileLocked
{
//caveat: check for file types that block during open(): character device, block device, named pipe
struct ::stat fileInfo = {};
if (::stat(filePath.c_str(), &fileInfo) == 0) //follows symlinks
{
if (!S_ISREG(fileInfo.st_mode) &&
!S_ISDIR(fileInfo.st_mode) && //open() will fail with "EISDIR: Is a directory" => nice
!S_ISLNK(fileInfo.st_mode)) //?? shouldn't be possible after successful stat()
{
const std::wstring typeName = [m = fileInfo.st_mode]
{
std::wstring name =
S_ISCHR (m) ? L"character device" : //e.g. /dev/null
S_ISBLK (m) ? L"block device" : //e.g. /dev/sda1
S_ISFIFO(m) ? L"FIFO, named pipe" :
S_ISSOCK(m) ? L"socket" : L""; //doesn't block but open() error is unclear: "ENXIO: No such device or address"
if (!name.empty())
name += L", ";
return name + printNumber<std::wstring>(L"0%06o", m & S_IFMT);
}();
throw FileError(replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filePath)),
_("Unsupported item type.") + L" [" + typeName + L']');
}
}
//else: let ::open() fail for errors like "not existing"
//don't use O_DIRECT: https://yarchive.net/comp/linux/o_direct.html
const FileBase::FileHandle fileHandle = ::open(filePath.c_str(), O_RDONLY | O_CLOEXEC);
if (fileHandle == -1) //don't check "< 0" -> docu seems to allow "-2" to be a valid file handle
THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filePath)), "open");
return fileHandle; //pass ownership
}
}
FileInput::FileInput(FileHandle handle, const Zstring& filePath, const IOCallback& notifyUnbufferedIO) :
FileBase(handle, filePath), notifyUnbufferedIO_(notifyUnbufferedIO) {}
FileInput::FileInput(const Zstring& filePath, const IOCallback& notifyUnbufferedIO) :
FileBase(openHandleForRead(filePath), filePath), //throw FileError, ErrorFileLocked
notifyUnbufferedIO_(notifyUnbufferedIO)
{
//optimize read-ahead on input file:
if (::posix_fadvise(getHandle(), 0, 0, POSIX_FADV_SEQUENTIAL) != 0)
THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file %x."), L"%x", fmtPath(filePath)), "posix_fadvise");
}
size_t FileInput::tryRead(void* buffer, size_t bytesToRead) //throw FileError, ErrorFileLocked; may return short, only 0 means EOF!
{
if (bytesToRead == 0) //"read() with a count of 0 returns zero" => indistinguishable from end of file! => check!
throw std::logic_error("Contract violation! " + std::string(__FILE__) + ':' + numberTo<std::string>(__LINE__));
assert(bytesToRead == getBlockSize());
ssize_t bytesRead = 0;
do
{
bytesRead = ::read(getHandle(), buffer, bytesToRead);
}
while (bytesRead < 0 && errno == EINTR); //Compare copy_reg() in copy.c: ftp://ftp.gnu.org/gnu/coreutils/coreutils-8.23.tar.xz
//EINTR is not checked on macOS' copyfile: https://opensource.apple.com/source/copyfile/copyfile-146/copyfile.c.auto.html
//read() on macOS: https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/read.2.html
if (bytesRead < 0)
THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file %x."), L"%x", fmtPath(getFilePath())), "read");
if (static_cast<size_t>(bytesRead) > bytesToRead) //better safe than sorry
throw FileError(replaceCpy(_("Cannot read file %x."), L"%x", fmtPath(getFilePath())), formatSystemError("ReadFile", L"", L"Buffer overflow."));
//if ::read is interrupted (EINTR) right in the middle, it will return successfully with "bytesRead < bytesToRead"
return bytesRead; //"zero indicates end of file"
}
size_t FileInput::read(void* buffer, size_t bytesToRead) //throw FileError, ErrorFileLocked, X; return "bytesToRead" bytes unless end of stream!
{
/*
FFS 8.9-9.5 perf issues on macOS: https://freefilesync.org/forum/viewtopic.php?t=4808
app-level buffering is essential to optimize random data sizes; e.g. "export file list":
=> big perf improvement on Windows, Linux. No significant improvement on macOS in tests
impact on stream-based file copy:
=> no drawback vs block-wise copy loop on Linux, HOWEVER: big perf issue on macOS!
Possible cause of macOS perf issue unclear:
- getting rid of std::vector::resize() and std::vector::erase() "fixed" the problem
=> costly zero-initializing memory? problem with inlining? QOI issue of std:vector on clang/macOS?
- replacing std::copy() with memcpy() also *seems* to have improved speed "somewhat"
*/
const size_t blockSize = getBlockSize();
assert(memBuf_.size() >= blockSize);
assert(bufPos_ <= bufPosEnd_ && bufPosEnd_ <= memBuf_.size());
auto it = static_cast<std::byte*>(buffer);
const auto itEnd = it + bytesToRead;
for (;;)
{
const size_t junkSize = std::min(static_cast<size_t>(itEnd - it), bufPosEnd_ - bufPos_);
std::memcpy(it, &memBuf_[0] + bufPos_ /*caveat: vector debug checks*/, junkSize);
bufPos_ += junkSize;
it += junkSize;
if (it == itEnd)
break;
//--------------------------------------------------------------------
const size_t bytesRead = tryRead(&memBuf_[0], blockSize); //throw FileError, ErrorFileLocked; may return short, only 0 means EOF! => CONTRACT: bytesToRead > 0
bufPos_ = 0;
bufPosEnd_ = bytesRead;
if (notifyUnbufferedIO_) notifyUnbufferedIO_(bytesRead); //throw X
if (bytesRead == 0) //end of file
break;
}
return it - static_cast<std::byte*>(buffer);
}
//----------------------------------------------------------------------------------------------------
namespace
{
FileBase::FileHandle openHandleForWrite(const Zstring& filePath, FileOutput::AccessFlag access) //throw FileError, ErrorTargetExisting
{
//checkForUnsupportedType(filePath); -> not needed, open() + O_WRONLY should fail fast
const FileBase::FileHandle fileHandle = ::open(filePath.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | (access == FileOutput::ACC_CREATE_NEW ? O_EXCL : O_TRUNC),
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); //0666
if (fileHandle == -1)
{
const int ec = errno; //copy before making other system calls!
const std::wstring errorMsg = replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(filePath));
const std::wstring errorDescr = formatSystemError("open", ec);
if (ec == EEXIST)
throw ErrorTargetExisting(errorMsg, errorDescr);
//if (ec == ENOENT) throw ErrorTargetPathMissing(errorMsg, errorDescr);
throw FileError(errorMsg, errorDescr);
}
return fileHandle; //pass ownership
}
}
FileOutput::FileOutput(FileHandle handle, const Zstring& filePath, const IOCallback& notifyUnbufferedIO) :
FileBase(handle, filePath), notifyUnbufferedIO_(notifyUnbufferedIO) {}
FileOutput::FileOutput(AccessFlag access, const Zstring& filePath, const IOCallback& notifyUnbufferedIO) :
FileBase(openHandleForWrite(filePath, access), filePath), notifyUnbufferedIO_(notifyUnbufferedIO) {} //throw FileError, ErrorTargetExisting
FileOutput::~FileOutput()
{
notifyUnbufferedIO_ = nullptr; //no call-backs during destruction!!!
try
{
flushBuffers(); //throw FileError, (X)
}
catch (...) { assert(false); }
}
size_t FileOutput::tryWrite(const void* buffer, size_t bytesToWrite) //throw FileError; may return short! CONTRACT: bytesToWrite > 0
{
if (bytesToWrite == 0)
throw std::logic_error("Contract violation! " + std::string(__FILE__) + ':' + numberTo<std::string>(__LINE__));
assert(bytesToWrite <= getBlockSize());
ssize_t bytesWritten = 0;
do
{
bytesWritten = ::write(getHandle(), buffer, bytesToWrite);
}
while (bytesWritten < 0 && errno == EINTR);
//write() on macOS: https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/write.2.html
if (bytesWritten <= 0)
{
if (bytesWritten == 0) //comment in safe-read.c suggests to treat this as an error due to buggy drivers
errno = ENOSPC;
THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(getFilePath())), "write");
}
if (bytesWritten > static_cast<ssize_t>(bytesToWrite)) //better safe than sorry
throw FileError(replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(getFilePath())), formatSystemError("write", L"", L"Buffer overflow."));
//if ::write() is interrupted (EINTR) right in the middle, it will return successfully with "bytesWritten < bytesToWrite"!
return bytesWritten;
}
void FileOutput::write(const void* buffer, size_t bytesToWrite) //throw FileError, X
{
const size_t blockSize = getBlockSize();
assert(memBuf_.size() >= blockSize);
assert(bufPos_ <= bufPosEnd_ && bufPosEnd_ <= memBuf_.size());
auto it = static_cast<const std::byte*>(buffer);
const auto itEnd = it + bytesToWrite;
for (;;)
{
if (memBuf_.size() - bufPos_ < blockSize) //support memBuf_.size() > blockSize to reduce memmove()s, but perf test shows: not really needed!
// || bufPos_ == bufPosEnd_) -> not needed while memBuf_.size() == blockSize
{
std::memmove(&memBuf_[0], &memBuf_[0] + bufPos_, bufPosEnd_ - bufPos_);
bufPosEnd_ -= bufPos_;
bufPos_ = 0;
}
const size_t junkSize = std::min(static_cast<size_t>(itEnd - it), blockSize - (bufPosEnd_ - bufPos_));
std::memcpy(&memBuf_[0] + bufPosEnd_ /*caveat: vector debug checks*/, it, junkSize);
bufPosEnd_ += junkSize;
it += junkSize;
if (it == itEnd)
return;
//--------------------------------------------------------------------
const size_t bytesWritten = tryWrite(&memBuf_[bufPos_], blockSize); //throw FileError; may return short! CONTRACT: bytesToWrite > 0
bufPos_ += bytesWritten;
if (notifyUnbufferedIO_) notifyUnbufferedIO_(bytesWritten); //throw X!
}
}
void FileOutput::flushBuffers() //throw FileError, X
{
assert(bufPosEnd_ - bufPos_ <= getBlockSize());
assert(bufPos_ <= bufPosEnd_ && bufPosEnd_ <= memBuf_.size());
while (bufPos_ != bufPosEnd_)
{
const size_t bytesWritten = tryWrite(&memBuf_[bufPos_], bufPosEnd_ - bufPos_); //throw FileError; may return short! CONTRACT: bytesToWrite > 0
bufPos_ += bytesWritten;
if (notifyUnbufferedIO_) notifyUnbufferedIO_(bytesWritten); //throw X!
}
}
void FileOutput::finalize() //throw FileError, X
{
flushBuffers(); //throw FileError, X
//~FileBase() calls this one, too, but we want to propagate errors if any:
close(); //throw FileError
}
void FileOutput::preAllocateSpaceBestEffort(uint64_t expectedSize) //throw FileError
{
const FileHandle fh = getHandle();
//don't use potentially inefficient ::posix_fallocate!
const int rv = ::fallocate(fh, //int fd,
0, //int mode,
0, //off_t offset
expectedSize); //off_t len
if (rv != 0)
return; //may fail with EOPNOTSUPP, unlike posix_fallocate
}
|