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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
#include "dir_lock.h"
#include <utility>
#include <wx/utils.h>
#include <wx/timer.h>
#include <wx/log.h>
#include <wx/msgdlg.h>
#include <memory>
#include <boost/weak_ptr.hpp>
#include "../shared/string_conv.h"
#include "../shared/i18n.h"
#include "../shared/last_error.h"
#include "../shared/boost_thread_wrap.h" //include <boost/thread.hpp>
#include "../shared/loki/ScopeGuard.h"
#include "../shared/system_constants.h"
#include "../shared/guid.h"
#include "../shared/file_io.h"
#include "../shared/assert_static.h"
#include "../shared/serialize.h"
#include "../shared/build_info.h"
#include "../shared/int64.h"
#ifdef FFS_WIN
#include <tlhelp32.h>
#include <wx/msw/wrapwin.h> //includes "windows.h"
#include "../shared/long_path_prefix.h"
#elif defined FFS_LINUX
#include "../shared/file_handling.h"
#include <sys/stat.h>
#include <cerrno>
#include <unistd.h>
#endif
using namespace zen;
using namespace std::rel_ops;
namespace
{
const size_t EMIT_LIFE_SIGN_INTERVAL = 5000; //show life sign; unit [ms]
const size_t POLL_LIFE_SIGN_INTERVAL = 6000; //poll for life sign; unit [ms]
const size_t DETECT_EXITUS_INTERVAL = 30000; //assume abandoned lock; unit [ms]
const char LOCK_FORMAT_DESCR[] = "FreeFileSync";
const int LOCK_FORMAT_VER = 1; //lock file format version
typedef Zbase<Zchar, StorageDeepCopy> BasicString; //thread safe string class
}
class LifeSigns
{
public:
LifeSigns(const BasicString& lockfilename) : //throw()!!! siehe SharedDirLock()
lockfilename_(lockfilename) //thread safety: make deep copy!
{
threadObj = boost::thread(boost::cref(*this)); //localize all thread logic to this class!
}
~LifeSigns()
{
threadObj.interrupt(); //thread lifetime is subset of this instances's life
threadObj.join();
}
void operator()() const //thread entry
{
try
{
while (true)
{
boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(EMIT_LIFE_SIGN_INTERVAL)); //interruption point!
//actual work
emitLifeSign(); //throw ()
}
}
catch (const std::exception& e) //exceptions must be catched per thread
{
wxSafeShowMessage(wxString(_("An exception occurred!")) + wxT("(Dirlock)"), wxString::FromAscii(e.what())); //simple wxMessageBox won't do for threads
}
}
void emitLifeSign() const //try to append one byte...; throw()
{
const char buffer[1] = {' '};
#ifdef FFS_WIN
//ATTENTION: setting file pointer IS required! => use CreateFile/FILE_GENERIC_WRITE + SetFilePointerEx!
//although CreateFile/FILE_APPEND_DATA without SetFilePointerEx works locally, it MAY NOT work on some network shares creating a 4 gig file!!!
const HANDLE fileHandle = ::CreateFile(applyLongPathPrefix(lockfilename_.c_str()).c_str(),
FILE_GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
return;
Loki::ScopeGuard dummy = Loki::MakeGuard(::CloseHandle, fileHandle);
(void)dummy; //silence warning "unused variable"
const LARGE_INTEGER moveDist = {};
if (!::SetFilePointerEx(fileHandle, //__in HANDLE hFile,
moveDist, //__in LARGE_INTEGER liDistanceToMove,
NULL, //__out_opt PLARGE_INTEGER lpNewFilePointer,
FILE_END)) //__in DWORD dwMoveMethod
return;
DWORD bytesWritten = 0;
::WriteFile(fileHandle, //__in HANDLE hFile,
buffer, //__out LPVOID lpBuffer,
1, //__in DWORD nNumberOfBytesToRead,
&bytesWritten, //__out_opt LPDWORD lpNumberOfBytesWritten,
NULL); //__inout_opt LPOVERLAPPED lpOverlapped
#elif defined FFS_LINUX
const int fileHandle = ::open(lockfilename_.c_str(), O_WRONLY | O_APPEND); //O_EXCL contains a race condition on NFS file systems: http://linux.die.net/man/2/open
if (fileHandle == -1)
return;
Loki::ScopeGuard dummy = Loki::MakeGuard(::close, fileHandle);
(void)dummy; //silence warning "unused variable"
const ssize_t bytesWritten = ::write(fileHandle, buffer, 1);
(void)bytesWritten;
#endif
}
private:
LifeSigns(const LifeSigns&); //just be sure this ref-counting Zstring doesn't bite
LifeSigns& operator=(const LifeSigns&); //
boost::thread threadObj;
const BasicString lockfilename_; //used by worker thread only! Not ref-counted!
};
namespace
{
void deleteLockFile(const Zstring& filename) //throw (FileError)
{
#ifdef FFS_WIN
if (!::DeleteFile(applyLongPathPrefix(filename).c_str()))
#elif defined FFS_LINUX
if (::unlink(filename.c_str()) != 0)
#endif
{
wxString errorMessage = wxString(_("Error deleting file:")) + wxT("\n\"") + zToWx(filename) + wxT("\"");
throw FileError(errorMessage + wxT("\n\n") + getLastErrorFormatted());
}
}
zen::UInt64 getLockFileSize(const Zstring& filename) //throw (FileError, ErrorNotExisting)
{
#ifdef FFS_WIN
WIN32_FIND_DATA fileInfo = {};
const HANDLE searchHandle = ::FindFirstFile(applyLongPathPrefix(filename).c_str(), &fileInfo);
if (searchHandle == INVALID_HANDLE_VALUE)
{
const DWORD lastError = ::GetLastError();
wxString errorMessage = wxString(_("Error reading file attributes:")) + wxT("\n\"") + zToWx(filename) + wxT("\"");
errorMessage += wxT("\n\n") + getLastErrorFormatted(lastError);
if (lastError == ERROR_FILE_NOT_FOUND ||
lastError == ERROR_PATH_NOT_FOUND)
throw ErrorNotExisting(errorMessage);
else
throw FileError(errorMessage);
}
::FindClose(searchHandle);
return zen::UInt64(fileInfo.nFileSizeLow, fileInfo.nFileSizeHigh);
#elif defined FFS_LINUX
struct stat fileInfo = {};
if (::stat(filename.c_str(), &fileInfo) != 0) //follow symbolic links
{
const int lastError = errno;
wxString errorMessage = wxString(_("Error reading file attributes:")) + wxT("\n\"") + zToWx(filename) + wxT("\"");
errorMessage += wxT("\n\n") + getLastErrorFormatted(lastError);
if (lastError == ENOENT)
throw ErrorNotExisting(errorMessage);
else
throw FileError(errorMessage);
}
return zen::UInt64(fileInfo.st_size);
#endif
}
Zstring deleteAbandonedLockName(const Zstring& lockfilename)
{
const size_t pos = lockfilename.rfind(common::FILE_NAME_SEPARATOR); //search from end
return pos == Zstring::npos ? Zstr("Del.") + lockfilename :
Zstring(lockfilename.c_str(), pos + 1) + //include path separator
Zstr("Del.") +
lockfilename.AfterLast(common::FILE_NAME_SEPARATOR); //returns the whole string if ch not found
}
namespace
{
//read string from file stream
inline
std::string readString(wxInputStream& stream) //throw (std::exception)
{
const boost::uint32_t strLength = readPOD<boost::uint32_t>(stream);
std::string output;
if (strLength > 0)
{
output.resize(strLength); //may throw for corrupted data
stream.Read(&output[0], strLength);
}
return output;
}
inline
void writeString(wxOutputStream& stream, const std::string& str) //write string to filestream
{
const boost::uint32_t strLength = static_cast<boost::uint32_t>(str.length());
writePOD<boost::uint32_t>(stream, strLength);
stream.Write(str.c_str(), strLength);
}
std::string getComputerId() //returns empty string on error
{
const wxString fhn = ::wxGetFullHostName();
if (fhn.empty()) return std::string();
#ifdef FFS_WIN
return "Windows " + std::string(fhn.ToUTF8());
#elif defined FFS_LINUX
return "Linux " + std::string(fhn.ToUTF8());
#endif
}
struct LockInformation
{
LockInformation()
{
lockId = util::generateGUID();
#ifdef FFS_WIN
procDescr.processId = ::GetCurrentProcessId();
#elif defined FFS_LINUX
procDescr.processId = ::getpid();
#endif
procDescr.computerId = getComputerId();
}
LockInformation(wxInputStream& stream) //read
{
char formatDescr[sizeof(LOCK_FORMAT_DESCR)] = {};
stream.Read(formatDescr, sizeof(LOCK_FORMAT_DESCR)); //file format header
const int lockFileVersion = readPOD<boost::int32_t>(stream); //
(void)lockFileVersion;
//some format checking here?
lockId = readString(stream);
procDescr.processId = static_cast<ProcessId>(readPOD<boost::uint64_t>(stream)); //possible loss of precision (32/64 bit process) covered by buildId
procDescr.computerId = readString(stream);
}
void toStream(wxOutputStream& stream) const //write
{
assert_static(sizeof(ProcessId) <= sizeof(boost::uint64_t)); //ensure portability
stream.Write(LOCK_FORMAT_DESCR, sizeof(LOCK_FORMAT_DESCR));
writePOD<boost::int32_t>(stream, LOCK_FORMAT_VER);
writeString(stream, lockId);
writePOD<boost::uint64_t>(stream, procDescr.processId);
writeString(stream, procDescr.computerId);
}
#ifdef FFS_WIN
typedef DWORD ProcessId; //same size on 32 and 64 bit windows!
#elif defined FFS_LINUX
typedef pid_t ProcessId;
#endif
std::string lockId; //16 byte UUID
struct ProcessDescription
{
ProcessId processId;
std::string computerId;
} procDescr;
};
//true: process not available, false: cannot say anything
enum ProcessStatus
{
PROC_STATUS_NOT_RUNNING,
PROC_STATUS_RUNNING,
PROC_STATUS_NO_IDEA
};
ProcessStatus getProcessStatus(const LockInformation::ProcessDescription& procDescr)
{
if (procDescr.computerId != getComputerId() ||
procDescr.computerId.empty()) //both names are empty
return PROC_STATUS_NO_IDEA; //lock owned by different computer
#ifdef FFS_WIN
//note: ::OpenProcess() is no option as it may successfully return for crashed processes!
HANDLE snapshot = ::CreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, //__in DWORD dwFlags,
0); //__in DWORD th32ProcessID
if (snapshot == INVALID_HANDLE_VALUE)
return PROC_STATUS_NO_IDEA;
Loki::ScopeGuard dummy = Loki::MakeGuard(::CloseHandle, snapshot);
(void)dummy; //silence warning "unused variable"
PROCESSENTRY32 processEntry = {};
processEntry.dwSize = sizeof(processEntry);
if (!::Process32First(snapshot, //__in HANDLE hSnapshot,
&processEntry)) //__inout LPPROCESSENTRY32 lppe
return PROC_STATUS_NO_IDEA;
do
{
if (processEntry.th32ProcessID == procDescr.processId)
return PROC_STATUS_RUNNING; //process still running
}
while(::Process32Next(snapshot, &processEntry));
return PROC_STATUS_NOT_RUNNING;
#elif defined FFS_LINUX
if (procDescr.processId <= 0 || procDescr.processId >= 65536)
return PROC_STATUS_NO_IDEA; //invalid process id
return zen::dirExists(Zstr("/proc/") + Zstring::fromNumber(procDescr.processId)) ? PROC_STATUS_RUNNING : PROC_STATUS_NOT_RUNNING;
#endif
}
void writeLockInfo(const Zstring& lockfilename) //throw (FileError)
{
//write GUID at the beginning of the file: this ID is a universal identifier for this lock (no matter what the path is, considering symlinks, distributed network, etc.)
FileOutputStream lockFile(lockfilename); //throw (FileError)
LockInformation().toStream(lockFile);
}
LockInformation retrieveLockInfo(const Zstring& lockfilename) //throw (FileError, ErrorNotExisting)
{
//read GUID from beginning of file
FileInputStream lockFile(lockfilename); //throw (FileError, ErrorNotExisting)
return LockInformation(lockFile);
}
std::string retrieveLockId(const Zstring& lockfilename) //throw (FileError, ErrorNotExisting)
{
return retrieveLockInfo(lockfilename).lockId;
}
}
void waitOnDirLock(const Zstring& lockfilename, DirLockCallback* callback) //throw (FileError)
{
Zstring infoMsg;
infoMsg = wxToZ(_("Waiting while directory is locked (%x)..."));
infoMsg.Replace(Zstr("%x"), Zstr("\"") + lockfilename + Zstr("\""));
if (callback) callback->reportInfo(infoMsg);
//---------------------------------------------------------------
try
{
const LockInformation lockInfo = retrieveLockInfo(lockfilename); //throw (FileError, ErrorNotExisting)
const bool lockOwnderDead = getProcessStatus(lockInfo.procDescr) == PROC_STATUS_NOT_RUNNING; //convenience optimization: if we know the owning process crashed, we needn't wait DETECT_EXITUS_INTERVAL sec
zen::UInt64 fileSizeOld;
wxLongLong lockSilentStart = wxGetLocalTimeMillis();
while (true)
{
const zen::UInt64 fileSizeNew = ::getLockFileSize(lockfilename); //throw (FileError, ErrorNotExisting)
wxLongLong currentTime = wxGetLocalTimeMillis();
if (fileSizeNew != fileSizeOld)
{
//received life sign from lock
fileSizeOld = fileSizeNew;
lockSilentStart = currentTime;
}
if (lockOwnderDead || //no need to wait any longer...
currentTime - lockSilentStart > DETECT_EXITUS_INTERVAL)
{
DirLock dummy(deleteAbandonedLockName(lockfilename), callback); //throw (FileError)
//now that the lock is in place check existence again: meanwhile another process may have deleted and created a new lock!
if (retrieveLockId(lockfilename) != lockInfo.lockId) //throw (FileError, ErrorNotExisting)
return; //another process has placed a new lock, leave scope: the wait for the old lock is technically over...
if (getLockFileSize(lockfilename) != fileSizeOld) //throw (FileError, ErrorNotExisting)
continue; //belated lifesign
::deleteLockFile(lockfilename); //throw (FileError)
return;
}
//wait some time...
const size_t GUI_CALLBACK_INTERVAL = 100;
for (size_t i = 0; i < POLL_LIFE_SIGN_INTERVAL / GUI_CALLBACK_INTERVAL; ++i)
{
if (callback) callback->requestUiRefresh();
wxMilliSleep(GUI_CALLBACK_INTERVAL);
//show some countdown on abandoned locks
if (callback)
{
if (currentTime - lockSilentStart > EMIT_LIFE_SIGN_INTERVAL) //one signal missed: it's likely this is an abandoned lock:
{
long remainingSeconds = ((DETECT_EXITUS_INTERVAL - (wxGetLocalTimeMillis() - lockSilentStart)) / 1000).ToLong();
remainingSeconds = std::max(0L, remainingSeconds);
Zstring remSecMsg = wxToZ(_P("1 sec", "%x sec", remainingSeconds));
remSecMsg.Replace(Zstr("%x"), Zstring::fromNumber(remainingSeconds));
callback->reportInfo(infoMsg + Zstr(" ") + remSecMsg);
}
else
callback->reportInfo(infoMsg); //emit a message in any case (might clear other one)
}
}
}
}
catch (const ErrorNotExisting&)
{
return; //what we are waiting for...
}
}
void releaseLock(const Zstring& lockfilename) //throw ()
{
try
{
::deleteLockFile(lockfilename);
}
catch(...) {}
}
bool tryLock(const Zstring& lockfilename) //throw (FileError)
{
#ifdef FFS_WIN
const HANDLE fileHandle = ::CreateFile(applyLongPathPrefix(lockfilename).c_str(),
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
{
if (::GetLastError() == ERROR_FILE_EXISTS)
return false;
else
{
wxString errorMessage = wxString(_("Error setting directory lock:")) + wxT("\n\"") + zToWx(lockfilename) + wxT("\"");
throw FileError(errorMessage + wxT("\n\n") + getLastErrorFormatted());
}
}
::CloseHandle(fileHandle);
#elif defined FFS_LINUX
//O_EXCL contains a race condition on NFS file systems: http://linux.die.net/man/2/open
::umask(0); //important!
const int fileHandle = ::open(lockfilename.c_str(), O_CREAT | O_WRONLY | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);
if (fileHandle == -1)
{
if (errno == EEXIST)
return false;
else
{
wxString errorMessage = wxString(_("Error setting directory lock:")) + wxT("\n\"") + zToWx(lockfilename) + wxT("\"");
throw FileError(errorMessage + wxT("\n\n") + getLastErrorFormatted());
}
}
::close(fileHandle);
#endif
Loki::ScopeGuard guardLockFile = Loki::MakeGuard(::releaseLock, lockfilename);
//write UUID at the beginning of the file: this ID is a universal identifier for this lock (no matter what the path is, considering symlinks ,etc.)
writeLockInfo(lockfilename); //throw (FileError)
guardLockFile.Dismiss(); //lockfile created successfully
return true;
}
}
class DirLock::SharedDirLock
{
public:
SharedDirLock(const Zstring& lockfilename, DirLockCallback* callback = NULL) : //throw (FileError)
lockfilename_(lockfilename)
{
while (!::tryLock(lockfilename)) //throw (FileError)
::waitOnDirLock(lockfilename, callback); //
emitLifeSigns.reset(new LifeSigns(lockfilename.c_str())); //throw()! ownership of lockfile not yet managed!
}
~SharedDirLock()
{
emitLifeSigns.reset();
::releaseLock(lockfilename_); //throw ()
}
private:
SharedDirLock(const DirLock&);
SharedDirLock& operator=(const DirLock&);
const Zstring lockfilename_;
std::auto_ptr<LifeSigns> emitLifeSigns;
};
class DirLock::LockAdmin //administrate all locks held by this process to avoid deadlock by recursion
{
public:
static LockAdmin& instance()
{
static LockAdmin inst;
return inst;
}
//create or retrieve a SharedDirLock
std::shared_ptr<SharedDirLock> retrieve(const Zstring& lockfilename, DirLockCallback* callback) //throw (FileError)
{
//optimization: check if there is an active(!) lock for "lockfilename"
FileToUuidMap::const_iterator iterUuid = fileToUuid.find(lockfilename);
if (iterUuid != fileToUuid.end())
{
const std::shared_ptr<SharedDirLock>& activeLock = findActive(iterUuid->second); //returns null-lock if not found
if (activeLock)
return activeLock; //SharedDirLock is still active -> enlarge circle of shared ownership
}
try //actual check based on lock UUID, deadlock prevention: "lockfilename" may be an alternative name for an already active lock
{
const std::string lockId = retrieveLockId(lockfilename); //throw (FileError, ErrorNotExisting)
const std::shared_ptr<SharedDirLock>& activeLock = findActive(lockId); //returns null-lock if not found
if (activeLock)
{
fileToUuid[lockfilename] = lockId; //perf-optimization: update relation
return activeLock;
}
}
catch (const ErrorNotExisting&) {} //let other FileError(s) propagate!
//not yet in buffer, so create a new directory lock
std::shared_ptr<SharedDirLock> newLock(new SharedDirLock(lockfilename, callback)); //throw (FileError)
const std::string newLockId = retrieveLockId(lockfilename); //throw (FileError, ErrorNotExisting)
//update registry
fileToUuid[lockfilename] = newLockId; //throw()
uuidToLock[newLockId] = newLock; //
return newLock;
}
private:
LockAdmin() {}
std::shared_ptr<SharedDirLock> findActive(const std::string& lockId) //returns null-lock if not found
{
UuidToLockMap::const_iterator iterLock = uuidToLock.find(lockId);
return iterLock != uuidToLock.end() ?
iterLock->second.lock() : //try to get shared_ptr; throw()
std::shared_ptr<SharedDirLock>();
}
typedef std::weak_ptr<SharedDirLock> SharedLock;
typedef std::string UniqueId;
typedef std::map<Zstring, UniqueId, LessFilename> FileToUuidMap; //n:1 handle uppper/lower case correctly
typedef std::map<UniqueId, SharedLock> UuidToLockMap; //1:1
FileToUuidMap fileToUuid; //lockname |-> UUID; locks can be referenced by a lockfilename or alternatively a UUID
UuidToLockMap uuidToLock; //UUID |-> "shared lock ownership"
};
DirLock::DirLock(const Zstring& lockfilename, DirLockCallback* callback) : //throw (FileError)
sharedLock(LockAdmin::instance().retrieve(lockfilename, callback)) {}
|