Measure free disk space to size block count.

This commit is contained in:
Unknown W. Brackets committed 2015-11-18 21:56:03 -08:00
1 parent f0ec6fab4d
commit df6cbafe0f
10 files changed
+112 -28

No files matched your search

+2
View File
@@ -923,6 +923,8 @@ add_library(native STATIC
ext/native/file/fd_util.h
ext/native/file/file_util.cpp
ext/native/file/file_util.h
ext/native/file/free.cpp
ext/native/file/free.h
ext/native/file/ini_file.cpp
ext/native/file/ini_file.h
ext/native/file/path.cpp
+28 -3
View File
@@ -18,11 +18,13 @@
#include <algorithm>
#include <string.h>
#include "file/file_util.h"
#include "file/free.h"
#include "Common/FileUtil.h"
#include "Core/FileLoaders/DiskCachingFileLoader.h"
#include "Core/System.h"
static const char *CACHEFILE_MAGIC = "ppssppDC";
static const s64 SAFETY_FREE_DISK_SPACE = 768 * 1024 * 1024; // 768 MB
std::string DiskCachingFileLoaderCache::cacheDir_;
@@ -534,7 +536,30 @@ void DiskCachingFileLoaderCache::CreateCacheFile(const std::string &path) {
}
}
u32 DiskCachingFileLoaderCache::DetermineMaxBlocks() {
// TODO (don't forget to clamp UPPER)
return 4096;
u64 DiskCachingFileLoaderCache::FreeDiskSpace() {
std::string dir = cacheDir_;
if (dir.empty()) {
dir = GetSysDirectory(DIRECTORY_CACHE);
}
uint64_t result = 0;
if (free_disk_space(dir, result)) {
return result;
}
// We can't know for sure how much is free, so we have to assume none.
return 0;
}
u32 DiskCachingFileLoaderCache::DetermineMaxBlocks() {
const s64 freeBytes = FreeDiskSpace();
// We want to leave them some room for other stuff.
const u64 availBytes = std::max(0LL, freeBytes - SAFETY_FREE_DISK_SPACE);
const u64 freeBlocks = availBytes / (u64)DEFAULT_BLOCK_SIZE;
if (freeBlocks > MAX_BLOCKS_UPPER_BOUND) {
return MAX_BLOCKS_UPPER_BOUND;
}
// Might be lower than LOWER_BOUND, but that's okay. It means not enough space.
return freeBlocks;
}
+1
View File
@@ -104,6 +104,7 @@ private:
void LoadCacheIndex();
void CreateCacheFile(const std::string &path);
u64 FreeDiskSpace();
u32 DetermineMaxBlocks();
// File format:
+8 -24
View File
@@ -16,6 +16,7 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <limits>
#include "file/free.h"
#include "file/zip_read.h"
#include "i18n/i18n.h"
#include "util/text/utf8.h"
@@ -798,35 +799,18 @@ std::vector<PSPFileInfo> DirectoryFileSystem::GetDirListing(std::string path) {
}
u64 DirectoryFileSystem::FreeSpace(const std::string &path) {
#ifdef _WIN32
const std::wstring w32path = ConvertUTF8ToWString(GetLocalPath(path));
ULARGE_INTEGER free;
if (GetDiskFreeSpaceExW(w32path.c_str(), &free, nullptr, nullptr))
return free.QuadPart;
#elif defined(__SYMBIAN32__)
QSystemStorageInfo storageInfo;
return (u64)storageInfo.availableDiskSpace("E");
#else
std::string localPath = GetLocalPath(path);
struct statvfs diskstat;
int res = statvfs(localPath.c_str(), &diskstat);
uint64_t result = 0;
if (free_disk_space(GetLocalPath(path), result)) {
return result;
}
#if HOST_IS_CASE_SENSITIVE
std::string fixedCase = path;
if (res != 0 && FixPathCase(basePath, fixedCase, FPC_FILE_MUST_EXIST)) {
if (FixPathCase(basePath, fixedCase, FPC_FILE_MUST_EXIST)) {
// May have failed due to case sensitivity, try again.
localPath = GetLocalPath(fixedCase);
res = statvfs(localPath.c_str(), &diskstat);
}
#endif
if (res == 0) {
#ifndef ANDROID
if (diskstat.f_flag & ST_RDONLY) {
return 0;
if (free_disk_space(GetLocalPath(fixedCase), result)) {
return result;
}
#endif
return (u64)diskstat.f_bavail * (u64)diskstat.f_frsize;
}
#endif
+1
View File
@@ -41,6 +41,7 @@ LOCAL_SRC_FILES :=\
file/fd_util.cpp \
file/chunk_file.cpp \
file/file_util.cpp \
file/free.cpp \
file/path.cpp \
file/ini_file.cpp \
file/zip_read.cpp \
+2 -1
View File
@@ -1,7 +1,8 @@
set(SRCS
chunk_file.cpp
zip_read.cpp
file_util.cpp)
file_util.cpp
free.cpp)
set(SRCS ${SRCS})
+56
View File
@@ -0,0 +1,56 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <sys/stat.h>
#else
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#if defined(ANDROID)
#include <sys/types.h>
#include <sys/vfs.h>
#define statvfs statfs
#elif defined(__SYMBIAN32__)
#include <mw/QSystemStorageInfo>
QTM_USE_NAMESPACE
#else
#include <sys/statvfs.h>
#endif
#include <ctype.h>
#include <fcntl.h>
#endif
#include "base/basictypes.h"
#include "util/text/utf8.h"
bool free_disk_space(const std::string &dir, uint64_t &space) {
#ifdef _WIN32
const std::wstring w32path = ConvertUTF8ToWString(dir);
ULARGE_INTEGER free;
if (GetDiskFreeSpaceExW(w32path.c_str(), &free, nullptr, nullptr)) {
space = free.QuadPart;
return true;
}
#elif defined(__SYMBIAN32__)
QSystemStorageInfo storageInfo;
space = (uint64_t)storageInfo.availableDiskSpace("E");
return true;
#else
struct statvfs diskstat;
int res = statvfs(dir.c_str(), &diskstat);
if (res == 0) {
#ifndef ANDROID
if (diskstat.f_flag & ST_RDONLY) {
space = 0;
return true;
}
#endif
space = (uint64_t)diskstat.f_bavail * (uint64_t)diskstat.f_frsize;
return true;
}
#endif
// We can't know how much is free.
return false;
}
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include <string>
#include "base/basictypes.h"
bool free_disk_space(const std::string &dir, uint64_t &space);
+2
View File
@@ -226,6 +226,7 @@
<ClInclude Include="file\ini_file.h" />
<ClInclude Include="file\vfs.h" />
<ClInclude Include="file\zip_read.h" />
<ClInclude Include="file\free.h" />
<ClInclude Include="gfx\gl_common.h" />
<ClInclude Include="gfx\gl_debug_log.h" />
<ClInclude Include="gfx\gl_lost_manager.h" />
@@ -680,6 +681,7 @@
<ClCompile Include="file\path.cpp" />
<ClCompile Include="file\ini_file.cpp" />
<ClCompile Include="file\zip_read.cpp" />
<ClCompile Include="file\free.cpp" />
<ClCompile Include="gfx\gl_debug_log.cpp" />
<ClCompile Include="gfx\gl_lost_manager.cpp" />
<ClCompile Include="gfx\texture_atlas.cpp" />
+6
View File
@@ -302,6 +302,9 @@
<ClInclude Include="input\keycodes.h">
<Filter>input</Filter>
</ClInclude>
<ClInclude Include="file\free.h">
<Filter>file</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@@ -736,6 +739,9 @@
<ClCompile Include="base\QtMain.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="file\free.cpp">
<Filter>file</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">