Commit d572a6d7
d572a6d7b1fcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #1485 from rubys/add-file-size
Add File.size class method
Details
- Commit
- d572a6d7b1fc
- Parent
- c9233382b135
- Merge parent
- 92a4e169e5de
lib/sp_runtime.hmodifiedView file
@@ -5180,6 +5180,28 @@
51805180
return (sp_Time){(int64_t)st.st_mtim.tv_sec, (int32_t)st.st_mtim.tv_nsec, 0};51815181
#endif51825182
}5183+
/* File.size(path) -> byte size. Raises Errno::ENOENT on a missing path,5184+
matching MRI (and sp_file_read / sp_file_mtime, which stat/open the5185+
same way). */5186+
static mrb_int sp_file_size(const char *path) {5187+
if (!path) {5188+
sp_raise_cls("TypeError", "no implicit conversion of nil into String");5189+
return 0;5190+
}5191+
struct stat st;5192+
if (stat(path, &st) == -1) {5193+
int err = errno; /* capture once: strerror() may clobber errno */5194+
sp_raise_cls(err == ENOENT ? "Errno::ENOENT" : "RuntimeError", sp_sprintf("%s @ File.size - %s", strerror(err), path));5195+
return 0;5196+
}5197+
/* off_t (typically 64-bit) into mrb_int (intptr_t -> 32-bit on a 32-bit5198+
build): guard the narrowing, as spinel does for int arithmetic. */5199+
if ((off_t)(mrb_int)st.st_size != st.st_size) {5200+
sp_raise_cls("RangeError", "file size out of range for Integer");5201+
return 0;5202+
}5203+
return (mrb_int)st.st_size;5204+
}51835205
static const char *sp_backtick(const char *cmd) {51845206
FILE *p = popen(cmd, "r");51855207
if (!p) { sp_last_status = -1; return sp_str_empty; }src/analyze_infer.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/file_size.rbaddedView file
Loading diff…
test/file_size.rb.expectedaddedView file
Loading diff…