[videodb] remove unused seasons table from episode_view
[xbmc.git] / xbmc / platform / posix / Filesystem.cpp
blob6e7282ca0013bd6b8f70b3039d2a3dd0d46da853
1 /*
2 * Copyright (C) 2005-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 */
9 #include "platform/Filesystem.h"
10 #include "filesystem/SpecialProtocol.h"
11 #include "utils/URIUtils.h"
13 #if defined(TARGET_LINUX)
14 #include <sys/statvfs.h>
15 #elif defined(TARGET_DARWIN) || defined(TARGET_FREEBSD)
16 #include <sys/param.h>
17 #include <sys/mount.h>
18 #elif defined(TARGET_ANDROID)
19 #include <sys/statfs.h>
20 #endif
22 #include <cstdint>
23 #include <cstdlib>
24 #include <limits.h>
25 #include <string.h>
26 #include <unistd.h>
28 namespace KODI
30 namespace PLATFORM
32 namespace FILESYSTEM
35 space_info space(const std::string& path, std::error_code& ec)
37 ec.clear();
38 space_info sp;
39 #if defined(TARGET_LINUX)
40 struct statvfs64 fsInfo;
41 auto result = statvfs64(CSpecialProtocol::TranslatePath(path).c_str(), &fsInfo);
42 #else
43 struct statfs fsInfo;
44 // is 64-bit on android and darwin (10.6SDK + any iOS)
45 auto result = statfs(CSpecialProtocol::TranslatePath(path).c_str(), &fsInfo);
46 #endif
48 if (result != 0)
50 ec.assign(result, std::system_category());
51 sp.available = static_cast<uintmax_t>(-1);
52 sp.capacity = static_cast<uintmax_t>(-1);
53 sp.free = static_cast<uintmax_t>(-1);
54 return sp;
56 sp.available = static_cast<uintmax_t>(fsInfo.f_bavail * fsInfo.f_bsize);
57 sp.capacity = static_cast<uintmax_t>(fsInfo.f_blocks * fsInfo.f_bsize);
58 sp.free = static_cast<uintmax_t>(fsInfo.f_bfree * fsInfo.f_bsize);
60 return sp;
63 std::string temp_directory_path(std::error_code &ec)
65 ec.clear();
67 auto result = getenv("TMPDIR");
68 if (result)
69 return URIUtils::AppendSlash(result);
71 return "/tmp/";
74 std::string create_temp_directory(std::error_code &ec)
76 char buf[PATH_MAX];
78 auto path = temp_directory_path(ec);
80 strncpy(buf, (path + "xbmctempXXXXXX").c_str(), sizeof(buf) - 1);
81 buf[sizeof(buf) - 1] = '\0';
83 auto tmp = mkdtemp(buf);
84 if (!tmp)
86 ec.assign(errno, std::system_category());
87 return std::string();
90 ec.clear();
91 return std::string(tmp);
94 std::string temp_file_path(const std::string& suffix, std::error_code& ec)
96 char tmp[PATH_MAX];
98 auto tempPath = create_temp_directory(ec);
99 if (ec)
100 return std::string();
102 tempPath = URIUtils::AddFileToFolder(tempPath, "xbmctempfileXXXXXX" + suffix);
103 if (tempPath.length() >= PATH_MAX)
105 ec.assign(EOVERFLOW, std::system_category());
106 return std::string();
109 strncpy(tmp, tempPath.c_str(), sizeof(tmp) - 1);
110 tmp[sizeof(tmp) - 1] = '\0';
112 auto fd = mkstemps(tmp, suffix.length());
113 if (fd < 0)
115 ec.assign(errno, std::system_category());
116 return std::string();
119 close(fd);
121 ec.clear();
122 return std::string(tmp);