Skip to content

Commit

Permalink
Use File::NULL instead of hard coded null device names
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jul 10, 2023
1 parent 092c9b2 commit c8d0470
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 20 deletions.
6 changes: 1 addition & 5 deletions ext/extmk.rb
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions file.c
Expand Up @@ -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"
*
*/
Expand Down Expand Up @@ -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
*
*/

Expand Down
6 changes: 3 additions & 3 deletions io.c
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mkmf.rb
Expand Up @@ -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))
Expand Down
14 changes: 8 additions & 6 deletions process.c
Expand Up @@ -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.
Expand Down Expand Up @@ -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::*.
*/

Expand All @@ -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)
{
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_file_exhaustive.rb
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions tool/lib/vcs.rb
Expand Up @@ -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
Expand Down

0 comments on commit c8d0470

Please sign in to comment.