2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "HDDirectory.h"
26 #include "utils/AutoPtrHandle.h"
27 #include "utils/AliasShortcutUtils.h"
28 #include "utils/URIUtils.h"
31 #include "utils/CharsetConverter.h"
32 #include "win32/WIN32Util.h"
35 #ifndef INVALID_FILE_ATTRIBUTES
36 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
40 typedef WIN32_FIND_DATAW LOCAL_WIN32_FIND_DATA
;
41 #define LocalFindFirstFile FindFirstFileW
42 #define LocalFindNextFile FindNextFileW
44 typedef WIN32_FIND_DATA LOCAL_WIN32_FIND_DATA
;
45 #define LocalFindFirstFile FindFirstFile
46 #define LocalFindNextFile FindNextFile
49 using namespace AUTOPTR
;
50 using namespace XFILE
;
52 CHDDirectory::CHDDirectory(void)
55 CHDDirectory::~CHDDirectory(void)
58 bool CHDDirectory::GetDirectory(const CStdString
& strPath1
, CFileItemList
&items
)
60 LOCAL_WIN32_FIND_DATA wfd
;
61 memset(&wfd
, 0, sizeof(wfd
));
63 CStdString strPath
=strPath1
;
65 if (IsAliasShortcut(strPath
))
66 TranslateAliasShortcut(strPath
);
68 CStdString strRoot
= strPath
;
71 URIUtils::AddSlashAtEnd(strRoot
);
72 if (URIUtils::IsDVD(strRoot
) && m_isoReader
.IsScanned())
74 // Reset iso reader and remount or
75 // we can't access the dvd-rom
80 std::wstring
strSearchMask(CWIN32Util::ConvertPathToWin32Form(strRoot
));
81 strSearchMask
+= L
"*.*";
83 CStdString
strSearchMask(strRoot
);
87 CAutoPtrFind
hFind ( LocalFindFirstFile(strSearchMask
.c_str(), &wfd
));
89 // on error, check if path exists at all, this will return true if empty folder
91 return Exists(strPath1
);
97 if (wfd
.cFileName
[0] != 0)
100 #ifdef TARGET_WINDOWS
101 g_charsetConverter
.wToUTF8(wfd
.cFileName
,strLabel
, true);
103 strLabel
= wfd
.cFileName
;
105 if ( (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) )
107 if (strLabel
!= "." && strLabel
!= "..")
109 CFileItemPtr
pItem(new CFileItem(strLabel
));
110 CStdString
itemPath(URIUtils::AddFileToFolder(strRoot
, strLabel
));
111 URIUtils::AddSlashAtEnd(itemPath
);
112 pItem
->SetPath(itemPath
);
113 pItem
->m_bIsFolder
= true;
114 FileTimeToLocalFileTime(&wfd
.ftLastWriteTime
, &localTime
);
115 pItem
->m_dateTime
=localTime
;
117 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
)
118 pItem
->SetProperty("file:hidden", true);
124 CFileItemPtr
pItem(new CFileItem(strLabel
));
125 pItem
->SetPath(URIUtils::AddFileToFolder(strRoot
, strLabel
));
126 pItem
->m_bIsFolder
= false;
127 pItem
->m_dwSize
= CUtil::ToInt64(wfd
.nFileSizeHigh
, wfd
.nFileSizeLow
);
128 FileTimeToLocalFileTime(&wfd
.ftLastWriteTime
, &localTime
);
129 pItem
->m_dateTime
=localTime
;
131 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
)
132 pItem
->SetProperty("file:hidden", true);
138 while (LocalFindNextFile((HANDLE
)hFind
, &wfd
));
143 bool CHDDirectory::Create(const char* strPath
)
145 if (!strPath
|| !*strPath
)
147 CStdString strPath1
= strPath
;
148 URIUtils::AddSlashAtEnd(strPath1
);
150 #ifdef TARGET_WINDOWS
151 if (strPath1
.size() == 3 && strPath1
[1] == ':')
152 return Exists(strPath
); // A drive - we can't "create" a drive
153 if(::CreateDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath1
).c_str(), NULL
))
155 if(::CreateDirectory(strPath1
.c_str(), NULL
))
158 else if(GetLastError() == ERROR_ALREADY_EXISTS
)
164 bool CHDDirectory::Remove(const char* strPath
)
166 if (!strPath
|| !*strPath
)
168 #ifdef TARGET_WINDOWS
169 return (::RemoveDirectoryW(CWIN32Util::ConvertPathToWin32Form(strPath
).c_str()) || GetLastError() == ERROR_PATH_NOT_FOUND
) ? true : false;
171 return ::RemoveDirectory(strPath
) ? true : false;
175 bool CHDDirectory::Exists(const char* strPath
)
177 if (!strPath
|| !*strPath
)
179 #ifdef TARGET_WINDOWS
180 DWORD attributes
= GetFileAttributesW(CWIN32Util::ConvertPathToWin32Form(strPath
).c_str());
182 DWORD attributes
= GetFileAttributes(strPath
);
184 if(attributes
== INVALID_FILE_ATTRIBUTES
)
186 if (FILE_ATTRIBUTE_DIRECTORY
& attributes
) return true;