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.
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>
35 space_info
space(const std::string
& path
, std::error_code
& ec
)
39 #if defined(TARGET_LINUX)
40 struct statvfs64 fsInfo
;
41 auto result
= statvfs64(CSpecialProtocol::TranslatePath(path
).c_str(), &fsInfo
);
44 // is 64-bit on android and darwin (10.6SDK + any iOS)
45 auto result
= statfs(CSpecialProtocol::TranslatePath(path
).c_str(), &fsInfo
);
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);
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
);
63 std::string
temp_directory_path(std::error_code
&ec
)
67 auto result
= getenv("TMPDIR");
69 return URIUtils::AppendSlash(result
);
74 std::string
create_temp_directory(std::error_code
&ec
)
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
);
86 ec
.assign(errno
, std::system_category());
91 return std::string(tmp
);
94 std::string
temp_file_path(const std::string
& suffix
, std::error_code
& ec
)
98 auto tempPath
= create_temp_directory(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());
115 ec
.assign(errno
, std::system_category());
116 return std::string();
122 return std::string(tmp
);