4 * @brief Implementation file for Directory traversal functions.
11 #include <Poco/DirectoryIterator.h>
12 #include <Poco/Timestamp.h>
15 #include "UnicodeString.h"
19 #include "Win_VersionHelper.h"
22 using Poco::DirectoryIterator;
23 using Poco::Timestamp;
25 static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files);
26 static void Sort(DirItemArray * dirs, bool casesensitive);
29 * @brief Load arrays with all directories & files in specified dir
31 void LoadAndSortFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files, bool casesensitive)
33 LoadFiles(sDir, dirs, files);
34 Sort(dirs, casesensitive);
35 Sort(files, casesensitive);
39 * @brief Find file and sub-folder names from given folder.
40 * This function saves all file and sub-folder names in given folder to arrays.
41 * We use 64-bit version of stat() to get times since find doesn't return
42 * valid times for very old files (around year 1970). Even stat() seems to
43 * give negative time values but we can live with that. Those around 1970
44 * times can happen when file is created so that it doesn't get valid
45 * creation or modification dates.
46 * @param [in] sDir Base folder for files and subfolders.
47 * @param [in, out] dirs Array where subfolder names are stored.
48 * @param [in, out] files Array where file names are stored.
50 static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files)
52 boost::flyweight<String> dir(sDir);
54 DirectoryIterator it(ucr::toUTF8(sDir));
55 DirectoryIterator end;
57 for (; it != end; ++it)
59 bool bIsDirectory = it->isDirectory();
64 ent.ctime = it->created();
67 ent.mtime = it->getLastModified();
70 ent.size = it->getSize();
72 ent.filename = ucr::toTString(it.name());
73 ent.flags.attributes = GetFileAttributes(ucr::toTString(it.name()).c_str());
74 (bIsDirectory ? dirs : files)->push_back(ent);
78 String sPattern = paths::ConcatPath(sDir, _T("*.*"));
82 if (IsWin7_OrGreater()) // (also 'Windows Server 2008 R2' and greater) for FindExInfoBasic and FIND_FIRST_EX_LARGE_FETCH
83 h = FindFirstFileEx(TFile(sPattern).wpath().c_str(), FindExInfoBasic, &ff, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH);
85 h = FindFirstFile(TFile(sPattern).wpath().c_str(), &ff);
86 if (h != INVALID_HANDLE_VALUE)
90 bool bIsDirectory = (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
91 if (bIsDirectory && _tcsstr(_T(".."), ff.cFileName))
96 // Save filetimes as seconds since January 1, 1970
97 // Note that times can be < 0 if they are around that 1970..
98 // Anyway that is not sensible case for normal files so we can
99 // just use zero for their time.
100 ent.ctime = Timestamp::fromFileTimeNP(ff.ftCreationTime.dwLowDateTime, ff.ftCreationTime.dwHighDateTime);
103 ent.mtime = Timestamp::fromFileTimeNP(ff.ftLastWriteTime.dwLowDateTime, ff.ftLastWriteTime.dwHighDateTime);
107 if (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
108 ent.size = DirItem::FILE_SIZE_NONE; // No size for directories
111 ent.size = ((int64_t)ff.nFileSizeHigh << 32) + ff.nFileSizeLow;
115 ent.filename = ff.cFileName;
116 ent.flags.attributes = ff.dwFileAttributes;
118 (bIsDirectory ? dirs : files)->push_back(ent);
119 } while (FindNextFile(h, &ff));
126 static inline int collate(const String &str1, const String &str2)
128 return _tcscoll(str1.c_str(), str2.c_str());
131 static inline int collate_ignore_case(const String &str1, const String &str2)
133 return _tcsicoll(str1.c_str(), str2.c_str());
137 template<int (*compfunc)(const TCHAR *, const TCHAR *)>
138 struct StringComparer
140 bool operator()(const DirItem &elem1, const DirItem &elem2)
142 return compfunc(elem1.filename.get().c_str(), elem2.filename.get().c_str()) < 0;
147 * @brief sort specified array
149 static void Sort(DirItemArray * dirs, bool casesensitive)
152 std::sort(dirs->begin(), dirs->end(), StringComparer<_tcscoll>());
154 std::sort(dirs->begin(), dirs->end(), StringComparer<_tcsicoll>());
158 * @brief Compare (NLS aware) two strings, either case-sensitive or
159 * case-insensitive as caller specifies
161 int collstr(const String & s1, const String & s2, bool casesensitive)
164 return collate(s1, s2);
166 return collate_ignore_case(s1, s2);