/
HDDirectory.cpp
188 lines (164 loc) · 5.12 KB
/
HDDirectory.cpp
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
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "HDDirectory.h"
#include "Util.h"
#include "iso9660.h"
#include "URL.h"
#include "FileItem.h"
#include "utils/AutoPtrHandle.h"
#include "utils/AliasShortcutUtils.h"
#include "utils/URIUtils.h"
#ifdef TARGET_WINDOWS
#include "utils/CharsetConverter.h"
#include "win32/WIN32Util.h"
#endif
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
#endif
#ifdef TARGET_WINDOWS
typedef WIN32_FIND_DATAW LOCAL_WIN32_FIND_DATA;
#define LocalFindFirstFile FindFirstFileW
#define LocalFindNextFile FindNextFileW
#else
typedef WIN32_FIND_DATA LOCAL_WIN32_FIND_DATA;
#define LocalFindFirstFile FindFirstFile
#define LocalFindNextFile FindNextFile
#endif
using namespace AUTOPTR;
using namespace XFILE;
CHDDirectory::CHDDirectory(void)
{}
CHDDirectory::~CHDDirectory(void)
{}
bool CHDDirectory::GetDirectory(const CStdString& strPath1, CFileItemList &items)
{
LOCAL_WIN32_FIND_DATA wfd;
memset(&wfd, 0, sizeof(wfd));
CStdString strPath=strPath1;
if (IsAliasShortcut(strPath))
TranslateAliasShortcut(strPath);
CStdString strRoot = strPath;
CURL url(strPath);
URIUtils::AddSlashAtEnd(strRoot);
if (URIUtils::IsDVD(strRoot) && m_isoReader.IsScanned())
{
// Reset iso reader and remount or
// we can't access the dvd-rom
m_isoReader.Reset();
}
#ifdef TARGET_WINDOWS
std::wstring strSearchMask(CWIN32Util::ConvertPathToWin32Form(strRoot));
strSearchMask += L"*.*";
#else
CStdString strSearchMask(strRoot);
#endif
FILETIME localTime;
CAutoPtrFind hFind ( LocalFindFirstFile(strSearchMask.c_str(), &wfd));
// on error, check if path exists at all, this will return true if empty folder
if (!hFind.isValid())
return Exists(strPath1);
if (hFind.isValid())
{
do
{
if (wfd.cFileName[0] != 0)
{
CStdString strLabel;
#ifdef TARGET_WINDOWS
g_charsetConverter.wToUTF8(wfd.cFileName,strLabel, true);
#else
strLabel = wfd.cFileName;
#endif
if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
if (strLabel != "." && strLabel != "..")
{
CFileItemPtr pItem(new CFileItem(strLabel));
CStdString itemPath = strRoot + strLabel;
URIUtils::AddSlashAtEnd(itemPath);
pItem->SetPath(itemPath);
pItem->m_bIsFolder = true;
FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
pItem->m_dateTime=localTime;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
pItem->SetProperty("file:hidden", true);
items.Add(pItem);
}
}
else
{
CFileItemPtr pItem(new CFileItem(strLabel));
pItem->SetPath(strRoot + strLabel);
pItem->m_bIsFolder = false;
pItem->m_dwSize = CUtil::ToInt64(wfd.nFileSizeHigh, wfd.nFileSizeLow);
FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localTime);
pItem->m_dateTime=localTime;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
pItem->SetProperty("file:hidden", true);
items.Add(pItem);
}
}
}
while (LocalFindNextFile((HANDLE)hFind, &wfd));
}
return true;
}
bool CHDDirectory::Create(const char* strPath)
{
if (!strPath || !*strPath)
return false;
CStdString strPath1 = strPath;
URIUtils::AddSlashAtEnd(strPath1);
#ifdef TARGET_WINDOWS
if (strPath1.size() == 3 && strPath1[1] == ':')
return Exists(strPath); // A drive - we can't "create" a drive
if(::CreateDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath1).c_str(), NULL))
#else
if(::CreateDirectory(strPath1.c_str(), NULL))
#endif
return true;
else if(GetLastError() == ERROR_ALREADY_EXISTS)
return true;
return false;
}
bool CHDDirectory::Remove(const char* strPath)
{
if (!strPath || !*strPath)
return false;
#ifdef TARGET_WINDOWS
return (::RemoveDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath).c_str()) || GetLastError() == ERROR_PATH_NOT_FOUND) ? true : false;
#else
return ::RemoveDirectory(strPath) ? true : false;
#endif
}
bool CHDDirectory::Exists(const char* strPath)
{
if (!strPath || !*strPath)
return false;
#ifdef TARGET_WINDOWS
DWORD attributes = GetFileAttributesW(CWIN32Util::ConvertPathToWin32Form(strPath).c_str());
#else
DWORD attributes = GetFileAttributes(strPath);
#endif
if(attributes == INVALID_FILE_ATTRIBUTES)
return false;
if (FILE_ATTRIBUTE_DIRECTORY & attributes) return true;
return false;
}