CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/DiskFree.cpp
Views: 1401
1
#ifdef _WIN32
2
#define WIN32_LEAN_AND_MEAN
3
#include <Windows.h>
4
#include <sys/stat.h>
5
#else
6
#include <dirent.h>
7
#include <unistd.h>
8
#include <sys/stat.h>
9
#if defined(__ANDROID__)
10
#include <sys/types.h>
11
#include <sys/vfs.h>
12
#define statvfs statfs
13
#else
14
#include <sys/statvfs.h>
15
#endif
16
#include <ctype.h>
17
#include <fcntl.h>
18
#endif
19
#include <cinttypes>
20
21
#include "Common/Log.h"
22
#include "Common/File/Path.h"
23
#include "Common/File/AndroidStorage.h"
24
#include "Common/Data/Encoding/Utf8.h"
25
26
#if PPSSPP_PLATFORM(UWP)
27
#include "UWP/UWPHelpers/StorageManager.h"
28
#endif
29
30
bool free_disk_space(const Path &path, int64_t &space) {
31
#ifdef _WIN32
32
ULARGE_INTEGER free;
33
#if PPSSPP_PLATFORM(UWP)
34
if (GetDriveFreeSpace(path, space)) {
35
return true;
36
}
37
else
38
#endif
39
if (GetDiskFreeSpaceExW(path.ToWString().c_str(), &free, nullptr, nullptr)) {
40
space = free.QuadPart;
41
return true;
42
}
43
#else
44
if (path.Type() == PathType::CONTENT_URI) {
45
space = Android_GetFreeSpaceByContentUri(path.ToString());
46
INFO_LOG(Log::Common, "Free space at '%s': %" PRIu64, path.c_str(), space);
47
return space >= 0;
48
}
49
50
struct statvfs diskstat;
51
int res = statvfs(path.c_str(), &diskstat);
52
53
if (res == 0) {
54
// Not sure why we're excluding Android here anyway...
55
#ifndef __ANDROID__
56
if (diskstat.f_flag & ST_RDONLY) {
57
// No space to write.
58
space = 0;
59
return true;
60
}
61
#endif
62
space = (uint64_t)diskstat.f_bavail * (uint64_t)diskstat.f_frsize;
63
return true;
64
}
65
#endif
66
67
// We can't know how much is free.
68
return false;
69
}
70
71