Commit f4ff48f4

f4ff48f4c210Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Move the File metadata predicates to libspinel_rt.a

Following the sp_io.c handle-ops slimming, move the File metadata predicates out of sp_runtime.h into lib/sp_io.c (+ prototypes in sp_io.h): File.directory?/file?/exist?/delete and the Windows sp_file_attributes helper. They touch only libc / WinAPI (stat, GetFileAttributesW, fopen, remove), allocate no spinel strings, and reference no shared mutable state, so they move byte-for-byte and stay perf-neutral (none were inline-hot).

The string-returning / raising ops stay inline in sp_runtime.h: sp_file_read / write / mtime / binread allocate via the hot static sp_str_alloc (per-TU string heap) or build an sp_Time; they call the now-extern sp_file_directory, which resolves from the library copy. The compiler doesn't use any of these (it has its own file reader), so this is bootstrap-safe.

sp_io.c gains <sys/stat.h> (POSIX) / <windows.h> (Win) for the predicate bodies. Gate green: test 846, optcarrot checksum 59662, bench 57, clang build clean.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
f4ff48f4c210
Parent
80894e3a7b34

lib/sp_io.cmodifiedView file

@@ -14,10 +14,12 @@
1414 #include <stdlib.h>
1515 #include <string.h>
1616 #ifdef _WIN32
17-#include <io.h> /* _pipe */
18-#include <fcntl.h> /* _O_BINARY */
17+#include <io.h> /* _pipe */
18+#include <fcntl.h> /* _O_BINARY */
19+#include <windows.h> /* GetFileAttributesW for the File predicates */
1920 #else
20-#include <unistd.h> /* pipe */
21+#include <unistd.h> /* pipe */
22+#include <sys/stat.h> /* stat() for the File predicates */
2123 #endif
2224
2325 /* Provided by the generated TU / libspinel_rt.a. */
@@ -96,3 +98,46 @@
9698 ungetc(c, f->fp);
9799 return FALSE;
98100 }
101+
102+/* ---- File metadata predicates ----
103+ libc / WinAPI only, no spinel-string allocation and no shared mutable
104+ state, so they live here rather than inline in sp_runtime.h. */
105+#ifdef _WIN32
106+static DWORD sp_file_attributes(const char *path) {
107+ if (!path) return INVALID_FILE_ATTRIBUTES;
108+ int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);
109+ if (len <= 0) return INVALID_FILE_ATTRIBUTES;
110+ wchar_t *wpath = (wchar_t *)malloc(sizeof(wchar_t) * (size_t)len);
111+ if (!wpath) return INVALID_FILE_ATTRIBUTES;
112+ if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, len) <= 0) {
113+ free(wpath);
114+ return INVALID_FILE_ATTRIBUTES;
115+ }
116+ DWORD attrs = GetFileAttributesW(wpath);
117+ free(wpath);
118+ return attrs;
119+}
120+
121+mrb_bool sp_file_directory(const char *path) {
122+ DWORD attrs = sp_file_attributes(path);
123+ return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY);
124+}
125+
126+mrb_bool sp_file_file(const char *path) {
127+ DWORD attrs = sp_file_attributes(path);
128+ return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);
129+}
130+#else
131+mrb_bool sp_file_directory(const char *path) {
132+ struct stat st;
133+ return path && stat(path, &st) == 0 && S_ISDIR(st.st_mode);
134+}
135+
136+mrb_bool sp_file_file(const char *path) {
137+ struct stat st;
138+ return path && stat(path, &st) == 0 && S_ISREG(st.st_mode);
139+}
140+#endif
141+
142+mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }
143+void sp_file_delete(const char *path) { remove(path); }