Skip to content

Commit 0b2f85e

Browse files
committed
MemStick screen: Call free_disk_space from async tasks
See #19522
1 parent 3f3fd5b commit 0b2f85e

13 files changed

+65
-30
lines changed

Common/File/DiskFree.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
#include "Common/File/Path.h"
66

77
// If this fails, false is returned and space is negative.
8+
// Try to avoid calling this from the main thread, if possible. Can be SLOW.
89
bool free_disk_space(const Path &path, int64_t &space);

Core/FileSystems/BlobFileSystem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ bool BlobFileSystem::RemoveFile(const std::string &filename) {
130130
return false;
131131
}
132132

133-
u64 BlobFileSystem::FreeSpace(const std::string &path) {
133+
u64 BlobFileSystem::FreeDiskSpace(const std::string &path) {
134134
return 0;
135135
}

Core/FileSystems/BlobFileSystem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class BlobFileSystem : public IFileSystem {
5252
bool RmDir(const std::string &dirname) override;
5353
int RenameFile(const std::string &from, const std::string &to) override;
5454
bool RemoveFile(const std::string &filename) override;
55-
u64 FreeSpace(const std::string &path) override;
55+
u64 FreeDiskSpace(const std::string &path) override;
5656

5757
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
5858

Core/FileSystems/DirectoryFileSystem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ std::vector<PSPFileInfo> DirectoryFileSystem::GetDirListing(const std::string &p
871871
return ReplayApplyDiskListing(myVector, CoreTiming::GetGlobalTimeUs());
872872
}
873873

874-
u64 DirectoryFileSystem::FreeSpace(const std::string &path) {
874+
u64 DirectoryFileSystem::FreeDiskSpace(const std::string &path) {
875875
int64_t result = 0;
876876
if (free_disk_space(GetLocalPath(path), result)) {
877877
return ReplayApplyDisk64(ReplayAction::FREESPACE, (uint64_t)result, CoreTiming::GetGlobalTimeUs());

Core/FileSystems/DirectoryFileSystem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class DirectoryFileSystem : public IFileSystem {
8383
int RenameFile(const std::string &from, const std::string &to) override;
8484
bool RemoveFile(const std::string &filename) override;
8585
FileSystemFlags Flags() override { return flags; }
86-
u64 FreeSpace(const std::string &path) override;
86+
u64 FreeDiskSpace(const std::string &path) override;
8787

8888
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;
8989

@@ -129,7 +129,7 @@ class VFSFileSystem : public IFileSystem {
129129
int RenameFile(const std::string &from, const std::string &to) override;
130130
bool RemoveFile(const std::string &filename) override;
131131
FileSystemFlags Flags() override { return FileSystemFlags::FLASH; }
132-
u64 FreeSpace(const std::string &path) override { return 0; }
132+
u64 FreeDiskSpace(const std::string &path) override { return 0; }
133133

134134
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
135135

Core/FileSystems/FileSystem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class IFileSystem {
145145
virtual int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) = 0;
146146
virtual PSPDevType DevType(u32 handle) = 0;
147147
virtual FileSystemFlags Flags() = 0;
148-
virtual u64 FreeSpace(const std::string &path) = 0;
148+
virtual u64 FreeDiskSpace(const std::string &path) = 0;
149149
virtual bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) = 0;
150150
};
151151

@@ -175,7 +175,7 @@ class EmptyFileSystem : public IFileSystem {
175175
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override { return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED; }
176176
PSPDevType DevType(u32 handle) override { return PSPDevType::INVALID; }
177177
FileSystemFlags Flags() override { return FileSystemFlags::NONE; }
178-
u64 FreeSpace(const std::string &path) override { return 0; }
178+
u64 FreeDiskSpace(const std::string &path) override { return 0; }
179179
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
180180
};
181181

Core/FileSystems/ISOFileSystem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ISOFileSystem : public IFileSystem {
4444
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
4545
PSPDevType DevType(u32 handle) override;
4646
FileSystemFlags Flags() override;
47-
u64 FreeSpace(const std::string &path) override { return 0; }
47+
u64 FreeDiskSpace(const std::string &path) override { return 0; }
4848

4949
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
5050
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
@@ -146,7 +146,7 @@ class ISOBlockSystem : public IFileSystem {
146146
return isoFileSystem_->DevType(handle);
147147
}
148148
FileSystemFlags Flags() override { return isoFileSystem_->Flags(); }
149-
u64 FreeSpace(const std::string &path) override { return isoFileSystem_->FreeSpace(path); }
149+
u64 FreeDiskSpace(const std::string &path) override { return isoFileSystem_->FreeDiskSpace(path); }
150150

151151
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override {
152152
return isoFileSystem_->WriteFile(handle, pointer, size);

Core/FileSystems/MetaFileSystem.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -606,20 +606,18 @@ int MetaFileSystem::ReadEntireFile(const std::string &filename, std::vector<u8>
606606
return 0;
607607
}
608608

609-
u64 MetaFileSystem::FreeSpace(const std::string &path)
610-
{
609+
u64 MetaFileSystem::FreeDiskSpace(const std::string &path) {
611610
std::lock_guard<std::recursive_mutex> guard(lock);
612611
std::string of;
613612
IFileSystem *system;
614613
int error = MapFilePath(path, of, &system);
615614
if (error == 0)
616-
return system->FreeSpace(of);
615+
return system->FreeDiskSpace(of);
617616
else
618617
return 0;
619618
}
620619

621-
void MetaFileSystem::DoState(PointerWrap &p)
622-
{
620+
void MetaFileSystem::DoState(PointerWrap &p) {
623621
std::lock_guard<std::recursive_mutex> guard(lock);
624622

625623
auto s = p.Section("MetaFileSystem", 1);
@@ -634,8 +632,7 @@ void MetaFileSystem::DoState(PointerWrap &p)
634632
u32 n = (u32) fileSystems.size();
635633
Do(p, n);
636634
bool skipPfat0 = false;
637-
if (n != (u32) fileSystems.size())
638-
{
635+
if (n != (u32) fileSystems.size()) {
639636
if (n == (u32) fileSystems.size() - 1) {
640637
skipPfat0 = true;
641638
} else {

Core/FileSystems/MetaFileSystem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class MetaFileSystem : public IHandleAllocator, public IFileSystem {
127127
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
128128
PSPDevType DevType(u32 handle) override;
129129
FileSystemFlags Flags() override { return FileSystemFlags::NONE; }
130-
u64 FreeSpace(const std::string &path) override;
130+
u64 FreeDiskSpace(const std::string &path) override;
131131

132132
// Convenience helper - returns < 0 on failure.
133133
int ReadEntireFile(const std::string &filename, std::vector<u8> &data, bool quiet = false);

Core/FileSystems/VirtualDiscFileSystem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class VirtualDiscFileSystem: public IFileSystem {
4444
PSPDevType DevType(u32 handle) override;
4545
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
4646
FileSystemFlags Flags() override { return FileSystemFlags::UMD; }
47-
u64 FreeSpace(const std::string &path) override { return 0; }
47+
u64 FreeDiskSpace(const std::string &path) override { return 0; }
4848

4949
// unsupported operations
5050
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;

Core/HW/MemoryStick.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void MemoryStick_CalcInitialFree() {
103103

104104
AndroidJNIThreadContext jniContext;
105105

106-
memstickInitialFree = pspFileSystem.FreeSpace("ms0:/") + pspFileSystem.ComputeRecursiveDirectorySize("ms0:/PSP/SAVEDATA/");
106+
memstickInitialFree = pspFileSystem.FreeDiskSpace("ms0:/") + pspFileSystem.ComputeRecursiveDirectorySize("ms0:/PSP/SAVEDATA/");
107107

108108
std::unique_lock<std::mutex> guard(freeCalcMutex);
109109
freeCalcStatus = FreeCalcStatus::DONE;
@@ -122,10 +122,12 @@ static void MemoryStick_WaitInitialFree() {
122122
}
123123

124124
u64 MemoryStick_FreeSpace() {
125+
NOTICE_LOG(Log::IO, "Calculated free disk space");
126+
125127
MemoryStick_WaitInitialFree();
126128

127129
const CompatFlags &flags = PSP_CoreParameter().compat.flags();
128-
u64 realFreeSpace = pspFileSystem.FreeSpace("ms0:/");
130+
u64 realFreeSpace = pspFileSystem.FreeDiskSpace("ms0:/");
129131

130132
// Cap the memory stick size to avoid math errors when old games get sizes that were
131133
// not planned for back then (even though 2GB cards were available.)

UI/MemStickScreen.cpp

+37-10
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ ConfirmMemstickMoveScreen::~ConfirmMemstickMoveScreen() {
470470
moveDataTask_->BlockUntilReady();
471471
delete moveDataTask_;
472472
}
473+
if (oldSpaceTask_) {
474+
oldSpaceTask_->BlockUntilReady();
475+
delete oldSpaceTask_;
476+
}
477+
if (newSpaceTask_) {
478+
newSpaceTask_->BlockUntilReady();
479+
delete newSpaceTask_;
480+
}
473481
}
474482

475483
void ConfirmMemstickMoveScreen::CreateViews() {
@@ -479,7 +487,7 @@ void ConfirmMemstickMoveScreen::CreateViews() {
479487

480488
root_ = new LinearLayout(ORIENT_HORIZONTAL);
481489

482-
Path oldMemstickFolder = g_Config.memStickDirectory;
490+
Path &oldMemstickFolder = g_Config.memStickDirectory;
483491

484492
Spacer *spacerColumn = new Spacer(new LinearLayoutParams(20.0, FILL_PARENT, 0.0f));
485493
ViewGroup *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0));
@@ -488,18 +496,20 @@ void ConfirmMemstickMoveScreen::CreateViews() {
488496
root_->Add(leftColumn);
489497
root_->Add(rightColumn);
490498

491-
int64_t freeSpaceNew;
492-
int64_t freeSpaceOld;
493-
free_disk_space(newMemstickFolder_, freeSpaceNew);
494-
free_disk_space(oldMemstickFolder, freeSpaceOld);
495-
496499
leftColumn->Add(new TextView(ms->T("Selected PSP Data Folder"), ALIGN_LEFT, false));
497500
if (!initialSetup_) {
498501
leftColumn->Add(new NoticeView(NoticeLevel::WARN, ms->T("PPSSPP will restart after the change"), ""));
499502
}
500503
leftColumn->Add(new TextView(newMemstickFolder_.ToVisualString(), ALIGN_LEFT, false));
501-
std::string newFreeSpaceText = std::string(ms->T("Free space")) + ": " + FormatSpaceString(freeSpaceNew);
502-
leftColumn->Add(new TextView(newFreeSpaceText, ALIGN_LEFT, false));
504+
505+
newFreeSpaceView_ = leftColumn->Add(new TextView(ms->T("Free space"), ALIGN_LEFT, false));
506+
507+
newSpaceTask_ = Promise<SpaceResult *>::Spawn(&g_threadManager, [&]() -> SpaceResult * {
508+
int64_t freeSpaceNew;
509+
free_disk_space(newMemstickFolder_, freeSpaceNew);
510+
return new SpaceResult{ freeSpaceNew };
511+
}, TaskType::IO_BLOCKING, TaskPriority::HIGH);
512+
503513
if (existingFilesInNewFolder_) {
504514
leftColumn->Add(new NoticeView(NoticeLevel::SUCCESS, ms->T("Already contains PSP data"), ""));
505515
if (!moveData_) {
@@ -511,11 +521,15 @@ void ConfirmMemstickMoveScreen::CreateViews() {
511521
}
512522

513523
if (!oldMemstickFolder.empty()) {
514-
std::string oldFreeSpaceText = std::string(ms->T("Free space")) + ": " + FormatSpaceString(freeSpaceOld);
524+
oldSpaceTask_ = Promise<SpaceResult *>::Spawn(&g_threadManager, [&]() -> SpaceResult * {
525+
int64_t freeSpaceOld;
526+
free_disk_space(oldMemstickFolder, freeSpaceOld);
527+
return new SpaceResult{ freeSpaceOld };
528+
}, TaskType::IO_BLOCKING, TaskPriority::HIGH);
515529

516530
rightColumn->Add(new TextView(std::string(ms->T("Current")) + ":", ALIGN_LEFT, false));
517531
rightColumn->Add(new TextView(oldMemstickFolder.ToVisualString(), ALIGN_LEFT, false));
518-
rightColumn->Add(new TextView(oldFreeSpaceText, ALIGN_LEFT, false));
532+
oldFreeSpaceView_ = rightColumn->Add(new TextView(ms->T("Free space"), ALIGN_LEFT, false));
519533
}
520534

521535
if (moveDataTask_) {
@@ -567,6 +581,19 @@ void ConfirmMemstickMoveScreen::update() {
567581
moveDataTask_ = nullptr;
568582
}
569583
}
584+
585+
if (newSpaceTask_ && newFreeSpaceView_) {
586+
SpaceResult *result = newSpaceTask_->Poll();
587+
newFreeSpaceView_->SetText(std::string(ms->T("Free space")) + ": " + FormatSpaceString(result->bytesFree));
588+
delete newSpaceTask_;
589+
newSpaceTask_ = nullptr;
590+
}
591+
if (oldSpaceTask_ && oldFreeSpaceView_) {
592+
SpaceResult *result = oldSpaceTask_->Poll();
593+
oldFreeSpaceView_->SetText(std::string(ms->T("Free space")) + ": " + FormatSpaceString(result->bytesFree));
594+
delete oldSpaceTask_;
595+
oldSpaceTask_ = nullptr;
596+
}
570597
}
571598

572599
UI::EventReturn ConfirmMemstickMoveScreen::OnConfirm(UI::EventParams &params) {

UI/MemStickScreen.h

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class MemStickScreen : public UIDialogScreenWithBackground {
9393
#endif
9494
};
9595

96+
struct SpaceResult {
97+
int64_t bytesFree;
98+
};
99+
96100
class ConfirmMemstickMoveScreen : public UIDialogScreenWithBackground {
97101
public:
98102
ConfirmMemstickMoveScreen(const Path &newMemstickFolder, bool initialSetup);
@@ -121,8 +125,12 @@ class ConfirmMemstickMoveScreen : public UIDialogScreenWithBackground {
121125

122126
MoveProgressReporter progressReporter_;
123127
UI::TextView *progressView_ = nullptr;
128+
UI::TextView *newFreeSpaceView_ = nullptr;
129+
UI::TextView *oldFreeSpaceView_ = nullptr;
124130

125131
Promise<MoveResult *> *moveDataTask_ = nullptr;
132+
Promise<SpaceResult *> *oldSpaceTask_ = nullptr;
133+
Promise<SpaceResult *> *newSpaceTask_ = nullptr;
126134

127135
std::string error_;
128136
};

0 commit comments

Comments
 (0)