2019-08-17 13:51:31 -04:00
|
|
|
#include <cassert>
|
2008-01-15 20:27:44 +00:00
|
|
|
#include "DirectoryDevice.h"
|
2021-01-05 17:25:38 -05:00
|
|
|
#include "../Iop_PathUtils.h"
|
2022-02-25 17:32:56 -05:00
|
|
|
#include "PathDirectoryIterator.h"
|
2008-01-15 20:27:44 +00:00
|
|
|
#include "StdStream.h"
|
2017-12-31 13:38:32 -05:00
|
|
|
#include "string_cast.h"
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2008-01-19 03:36:27 +00:00
|
|
|
using namespace Iop::Ioman;
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
template <typename StringType>
|
|
|
|
Framework::CStdStream* CreateStdStream(const StringType&, const char*);
|
|
|
|
|
|
|
|
template <>
|
|
|
|
Framework::CStdStream* CreateStdStream(const std::string& path, const char* mode)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2017-12-31 13:38:32 -05:00
|
|
|
return new Framework::CStdStream(path.c_str(), mode);
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
template <>
|
|
|
|
Framework::CStdStream* CreateStdStream(const std::wstring& path, const char* mode)
|
|
|
|
{
|
|
|
|
auto cvtMode = string_cast<std::wstring>(mode);
|
|
|
|
return new Framework::CStdStream(path.c_str(), cvtMode.c_str());
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
Framework::CStream* CDirectoryDevice::GetFile(uint32 accessType, const char* devicePath)
|
|
|
|
{
|
2021-01-05 17:19:49 -05:00
|
|
|
auto basePath = GetBasePath();
|
2019-12-04 01:18:18 +00:00
|
|
|
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2021-03-09 17:42:50 -05:00
|
|
|
//Get rid of unwanted flags
|
|
|
|
//Used by Midnight Club 3
|
|
|
|
accessType &= ~OPEN_FLAG_NOWAIT;
|
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
const char* mode = nullptr;
|
2008-01-15 20:27:44 +00:00
|
|
|
switch(accessType)
|
|
|
|
{
|
2021-03-09 17:41:29 -05:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
[[fallthrough]];
|
2008-01-15 20:27:44 +00:00
|
|
|
case 0:
|
2012-09-05 20:40:23 +00:00
|
|
|
case OPEN_FLAG_RDONLY:
|
2008-01-15 20:27:44 +00:00
|
|
|
mode = "rb";
|
|
|
|
break;
|
2018-05-09 16:11:56 -04:00
|
|
|
case(OPEN_FLAG_WRONLY | OPEN_FLAG_CREAT):
|
2018-05-10 19:19:01 -04:00
|
|
|
case(OPEN_FLAG_WRONLY | OPEN_FLAG_CREAT | OPEN_FLAG_TRUNC):
|
2018-05-09 16:11:56 -04:00
|
|
|
mode = "wb";
|
|
|
|
break;
|
2021-01-05 17:19:49 -05:00
|
|
|
case OPEN_FLAG_WRONLY:
|
2018-09-18 12:29:59 -04:00
|
|
|
case OPEN_FLAG_RDWR:
|
2020-07-06 12:11:17 -04:00
|
|
|
mode = "r+b";
|
2018-09-18 12:29:59 -04:00
|
|
|
break;
|
2018-04-30 21:01:23 +01:00
|
|
|
case(OPEN_FLAG_RDWR | OPEN_FLAG_CREAT):
|
2021-01-03 20:08:28 -05:00
|
|
|
case(OPEN_FLAG_RDWR | OPEN_FLAG_CREAT | OPEN_FLAG_TRUNC):
|
2020-07-06 12:11:17 -04:00
|
|
|
mode = "w+b";
|
2008-01-15 20:27:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return CreateStdStream(path.native(), mode);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
2018-09-20 19:05:24 -04:00
|
|
|
|
2022-02-25 17:32:56 -05:00
|
|
|
DirectoryIteratorPtr CDirectoryDevice::GetDirectory(const char* devicePath)
|
2018-09-20 19:05:24 -04:00
|
|
|
{
|
2021-01-05 17:19:49 -05:00
|
|
|
auto basePath = GetBasePath();
|
2019-12-04 01:18:18 +00:00
|
|
|
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
|
2019-10-16 20:51:11 -04:00
|
|
|
if(!fs::is_directory(path))
|
2018-09-20 19:05:24 -04:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Not a directory.");
|
|
|
|
}
|
2022-02-25 17:32:56 -05:00
|
|
|
return std::make_unique<CPathDirectoryIterator>(path);
|
2018-09-20 19:05:24 -04:00
|
|
|
}
|
2020-04-23 17:00:09 +02:00
|
|
|
|
2021-01-06 11:06:44 -05:00
|
|
|
void CDirectoryDevice::MakeDirectory(const char* devicePath)
|
2020-04-23 17:00:09 +02:00
|
|
|
{
|
2021-01-05 17:19:49 -05:00
|
|
|
auto basePath = GetBasePath();
|
2020-04-23 17:00:09 +02:00
|
|
|
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
|
|
|
|
if(!fs::create_directory(path))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to create directory.");
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 18:10:33 -04:00
|
|
|
|
|
|
|
void CDirectoryDevice::Rename(const char* srcDevicePath, const char* dstDevicePath)
|
|
|
|
{
|
|
|
|
auto basePath = GetBasePath();
|
|
|
|
auto srcPath = Iop::PathUtils::MakeHostPath(basePath, srcDevicePath);
|
|
|
|
auto dstPath = Iop::PathUtils::MakeHostPath(basePath, dstDevicePath);
|
|
|
|
fs::rename(srcPath, dstPath);
|
|
|
|
}
|