// Runtime check 2: files, directories and time through newlib on the
// kernel's Nt* API. Writes a report to every writable device and shows a
// summary as guide notifications; the full report is also on DbgPrint.
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <fstream>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <ctime>
#include <cerrno>
#include "xex_runtime.h"
static std::string report;
static void log(const std::string &line) {
report += line + "\n";
printf("fs-test: %s\n", line.c_str());
}
static int listDir(const std::string &path, int maxShow) {
DIR *d = opendir(path.c_str());
if (!d) {
log("opendir " + path + " FAILED");
return -1;
}
int n = 0;
while (struct dirent *e = readdir(d)) {
if (n < maxShow) log(std::string(" ") + (e->d_type == DT_DIR ? "[" : "") + e->d_name + (e->d_type == DT_DIR ? "]" : ""));
n++;
}
closedir(d);
log("opendir " + path + ": " + std::to_string(n) + " entries");
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) return fail("fopen(w)");
fputs("hello xbox\n", f);
fclose(f);
f = fopen(path.c_str(), "a");
if (!f) return fail("fopen(a)");
fputs("line2\n", f);
fclose(f);
struct stat st;
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);
in.close(); // an open file cannot be deleted on the console (sharing violation)
if (a != "hello xbox" || b != "line2") { errno = 0; return fail("readback '" + a + "'"); }
f = fopen(path.c_str(), "rb");
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 6, SEEK_SET);
char buf[5] = {0};
fread(buf, 1, 4, f);
fclose(f);
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");
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;
}
int main(int, char **) {
std::vector<std::string> summary;
struct timeval tv;
gettimeofday(&tv, nullptr);
time_t t = tv.tv_sec;
char date[64];
strftime(date, sizeof date, "%Y-%m-%d %H:%M:%S", gmtime(&t));
log(std::string("time: ") + date + " UTC");
clock_t c0 = clock();
xex_sleep_ms(500);
long ms = (long)((clock() - c0) * 1000 / CLOCKS_PER_SEC);
log("clock over sleep(500): " + std::to_string(ms) + " ms");
summary.push_back(std::string("time ") + date + " clk " + std::to_string(ms));
// 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) {
log(std::string(dev) + " not present");
continue;
}
int n = listDir(dev, 8);
bool rw = fileRoundTrip(dev);
std::string s = std::string(dev) + " " + std::to_string(n) + " files, rw " + (rw ? "ok" : "FAIL: " + g_failStep);
log(s);
summary.push_back(s);
}
listDir("usb0:/p8carts", 5);
// keep the full report next to the XEX
if (FILE *f = fopen("game:/fs-test-report.txt", "w")) {
fputs(report.c_str(), f);
fclose(f);
}
for (auto &s : summary) {
xex_notify(s.c_str());
xex_sleep_ms(4000);
}
return 0;
}