1 /* 2 * Copyright (C) 2005-2018 Team Kodi 3 * This file is part of Kodi - https://kodi.tv 4 * 5 * SPDX-License-Identifier: GPL-2.0-or-later 6 * See LICENSES/README.md for more information. 7 */ 8 9 #include "ModuleXbmcvfs.h" 10 11 #include "LanguageHook.h" 12 #include "URL.h" 13 #include "Util.h" 14 #include "filesystem/Directory.h" 15 #include "filesystem/File.h" 16 #include "filesystem/SpecialProtocol.h" 17 #include "utils/FileUtils.h" 18 #include "utils/URIUtils.h" 19 20 namespace XBMCAddon 21 { 22 23 namespace xbmcvfs 24 { copy(const String & strSource,const String & strDestination)25 bool copy(const String& strSource, const String& strDestination) 26 { 27 DelayedCallGuard dg; 28 return XFILE::CFile::Copy(strSource, strDestination); 29 } 30 31 // delete a file deleteFile(const String & strSource)32 bool deleteFile(const String& strSource) 33 { 34 DelayedCallGuard dg; 35 return XFILE::CFile::Delete(strSource); 36 } 37 38 // rename a file rename(const String & file,const String & newFile)39 bool rename(const String& file, const String& newFile) 40 { 41 DelayedCallGuard dg; 42 return XFILE::CFile::Rename(file,newFile); 43 } 44 45 // check for a file or folder existence, mimics Pythons os.path.exists() exists(const String & path)46 bool exists(const String& path) 47 { 48 DelayedCallGuard dg; 49 if (URIUtils::HasSlashAtEnd(path, true)) 50 return XFILE::CDirectory::Exists(path, false); 51 return XFILE::CFile::Exists(path, false); 52 } 53 54 // make legal file name makeLegalFilename(const String & filename)55 String makeLegalFilename(const String& filename) 56 { 57 XBMC_TRACE; 58 return CUtil::MakeLegalPath(filename); 59 } 60 61 // translate path translatePath(const String & path)62 String translatePath(const String& path) 63 { 64 XBMC_TRACE; 65 return CSpecialProtocol::TranslatePath(path); 66 } 67 68 // validate path validatePath(const String & path)69 String validatePath(const String& path) 70 { 71 XBMC_TRACE; 72 return CUtil::ValidatePath(path, true); 73 } 74 75 // make a directory mkdir(const String & path)76 bool mkdir(const String& path) 77 { 78 DelayedCallGuard dg; 79 return XFILE::CDirectory::Create(path); 80 } 81 82 // make all directories along the path mkdirs(const String & path)83 bool mkdirs(const String& path) 84 { 85 DelayedCallGuard dg; 86 return CUtil::CreateDirectoryEx(path); 87 } 88 rmdir(const String & path,bool force)89 bool rmdir(const String& path, bool force) 90 { 91 DelayedCallGuard dg; 92 93 if (force) 94 return CFileUtils::DeleteItem(path); 95 else 96 return XFILE::CDirectory::Remove(path); 97 } 98 listdir(const String & path)99 Tuple<std::vector<String>, std::vector<String> > listdir(const String& path) 100 { 101 DelayedCallGuard dg; 102 CFileItemList items; 103 std::string strSource; 104 strSource = path; 105 XFILE::CDirectory::GetDirectory(strSource, items, "", XFILE::DIR_FLAG_NO_FILE_DIRS); 106 107 Tuple<std::vector<String>, std::vector<String> > ret; 108 // initialize the Tuple to two values 109 ret.second(); 110 111 for (int i=0; i < items.Size(); i++) 112 { 113 std::string itemPath = items[i]->GetPath(); 114 115 if (URIUtils::HasSlashAtEnd(itemPath)) // folder 116 { 117 URIUtils::RemoveSlashAtEnd(itemPath); 118 std::string strFileName = URIUtils::GetFileName(itemPath); 119 if (strFileName.empty()) 120 { 121 CURL url(itemPath); 122 strFileName = url.GetHostName(); 123 } 124 ret.first().push_back(strFileName); 125 } 126 else // file 127 { 128 std::string strFileName = URIUtils::GetFileName(itemPath); 129 ret.second().push_back(strFileName); 130 } 131 } 132 133 return ret; 134 } 135 } 136 } 137