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.
8 #include "FilesystemInstaller.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
))
37 if (!UnpackArchive(archive
, newAddonData
))
39 CLog::Log(LOGERROR
, "Failed to unpack archive '{}' to '{}'", archive
, newAddonData
);
43 bool hasOldData
= CDirectory::Exists(addonFolder
);
46 if (!CFile::Rename(addonFolder
, oldAddonData
))
48 CLog::Log(LOGERROR
, "Failed to move old addon files from '{}' to '{}'", addonFolder
,
54 if (!CFile::Rename(newAddonData
, addonFolder
))
56 CLog::Log(LOGERROR
, "Failed to move new addon files from '{}' to '{}'", newAddonData
,
63 if (!CDirectory::RemoveRecursive(oldAddonData
))
65 CLog::Log(LOGWARNING
, "Failed to delete old addon files in '{}'", oldAddonData
);
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
,
81 if (!CDirectory::RemoveRecursive(tempFolder
))
83 CLog::Log(LOGWARNING
, "Failed to delete old addon files in '{}'", tempFolder
);
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();
94 if (!CDirectory::GetDirectory(path
, files
, "", DIR_FLAG_DEFAULTS
))
97 if (files
.Size() == 1 && files
[0]->m_bIsFolder
)
99 path
= files
[0]->GetPath();
101 if (!CDirectory::GetDirectory(path
, files
, "", DIR_FLAG_DEFAULTS
))
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
);