Extract socket fstat to a method in Socket

This commit is contained in:
Marcin Mikołajczyk 2025-07-14 22:21:06 +01:00
parent 4d6263f368
commit 0787dc3e89
3 changed files with 35 additions and 14 deletions

View File

@ -28,7 +28,6 @@
#else
#include <sys/select.h>
#endif
#include <sys/stat.h>
namespace D = Core::Devices;
using FactoryDevice = std::function<std::shared_ptr<D::BaseDevice>(u32, const char*, int, u16)>;
@ -691,18 +690,12 @@ s32 PS4_SYSV_ABI fstat(s32 fd, OrbisKernelStat* sb) {
break;
}
case Core::FileSys::FileType::Socket: {
struct stat st{};
s32 result = ::fstat(file->socket->Native(), &st);
s32 result = file->socket->fstat(sb);
if (result < 0) {
ErrSceToPosix(result);
return -1;
}
sb->st_mode = 0000777u | 0140000u;
sb->st_size = st.st_size;
sb->st_blocks = st.st_blocks;
sb->st_blksize = st.st_blksize;
// sb->st_flags = st.st_flags;
break;
return result;
}
default:
UNREACHABLE();
@ -1106,10 +1099,10 @@ s32 PS4_SYSV_ABI posix_select(int nfds, fd_set* readfds, fd_set* writefds, fd_se
FD_ZERO(&read_host);
FD_ZERO(&write_host);
FD_ZERO(&except_host);
std::map<int, int> host_to_guest;
std::map<u64, int> host_to_guest;
auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
int max_fd = 0;
u64 max_fd = 0;
for (auto i = 0; i < nfds; ++i) {
auto read = readfds && FD_ISSET(i, readfds);
@ -1126,12 +1119,12 @@ s32 PS4_SYSV_ABI posix_select(int nfds, fd_set* readfds, fd_set* writefds, fd_se
return -1;
}
int fd = [&] {
u64 fd = [&] {
switch (file->type) {
case Core::FileSys::FileType::Regular:
return static_cast<int>(file->f.GetFileMapping());
return static_cast<u64>(file->f.GetFileMapping());
case Core::FileSys::FileType::Socket:
return file->socket->Native();
return static_cast<u64>(file->socket->Native());
default:
UNREACHABLE();
}

View File

@ -3,7 +3,11 @@
#include <common/assert.h>
#include "common/error.h"
#include "core/libraries/kernel/file_system.h"
#include "net.h"
#ifndef _WIN32
#include <sys/stat.h>
#endif
#include "net_error.h"
#include "sockets.h"
@ -458,6 +462,23 @@ int PosixSocket::GetSocketOptions(int level, int optname, void* optval, u32* opt
return 0;
}
int PosixSocket::fstat(Libraries::Kernel::OrbisKernelStat* sb) {
#ifdef _WIN32
LOG_ERROR(Lib_Net, "(STUBBED) called");
sb->st_mode = 0000777u | 0140000u;
return 0;
#else
struct stat st{};
int result = ::fstat(sock, &st);
sb->st_mode = 0000777u | 0140000u;
sb->st_size = st.st_size;
sb->st_blocks = st.st_blocks;
sb->st_blksize = st.st_blksize;
// sb->st_flags = st.st_flags;
return result;
#endif
}
int PosixSocket::read(void* buf, size_t len) {
#ifdef _WIN32
return recv(sock, buf, len, 0);

View File

@ -26,6 +26,10 @@ typedef int net_socket;
#include <mutex>
#include "net.h"
namespace Libraries::Kernel {
struct OrbisKernelStat;
}
namespace Libraries::Net {
struct Socket;
@ -52,6 +56,7 @@ struct Socket {
virtual int Connect(const OrbisNetSockaddr* addr, u32 namelen) = 0;
virtual int GetSocketAddress(OrbisNetSockaddr* name, u32* namelen) = 0;
virtual int GetPeerName(OrbisNetSockaddr* addr, u32* namelen) = 0;
virtual int fstat(Libraries::Kernel::OrbisKernelStat* stat) = 0;
virtual int read(void* buf, size_t len) = 0;
virtual int write(const void* buf, size_t len) = 0;
virtual bool IsValid() const = 0;
@ -84,6 +89,7 @@ struct PosixSocket : public Socket {
int Connect(const OrbisNetSockaddr* addr, u32 namelen) override;
int GetSocketAddress(OrbisNetSockaddr* name, u32* namelen) override;
int GetPeerName(OrbisNetSockaddr* addr, u32* namelen) override;
int fstat(Libraries::Kernel::OrbisKernelStat* stat) override;
int read(void* buf, size_t len) override;
int write(const void* buf, size_t len) override;
bool IsValid() const override {
@ -110,6 +116,7 @@ struct P2PSocket : public Socket {
int GetPeerName(OrbisNetSockaddr* addr, u32* namelen) override;
int read(void* buf, size_t len) override;
int write(const void* buf, size_t len) override;
int fstat(Libraries::Kernel::OrbisKernelStat* stat) override;
bool IsValid() const override {
return true;
}