From f5aae8eb48ccd0a124acbaa4999d3f76bd274567 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Sat, 13 Jun 2015 11:46:45 +0200 Subject: [PATCH] file.c : `load` now supports reading from a FIFO file * file.c (rb_file_load_ok): `load` can now load not only regular files but also FIFO files. --- ChangeLog | 7 +++++++ NEWS | 4 ++++ file.c | 24 ++++++++++++++++-------- test/ruby/test_require.rb | 13 +++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index f33500b..9c88b6a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed Jun 17 16:00:00 2015 Franck Verrot + + file.c : `load` now supports reading from a FIFO file + + * file.c (rb_file_load_ok): `load` can now load not only regular files + but also FIFOS. + Wed Jun 17 15:15:53 2015 NAKAMURA Usaku * safe.c (safe_setter): of course, don't have to warn the limitation of diff --git a/NEWS b/NEWS index 40f58fb..52371b9 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,10 @@ with all sufficient information, see the ChangeLog file. === Core classes updates (outstanding ones only) +* Kernel + + * Kernel.load now supports reading from a FIFO file. + * Enumerable * Enumerable#grep_v is added as inverse version of Enumerable#grep. diff --git a/file.c b/file.c index 026ed03..c407332 100644 --- a/file.c +++ b/file.c @@ -115,6 +115,12 @@ int flock(int, int); #define STAT(p, s) stat((p), (s)) #endif +#ifdef S_IFIFO +# ifndef S_ISFIFO +# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +# endif +#endif + #if defined(__BEOS__) || defined(__HAIKU__) /* should not change ID if -1 */ static int be_chown(const char *path, uid_t owner, gid_t group) @@ -1367,10 +1373,6 @@ static VALUE rb_file_pipe_p(VALUE obj, VALUE fname) { #ifdef S_IFIFO -# ifndef S_ISFIFO -# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) -# endif - struct stat st; if (rb_stat(fname, &st) < 0) return Qfalse; @@ -5639,10 +5641,16 @@ rb_file_load_ok(const char *path) rb_update_max_fd(fd); #if !defined DOSISH { - struct stat st; - if (fstat(fd, &st) || !S_ISREG(st.st_mode)) { - ret = 0; - } + struct stat st; + fstat(fd, &st); + + #ifdef S_IFIFO + if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) { + #else + if (!S_ISREG(st.st_mode)) { + #endif + ret = 0; + } } #endif (void)close(fd); diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index e7a903d..17e489d 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -706,4 +706,17 @@ class TestRequire < Test::Unit::TestCase END } end unless /mswin|mingw/ =~ RUBY_PLATFORM + + def test_loading_from_fifo + Dir.mktmpdir {|tmp| + file = File.join(tmp,'fifo.rb') + File.mkfifo(file) + thread = Thread.new { open(file,'w') { |f| f.puts "class Foo; end" } } + assert_separately([], <<-INPUT) + assert_nothing_raised(LoadError) { load("#{ file }") } + assert_nothing_raised(NameError) { Foo } + INPUT + } + end unless /mswin|mingw/ =~ RUBY_PLATFORM + end -- 2.3.2 (Apple Git-55)