Use File::NULL instead of hard coded null device names

This commit is contained in:
Nobuyoshi Nakada 2023-07-10 19:18:48 +09:00
parent 092c9b266a
commit c8d0470bb0
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2023-07-10 11:13:51 +00:00
7 changed files with 17 additions and 20 deletions

View File

@ -43,11 +43,7 @@ $" << "mkmf.rb"
load File.expand_path("lib/mkmf.rb", srcdir) load File.expand_path("lib/mkmf.rb", srcdir)
require 'optparse/shellwords' require 'optparse/shellwords'
if defined?(File::NULL) @null = File::NULL
@null = File::NULL
elsif !File.chardev?(@null = "/dev/null")
@null = "nul"
end
def verbose? def verbose?
$mflags.defined?("V") == "1" $mflags.defined?("V") == "1"

4
file.c
View File

@ -4971,7 +4971,7 @@ rb_file_s_extname(VALUE klass, VALUE fname)
* *
* Returns the string representation of the path * 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" * File.path(Pathname.new("/tmp")) #=> "/tmp"
* *
*/ */
@ -6086,7 +6086,7 @@ rb_stat_z(VALUE obj)
* the file otherwise. * the file otherwise.
* *
* File.stat("testfile").size? #=> 66 * File.stat("testfile").size? #=> 66
* File.stat("/dev/null").size? #=> nil * File.stat(File::NULL).size? #=> nil
* *
*/ */

6
io.c
View File

@ -5256,7 +5256,7 @@ rb_io_close_on_exec_p(VALUE io)
* *
* Sets a close-on-exec flag. * Sets a close-on-exec flag.
* *
* f = open("/dev/null") * f = File.open(File::NULL)
* f.close_on_exec = true * f.close_on_exec = true
* system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory * system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
* f.closed? #=> false * f.closed? #=> false
@ -9634,11 +9634,11 @@ rb_io_autoclose_p(VALUE io)
* *
* Sets auto-close flag. * Sets auto-close flag.
* *
* f = open("/dev/null") * f = File.open(File::NULL)
* IO.for_fd(f.fileno).close * IO.for_fd(f.fileno).close
* f.gets # raises Errno::EBADF * f.gets # raises Errno::EBADF
* *
* f = open("/dev/null") * f = File.open(File::NULL)
* g = IO.for_fd(f.fileno) * g = IO.for_fd(f.fileno)
* g.autoclose = false * g.autoclose = false
* g.close * g.close

View File

@ -2699,7 +2699,7 @@ MESSAGE
when $mswin when $mswin
$nmake = ?m if /nmake/i =~ make $nmake = ?m if /nmake/i =~ make
end end
$ignore_error = $nmake ? '' : ' 2> /dev/null || true' $ignore_error = $nmake ? '' : " 2> #{File::NULL} || true"
RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] = RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] =
$srcdir = arg_config("--srcdir", File.dirname($0)) $srcdir = arg_config("--srcdir", File.dirname($0))

View File

@ -4804,11 +4804,11 @@ rb_f_system(int argc, VALUE *argv, VALUE _)
* *
* A filename can be specified as a hash value. * A filename can be specified as a hash value.
* *
* pid = spawn(command, :in=>"/dev/null") # read mode * pid = spawn(command, :in=>File::NULL) # read mode
* pid = spawn(command, :out=>"/dev/null") # write mode * pid = spawn(command, :out=>File::NULL) # write mode
* pid = spawn(command, :err=>"log") # write mode * pid = spawn(command, :err=>"log") # write mode
* pid = spawn(command, [:out, :err]=>"/dev/null") # write mode * pid = spawn(command, [:out, :err]=>File::NULL) # write mode
* pid = spawn(command, 3=>"/dev/null") # read mode * pid = spawn(command, 3=>File::NULL) # read mode
* *
* For stdout and stderr (and combination of them), * For stdout and stderr (and combination of them),
* it is opened in write mode. * 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 * nochdir is true (i.e. non false), it changes the current
* working directory to the root ("/"). Unless the argument * working directory to the root ("/"). Unless the argument
* noclose is true, daemon() will redirect standard input, * 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::*. * Return zero on success, or raise one of Errno::*.
*/ */
@ -6854,6 +6854,8 @@ proc_daemon(int argc, VALUE *argv, VALUE _)
return INT2FIX(n); return INT2FIX(n);
} }
extern const char ruby_null_device[];
static int static int
rb_daemon(int nochdir, int noclose) rb_daemon(int nochdir, int noclose)
{ {
@ -6877,7 +6879,7 @@ rb_daemon(int nochdir, int noclose)
if (!nochdir) if (!nochdir)
err = chdir("/"); 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); rb_update_max_fd(n);
(void)dup2(n, 0); (void)dup2(n, 0);
(void)dup2(n, 1); (void)dup2(n, 1);

View File

@ -1518,7 +1518,7 @@ class TestFileExhaustive < Test::Unit::TestCase
stat = File.stat(f) stat = File.stat(f)
unless stat.chardev? 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.atime, File.atime(f), f)
assert_equal(stat.ctime, File.ctime(f), f) assert_equal(stat.ctime, File.ctime(f), f)
assert_equal(stat.mtime, File.mtime(f), f) assert_equal(stat.mtime, File.mtime(f), f)

View File

@ -154,8 +154,7 @@ class VCS
alias dryrun? dryrun alias dryrun? dryrun
alias debug? debug alias debug? debug
NullDevice = defined?(IO::NULL) ? IO::NULL : NullDevice = IO::NULL
%w[/dev/null NUL NIL: NL:].find {|dev| File.exist?(dev)}
# returns # returns
# * the last revision of the current branch # * the last revision of the current branch