[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / addons / FilesystemInstaller.cpp
blobf6dd80131aadee8dbd1d25c9027e207fc788740c
1 /*
2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8 #include "FilesystemInstaller.h"
10 #include "FileItem.h"
11 #include "FileItemList.h"
12 #include "filesystem/Directory.h"
13 #include "filesystem/File.h"
14 #include "filesystem/SpecialProtocol.h"
15 #include "utils/FileOperationJob.h"
16 #include "utils/StringUtils.h"
17 #include "utils/URIUtils.h"
18 #include "utils/log.h"
20 using namespace XFILE;
22 CFilesystemInstaller::CFilesystemInstaller()
23 : m_addonFolder(CSpecialProtocol::TranslatePath("special://home/addons/")),
24 m_tempFolder(CSpecialProtocol::TranslatePath("special://home/addons/temp/"))
28 bool CFilesystemInstaller::InstallToFilesystem(const std::string& archive, const std::string& addonId)
30 auto addonFolder = URIUtils::AddFileToFolder(m_addonFolder, addonId);
31 auto newAddonData = URIUtils::AddFileToFolder(m_tempFolder, StringUtils::CreateUUID());
32 auto oldAddonData = URIUtils::AddFileToFolder(m_tempFolder, StringUtils::CreateUUID());
34 if (!CDirectory::Create(newAddonData))
35 return false;
37 if (!UnpackArchive(archive, newAddonData))
39 CLog::Log(LOGERROR, "Failed to unpack archive '{}' to '{}'", archive, newAddonData);
40 return false;
43 bool hasOldData = CDirectory::Exists(addonFolder);
44 if (hasOldData)
46 if (!CFile::Rename(addonFolder, oldAddonData))
48 CLog::Log(LOGERROR, "Failed to move old addon files from '{}' to '{}'", addonFolder,
49 oldAddonData);
50 return false;
54 if (!CFile::Rename(newAddonData, addonFolder))
56 CLog::Log(LOGERROR, "Failed to move new addon files from '{}' to '{}'", newAddonData,
57 addonFolder);
58 return false;
61 if (hasOldData)
63 if (!CDirectory::RemoveRecursive(oldAddonData))
65 CLog::Log(LOGWARNING, "Failed to delete old addon files in '{}'", oldAddonData);
68 return true;
71 bool CFilesystemInstaller::UnInstallFromFilesystem(const std::string& addonFolder)
73 auto tempFolder = URIUtils::AddFileToFolder(m_tempFolder, StringUtils::CreateUUID());
74 if (!CFile::Rename(addonFolder, tempFolder))
76 CLog::Log(LOGERROR, "Failed to move old addon files from '{}' to '{}'", addonFolder,
77 tempFolder);
78 return false;
81 if (!CDirectory::RemoveRecursive(tempFolder))
83 CLog::Log(LOGWARNING, "Failed to delete old addon files in '{}'", tempFolder);
85 return true;
88 bool CFilesystemInstaller::UnpackArchive(std::string path, const std::string& dest)
90 if (!URIUtils::IsProtocol(path, "zip"))
91 path = URIUtils::CreateArchivePath("zip", CURL(path), "").Get();
93 CFileItemList files;
94 if (!CDirectory::GetDirectory(path, files, "", DIR_FLAG_DEFAULTS))
95 return false;
97 if (files.Size() == 1 && files[0]->m_bIsFolder)
99 path = files[0]->GetPath();
100 files.Clear();
101 if (!CDirectory::GetDirectory(path, files, "", DIR_FLAG_DEFAULTS))
102 return false;
104 CLog::Log(LOGDEBUG, "Unpacking {} to {}", path, dest);
106 for (auto i = 0; i < files.Size(); ++i)
107 files[i]->Select(true);
109 CFileOperationJob job(CFileOperationJob::ActionCopy, files, dest);
110 return job.DoWork();