Commit 690c5939

690c5939a642Merged

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

Description

Raise Errno::ENOENT on File.read/write/binread open failure

File.read / File.write / File.binread silently returned an empty string/array (or no-op'd the write) when fopen failed, instead of raising like CRuby. Raise Errno::ENOENT / Errno::EACCES / RuntimeError from the open-failure branch, matching the message shape sp_file_mtime already uses.

Refs #1391.

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

Details

Commit
690c5939a642
Parent
5f4b4e80e6fb

lib/sp_runtime.hmodifiedView file

@@ -4796,7 +4796,11 @@
47964796 sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path));
47974797 }
47984798 FILE *f = fopen(path, "r");
4799- if (!f) return &("\xff" "")[1];
4799+ if (!f) {
4800+ sp_raise_cls(errno == ENOENT ? "Errno::ENOENT" : errno == EACCES ? "Errno::EACCES" : "RuntimeError",
4801+ sp_sprintf("%s @ rb_sysopen - %s", strerror(errno), path));
4802+ return &("\xff" "")[1];
4803+ }
48004804 fseek(f, 0, SEEK_END);
48014805 long sz = ftell(f);
48024806 fseek(f, 0, SEEK_SET);
@@ -4815,10 +4819,13 @@
48154819 sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ rb_sysopen - %s", path));
48164820 }
48174821 FILE *f = fopen(path, "wb");
4818- if (f) {
4819- fwrite(data, 1, sp_str_byte_len(data), f);
4820- fclose(f);
4822+ if (!f) {
4823+ sp_raise_cls(errno == ENOENT ? "Errno::ENOENT" : errno == EACCES ? "Errno::EACCES" : "RuntimeError",
4824+ sp_sprintf("%s @ rb_sysopen - %s", strerror(errno), path));
4825+ return;
48214826 }
4827+ fwrite(data, 1, sp_str_byte_len(data), f);
4828+ fclose(f);
48224829 }
48234830 static sp_Time sp_file_mtime(const char *path) {
48244831 if (!path) {
@@ -5110,7 +5117,11 @@
51105117 }
51115118 FILE *f = fopen(path, "rb");
51125119 sp_IntArray *a = sp_IntArray_new();
5113- if (!f) return a;
5120+ if (!f) {
5121+ sp_raise_cls(errno == ENOENT ? "Errno::ENOENT" : errno == EACCES ? "Errno::EACCES" : "RuntimeError",
5122+ sp_sprintf("%s @ rb_sysopen - %s", strerror(errno), path));
5123+ return a;
5124+ }
51145125 fseek(f, 0, SEEK_END);
51155126 long sz = ftell(f);
51165127 fseek(f, 0, SEEK_SET);