From c8d0470bb0888bcb6719ba536e5f3f6a8b6551bb Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 10 Jul 2023 19:18:48 +0900 Subject: [PATCH] Use `File::NULL` instead of hard coded null device names --- ext/extmk.rb | 6 +----- file.c | 4 ++-- io.c | 6 +++--- lib/mkmf.rb | 2 +- process.c | 14 ++++++++------ test/ruby/test_file_exhaustive.rb | 2 +- tool/lib/vcs.rb | 3 +-- 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/ext/extmk.rb b/ext/extmk.rb index d6a4b80bf7a853..428ffc91a639ad 100755 --- a/ext/extmk.rb +++ b/ext/extmk.rb @@ -43,11 +43,7 @@ module Gem; end # only needs Gem::Platform load File.expand_path("lib/mkmf.rb", srcdir) require 'optparse/shellwords' -if defined?(File::NULL) - @null = File::NULL -elsif !File.chardev?(@null = "/dev/null") - @null = "nul" -end +@null = File::NULL def verbose? $mflags.defined?("V") == "1" diff --git a/file.c b/file.c index d9446e6dcfd360..0966d5bb23561c 100644 --- a/file.c +++ b/file.c @@ -4971,7 +4971,7 @@ rb_file_s_extname(VALUE klass, VALUE fname) * * Returns the string representation of the path * - * File.path("/dev/null") #=> "/dev/null" + * File.path(File::NULL) #=> "/dev/null" * File.path(Pathname.new("/tmp")) #=> "/tmp" * */ @@ -6086,7 +6086,7 @@ rb_stat_z(VALUE obj) * the file otherwise. * * File.stat("testfile").size? #=> 66 - * File.stat("/dev/null").size? #=> nil + * File.stat(File::NULL).size? #=> nil * */ diff --git a/io.c b/io.c index c3661a90a795ee..0eb8da3f4fa107 100644 --- a/io.c +++ b/io.c @@ -5256,7 +5256,7 @@ rb_io_close_on_exec_p(VALUE io) * * Sets a close-on-exec flag. * - * f = open("/dev/null") + * f = File.open(File::NULL) * f.close_on_exec = true * system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory * f.closed? #=> false @@ -9634,11 +9634,11 @@ rb_io_autoclose_p(VALUE io) * * Sets auto-close flag. * - * f = open("/dev/null") + * f = File.open(File::NULL) * IO.for_fd(f.fileno).close * f.gets # raises Errno::EBADF * - * f = open("/dev/null") + * f = File.open(File::NULL) * g = IO.for_fd(f.fileno) * g.autoclose = false * g.close diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 20389ed7056470..cd67645a5d87d1 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -2699,7 +2699,7 @@ def MAIN_DOES_NOTHING(*refs) when $mswin $nmake = ?m if /nmake/i =~ make end - $ignore_error = $nmake ? '' : ' 2> /dev/null || true' + $ignore_error = $nmake ? '' : " 2> #{File::NULL} || true" RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] = $srcdir = arg_config("--srcdir", File.dirname($0)) diff --git a/process.c b/process.c index b4435446f0c4b7..fe9c0a31d7b708 100644 --- a/process.c +++ b/process.c @@ -4804,11 +4804,11 @@ rb_f_system(int argc, VALUE *argv, VALUE _) * * A filename can be specified as a hash value. * - * pid = spawn(command, :in=>"/dev/null") # read mode - * pid = spawn(command, :out=>"/dev/null") # write mode + * pid = spawn(command, :in=>File::NULL) # read mode + * pid = spawn(command, :out=>File::NULL) # write mode * pid = spawn(command, :err=>"log") # write mode - * pid = spawn(command, [:out, :err]=>"/dev/null") # write mode - * pid = spawn(command, 3=>"/dev/null") # read mode + * pid = spawn(command, [:out, :err]=>File::NULL) # write mode + * pid = spawn(command, 3=>File::NULL) # read mode * * For stdout and stderr (and combination of them), * it is opened in write mode. @@ -6834,7 +6834,7 @@ static int rb_daemon(int nochdir, int noclose); * nochdir is true (i.e. non false), it changes the current * working directory to the root ("/"). Unless the argument * noclose is true, daemon() will redirect standard input, - * standard output and standard error to /dev/null. + * standard output and standard error to null device. * Return zero on success, or raise one of Errno::*. */ @@ -6854,6 +6854,8 @@ proc_daemon(int argc, VALUE *argv, VALUE _) return INT2FIX(n); } +extern const char ruby_null_device[]; + static int rb_daemon(int nochdir, int noclose) { @@ -6877,7 +6879,7 @@ rb_daemon(int nochdir, int noclose) if (!nochdir) err = chdir("/"); - if (!noclose && (n = rb_cloexec_open("/dev/null", O_RDWR, 0)) != -1) { + if (!noclose && (n = rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) { rb_update_max_fd(n); (void)dup2(n, 0); (void)dup2(n, 1); diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb index be6e1f232643aa..fbb18f07f9ab5f 100644 --- a/test/ruby/test_file_exhaustive.rb +++ b/test/ruby/test_file_exhaustive.rb @@ -1518,7 +1518,7 @@ def test_test stat = File.stat(f) unless stat.chardev? - # /dev/null may be accessed by other processes + # null device may be accessed by other processes assert_equal(stat.atime, File.atime(f), f) assert_equal(stat.ctime, File.ctime(f), f) assert_equal(stat.mtime, File.mtime(f), f) diff --git a/tool/lib/vcs.rb b/tool/lib/vcs.rb index 650f2752507d01..8566d723497554 100644 --- a/tool/lib/vcs.rb +++ b/tool/lib/vcs.rb @@ -154,8 +154,7 @@ def set_options(opts) alias dryrun? dryrun alias debug? debug - NullDevice = defined?(IO::NULL) ? IO::NULL : - %w[/dev/null NUL NIL: NL:].find {|dev| File.exist?(dev)} + NullDevice = IO::NULL # returns # * the last revision of the current branch