Commit 38eba378

38eba378250bMerged

ydah <[email protected]> · 6 months ago · 4 files · ~4 modified

Description

fix(runtime): use Windows attributes for file predicates

Details

Commit
38eba378250b
Parent
6e823f8811fb

lib/sp_runtime.hmodifiedView file

@@ -1699,8 +1699,42 @@
16991699 static volatile int sp_catch_top = 0;
17001700 static void sp_throw(const char *tag, mrb_int val) { int i = sp_catch_top - 1; while (i >= 0) { if (strcmp(sp_catch_tag[i], tag) == 0) { sp_catch_val[i] = val; sp_catch_top = i + 1; longjmp(sp_catch_stack[i], 1); } i--; } fprintf(stderr, "uncaught throw: %s\n", tag); exit(1); }
17011701
1702-static mrb_bool sp_file_directory(const char *path) { struct stat st; return path && stat(path, &st) == 0 && S_ISDIR(st.st_mode); }
1703-static mrb_bool sp_file_regular(const char *path) { struct stat st; return path && stat(path, &st) == 0 && S_ISREG(st.st_mode); }
1702+#ifdef _WIN32
1703+static DWORD sp_file_attributes(const char *path) {
1704+ if (!path) return INVALID_FILE_ATTRIBUTES;
1705+ int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);
1706+ if (len <= 0) return INVALID_FILE_ATTRIBUTES;
1707+ wchar_t *wpath = (wchar_t *)malloc(sizeof(wchar_t) * (size_t)len);
1708+ if (!wpath) return INVALID_FILE_ATTRIBUTES;
1709+ if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, len) <= 0) {
1710+ free(wpath);
1711+ return INVALID_FILE_ATTRIBUTES;
1712+ }
1713+ DWORD attrs = GetFileAttributesW(wpath);
1714+ free(wpath);
1715+ return attrs;
1716+}
1717+
1718+static mrb_bool sp_file_directory(const char *path) {
1719+ DWORD attrs = sp_file_attributes(path);
1720+ return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY);
1721+}
1722+
1723+static mrb_bool sp_file_file(const char *path) {
1724+ DWORD attrs = sp_file_attributes(path);
1725+ return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);
1726+}
1727+#else
1728+static mrb_bool sp_file_directory(const char *path) {
1729+ struct stat st;
1730+ return path && stat(path, &st) == 0 && S_ISDIR(st.st_mode);
1731+}
1732+
1733+static mrb_bool sp_file_file(const char *path) {
1734+ struct stat st;
1735+ return path && stat(path, &st) == 0 && S_ISREG(st.st_mode);
1736+}
1737+#endif
17041738
17051739 /* Text mode ("r") matches CRuby's File.read: on Windows, CRLF is
17061740 normalized to LF on read, which cancels out fopen("w")'s