diff options
Diffstat (limited to 'shared/fileID.cpp')
-rw-r--r-- | shared/fileID.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/shared/fileID.cpp b/shared/fileID.cpp new file mode 100644 index 00000000..005707dc --- /dev/null +++ b/shared/fileID.cpp @@ -0,0 +1,82 @@ +#include "fileID.h" + +#ifdef FFS_WIN +#include "staticAssert.h" +#include <wx/msw/wrapwin.h> //includes "windows.h" +#include "longPathPrefix.h" + +#elif defined FFS_LINUX + +#endif + + + +#ifdef FFS_WIN +class CloseHandleOnExit +{ +public: + CloseHandleOnExit(HANDLE fileHandle) : fileHandle_(fileHandle) {} + + ~CloseHandleOnExit() + { + ::CloseHandle(fileHandle_); + } + +private: + HANDLE fileHandle_; +}; + + +Utility::FileID Utility::retrieveFileID(const Zstring& filename) +{ + //ensure our DWORD_FFS really is the same as DWORD + assert_static(sizeof(Utility::FileID::DWORD_FFS) == sizeof(DWORD)); + +//WARNING: CreateFile() is SLOW, while GetFileInformationByHandle() is quite cheap! +//http://msdn.microsoft.com/en-us/library/aa363788(VS.85).aspx + + const HANDLE hFile = ::CreateFile(FreeFileSync::applyLongPathPrefix(filename).c_str(), + 0, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + NULL, + OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, //FILE_FLAG_BACKUP_SEMANTICS needed to open directories + NULL); + if (hFile != INVALID_HANDLE_VALUE) + { + CloseHandleOnExit dummy(hFile); + + BY_HANDLE_FILE_INFORMATION info; + if (::GetFileInformationByHandle(hFile, &info)) + { + return Utility::FileID(info.dwVolumeSerialNumber, + info.nFileIndexHigh, + info.nFileIndexLow); + } + } + return Utility::FileID(); //empty ID +} + + +#elif defined FFS_LINUX +Utility::FileID Utility::retrieveFileID(const Zstring& filename) +{ + struct stat fileInfo; + if (::lstat(filename.c_str(), &fileInfo) == 0) //lstat() does not resolve symlinks + return Utility::FileID(fileInfo.st_dev, fileInfo.st_ino); + + return Utility::FileID(); //empty ID +} +#endif + + +bool Utility::sameFileSpecified(const Zstring& file1, const Zstring& file2) +{ + const Utility::FileID id1 = retrieveFileID(file1); + const Utility::FileID id2 = retrieveFileID(file2); + + if (id1 != FileID() && id2 != FileID()) + return id1 == id2; + + return false; +} |