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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "Directory.h"
#include "DirectoryCache.h"
#include "DirectoryFactory.h"
#include "FileDirectoryFactory.h"
#include "FileItem.h"
#include "PasswordManager.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "commons/Exception.h"
#include "dialogs/GUIDialogBusy.h"
#include "guilib/GUIWindowManager.h"
#include "messaging/ApplicationMessenger.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "utils/Job.h"
#include "utils/JobManager.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
using namespace XFILE;
using namespace std::chrono_literals;
#define TIME_TO_BUSY_DIALOG 500
class CGetDirectory
{
private:
struct CResult
{
CResult(const CURL& dir, const CURL& listDir) : m_event(true), m_dir(dir), m_listDir(listDir), m_result(false) {}
CEvent m_event;
CFileItemList m_list;
CURL m_dir;
CURL m_listDir;
bool m_result;
};
struct CGetJob
: CJob
{
CGetJob(std::shared_ptr<IDirectory>& imp
, std::shared_ptr<CResult>& result)
: m_result(result)
, m_imp(imp)
{}
public:
bool DoWork() override
{
m_result->m_list.SetURL(m_result->m_listDir);
m_result->m_result = m_imp->GetDirectory(m_result->m_dir, m_result->m_list);
m_result->m_event.Set();
return m_result->m_result;
}
std::shared_ptr<CResult> m_result;
std::shared_ptr<IDirectory> m_imp;
};
public:
CGetDirectory(std::shared_ptr<IDirectory>& imp, const CURL& dir, const CURL& listDir)
: m_result(new CResult(dir, listDir))
{
m_id = CServiceBroker::GetJobManager()->AddJob(new CGetJob(imp, m_result), nullptr,
CJob::PRIORITY_HIGH);
if (m_id == 0)
{
CGetJob job(imp, m_result);
job.DoWork();
}
}
~CGetDirectory() { CServiceBroker::GetJobManager()->CancelJob(m_id); }
CEvent& GetEvent()
{
return m_result->m_event;
}
bool Wait(unsigned int timeout)
{
return m_result->m_event.Wait(std::chrono::milliseconds(timeout));
}
bool GetDirectory(CFileItemList& list)
{
/* if it was not finished or failed, return failure */
if (!m_result->m_event.Wait(0ms) || !m_result->m_result)
{
list.Clear();
return false;
}
list.Copy(m_result->m_list);
return true;
}
std::shared_ptr<CResult> m_result;
unsigned int m_id;
};
CDirectory::CDirectory() = default;
CDirectory::~CDirectory() = default;
bool CDirectory::GetDirectory(const std::string& strPath, CFileItemList &items, const std::string &strMask, int flags)
{
CHints hints;
hints.flags = flags;
hints.mask = strMask;
const CURL pathToUrl(strPath);
return GetDirectory(pathToUrl, items, hints);
}
bool CDirectory::GetDirectory(const std::string& strPath,
const std::shared_ptr<IDirectory>& pDirectory,
CFileItemList& items,
const std::string& strMask,
int flags)
{
CHints hints;
hints.flags = flags;
hints.mask = strMask;
const CURL pathToUrl(strPath);
return GetDirectory(pathToUrl, pDirectory, items, hints);
}
bool CDirectory::GetDirectory(const std::string& strPath, CFileItemList &items, const CHints &hints)
{
const CURL pathToUrl(strPath);
return GetDirectory(pathToUrl, items, hints);
}
bool CDirectory::GetDirectory(const CURL& url, CFileItemList &items, const std::string &strMask, int flags)
{
CHints hints;
hints.flags = flags;
hints.mask = strMask;
return GetDirectory(url, items, hints);
}
bool CDirectory::GetDirectory(const CURL& url, CFileItemList &items, const CHints &hints)
{
CURL realURL = URIUtils::SubstitutePath(url);
std::shared_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
return CDirectory::GetDirectory(url, pDirectory, items, hints);
}
bool CDirectory::GetDirectory(const CURL& url,
const std::shared_ptr<IDirectory>& pDirectory,
CFileItemList& items,
const CHints& hints)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
if (!pDirectory)
return false;
// check our cache for this path
if (g_directoryCache.GetDirectory(realURL.Get(), items, (hints.flags & DIR_FLAG_READ_CACHE) == DIR_FLAG_READ_CACHE))
items.SetURL(url);
else
{
// need to clear the cache (in case the directory fetch fails)
// and (re)fetch the folder
if (!(hints.flags & DIR_FLAG_BYPASS_CACHE))
g_directoryCache.ClearDirectory(realURL.Get());
pDirectory->SetFlags(hints.flags);
bool result = false;
CURL authUrl = realURL;
while (!result)
{
const std::string pathToUrl(url.Get());
// don't change auth if it's set explicitly
if (CPasswordManager::GetInstance().IsURLSupported(authUrl) && authUrl.GetUserName().empty())
CPasswordManager::GetInstance().AuthenticateURL(authUrl);
items.SetURL(url);
result = pDirectory->GetDirectory(authUrl, items);
if (!result)
{
// @TODO ProcessRequirements() can bring up the keyboard input dialog
// filesystem must not depend on GUI
if (CServiceBroker::GetAppMessenger()->IsProcessThread() &&
pDirectory->ProcessRequirements())
{
authUrl.SetDomain("");
authUrl.SetUserName("");
authUrl.SetPassword("");
continue;
}
CLog::Log(LOGERROR, "{} - Error getting {}", __FUNCTION__, url.GetRedacted());
return false;
}
}
// hide credentials if necessary
if (CPasswordManager::GetInstance().IsURLSupported(realURL))
{
bool hide = false;
// for explicitly credentials
if (!realURL.GetUserName().empty())
{
// credentials was changed i.e. were stored in the password
// manager, in this case we can hide them from an item URL,
// otherwise we have to keep credentials in an item URL
if ( realURL.GetUserName() != authUrl.GetUserName()
|| realURL.GetPassWord() != authUrl.GetPassWord()
|| realURL.GetDomain() != authUrl.GetDomain())
{
hide = true;
}
}
else
{
// hide credentials in any other cases
hide = true;
}
if (hide)
{
for (int i = 0; i < items.Size(); ++i)
{
CFileItemPtr item = items[i];
CURL itemUrl = item->GetURL();
itemUrl.SetDomain("");
itemUrl.SetUserName("");
itemUrl.SetPassword("");
item->SetPath(itemUrl.Get());
}
}
}
// cache the directory, if necessary
if (!(hints.flags & DIR_FLAG_BYPASS_CACHE))
g_directoryCache.SetDirectory(realURL.Get(), items, pDirectory->GetCacheType(url));
}
// now filter for allowed files
if (!pDirectory->AllowAll())
{
pDirectory->SetMask(hints.mask);
for (int i = 0; i < items.Size(); ++i)
{
CFileItemPtr item = items[i];
if (!item->m_bIsFolder && !pDirectory->IsAllowed(item->GetURL()))
{
items.Remove(i);
i--; // don't confuse loop
}
}
}
// filter hidden files
//! @todo we shouldn't be checking the gui setting here, callers should use getHidden instead
if (!CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_SHOWHIDDEN) && !(hints.flags & DIR_FLAG_GET_HIDDEN))
{
for (int i = 0; i < items.Size(); ++i)
{
if (items[i]->GetProperty("file:hidden").asBoolean())
{
items.Remove(i);
i--; // don't confuse loop
}
}
}
// Should any of the files we read be treated as a directory?
// Disable for database folders, as they already contain the extracted items
if (!(hints.flags & DIR_FLAG_NO_FILE_DIRS) && !items.IsMusicDb() && !items.IsVideoDb() && !items.IsSmartPlayList())
FilterFileDirectories(items, hints.mask);
// Correct items for path substitution
const std::string pathToUrl(url.Get());
const std::string pathToUrl2(realURL.Get());
if (pathToUrl != pathToUrl2)
{
for (int i = 0; i < items.Size(); ++i)
{
CFileItemPtr item = items[i];
item->SetPath(URIUtils::SubstitutePath(item->GetPath(), true));
}
}
return true;
}
XBMCCOMMONS_HANDLE_UNCHECKED
catch (...) { CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__); }
CLog::Log(LOGERROR, "{} - Error getting {}", __FUNCTION__, url.GetRedacted());
return false;
}
bool CDirectory::Create(const std::string& strPath)
{
const CURL pathToUrl(strPath);
return Create(pathToUrl);
}
bool CDirectory::Create(const CURL& url)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
if (CPasswordManager::GetInstance().IsURLSupported(realURL) && realURL.GetUserName().empty())
CPasswordManager::GetInstance().AuthenticateURL(realURL);
std::unique_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
if (pDirectory)
if(pDirectory->Create(realURL))
return true;
}
XBMCCOMMONS_HANDLE_UNCHECKED
catch (...) { CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__); }
CLog::Log(LOGERROR, "{} - Error creating {}", __FUNCTION__, url.GetRedacted());
return false;
}
bool CDirectory::Exists(const std::string& strPath, bool bUseCache /* = true */)
{
const CURL pathToUrl(strPath);
return Exists(pathToUrl, bUseCache);
}
bool CDirectory::Exists(const CURL& url, bool bUseCache /* = true */)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
if (bUseCache)
{
bool bPathInCache;
std::string realPath(realURL.Get());
URIUtils::AddSlashAtEnd(realPath);
if (g_directoryCache.FileExists(realPath, bPathInCache))
return true;
if (bPathInCache)
return false;
}
if (CPasswordManager::GetInstance().IsURLSupported(realURL) && realURL.GetUserName().empty())
CPasswordManager::GetInstance().AuthenticateURL(realURL);
std::unique_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
if (pDirectory)
return pDirectory->Exists(realURL);
}
XBMCCOMMONS_HANDLE_UNCHECKED
catch (...) { CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__); }
CLog::Log(LOGERROR, "{} - Error checking for {}", __FUNCTION__, url.GetRedacted());
return false;
}
bool CDirectory::Remove(const std::string& strPath)
{
const CURL pathToUrl(strPath);
return Remove(pathToUrl);
}
bool CDirectory::RemoveRecursive(const std::string& strPath)
{
return RemoveRecursive(CURL{ strPath });
}
bool CDirectory::Remove(const CURL& url)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
CURL authUrl = realURL;
if (CPasswordManager::GetInstance().IsURLSupported(authUrl) && authUrl.GetUserName().empty())
CPasswordManager::GetInstance().AuthenticateURL(authUrl);
std::unique_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
if (pDirectory)
if(pDirectory->Remove(authUrl))
{
g_directoryCache.ClearFile(realURL.Get());
return true;
}
}
XBMCCOMMONS_HANDLE_UNCHECKED
catch (...) { CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__); }
CLog::Log(LOGERROR, "{} - Error removing {}", __FUNCTION__, url.GetRedacted());
return false;
}
bool CDirectory::RemoveRecursive(const CURL& url)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
CURL authUrl = realURL;
if (CPasswordManager::GetInstance().IsURLSupported(authUrl) && authUrl.GetUserName().empty())
CPasswordManager::GetInstance().AuthenticateURL(authUrl);
std::unique_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
if (pDirectory)
if(pDirectory->RemoveRecursive(authUrl))
{
g_directoryCache.ClearFile(realURL.Get());
return true;
}
}
XBMCCOMMONS_HANDLE_UNCHECKED
catch (...) { CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__); }
CLog::Log(LOGERROR, "{} - Error removing {}", __FUNCTION__, url.GetRedacted());
return false;
}
void CDirectory::FilterFileDirectories(CFileItemList &items, const std::string &mask,
bool expandImages)
{
for (int i=0; i< items.Size(); ++i)
{
CFileItemPtr pItem=items[i];
auto mode = expandImages && pItem->IsDiscImage() ? EFILEFOLDER_TYPE_ONBROWSE : EFILEFOLDER_TYPE_ALWAYS;
if (!pItem->m_bIsFolder && pItem->IsFileFolder(mode))
{
std::unique_ptr<IFileDirectory> pDirectory(CFileDirectoryFactory::Create(pItem->GetURL(),pItem.get(),mask));
if (pDirectory)
pItem->m_bIsFolder = true;
else
if (pItem->m_bIsFolder)
{
items.Remove(i);
i--; // don't confuse loop
}
}
}
}
|