mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libkernel): implement lstat
This commit is contained in:
@@ -9,6 +9,7 @@ namespace File {
|
||||
void FillFileStat(const std::filesystem::path& nativePath, FileStat* sb);
|
||||
void FillFileStat(int nativeDescriptor, FileStat* sb);
|
||||
bool FillFileStatFromDescriptor(int fd, FileStat* sb);
|
||||
bool FillLinkStat(const std::filesystem::path& nativePath, FileStat* sb);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "prx/libkernel/File/include/NativeStat.hpp"
|
||||
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -15,6 +16,11 @@ static int DoFstat(int fd, NativeStat* st) {
|
||||
if (const auto directory = File::DirectoryDescriptorPath(fd)) return DoStat(*directory, st);
|
||||
return _fstat64(fd, st);
|
||||
}
|
||||
static int DoLstat(const std::filesystem::path& p, NativeStat* st) {
|
||||
std::error_code error;
|
||||
if (std::filesystem::is_symlink(std::filesystem::symlink_status(p, error))) NotImplemented_nid_no_patch("lstat of a Windows symbolic link");
|
||||
return DoStat(p, st);
|
||||
}
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
using NativeStat = struct stat;
|
||||
@@ -24,6 +30,9 @@ static int DoStat(const std::filesystem::path& p, NativeStat* st) {
|
||||
static int DoFstat(int fd, NativeStat* st) {
|
||||
return ::fstat(fd, st);
|
||||
}
|
||||
static int DoLstat(const std::filesystem::path& p, NativeStat* st) {
|
||||
return ::lstat(p.c_str(), st);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void CopyNativeStat(const NativeStat& st, FileStat* sb) {
|
||||
@@ -99,4 +108,11 @@ bool FillFileStatFromDescriptor(int fd, FileStat* sb) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FillLinkStat(const std::filesystem::path& nativePath, FileStat* sb) {
|
||||
NativeStat st{};
|
||||
if (DoLstat(nativePath, &st) != 0) return false;
|
||||
CopyNativeStat(st, sb);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -400,6 +400,13 @@ int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb) {
|
||||
return PosixResult(sceKernelStat(path, sb));
|
||||
}
|
||||
|
||||
int APS5_VABI lstat_nid_postfix(const char* path, FileStat* sb) {
|
||||
if (sb == nullptr) return PosixFailure(GUEST_EFAULT);
|
||||
if (const int error = PathError(path)) return PosixFailure(error);
|
||||
if (!File::FillLinkStat(ResolvePath_nid_no_patch(path), sb)) return PosixResult(SceErrorFromErrno(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI unlink_nid_postfix(const char* path) {
|
||||
if (const int error = PathError(path)) return PosixFailure(error);
|
||||
return PosixResult(sceKernelUnlink(path));
|
||||
|
||||
@@ -28,6 +28,7 @@ int APS5_VABI open_nid_postfix(const char*, int, int);
|
||||
int APS5_VABI _open_nid_postfix(const char*, int, ...);
|
||||
int APS5_VABI close_nid_postfix(int);
|
||||
int APS5_VABI stat_nid_postfix(const char*, FileStat*);
|
||||
int APS5_VABI lstat_nid_postfix(const char*, FileStat*);
|
||||
int APS5_VABI unlink_nid_postfix(const char*);
|
||||
int APS5_VABI rmdir_nid_postfix(const char*);
|
||||
int APS5_VABI mkdir_nid_postfix(const char*, unsigned short);
|
||||
@@ -175,6 +176,27 @@ int main() {
|
||||
Require(stat_nid_postfix("", &status) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(stat_nid_postfix(nullptr, &status) == -1 && *__error_nid_postfix() == 14);
|
||||
Require(stat_nid_postfix(presentName.c_str(), nullptr) == -1 && *__error_nid_postfix() == 14);
|
||||
FileStat linkStatus{};
|
||||
Require(lstat_nid_postfix(presentName.c_str(), &linkStatus) == 0 && linkStatus.st_size == 5);
|
||||
Require((linkStatus.st_mode & 0170000) == 0100000 && linkStatus.st_ino == status.st_ino);
|
||||
Require(lstat_nid_postfix(rootName.c_str(), &linkStatus) == 0 && (linkStatus.st_mode & 0170000) == 0040000);
|
||||
Require(lstat_nid_postfix(missingName.c_str(), &linkStatus) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(lstat_nid_postfix("", &linkStatus) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(lstat_nid_postfix(nullptr, &linkStatus) == -1 && *__error_nid_postfix() == 14);
|
||||
Require(lstat_nid_postfix(presentName.c_str(), nullptr) == -1 && *__error_nid_postfix() == 14);
|
||||
#ifndef _WIN32
|
||||
const auto link = root / "link";
|
||||
std::filesystem::create_symlink("present.txt", link);
|
||||
Require(lstat_nid_postfix(link.string().c_str(), &linkStatus) == 0);
|
||||
Require((linkStatus.st_mode & 0170000) == 0120000 && linkStatus.st_size == 11);
|
||||
Require(stat_nid_postfix(link.string().c_str(), &status) == 0 && (status.st_mode & 0170000) == 0100000);
|
||||
const auto dangling = root / "dangling";
|
||||
std::filesystem::create_symlink("missing.txt", dangling);
|
||||
Require(lstat_nid_postfix(dangling.string().c_str(), &linkStatus) == 0 && (linkStatus.st_mode & 0170000) == 0120000);
|
||||
Require(stat_nid_postfix(dangling.string().c_str(), &status) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(lstat_nid_postfix((root / "present.txt" / "child").string().c_str(), &linkStatus) == -1 && *__error_nid_postfix() == 20);
|
||||
Require(unlink_nid_postfix(link.string().c_str()) == 0 && unlink_nid_postfix(dangling.string().c_str()) == 0);
|
||||
#endif
|
||||
const int opened = open_nid_postfix(presentName.c_str(), 0, 0);
|
||||
Require(opened >= 0 && close_nid_postfix(opened) == 0);
|
||||
const int reopened = _open_nid_postfix(presentName.c_str(), 0);
|
||||
|
||||
Reference in New Issue
Block a user