Commit 5077f77f
5077f77f6f16Merged
vikks <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Implement File.mtime support
- Add File.mtime dispatch to analyzer and codegen
- Implement sp_file_mtime in runtime using stat()
- Add regression test in test/file_mtime.rb
Fixes #1032
Details
- Commit
- 5077f77f6f16
- Parent
- cdcb9d6d4c80
lib/sp_runtime.hmodifiedView file
@@ -3235,6 +3235,48 @@
32353235
}32363236
static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }32373237
static void sp_file_delete(const char *path) { remove(path); }3238+
static sp_Time sp_file_mtime(const char *path) {3239+
if (!path) {3240+
sp_raise_cls("TypeError", "no implicit conversion of nil into String");3241+
return (sp_Time){0, 0, 0};3242+
}3243+
#if defined(_WIN32)3244+
int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);3245+
if (len > 0) {3246+
wchar_t *wpath = (wchar_t *)malloc(sizeof(wchar_t) * (size_t)len);3247+
if (wpath) {3248+
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, len) > 0) {3249+
WIN32_FILE_ATTRIBUTE_DATA attr_data;3250+
if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &attr_data)) {3251+
free(wpath);3252+
ULARGE_INTEGER ull;3253+
ull.LowPart = attr_data.ftLastWriteTime.dwLowDateTime;3254+
ull.HighPart = attr_data.ftLastWriteTime.dwHighDateTime;3255+
int64_t total_ns = (ull.QuadPart - 116444736000000000ULL) * 100;3256+
return (sp_Time){total_ns / 1000000000LL, (int32_t)(total_ns % 1000000000LL), 0};3257+
}3258+
}3259+
free(wpath);3260+
}3261+
}3262+
sp_raise_cls("Errno::ENOENT", sp_sprintf("No such file or directory @ File.mtime - %s", path));3263+
return (sp_Time){0, 0, 0};3264+
#else3265+
struct stat st;3266+
if (stat(path, &st) == -1) {3267+
sp_raise_cls(errno == ENOENT ? "Errno::ENOENT" : "RuntimeError", sp_sprintf("%s @ File.mtime - %s", strerror(errno), path));3268+
return (sp_Time){0, 0, 0};3269+
}3270+
#if defined(__APPLE__)3271+
return (sp_Time){(int64_t)st.st_mtimespec.tv_sec, (int32_t)st.st_mtimespec.tv_nsec, 0};3272+
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)3273+
return (sp_Time){(int64_t)st.st_mtimespec.tv_sec, (int32_t)st.st_mtimespec.tv_nsec, 0};3274+
#else3275+
/* Linux / others with st_mtim */3276+
return (sp_Time){(int64_t)st.st_mtim.tv_sec, (int32_t)st.st_mtim.tv_nsec, 0};3277+
#endif3278+
#endif3279+
}32383280
static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }32393281
static const char *sp_file_basename(const char *path) {32403282
const char *s = strrchr(path, '/');spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/file_mtime.rbaddedView file
Loading diff…
test/file_mtime.rb.expectedaddedView file
Loading diff…