From c3b061a6d710cb08e1b54b561a98dd2dfe83492d 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. [ruby-dev:48924] --- ChangeLog | 7 +++++++ NEWS | 4 ++++ file.c | 24 ++++++++++++++++-------- test/ruby/test_require.rb | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index dc725da..05d7589 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Jun 13 11:46:45 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. [ruby-dev:48924] + Sat Jun 13 20:28:14 2015 NARUSE, Yui * file.c (rb_stat_ino): get inode from the interval of struct st. diff --git a/NEWS b/NEWS index fc074d0..33d6647 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 a944402..8b6f3ad 100644 --- a/file.c +++ b/file.c @@ -114,6 +114,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) @@ -1366,10 +1372,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; @@ -5630,10 +5632,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 b7797f0..9e03262 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -329,6 +329,20 @@ class TestRequire < Test::Unit::TestCase end end + def test_load_from_fifo + bug = '[ruby-dev:48924] #load from a FIFO' + thread = nil + Dir.mktmpdir {|tmp| + file = File.join(tmp,'fifo.rb') + File.mkfifo(file) + thread = Thread.new { open(file,'w') { f.puts "puts 1" } } + assert_separately([], <<-INPUT) + assert_nothing_raised(LoadError) { load("#{ file }") } + INPUT + } + #thread.join + end + def test_tainted_loadpath Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| abs_dir, file = File.split(t.path) -- 2.3.2 (Apple Git-55)