summaryrefslogtreecommitdiffstats
path: root/xbmc/platform/posix/Filesystem.cpp
blob: 0db3af585c49bfbbef0c1ea3dcc57c649ba2a7c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "platform/Filesystem.h"
#include "filesystem/SpecialProtocol.h"
#include "utils/URIUtils.h"

#if defined(TARGET_LINUX)
#include <sys/statvfs.h>
#elif defined(TARGET_DARWIN) || defined(TARGET_FREEBSD)
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(TARGET_ANDROID)
#include <sys/statfs.h>
#endif

#include <cstdint>
#include <cstdlib>
#include <limits.h>
#include <string.h>

#include <unistd.h>

namespace KODI
{
namespace PLATFORM
{
namespace FILESYSTEM
{

space_info space(const std::string& path, std::error_code& ec)
{
  ec.clear();
  space_info sp;
#if defined(TARGET_LINUX)
  struct statvfs64 fsInfo;
  auto result = statvfs64(CSpecialProtocol::TranslatePath(path).c_str(), &fsInfo);
#else
  struct statfs fsInfo;
  // is 64-bit on android and darwin (10.6SDK + any iOS)
  auto result = statfs(CSpecialProtocol::TranslatePath(path).c_str(), &fsInfo);
#endif

  if (result != 0)
  {
    ec.assign(result, std::system_category());
    sp.available = static_cast<uintmax_t>(-1);
    sp.capacity = static_cast<uintmax_t>(-1);
    sp.free = static_cast<uintmax_t>(-1);
    return sp;
  }
  sp.available = static_cast<uintmax_t>(fsInfo.f_bavail * fsInfo.f_bsize);
  sp.capacity = static_cast<uintmax_t>(fsInfo.f_blocks * fsInfo.f_bsize);
  sp.free = static_cast<uintmax_t>(fsInfo.f_bfree * fsInfo.f_bsize);

  return sp;
}

std::string temp_directory_path(std::error_code &ec)
{
  ec.clear();

  auto result = getenv("TMPDIR");
  if (result)
    return URIUtils::AppendSlash(result);

  return "/tmp/";
}

std::string create_temp_directory(std::error_code &ec)
{
  char buf[PATH_MAX];

  auto path = temp_directory_path(ec);

  strncpy(buf, (path + "xbmctempXXXXXX").c_str(), sizeof(buf) - 1);
  buf[sizeof(buf) - 1] = '\0';

  auto tmp = mkdtemp(buf);
  if (!tmp)
  {
    ec.assign(errno, std::system_category());
    return std::string();
  }

  ec.clear();
  return std::string(tmp);
}

std::string temp_file_path(const std::string& suffix, std::error_code& ec)
{
  char tmp[PATH_MAX];

  auto tempPath = create_temp_directory(ec);
  if (ec)
    return std::string();

  tempPath = URIUtils::AddFileToFolder(tempPath, "xbmctempfileXXXXXX" + suffix);
  if (tempPath.length() >= PATH_MAX)
  {
    ec.assign(EOVERFLOW, std::system_category());
    return std::string();
  }

  strncpy(tmp, tempPath.c_str(), sizeof(tmp) - 1);
  tmp[sizeof(tmp) - 1] = '\0';

  auto fd = mkstemps(tmp, suffix.length());
  if (fd < 0)
  {
    ec.assign(errno, std::system_category());
    return std::string();
  }

  close(fd);

  ec.clear();
  return std::string(tmp);
}

}
}
}