foxygit / fake-08-xbox360 Log in
commit fe4c5c52223fd8c56733c865411418d252aae389
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Tue Nov 24 22:22:23 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Tue Nov 24 22:22:23 2026 +0100

    xex runtime: delete/stat fallbacks for real consoles, rmdir, xex_mount

    Console report showed write/append/stat/read/seek/mkdir working, but
    NtDeleteFile failing. Now:
    - unlink/rmdir fall back to FileDispositionInformation on an open handle.
    - stat falls back to opening the path and querying the handle.
    - File size uses FileStandardInformation (FileNetworkOpenInformation as
      fallback).
    - xex_mount()/xex_unmount() to map a kernel device to "name:".
    - fs test reports the failing step and probes kernel device names
      (USB naming) through temporary mounts.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/runtime/fs.c          | 139 +++++++++++++++++++++++++++++++------
 platform/xex/runtime/xex_runtime.h |   4 ++
 platform/xex/tests/fs/main.cpp     |  67 +++++++++++++++---
 3 files changed, 179 insertions(+), 31 deletions(-)

diff --git a/platform/xex/runtime/fs.c b/platform/xex/runtime/fs.c
index 33f78bb..439b48a 100644
--- a/platform/xex/runtime/fs.c
+++ b/platform/xex/runtime/fs.c
@@ -16,6 +16,7 @@
 #include <sys/types.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <stdio.h>

 #include <xbox360/xboxkrnl.h>

@@ -163,14 +164,40 @@ static xex_file *get_file(int fd)
     return &files[fd];
 }

-static long long file_size(HANDLE handle)
+/* FileStandardInformation: supported by every NT-style kernel. */
+typedef struct {
+    LARGE_INTEGER AllocationSize;
+    LARGE_INTEGER EndOfFile;
+    ULONG NumberOfLinks;
+    BOOLEAN DeletePending;
+    BOOLEAN Directory;
+} XEX_FILE_STANDARD_INFORMATION;
+
+/* Size and attributes of an open file. Tries FileStandardInformation
+ * first (FileNetworkOpenInformation is not available on every kernel). */
+static int query_file(HANDLE handle, long long *size, ULONG *attributes)
 {
-    FILE_NETWORK_OPEN_INFORMATION info;
     IO_STATUS_BLOCK iosb;
-    if (!nt_ok(NtQueryInformationFile(handle, &iosb, &info, sizeof(info), FileNetworkOpenInformation))) {
-        return -1;
+    XEX_FILE_STANDARD_INFORMATION standard;
+    if (nt_ok(NtQueryInformationFile(handle, &iosb, &standard, sizeof(standard), FileStandardInformation))) {
+        *size = standard.EndOfFile.QuadPart;
+        *attributes = standard.Directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
+        return 0;
+    }
+    FILE_NETWORK_OPEN_INFORMATION info;
+    if (nt_ok(NtQueryInformationFile(handle, &iosb, &info, sizeof(info), FileNetworkOpenInformation))) {
+        *size = info.EndOfFile.QuadPart;
+        *attributes = info.FileAttributes;
+        return 0;
     }
-    return info.EndOfFile.QuadPart;
+    return -1;
+}
+
+static long long file_size(HANDLE handle)
+{
+    long long size;
+    ULONG attributes;
+    return query_file(handle, &size, &attributes) == 0 ? size : -1;
 }

 /* ------------------------------------------------------------------------ */
@@ -333,14 +360,13 @@ int _fstat_r(struct _reent *r, int fd, struct stat *st)
         r->_errno = EBADF;
         return -1;
     }
-    FILE_NETWORK_OPEN_INFORMATION info;
-    IO_STATUS_BLOCK iosb;
-    NTSTATUS status = NtQueryInformationFile(f->handle, &iosb, &info, sizeof(info), FileNetworkOpenInformation);
-    if (!nt_ok(status)) {
-        r->_errno = status_to_errno(status);
+    long long size;
+    ULONG attributes;
+    if (query_file(f->handle, &size, &attributes) != 0) {
+        r->_errno = EIO;
         return -1;
     }
-    fill_stat(st, info.FileAttributes, info.EndOfFile.QuadPart);
+    fill_stat(st, attributes, size);
     return 0;
 }

@@ -358,15 +384,22 @@ int _stat_r(struct _reent *r, const char *path, struct stat *st)
     init_attributes(&attributes, &name, kpath);
     NTSTATUS status = NtQueryFullAttributesFile(&attributes, &info);
     if (!nt_ok(status)) {
-        /* volume roots ("usb0:\") have no attributes: probe as a directory */
+        /* No attributes by name (volume roots like "usb0:\", or kernels
+         * without the call): open it and ask the handle. */
         HANDLE handle;
-        status = open_path(path, &handle, FILE_LIST_DIRECTORY, FILE_OPEN, FILE_DIRECTORY_FILE);
-        if (!nt_ok(status)) {
+        NTSTATUS open_status = open_path(path, &handle, FILE_READ_ATTRIBUTES, FILE_OPEN, 0);
+        if (!nt_ok(open_status)) {
+            open_status = open_path(path, &handle, FILE_LIST_DIRECTORY, FILE_OPEN, FILE_DIRECTORY_FILE);
+        }
+        if (!nt_ok(open_status)) {
             r->_errno = status_to_errno(status);
             return -1;
         }
+        long long size = 0;
+        ULONG file_attributes = FILE_ATTRIBUTE_DIRECTORY;
+        query_file(handle, &size, &file_attributes);
         NtClose(handle);
-        fill_stat(st, FILE_ATTRIBUTE_DIRECTORY, 0);
+        fill_stat(st, file_attributes, size);
         return 0;
     }
     fill_stat(st, info.FileAttributes, info.EndOfFile.QuadPart);
@@ -379,24 +412,59 @@ int _isatty_r(struct _reent *r, int fd)
     return fd >= 0 && fd < FIRST_FD;
 }

-int _unlink_r(struct _reent *r, const char *path)
+#ifndef DELETE
+#define DELETE 0x00010000
+#endif
+
+/* Deletes through FileDispositionInformation on an open handle: the
+ * standard NT way, used when NtDeleteFile fails. */
+static NTSTATUS delete_by_handle(const char *path, ULONG options)
+{
+    HANDLE handle;
+    NTSTATUS status = open_path(path, &handle, DELETE, FILE_OPEN, options);
+    if (!nt_ok(status)) return status;
+    IO_STATUS_BLOCK iosb;
+    BOOLEAN delete_file = TRUE;
+    status = NtSetInformationFile(handle, &iosb, &delete_file, sizeof(delete_file), FileDispositionInformation);
+    NtClose(handle);
+    return status;
+}
+
+static int delete_path(const char *path, ULONG options)
 {
     char kpath[XEX_PATH_MAX];
     STRING name;
     OBJECT_ATTRIBUTES attributes;
     if (xex_kernel_path(path, kpath, sizeof(kpath)) != 0) {
-        r->_errno = ENAMETOOLONG;
+        errno = ENAMETOOLONG;
         return -1;
     }
     init_attributes(&attributes, &name, kpath);
     NTSTATUS status = NtDeleteFile(&attributes);
     if (!nt_ok(status)) {
-        r->_errno = status_to_errno(status);
+        status = delete_by_handle(path, options);
+    }
+    if (!nt_ok(status)) {
+        errno = status_to_errno(status);
         return -1;
     }
     return 0;
 }

+int _unlink_r(struct _reent *r, const char *path)
+{
+    if (delete_path(path, FILE_NON_DIRECTORY_FILE) != 0) {
+        r->_errno = errno;
+        return -1;
+    }
+    return 0;
+}
+
+int rmdir(const char *path)
+{
+    return delete_path(path, FILE_DIRECTORY_FILE);
+}
+
 int _link_r(struct _reent *r, const char *existing, const char *new_path)
 {
     (void)existing; (void)new_path;
@@ -514,7 +582,7 @@ int closedir(DIR *dir)
 /* ------------------------------------------------------------------------ */
 /* Mount points                                                             */

-static void link_device(const char *link, const char *device)
+static NTSTATUS link_device(const char *link, const char *device)
 {
     char link_buf[64], device_buf[96];
     STRING link_name, device_name;
@@ -526,7 +594,38 @@ static void link_device(const char *link, const char *device)
     device_name.Length = (USHORT)strlen(device_buf);
     device_name.MaximumLength = device_name.Length + 1;
     device_name.Buffer = device_buf;
-    ObCreateSymbolicLink(&link_name, &device_name);
+    return ObCreateSymbolicLink(&link_name, &device_name);
+}
+
+/* name "usbX" -> "\??\usbX:" */
+static void link_name_of(const char *name, char *out, size_t size)
+{
+    size_t n = strlen(name);
+    if (n > 0 && name[n - 1] == ':') n--;
+    snprintf(out, size, "\\??\\%.*s:", (int)n, name);
+}
+
+int xex_mount(const char *name, const char *device)
+{
+    char link[64];
+    link_name_of(name, link, sizeof(link));
+    NTSTATUS status = link_device(link, device);
+    if (!nt_ok(status)) {
+        errno = status_to_errno(status);
+        return -1;
+    }
+    return 0;
+}
+
+int xex_unmount(const char *name)
+{
+    char link[64];
+    STRING link_name;
+    link_name_of(name, link, sizeof(link));
+    link_name.Length = (USHORT)strlen(link);
+    link_name.MaximumLength = link_name.Length + 1;
+    link_name.Buffer = link;
+    return nt_ok(ObDeleteSymbolicLink(&link_name)) ? 0 : -1;
 }

 void xex_mount_devices(void)
diff --git a/platform/xex/runtime/xex_runtime.h b/platform/xex/runtime/xex_runtime.h
index 71eea14..3a2cae1 100644
--- a/platform/xex/runtime/xex_runtime.h
+++ b/platform/xex/runtime/xex_runtime.h
@@ -15,6 +15,10 @@ void xex_debug_write(const void *buf, size_t len);
 /* Creates usb0:..usb2: (\Device\Mass0..2) and hdd: (Partition1). */
 void xex_mount_devices(void);

+/* Mounts a kernel device ("\\Device\\Mass0") as "name:" ("usb0"). */
+int xex_mount(const char *name, const char *device);
+int xex_unmount(const char *name);
+
 /* "usb0:/a/b" -> "\??\usb0:\a\b" (relative paths are under game:). */
 int xex_kernel_path(const char *path, char *out, size_t size);

diff --git a/platform/xex/tests/fs/main.cpp b/platform/xex/tests/fs/main.cpp
index e328a2a..2210a1e 100644
--- a/platform/xex/tests/fs/main.cpp
+++ b/platform/xex/tests/fs/main.cpp
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <ctime>
+#include <cerrno>

 #include "xex_runtime.h"

@@ -36,30 +37,41 @@ static int listDir(const std::string &path, int maxShow) {
     return n;
 }

+#include <cerrno>
+#include <unistd.h>
+
+static std::string g_failStep;
+
+static bool fail(const std::string &step) {
+    if (g_failStep.empty()) g_failStep = step + " (" + strerror(errno) + ")";
+    log(step + " FAILED errno=" + std::to_string(errno) + " " + strerror(errno));
+    return false;
+}
+
 // write, read back with stdio and iostream, stat, append, seek
 static bool fileRoundTrip(const std::string &dir) {
+    g_failStep.clear();
     std::string path = dir + "/fake08-xex-test.txt";
     FILE *f = fopen(path.c_str(), "w");
-    if (!f) { log("fopen(w) " + path + " FAILED"); return false; }
+    if (!f) return fail("fopen(w)");
     fputs("hello xbox\n", f);
     fclose(f);

     f = fopen(path.c_str(), "a");
-    if (!f) { log("fopen(a) FAILED"); return false; }
+    if (!f) return fail("fopen(a)");
     fputs("line2\n", f);
     fclose(f);

     struct stat st;
-    if (stat(path.c_str(), &st) != 0 || st.st_size != 17) {
-        log("stat FAILED size=" + std::to_string((long)st.st_size));
-        return false;
-    }
+    st.st_size = -1;
+    if (stat(path.c_str(), &st) != 0) return fail("stat");
+    if (st.st_size != 17) { errno = 0; return fail("size=" + std::to_string((long)st.st_size)); }

     std::ifstream in(path);
     std::string a, b;
     std::getline(in, a);
     std::getline(in, b);
-    if (a != "hello xbox" || b != "line2") { log("read back FAILED '" + a + "' '" + b + "'"); return false; }
+    if (a != "hello xbox" || b != "line2") { errno = 0; return fail("readback '" + a + "'"); }

     f = fopen(path.c_str(), "rb");
     fseek(f, 0, SEEK_END);
@@ -68,17 +80,20 @@ static bool fileRoundTrip(const std::string &dir) {
     char buf[5] = {0};
     fread(buf, 1, 4, f);
     fclose(f);
-    if (size != 17 || strcmp(buf, "xbox") != 0) { log("seek FAILED"); return false; }
+    if (size != 17 || strcmp(buf, "xbox") != 0) { errno = 0; return fail("seek size=" + std::to_string(size)); }

     std::string sub = dir + "/fake08-xex-dir";
     mkdir(sub.c_str(), 0777);
     struct stat sd;
     bool dirOk = stat(sub.c_str(), &sd) == 0 && S_ISDIR(sd.st_mode);
     log(std::string("mkdir/stat dir: ") + (dirOk ? "ok" : "FAILED"));
+    if (!dirOk) fail("mkdir");
+    if (dirOk && rmdir(sub.c_str()) != 0) fail("rmdir");

-    remove(path.c_str());
+    if (remove(path.c_str()) != 0) fail("remove");
     bool gone = stat(path.c_str(), &st) != 0;
     log(std::string("remove: ") + (gone ? "ok" : "FAILED"));
+    if (!gone) { errno = 0; fail("still there"); }
     return dirOk && gone;
 }

@@ -98,7 +113,37 @@ int main(int, char **) {
     log("clock over sleep(500): " + std::to_string(ms) + " ms");
     summary.push_back(std::string("time ") + date + " clk " + std::to_string(ms));

-    const char *devices[] = {"game:", "usb0:", "usb1:", "hdd:"};
+    // Which kernel device names exist on this console? (USB naming differs
+    // between FAT32 sticks and sticks configured as Xbox storage.)
+    const char *probes[] = {
+        "\\Device\\Mass0", "\\Device\\Mass1", "\\Device\\Mass2",
+        "\\Device\\Mass0PartitionFile\\Storage", "\\Device\\Mass1PartitionFile\\Storage",
+        "\\Device\\Mass2PartitionFile\\Storage",
+        "\\Device\\Mass0\\Partition1", "\\Device\\Mass0\\Partition0",
+        "\\Device\\Harddisk1\\Partition1", "\\Device\\Harddisk1\\Partition0",
+        "\\Device\\Harddisk0\\Partition1", "\\Device\\Cdrom0",
+    };
+    std::string found = "dev:";
+    for (const char *probe : probes) {
+        xex_unmount("probe");
+        if (xex_mount("probe", probe) != 0) {
+            log(std::string("probe ") + probe + ": mount failed");
+            continue;
+        }
+        DIR *d = opendir("probe:");
+        bool ok = d != nullptr;
+        int n = 0;
+        if (d) { while (readdir(d)) n++; closedir(d); }
+        xex_unmount("probe");
+        log(std::string("probe ") + probe + ": " + (ok ? "OK, " + std::to_string(n) + " entries" : std::string("no (") + strerror(errno) + ")"));
+        if (ok) {
+            const char *tail = strrchr(probe, '\\');
+            found += std::string(" ") + (strncmp(probe, "\\Device\\Mass", 13) == 0 ? probe + 8 : tail + 1);
+        }
+    }
+    summary.push_back(found);
+
+    const char *devices[] = {"game:", "usb0:", "usb1:", "usb2:", "hdd:"};
     for (const char *dev : devices) {
         struct stat st;
         if (stat(dev, &st) != 0) {
@@ -107,7 +152,7 @@ int main(int, char **) {
         }
         int n = listDir(dev, 8);
         bool rw = fileRoundTrip(dev);
-        std::string s = std::string(dev) + " " + std::to_string(n) + " files, rw " + (rw ? "ok" : "FAIL");
+        std::string s = std::string(dev) + " " + std::to_string(n) + " files, rw " + (rw ? "ok" : "FAIL: " + g_failStep);
         log(s);
         summary.push_back(s);
     }