Index: src/ch/FileInfo.h =================================================================== diff -u -rd5c3edd0d167db9b5d47d04248820fda49499a5e -r22573f5fa8bfbf68a19deb7631962e026f028045 --- src/ch/FileInfo.h (.../FileInfo.h) (revision d5c3edd0d167db9b5d47d04248820fda49499a5e) +++ src/ch/FileInfo.h (.../FileInfo.h) (revision 22573f5fa8bfbf68a19deb7631962e026f028045) @@ -1,5 +1,5 @@ /*************************************************************************** -* Copyright (C) 2001-2008 by J�zef Starosczyk * +* Copyright (C) 2001-2008 by Jozef Starosczyk * * ixen@copyhandler.com * * * * This program is free software; you can redistribute it and/or modify * @@ -16,49 +16,21 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -/** - * @doc FILEINFO - * @module FileInfo.h 1.3 - Interface for the CFileInfo and CFileInfoArray classes | - * The classes contained in this file allow to gather recursively file information - * through directories. - * - * Codeguru & friends - * Coded by Antonio Tejada Lacaci. 1999 - * atejada@espanet.com - * CRC32 code by Floor A.C. Naaijkens - * - * Updates (aaaa-mm-dd): - * MANY CHANGES by Ixen Gerthannes... - * 1999-9-23 ATL: Opensource works! Again, Mr. Szucs (rszucs@cygron.hu) gets another bug: - * Missing "4-(dwRead & 0x3)" in the same lines as before, when calc'ing the padding mask. - * 1999-9-16 ATL: Corrected bug in GetCRC and GetChecksum as suggested by R�bert Szucs (rszucs@cygron.hu): - * There was a buffer overflow and checksum and crc for last dword +1 was calc'ed instead - * of the ones for last dword. Instead accessing buffer[dwRead +3...] it ought to access - * buffer[dwRead...] (shame on me! :'(). - * 1999-9-2 ATL: Corrected bug in AddFile(CString, LPARAM) as suggested by Nhycoh (Nhycoh44@yahoo.com): - * There was some weird stuff at CFileInfo::Create(strFilePath) - * stating strFilePath.GetLength()-nBarPos instead of nBarPos+1 - * (I'm quite sure I left my head on my pillow the day I did that %-#). - * 1999-6-27 ATL: Updated GetCRC & GetChecksum to avoid some bug cases - * 1999-4-7 ATL: Updated source code doc to conform Autoduck 2.0 standard - * 1999-4-7 ATL: Corrected bug in AddDir as suggested by Zhuang Yuyao (zhuangyy@koal.com): - * bIncludeDirs wasn't used if bRecurse was false. - * - * Keep this comment if you redistribute this file. And credit where credit's due! - */ +// File was originally based on FileInfo.h by Antonio Tejada Lacaci. +// Almost everything has changed since then. #ifndef __FILEINFO_H__ #define __FILEINFO_H__ -#include -#include "afxdisp.h" #include "DestPath.h" +#include +#include void FindFreeSubstituteName(CString strSrcPath, CString strDstPath, CString* pstrResult); extern void GetDriveData(LPCTSTR lpszPath, int *piDrvNum, UINT *puiDrvType); // CFileInfo flags -// flag stating that file has been processed (used to determine if file can be deleted at the end of copying) +// flag stating that file has been processed (used to determine if file can be deleted at the end of copying) #define FIF_PROCESSED 0x00000001 class CFiltersArray; @@ -67,23 +39,41 @@ class CClipboardEntry { public: - CClipboardEntry() { m_bMove=true; m_iDriveNumber=-1; m_uiDriveType=static_cast(-1); m_iBufferIndex=0; }; - CClipboardEntry(const CClipboardEntry& rEntry) { m_strPath=rEntry.m_strPath; m_bMove=rEntry.m_bMove; m_iDriveNumber=rEntry.m_iDriveNumber; m_uiDriveType=rEntry.m_uiDriveType; m_astrDstPaths.Copy(rEntry.m_astrDstPaths); }; - + CClipboardEntry(); + CClipboardEntry(const CClipboardEntry& rEntry); + void SetPath(const CString& strPath); void CalcBufferIndex(const CDestPath& dpDestPath); - const CString& GetPath() const { return m_strPath; }; + const CString& GetPath() const { return m_strPath; } + CString GetFileName() const; - void SetMove(bool bValue) { m_bMove=bValue; }; - bool GetMove() { return m_bMove; }; + void SetMove(bool bValue) { m_bMove=bValue; } + bool GetMove() { return m_bMove; } - int GetDriveNumber() const { return m_iDriveNumber; }; - UINT GetDriveType() const { return m_uiDriveType; }; + int GetDriveNumber() const { return m_iDriveNumber; } + UINT GetDriveType() const { return m_uiDriveType; } - int GetBufferIndex() const { return m_iBufferIndex; }; + int GetBufferIndex() const { return m_iBufferIndex; } - void Serialize(icpf::archive& ar, bool bData); + template + void Serialize(Archive& ar, unsigned int /*uiVersion*/, bool bData) + { + if(bData) + { + ar & m_strPath; + ar & m_bMove; + ar & m_iDriveNumber; + ar & m_uiDriveType; + ar & m_iBufferIndex; + } + else + ar & m_vDstPaths; + } + void AddDestinationPath(const CString& strPath); + size_t GetDestinationPathsCount() const; + CString GetDestinationPath(size_t stIndex); + private: CString m_strPath; // path (ie. c:\\windows\\) - always with ending '\\' bool m_bMove; // specifies if we can use MoveFile (if will be moved) @@ -93,23 +83,77 @@ int m_iBufferIndex; // buffer number, with which we'll copy this data -public: - CStringArray m_astrDstPaths; // dest paths table for this group of paths + std::vector m_vDstPaths; // dest paths table for this group of paths }; +typedef boost::shared_ptr CClipboardEntryPtr; + ////////////////////////////////////////////////////////////////////////// // CClipboardArray -class CClipboardArray : public CArray +class CClipboardArray { public: - ~CClipboardArray() { RemoveAll(); }; - - void Serialize(icpf::archive& ar, bool bData); + ~CClipboardArray(); - void SetAt(int nIndex, CClipboardEntry* pEntry) { delete [] GetAt(nIndex); SetAt(nIndex, pEntry); }; - void RemoveAt(int nIndex, int nCount = 1) { while (nCount--) { delete GetAt(nIndex); static_cast*>(this)->RemoveAt(nIndex, 1); } }; - void RemoveAll() { for (int i=0;i*>(this)->RemoveAll(); }; + template + void Store(Archive& ar, unsigned int /*uiVersion*/, bool bData) const + { + boost::shared_lock lock(m_lock); + // write data + size_t stCount = m_vEntries.size(); + ar << stCount; + + BOOST_FOREACH(const CClipboardEntryPtr& spEntry, m_vEntries) + { + spEntry->Serialize(ar, 0, bData); + } + } + + template + void Load(Archive& ar, unsigned int /*uiVersion*/, bool bData) + { + size_t stCount; + ar >> stCount; + + boost::unique_lock lock(m_lock); + + if(!bData && m_vEntries.size() != stCount) + THROW(_T("Count of entries with data differs from the count of state entries"), 0, 0, 0); + + if(bData) + { + m_vEntries.clear(); + m_vEntries.reserve(stCount); + } + + CClipboardEntryPtr spEntry; + for(size_t stIndex = 0; stIndex < stCount; ++stIndex) + { + if(bData) + spEntry.reset(new CClipboardEntry); + else + spEntry = m_vEntries.at(stIndex); + spEntry->Serialize(ar, 0, bData); + + if(bData) + m_vEntries.push_back(spEntry); + } + } + + CClipboardEntryPtr GetAt(size_t iPos) const; + + size_t GetSize() const; + void Add(const CClipboardEntryPtr& pEntry); + void SetAt(size_t nIndex, const CClipboardEntryPtr& pEntry); + void RemoveAt(size_t nIndex, size_t nCount = 1); + void RemoveAll(); + + int ReplacePathsPrefix(CString strOld, CString strNew); + +protected: + std::vector m_vEntries; + mutable boost::shared_mutex m_lock; }; class CFileInfo @@ -121,44 +165,43 @@ // static member static bool Exist(CString strPath); // check for file or folder existence - - void Create(const WIN32_FIND_DATA* pwfd, LPCTSTR pszFilePath, int iSrcIndex); - bool Create(CString strFilePath, int iSrcIndex); - - DWORD GetLength(void) const { return (DWORD) m_uhFileSize; }; - ULONGLONG GetLength64(void) const { return m_uhFileSize; }; - void SetLength64(ULONGLONG uhSize) { m_uhFileSize=uhSize; }; + void Create(const WIN32_FIND_DATA* pwfd, LPCTSTR pszFilePath, size_t stSrcIndex); + bool Create(CString strFilePath, size_t stSrcIndex); + + ULONGLONG GetLength64() const { return m_uhFileSize; } + void SetLength64(ULONGLONG uhSize) { m_uhFileSize=uhSize; } + // disk - path and disk number (-1 if none - ie. net disk) CString GetFileDrive(void) const; // returns string with src disk int GetDriveNumber() const; // disk number A - 0, b-1, c-2, ... UINT GetDriveType() const; // drive type - - CString GetFileDir(void) const; // @rdesc Returns \WINDOWS\ for C:\WINDOWS\WIN.INI - CString GetFileTitle(void) const; // @cmember returns WIN for C:\WINDOWS\WIN.INI - CString GetFileExt(void) const; /** @cmember returns INI for C:\WINDOWS\WIN.INI */ - CString GetFileRoot(void) const; /** @cmember returns C:\WINDOWS\ for C:\WINDOWS\WIN.INI */ - CString GetFileName(void) const; /** @cmember returns WIN.INI for C:\WINDOWS\WIN.INI */ - + + CString GetFileDir() const; // @rdesc Returns \WINDOWS\ for C:\WINDOWS\WIN.INI + CString GetFileTitle() const; // @cmember returns WIN for C:\WINDOWS\WIN.INI + CString GetFileExt() const; /** @cmember returns INI for C:\WINDOWS\WIN.INI */ + CString GetFileRoot() const; /** @cmember returns C:\WINDOWS\ for C:\WINDOWS\WIN.INI */ + CString GetFileName() const; /** @cmember returns WIN.INI for C:\WINDOWS\WIN.INI */ + const CString& GetFilePath(void) const { return m_strFilePath; } // returns path with m_strFilePath (probably not full) CString GetFullFilePath() const; /** @cmember returns C:\WINDOWS\WIN.INI for C:\WINDOWS\WIN.INI */ void SetFilePath(LPCTSTR lpszPath) { m_strFilePath=lpszPath; }; - + /* Get File times info (equivalent to CFindFile members) */ - const COleDateTime& GetCreationTime(void) const { return m_timCreation; }; /** @cmember returns creation time */ - const COleDateTime& GetLastAccessTime(void) const { return m_timLastAccess; }; /** @cmember returns last access time */ - const COleDateTime& GetLastWriteTime(void) const { return m_timLastWrite; }; /** @cmember returns las write time */ - + const FILETIME& GetCreationTime() const { return m_ftCreation; }; + const FILETIME& GetLastAccessTime() const { return m_ftLastAccess; }; + const FILETIME& GetLastWriteTime() const { return m_ftLastWrite; }; + /* Get File attributes info (equivalent to CFindFile members) */ - DWORD GetAttributes(void) const { return m_dwAttributes; }; /** @cmember returns file attributes */ - bool IsDirectory(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; }; /** @cmember returns TRUE if the file is a directory */ - bool IsArchived(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_ARCHIVE) != 0; }; /** @cmember Returns TRUE if the file has archive bit set */ - bool IsReadOnly(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_READONLY) != 0; }; /** @cmember Returns TRUE if the file is read-only */ - bool IsCompressed(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_COMPRESSED) != 0; }; /** @cmember Returns TRUE if the file is compressed */ - bool IsSystem(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_SYSTEM) != 0; }; /** @cmember Returns TRUE if the file is a system file */ - bool IsHidden(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_HIDDEN) != 0; }; /** @cmember Returns TRUE if the file is hidden */ - bool IsTemporary(void) const { return (m_dwAttributes & FILE_ATTRIBUTE_TEMPORARY) != 0; }; /** @cmember Returns TRUE if the file is temporary */ - bool IsNormal(void) const { return m_dwAttributes == 0; }; /** @cmember Returns TRUE if the file is a normal file */ + DWORD GetAttributes() const { return m_dwAttributes; } + bool IsDirectory() const { return (m_dwAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; } + bool IsArchived() const { return (m_dwAttributes & FILE_ATTRIBUTE_ARCHIVE) != 0; } + bool IsReadOnly() const { return (m_dwAttributes & FILE_ATTRIBUTE_READONLY) != 0; } + bool IsCompressed() const { return (m_dwAttributes & FILE_ATTRIBUTE_COMPRESSED) != 0; } + bool IsSystem() const { return (m_dwAttributes & FILE_ATTRIBUTE_SYSTEM) != 0; } + bool IsHidden() const { return (m_dwAttributes & FILE_ATTRIBUTE_HIDDEN) != 0; } + bool IsTemporary() const { return (m_dwAttributes & FILE_ATTRIBUTE_TEMPORARY) != 0; } + bool IsNormal() const { return m_dwAttributes == 0; } uint_t GetFlags() const { return m_uiFlags; } void SetFlags(uint_t uiFlags, uint_t uiMask = 0xffffffff) { m_uiFlags = (m_uiFlags & ~(uiFlags & uiMask)) | (uiFlags & uiMask); } @@ -167,172 +210,123 @@ void SetClipboard(CClipboardArray *pClipboard) { m_pClipboard=pClipboard; }; CString GetDestinationPath(CString strPath, unsigned char ucCopyNumber, int iFlags); - void SetSrcIndex(int iIndex) { m_iSrcIndex=iIndex; }; - int GetSrcIndex() const { return m_iSrcIndex; }; + void SetSrcIndex(size_t stIndex) { m_stSrcIndex = stIndex; }; + size_t GetSrcIndex() const { return m_stSrcIndex; }; - bool GetMove() { if (m_iSrcIndex != -1) return m_pClipboard->GetAt(m_iSrcIndex)->GetMove(); else return true; }; + bool GetMove() { if (m_stSrcIndex != std::numeric_limits::max()) return m_pClipboard->GetAt(m_stSrcIndex)->GetMove(); else return true; }; int GetBufferIndex() const; // operators bool operator==(const CFileInfo& rInfo); - - // (re)/store data - void Store(icpf::archive& ar); - void Load(icpf::archive& ar); + + template + void serialize(Archive& ar, unsigned int /*uiVersion*/) + { + ar & m_strFilePath; + ar & m_stSrcIndex; + ar & m_dwAttributes; + ar & m_uhFileSize; + ar & m_ftCreation.dwHighDateTime; + ar & m_ftCreation.dwLowDateTime; + ar & m_ftLastAccess.dwHighDateTime; + ar & m_ftLastAccess.dwLowDateTime; + ar & m_ftLastWrite.dwHighDateTime; + ar & m_ftLastWrite.dwLowDateTime; + ar & m_uiFlags; + } + private: CString m_strFilePath; // contains relative path (first path is in CClipboardArray) - int m_iSrcIndex; // index in CClipboardArray table (which contains the first part of the path) - + size_t m_stSrcIndex; // index in CClipboardArray table (which contains the first part of the path) + DWORD m_dwAttributes; // attributes - ULONGLONG m_uhFileSize; /** @cmember File of size. (COM states LONGLONG as hyper, so "uh" means unsigned hyper) */ - COleDateTime m_timCreation; /** @cmember Creation time */ - COleDateTime m_timLastAccess; /** @cmember Last Access time */ - COleDateTime m_timLastWrite; /** @cmember Last write time */ + ULONGLONG m_uhFileSize; + FILETIME m_ftCreation; + FILETIME m_ftLastAccess; + FILETIME m_ftLastWrite; uint_t m_uiFlags; + // ptrs to elements providing data - CClipboardArray *m_pClipboard; -}; + CClipboardArray* m_pClipboard; +}; -/** -* @class Allows to retrieve s from files/directories in a directory -*/ -class CFileInfoArray : public CArray +typedef boost::shared_ptr CFileInfoPtr; + +class CFileInfoArray { public: - /** @access Public members */ + CFileInfoArray(CClipboardArray& A_rClipboardArray) : + m_rClipboard(A_rClipboardArray) + { + } + + void AddFileInfo(const CFileInfoPtr& spFileInfo); + + size_t GetSize() const; + CFileInfoPtr GetAt(size_t stIndex) const; + CFileInfo GetCopyAt(size_t stIndex) const; - /** - * @cmember Default constructor - */ - CFileInfoArray() { m_pClipboard=NULL; SetSize(0, 5000); }; - void Init(CClipboardArray* pClipboard) { m_pClipboard=pClipboard; }; - - /** - * @cmember Adds a file or all contained in a directory to the CFileInfoArray - * Only "static" data for CFileInfo is filled (by default CRC and checksum are NOT - * calculated when inserting CFileInfos). Returns the number of s added to the array - * @parm Name of the directory, ended in backslash. - * @parm Mask of files to add in case that strDirName is a directory - * @parm Wether to recurse or not subdirectories - * @parmopt Parameter to pass to protected member function AddFileInfo - * @parmopt Wether to add or not CFileInfos for directories - * @parmopt Pointer to a variable to signal abort of directory retrieval - * (multithreaded apps). - * @parmopt pulCount Pointer to a variable incremented each time a CFileInfo is added to the - * array (multithreaded apps). - * @xref - */ - void AddDir(CString strDirName, const CFiltersArray* pFilters, int iSrcIndex, - const bool bRecurse, const bool bIncludeDirs, const volatile bool* pbAbort=NULL); - - /** - * @cmember Adds a single file or directory to the CFileInfoArray. In case of directory, files - * contained in the directory are NOT added to the array. - * Returns the position in the array where the was added (-1 if - * wasn't added) - * @parm Name of the file or directory to add. NOT ended with backslash. - * @parm Parameter to pass to protected member function AddFileInfo. - * @xref - */ - int AddFile(CString strFilePath, int iSrcIndex); - + void Clear(); + // store/restore - void Store(icpf::archive& ar, bool bOnlyFlags) + template + void Store(Archive& ar, unsigned int /*uiVersion*/, bool bOnlyFlags) { - INT_PTR iSize = GetSize(); - ar << iSize; - for (INT_PTR i=0;i::iterator iterFile = m_vFiles.begin(); iterFile != m_vFiles.end(); ++iterFile) { - CFileInfo& fi=GetAt(i); if(bOnlyFlags) - ar << fi.GetFlags(); + { + uint_t uiFlags = (*iterFile)->GetFlags(); + ar << uiFlags; + } else - fi.Store(ar); + ar << *(*iterFile); } } - void Load(icpf::archive& ar, bool bOnlyFlags) + template + void Load(Archive& ar, unsigned int /*uiVersion*/, bool bOnlyFlags) { - INT_PTR iSize; - ar>>iSize; - SetSize(iSize, 5000); - CFileInfo fi; - fi.SetClipboard(m_pClipboard); + size_t stCount; + ar >> stCount; + + if(!bOnlyFlags) + { + m_vFiles.clear(); + m_vFiles.reserve(stCount); + } + else if(stCount != m_vFiles.size()) + THROW(_T("Invalid count of flags received"), 0, 0, 0); + + CFileInfoPtr spFileInfo; + uint_t uiFlags = 0; - for (INT_PTR i=0;i> uiFlags; - rInfo.SetFlags(uiFlags); + spFileInfo->SetFlags(uiFlags); } else { - fi.Load(ar); - SetAt(i, fi); + spFileInfo.reset(new CFileInfo); + spFileInfo->SetClipboard(&m_rClipboard); + ar >> *spFileInfo; + m_vFiles.push_back(spFileInfo); } } } - + protected: - CClipboardArray* m_pClipboard; + CClipboardArray& m_rClipboard; + std::vector m_vFiles; + mutable boost::shared_mutex m_lock; }; - -/** -@ex This code adds all files in root directory and its subdirectories (but not directories themselves) to the array and TRACEs them: | - - CFileInfoArray fia; - - fia.AddDir( - "C:\\", // Directory - "*.*", // Filemask (all files) - TRUE, // Recurse subdirs - fia::AP_SORTBYNAME | fia::AP_SORTASCENDING, // Sort by name and ascending - FALSE // Do not add array entries for directories (only for files) - ); - TRACE("Dumping directory contents\n"); - for (int i=0;i