/* * plugin_disks - SCORPIOX BOT open-source local storage inspector plugin * * A single clean-room C99 binary that serves THREE modes: * 1. --meta / SX_PLUGIN_META=1 -> JSON metadata for discovery * 2. ?action=mounts | ?action=dirs&root=workspace|home | ?format=json * -> JSON node API * 3. GET (default) -> responsive HTML dashboard * * Local node only. Reads the local kernel interfaces (/proc/mounts + statvfs on * Linux, getmntinfo on macOS, GetLogicalDriveStringsA + GetDiskFreeSpaceExA on * Windows). Makes ZERO outbound network requests, opens no sockets, runs no * remote commands and reads no credentials. * * Directory scanning is deliberately bounded: only the allowlisted roots * /workspace and $HOME may be walked, never an arbitrary path from the query * string, and the walk is capped in depth and in total entries visited. * * Zero dependencies. Builds with gcc, musl-gcc -static, Apple clang and * x86_64-w64-mingw32-gcc -static. Runs as a CGI-style executable under * scorpiox-server on the node. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if defined(__APPLE__) /* sys/mount.h and friends pull BSD types that strict c99 would hide */ # define _DARWIN_C_SOURCE 1 #elif !defined(_WIN32) # define _POSIX_C_SOURCE 200809L # define _DEFAULT_SOURCE 1 #endif #if defined(_WIN32) && !defined(_WIN32_WINNT) # define _WIN32_WINNT 0x0601 /* GetDiskFreeSpaceEx / GetVolumeInformationA */ #endif #include #include #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include # include # include #else # include # include # include # include # include # if defined(__APPLE__) # include # include # endif #endif #include "bot_theme_plugin.h" #define PLUGIN_SLUG "disks" #define PLUGIN_ID "plugin_disks" #define PLUGIN_VER "1.0.2" #define MAX_MOUNTS 256 #define MAX_DIRS 512 #define MAX_SCAN_ENTRIES 120000 #define MAX_DEPTH 8 #define MAX_NAME 256 #define MAX_PATH 1024 /* ------------------------------------------------------------------ */ /* Small utilities */ /* ------------------------------------------------------------------ */ /* Percent-decode in place ('+' becomes space). */ static void url_decode(char *s) { char *o = s; if (!s) return; while (*s) { if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) { int hi = s[1] <= '9' ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); int lo = s[2] <= '9' ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); *o++ = (char)((hi << 4) | lo); s += 3; } else if (*s == '+') { *o++ = ' '; s++; } else { *o++ = *s++; } } *o = '\0'; } /* Read one parameter (decoded) out of a form/query string. */ static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) { size_t klen; const char *p; if (!out || outsz == 0) return 0; out[0] = '\0'; if (!qs || !key) return 0; klen = strlen(key); for (p = qs; *p; ) { if (strncmp(p, key, klen) == 0 && p[klen] == '=') { const char *v = p + klen + 1; size_t i = 0; while (*v && *v != '&' && i + 1 < outsz) out[i++] = *v++; out[i] = '\0'; url_decode(out); return 1; } p = strchr(p, '&'); if (!p) break; p++; } return 0; } static void json_str(const char *s) { putchar('"'); for (; s && *s; s++) { unsigned char c = (unsigned char)*s; switch (c) { case '"': fputs("\\\"", stdout); break; case '\\': fputs("\\\\", stdout); break; case '\n': fputs("\\n", stdout); break; case '\r': fputs("\\r", stdout); break; case '\t': fputs("\\t", stdout); break; default: if (c < 0x20) printf("\\u%04x", (unsigned)c); else putchar((int)c); } } putchar('"'); } static void utc_now(char *buf, size_t sz) { time_t t = time(NULL); struct tm tmv; #if defined(_WIN32) gmtime_s(&tmv, &t); #else gmtime_r(&t, &tmv); #endif if (strftime(buf, sz, "%Y-%m-%dT%H:%M:%SZ", &tmv) == 0) { strncpy(buf, "1970-01-01T00:00:00Z", sz - 1); buf[sz - 1] = '\0'; } } static long node_pid(void) { #if defined(_WIN32) return (long)GetCurrentProcessId(); #else return (long)getpid(); #endif } static double clamp_pct(double v) { if (v < 0.0) return 0.0; if (v > 100.0) return 100.0; return v; } /* Monotonic-ish millisecond clock used only to report scan duration. */ static long long now_ms(void) { #if defined(_WIN32) return (long long)GetTickCount64(); #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0; return (long long)ts.tv_sec * 1000LL + (long long)(ts.tv_nsec / 1000000L); #endif } static void copy_str(char *dst, size_t dstsz, const char *src) { size_t n; if (!dst || dstsz == 0) return; if (!src) { dst[0] = '\0'; return; } n = strlen(src); if (n >= dstsz) n = dstsz - 1; memcpy(dst, src, n); dst[n] = '\0'; } /* ------------------------------------------------------------------ */ /* Node authentication */ /* ------------------------------------------------------------------ */ /* * The node API on :8150 is reachable from the LAN. Every request except * --meta discovery must prove it knows the node password. Accepted forms * match the rest of the node API: X-Bot-Password (what bot-web proxies * with), Authorization: Bearer, and ?pwd= (what the plugin iframe appends). * When BOT_PASSWORD is unset the plugin is a deliberate no-op, which is how * a developer runs it locally from a shell. */ static void require_node_auth(void) { const char *np = getenv("BOT_PASSWORD"); const char *xp; const char *auth; char qpwd[256]; const char *qs; if (!np || !*np) return; xp = getenv("HTTP_X_BOT_PASSWORD"); if (xp && strcmp(xp, np) == 0) return; auth = getenv("HTTP_AUTHORIZATION"); if (auth) { while (*auth == ' ') auth++; if (strncmp(auth, "Bearer ", 7) == 0) { auth += 7; while (*auth == ' ') auth++; if (strcmp(auth, np) == 0) return; } } qs = getenv("QUERY_STRING"); if (qs && get_query_val(qs, "pwd", qpwd, sizeof(qpwd)) && strcmp(qpwd, np) == 0) return; fputs("Status: 401 Unauthorized\r\n" "Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n" "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); exit(0); } /* ------------------------------------------------------------------ */ /* Mount table */ /* ------------------------------------------------------------------ */ typedef struct { char device[MAX_NAME]; char fstype[64]; char mount[MAX_PATH]; unsigned long long total_bytes; unsigned long long used_bytes; unsigned long long free_bytes; unsigned long long avail_bytes; unsigned long long inodes_total; unsigned long long inodes_used; int has_inodes; } sx_mount_t; #if !defined(_WIN32) /* Filesystems that report kernel-internal state rather than user storage. */ static int pseudo_fstype(const char *fs) { static const char *PSEUDO[] = { "proc", "sysfs", "cgroup", "cgroup2", "devpts", "devtmpfs", "securityfs", "pstore", "bpf", "debugfs", "tracefs", "hugetlbfs", "mqueue", "autofs", "configfs", "fusectl", "efivarfs", "rpc_pipefs", "nsfs", "binfmt_misc", "selinuxfs", "fuse.gvfsd-fuse", "fuse.portal", "procfs", "devfs", "none", NULL }; int i; for (i = 0; PSEUDO[i]; i++) if (strcmp(fs, PSEUDO[i]) == 0) return 1; return 0; } /* * Kernel mountpoints that exist only to back kernel state. /dev, /sys, /proc * and anything below them are never interesting to a user asking about * storage; /run is a real tmpfs and is kept. */ static int pseudo_mountpoint(const char *mp) { static const char *PREFIX[] = { "/sys", "/proc", "/dev", NULL }; int i; for (i = 0; PREFIX[i]; i++) { size_t n = strlen(PREFIX[i]); if (strncmp(mp, PREFIX[i], n) == 0 && (mp[n] == '\0' || mp[n] == '/')) return 1; } return 0; } #endif /* !_WIN32 */ #if !defined(_WIN32) && !defined(__APPLE__) /* /proc/mounts and /etc/mtab escape space, tab, newline and backslash as octal. */ static void unescape_mount_field(char *s) { char *o = s; while (*s) { if (*s == '\\' && s[1] >= '0' && s[1] <= '3' && s[2] >= '0' && s[2] <= '7' && s[3] >= '0' && s[3] <= '7') { *o++ = (char)(((s[1] - '0') << 6) | ((s[2] - '0') << 3) | (s[3] - '0')); s += 4; } else { *o++ = *s++; } } *o = '\0'; } static void fill_vfs(const char *mp, sx_mount_t *m) { struct statvfs vfs; if (statvfs(mp, &vfs) != 0) return; m->total_bytes = (unsigned long long)vfs.f_blocks * (unsigned long long)vfs.f_frsize; m->free_bytes = (unsigned long long)vfs.f_bfree * (unsigned long long)vfs.f_frsize; m->avail_bytes = (unsigned long long)vfs.f_bavail * (unsigned long long)vfs.f_frsize; m->used_bytes = m->total_bytes >= m->free_bytes ? m->total_bytes - m->free_bytes : 0; if (vfs.f_files > 0) { m->has_inodes = 1; m->inodes_total = (unsigned long long)vfs.f_files; m->inodes_used = m->inodes_total >= (unsigned long long)vfs.f_ffree ? m->inodes_total - (unsigned long long)vfs.f_ffree : 0; } } #endif /* !_WIN32 */ #if defined(__APPLE__) static int collect_mounts(sx_mount_t *out, int max) { struct statfs *mnt = NULL; int n, i, count = 0; n = getmntinfo(&mnt, MNT_NOWAIT); if (n <= 0) return 0; for (i = 0; i < n && count < max; i++) { sx_mount_t *m = &out[count]; unsigned long long bs = (unsigned long long)mnt[i].f_bsize; if (pseudo_fstype(mnt[i].f_fstypename)) continue; if (pseudo_mountpoint(mnt[i].f_mntonname)) continue; memset(m, 0, sizeof(*m)); copy_str(m->device, sizeof(m->device), mnt[i].f_mntfromname); copy_str(m->fstype, sizeof(m->fstype), mnt[i].f_fstypename); copy_str(m->mount, sizeof(m->mount), mnt[i].f_mntonname); m->total_bytes = (unsigned long long)mnt[i].f_blocks * bs; m->free_bytes = (unsigned long long)mnt[i].f_bfree * bs; m->avail_bytes = (unsigned long long)mnt[i].f_bavail * bs; m->used_bytes = m->total_bytes >= m->free_bytes ? m->total_bytes - m->free_bytes : 0; if (mnt[i].f_files > 0) { m->has_inodes = 1; m->inodes_total = (unsigned long long)mnt[i].f_files; m->inodes_used = m->inodes_total >= (unsigned long long)mnt[i].f_ffree ? m->inodes_total - (unsigned long long)mnt[i].f_ffree : 0; } count++; } return count; } #elif defined(_WIN32) static int collect_mounts(sx_mount_t *out, int max) { char drives[512]; DWORD n; int count = 0; const char *p; n = GetLogicalDriveStringsA(sizeof(drives) - 1, drives); if (n == 0) return 0; for (p = drives; *p && count < max; p += strlen(p) + 1) { ULARGE_INTEGER avail, total, totalfree; char fsname[64] = "unknown"; sx_mount_t *m; if (GetDriveTypeA(p) == DRIVE_NO_ROOT_DIR) continue; if (!GetDiskFreeSpaceExA(p, &avail, &total, &totalfree)) continue; m = &out[count]; memset(m, 0, sizeof(*m)); copy_str(m->device, sizeof(m->device), p); copy_str(m->mount, sizeof(m->mount), p); if (GetVolumeInformationA(p, NULL, 0, NULL, NULL, NULL, fsname, (DWORD)sizeof(fsname)) && fsname[0]) copy_str(m->fstype, sizeof(m->fstype), fsname); else copy_str(m->fstype, sizeof(m->fstype), "win32"); m->total_bytes = (unsigned long long)total.QuadPart; m->free_bytes = (unsigned long long)totalfree.QuadPart; m->avail_bytes = (unsigned long long)avail.QuadPart; m->used_bytes = m->total_bytes >= m->free_bytes ? m->total_bytes - m->free_bytes : 0; count++; } return count; } #else /* Linux and other POSIX */ static int collect_mounts(sx_mount_t *out, int max) { FILE *fp; char line[2048]; int count = 0; fp = fopen("/proc/mounts", "r"); if (!fp) fp = fopen("/etc/mtab", "r"); if (!fp) return 0; while (fgets(line, sizeof(line), fp) && count < max) { char dev[MAX_NAME], mp[MAX_PATH], fs[64]; int i = 0; char *tok[3]; char *p = line; /* split the first three whitespace-separated fields */ while (i < 3) { while (*p == ' ' || *p == '\t') p++; if (!*p || *p == '\n') break; tok[i++] = p; while (*p && *p != ' ' && *p != '\t' && *p != '\n') p++; if (*p) *p++ = '\0'; } if (i < 3) continue; copy_str(dev, sizeof(dev), tok[0]); copy_str(mp, sizeof(mp), tok[1]); copy_str(fs, sizeof(fs), tok[2]); unescape_mount_field(dev); unescape_mount_field(mp); if (pseudo_fstype(fs)) continue; if (pseudo_mountpoint(mp)) continue; /* /proc/mounts can list the same mountpoint more than once (a bind * mount, or a container overlay that repeats a host mount). Keep the * first entry per mountpoint so the dashboard does not double-count. */ { int dup = 0, k; for (k = 0; k < count; k++) { if (strcmp(out[k].mount, mp) == 0) { dup = 1; break; } } if (dup) continue; } memset(&out[count], 0, sizeof(out[count])); copy_str(out[count].device, sizeof(out[count].device), dev); copy_str(out[count].fstype, sizeof(out[count].fstype), fs); copy_str(out[count].mount, sizeof(out[count].mount), mp); fill_vfs(mp, &out[count]); count++; } fclose(fp); return count; } #endif /* Largest first. Used for display order. */ static int mount_cmp(const void *a, const void *b) { const sx_mount_t *x = (const sx_mount_t *)a; const sx_mount_t *y = (const sx_mount_t *)b; if (x->total_bytes < y->total_bytes) return 1; if (x->total_bytes > y->total_bytes) return -1; return strcmp(x->mount, y->mount); } static const char *platform_name(void) { #if defined(_WIN32) return "windows"; #elif defined(__APPLE__) return "macos"; #else return "linux"; #endif } /* ------------------------------------------------------------------ */ /* Bounded 1-level directory sizing */ /* ------------------------------------------------------------------ */ typedef struct { char name[MAX_NAME]; char path[MAX_PATH]; unsigned long long bytes; int is_dir; } dir_sz_t; typedef struct { unsigned long long visited; int truncated; } scan_state_t; static int skip_entry_name(const char *n) { if (!n || !*n) return 1; if (strcmp(n, ".") == 0 || strcmp(n, "..") == 0) return 1; return 0; } #if defined(_WIN32) static unsigned long long sum_dir_win(const char *dir, int depth, scan_state_t *st) { char pattern[MAX_PATH]; WIN32_FIND_DATAA fd; HANDLE h; unsigned long long total = 0; if (depth > MAX_DEPTH || st->truncated) return 0; sprintf(pattern, "%s\\*", dir); h = FindFirstFileA(pattern, &fd); if (h == INVALID_HANDLE_VALUE) return 0; do { char child[MAX_PATH]; if (skip_entry_name(fd.cFileName)) continue; if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue; if (++st->visited > MAX_SCAN_ENTRIES) { st->truncated = 1; break; } sprintf(child, "%s\\%s", dir, fd.cFileName); if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) total += sum_dir_win(child, depth + 1, st); else total += ((unsigned long long)fd.nFileSizeHigh << 32) | (unsigned long long)fd.nFileSizeLow; } while (FindNextFileA(h, &fd)); FindClose(h); return total; } #else static unsigned long long sum_dir_posix(const char *dir, int depth, scan_state_t *st) { DIR *d; struct dirent *e; unsigned long long total = 0; if (depth > MAX_DEPTH || st->truncated) return 0; d = opendir(dir); if (!d) return 0; while ((e = readdir(d)) != NULL) { char child[MAX_PATH]; struct stat sb; if (skip_entry_name(e->d_name)) continue; if (++st->visited > MAX_SCAN_ENTRIES) { st->truncated = 1; break; } if (snprintf(child, sizeof(child), "%s/%s", dir, e->d_name) >= (int)sizeof(child)) continue; /* lstat: symlinks are never followed, so a link back up the tree * cannot make the walk unbounded. */ if (lstat(child, &sb) != 0) continue; if (S_ISLNK(sb.st_mode)) continue; if (S_ISDIR(sb.st_mode)) total += sum_dir_posix(child, depth + 1, st); else if (S_ISREG(sb.st_mode)) total += (unsigned long long)sb.st_size; } closedir(d); return total; } #endif static int dir_cmp(const void *a, const void *b) { const dir_sz_t *x = (const dir_sz_t *)a; const dir_sz_t *y = (const dir_sz_t *)b; if (x->bytes < y->bytes) return 1; if (x->bytes > y->bytes) return -1; return strcmp(x->name, y->name); } /* Resolve an allowlisted root token to a real path. * Returns 1 resolved, * 0 token is not an allowlisted root, * -1 token is allowlisted but the root does not exist on this node. */ static int resolve_root(const char *kind, char *out, size_t outsz, const char **label) { if (!kind || !*kind) return 0; if (strcmp(kind, "workspace") == 0) { *label = "/workspace"; #if defined(_WIN32) return -1; /* no /workspace on a native Windows node */ #else if (access("/workspace", R_OK | X_OK) != 0) return -1; copy_str(out, outsz, "/workspace"); return 1; #endif } if (strcmp(kind, "home") == 0) { #if defined(_WIN32) const char *up = getenv("USERPROFILE"); if (!up || !*up) return -1; copy_str(out, outsz, up); #else const char *hp = getenv("HOME"); if (!hp || !*hp) return -1; copy_str(out, outsz, hp); #endif *label = "$HOME"; return 1; } return 0; } static int scan_root(const char *root, dir_sz_t *out, int max, int *count_out, int *truncated, int *root_kind) { scan_state_t st; int count = 0; st.visited = 0; st.truncated = 0; *truncated = 0; *root_kind = 0; (void)root_kind; #if defined(_WIN32) { char pattern[MAX_PATH]; WIN32_FIND_DATAA fd; HANDLE h; sprintf(pattern, "%s\\*", root); h = FindFirstFileA(pattern, &fd); if (h == INVALID_HANDLE_VALUE) { *count_out = 0; return 0; } do { dir_sz_t *e; if (skip_entry_name(fd.cFileName)) continue; if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue; if (count >= max) { st.truncated = 1; break; } if (++st.visited > MAX_SCAN_ENTRIES) { st.truncated = 1; break; } e = &out[count]; memset(e, 0, sizeof(*e)); copy_str(e->name, sizeof(e->name), fd.cFileName); sprintf(e->path, "%s\\%s", root, fd.cFileName); if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { e->is_dir = 1; e->bytes = sum_dir_win(e->path, 1, &st); } else { e->bytes = ((unsigned long long)fd.nFileSizeHigh << 32) | (unsigned long long)fd.nFileSizeLow; } count++; } while (FindNextFileA(h, &fd)); FindClose(h); } #else { DIR *d = opendir(root); struct dirent *de; if (!d) { *count_out = 0; return 0; } while ((de = readdir(d)) != NULL) { dir_sz_t *e; struct stat sb; char path[MAX_PATH]; if (skip_entry_name(de->d_name)) continue; if (count >= max) { st.truncated = 1; break; } if (++st.visited > MAX_SCAN_ENTRIES) { st.truncated = 1; break; } if (snprintf(path, sizeof(path), "%s/%s", root, de->d_name) >= (int)sizeof(path)) continue; if (lstat(path, &sb) != 0) continue; if (S_ISLNK(sb.st_mode)) continue; e = &out[count]; memset(e, 0, sizeof(*e)); copy_str(e->name, sizeof(e->name), de->d_name); copy_str(e->path, sizeof(e->path), path); if (S_ISDIR(sb.st_mode)) { e->is_dir = 1; e->bytes = sum_dir_posix(path, 1, &st); } else if (S_ISREG(sb.st_mode)) { e->bytes = (unsigned long long)sb.st_size; } count++; } closedir(d); } #endif if (count > 1) qsort(out, (size_t)count, sizeof(out[0]), dir_cmp); *truncated = st.truncated; *count_out = count; return 1; } /* ------------------------------------------------------------------ */ /* Modes */ /* ------------------------------------------------------------------ */ static int mode_meta(void) { puts("{\"name\":\"disks\",\"desc\":\"Local storage\"}"); return 0; } static void json_header(void) { /* Deliberately no Content-Length - the server owns framing. */ fputs("Content-Type: application/json; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n", stdout); } static void emit_mounts(void) { sx_mount_t mnt[MAX_MOUNTS]; int i, n; memset(mnt, 0, sizeof(mnt)); n = collect_mounts(mnt, MAX_MOUNTS); if (n > 1) qsort(mnt, (size_t)n, sizeof(mnt[0]), mount_cmp); fputs("[", stdout); for (i = 0; i < n; i++) { const sx_mount_t *m = &mnt[i]; double pct = 0.0, ipct = 0.0; if (m->used_bytes + m->avail_bytes > 0) pct = (double)m->used_bytes * 100.0 / (double)(m->used_bytes + m->avail_bytes); if (m->has_inodes && m->inodes_total > 0) ipct = (double)m->inodes_used * 100.0 / (double)m->inodes_total; if (i) fputs(",", stdout); fputs("{\"device\":", stdout); json_str(m->device); fputs(",\"fstype\":", stdout); json_str(m->fstype); fputs(",\"mount\":", stdout); json_str(m->mount); printf(",\"total_bytes\":%llu,\"used_bytes\":%llu,\"free_bytes\":%llu," "\"avail_bytes\":%llu,\"pct\":%.1f", m->total_bytes, m->used_bytes, m->free_bytes, m->avail_bytes, clamp_pct(pct)); printf(",\"inodes_total\":%llu,\"inodes_used\":%llu," "\"inodes_pct\":%.1f,\"has_inodes\":%s}", m->inodes_total, m->inodes_used, clamp_pct(ipct), m->has_inodes ? "true" : "false"); } fputs("]", stdout); } static int mode_json(const char *qs) { char action[64]; char rootkind[32]; char utc[40]; action[0] = rootkind[0] = '\0'; get_query_val(qs, "action", action, sizeof(action)); get_query_val(qs, "root", rootkind, sizeof(rootkind)); if (action[0] && strcmp(action, "mounts") != 0 && strcmp(action, "dirs") != 0 && strcmp(action, "info") != 0) { json_header(); printf("{\"ok\":false,\"error\":\"unknown_action\"," "\"supported\":[\"mounts\",\"dirs\",\"info\"],\"requested\":"); json_str(action); fputs("}\n", stdout); return 0; } if (!action[0]) copy_str(action, sizeof(action), "mounts"); utc_now(utc, sizeof(utc)); if (strcmp(action, "info") == 0) { json_header(); fputs("{\"ok\":true,\"action\":\"info\",\"plugin\":\"" PLUGIN_ID "\"," "\"version\":\"" PLUGIN_VER "\",\"platform\":", stdout); json_str(platform_name()); fputs(",\"pid\":", stdout); printf("%ld", node_pid()); fputs(",\"utc\":", stdout); json_str(utc); fputs(",\"node_auth\":", stdout); { const char *np = getenv("BOT_PASSWORD"); fputs((np && *np) ? "true" : "false", stdout); } fputs(",\"supported\":[\"mounts\",\"dirs\",\"info\"],\"zero_network\":true," "\"allowed_roots\":[\"workspace\",\"home\"]," "\"max_depth\":", stdout); printf("%d", MAX_DEPTH); fputs(",\"max_scan_entries\":", stdout); printf("%d", MAX_SCAN_ENTRIES); fputs("}\n", stdout); return 0; } if (strcmp(action, "dirs") == 0) { char root[MAX_PATH]; const char *label = ""; dir_sz_t *dirs; int count = 0, truncated = 0, kind = 0; long long t0, t1; unsigned long long total_bytes = 0, used_bytes = 0, free_bytes = 0; double root_pct = 0.0; int i; if (!rootkind[0]) { json_header(); fputs("{\"ok\":false,\"error\":\"missing_root\"," "\"allowed_roots\":[\"workspace\",\"home\"]}\n", stdout); return 0; } /* Strict allowlist: an arbitrary path can never be walked. */ { int rv = resolve_root(rootkind, root, sizeof(root), &label); if (rv == 0) { json_header(); fputs("{\"ok\":false,\"error\":\"root_not_allowed\"," "\"allowed_roots\":[\"workspace\",\"home\"],\"requested\":", stdout); json_str(rootkind); fputs("}\n", stdout); return 0; } if (rv < 0) { json_header(); fputs("{\"ok\":false,\"error\":\"root_unavailable\"," "\"allowed_roots\":[\"workspace\",\"home\"],\"requested\":", stdout); json_str(rootkind); fputs(",\"message\":\"this root does not exist on this node\"}\n", stdout); return 0; } } #if defined(_WIN32) { /* GetDiskFreeSpaceExA needs a drive root, not a deep path. */ char drive[8]; ULARGE_INTEGER avail, total, totalfree; if (root[0] && root[1] == ':') { drive[0] = root[0]; drive[1] = ':'; drive[2] = '\\'; drive[3] = '\0'; if (GetDiskFreeSpaceExA(drive, &avail, &total, &totalfree)) { total_bytes = (unsigned long long)total.QuadPart; free_bytes = (unsigned long long)totalfree.QuadPart; used_bytes = total_bytes >= free_bytes ? total_bytes - free_bytes : 0; if (used_bytes + (unsigned long long)avail.QuadPart > 0) root_pct = (double)used_bytes * 100.0 / (double)(used_bytes + (unsigned long long)avail.QuadPart); } } } #else { struct statvfs vfs; if (statvfs(root, &vfs) == 0) { total_bytes = (unsigned long long)vfs.f_blocks * (unsigned long long)vfs.f_frsize; free_bytes = (unsigned long long)vfs.f_bfree * (unsigned long long)vfs.f_frsize; used_bytes = total_bytes >= free_bytes ? total_bytes - free_bytes : 0; if (used_bytes + (unsigned long long)vfs.f_bavail * (unsigned long long)vfs.f_frsize > 0) root_pct = (double)used_bytes * 100.0 / (double)(used_bytes + (unsigned long long)vfs.f_bavail * (unsigned long long)vfs.f_frsize); } } #endif dirs = (dir_sz_t *)calloc((size_t)MAX_DIRS, sizeof(dir_sz_t)); if (!dirs) { json_header(); fputs("{\"ok\":false,\"error\":\"out_of_memory\"}\n", stdout); return 0; } t0 = now_ms(); scan_root(root, dirs, MAX_DIRS, &count, &truncated, &kind); t1 = now_ms(); json_header(); fputs("{\"ok\":true,\"action\":\"dirs\",\"plugin\":\"" PLUGIN_ID "\"," "\"platform\":", stdout); json_str(platform_name()); fputs(",\"root_kind\":", stdout); json_str(rootkind); fputs(",\"root\":", stdout); json_str(root); fputs(",\"root_label\":", stdout); json_str(label); printf(",\"root_total_bytes\":%llu,\"root_used_bytes\":%llu," "\"root_free_bytes\":%llu,\"root_pct\":%.1f", total_bytes, used_bytes, free_bytes, clamp_pct(root_pct)); printf(",\"count\":%d,\"truncated\":%s,\"max_depth\":%d," "\"max_scan_entries\":%d,\"scan_ms\":%lld,\"utc\":", count, truncated ? "true" : "false", MAX_DEPTH, MAX_SCAN_ENTRIES, t1 - t0); json_str(utc); fputs(",\"entries\":[", stdout); for (i = 0; i < count; i++) { double pct = total_bytes > 0 ? (double)dirs[i].bytes * 100.0 / (double)total_bytes : 0.0; if (i) fputs(",", stdout); fputs("{\"name\":", stdout); json_str(dirs[i].name); fputs(",\"path\":", stdout); json_str(dirs[i].path); printf(",\"bytes\":%llu,\"pct\":%.1f,\"is_dir\":%s}", dirs[i].bytes, clamp_pct(pct), dirs[i].is_dir ? "true" : "false"); } fputs("]}\n", stdout); free(dirs); return 0; } /* action == mounts */ { sx_mount_t probe[MAX_MOUNTS]; int n; memset(probe, 0, sizeof(probe)); n = collect_mounts(probe, MAX_MOUNTS); json_header(); fputs("{\"ok\":true,\"action\":\"mounts\",\"plugin\":\"" PLUGIN_ID "\"," "\"version\":\"" PLUGIN_VER "\",\"platform\":", stdout); json_str(platform_name()); printf(",\"pid\":%ld,\"count\":%d,\"utc\":", node_pid(), n); json_str(utc); fputs(",\"zero_network\":true,\"mounts\":", stdout); emit_mounts(); fputs("}\n", stdout); } return 0; } /* ------------------------------------------------------------------ */ /* HTML UI */ /* ------------------------------------------------------------------ */ static int mode_html(int embed) { fputs("Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-store\r\n" "X-Content-Type-Options: nosniff\r\n\r\n", stdout); fputs("\n" "" "disks", stdout); fputs(SX_BOT_THEME_PREPAINT "" SX_BOT_THEME_HANDOFF "
", stdout); fputs("
" "

disks

" "—" "loading" "" "
" "" "" "" "
" "" "
", stdout); /* summary */ fputs("

Storage summary

" "
" "
filesystems
—
" "
total capacity
—
" "
used
—
" "
free
—
" "
most full
—
" "
" "
", stdout); /* mounts */ fputs("

Mounted filesystems

" "
" "
No local filesystems reported.
", stdout); /* dirs */ fputs("

Folder breakdown

" "
" "
" "" "" "
" "
" "
" "" "" "
NameSizeShareType
" "
Nothing to show for this root.
", stdout); fputs("

About this plugin

" "

Local node only. Reads " "/proc/mounts and statvfs on Linux, " "getmntinfo on macOS and GetLogicalDriveStringsA " "on Windows. Makes zero outbound network requests.

" "

Folder sizes are a bounded walk of one " "of two allowlisted roots; symlinks are never followed, depth is capped and " "the walk stops after a fixed number of entries.

", stdout); if (!embed) { fputs("
" "Opened directly from the node API.
", stdout); } fputs("\n", stdout); return 0; } /* ------------------------------------------------------------------ */ /* main */ /* ------------------------------------------------------------------ */ int main(int argc, char **argv) { const char *qs; int embed; if (argc > 1) { if (strcmp(argv[1], "--meta") == 0) return mode_meta(); if (strcmp(argv[1], "--version") == 0) { puts(PLUGIN_VER); return 0; } if (strcmp(argv[1], "--help") == 0) { puts("usage: " PLUGIN_ID " [--meta|--version|--help]"); puts(""); puts("Runs as a CGI-style program under scorpiox-server."); puts(" --meta print discovery metadata JSON and exit"); puts(" --version print the plugin version and exit"); puts(""); puts("Query string actions:"); puts(" (none) or action=mounts mounted filesystems with usage"); puts(" action=dirs&root=workspace|home bounded 1-level folder sizes"); puts(" action=info plugin/platform capabilities"); puts(" format=json same as an action, JSON output"); puts(" (any other) HTML dashboard"); return 0; } } if (getenv("SX_PLUGIN_META")) return mode_meta(); require_node_auth(); qs = getenv("QUERY_STRING"); if (!qs) qs = ""; if (strstr(qs, "action=") || strstr(qs, "format=json")) return mode_json(qs); embed = (strstr(qs, "embed=1") != NULL || strstr(qs, "embed=true") != NULL); return mode_html(embed); }