From 58daab21cd043e1dc37024a7f99b396788372918 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 9 Mar 2024 14:19:48 +0100 Subject: Merging upstream version 1.44.3. Signed-off-by: Daniel Baumann --- web/server/h2o/libh2o/deps/mruby-io/.gitignore | 1 + web/server/h2o/libh2o/deps/mruby-io/.travis.yml | 2 + web/server/h2o/libh2o/deps/mruby-io/README.md | 185 ++++ .../libh2o/deps/mruby-io/include/mruby/ext/io.h | 36 + web/server/h2o/libh2o/deps/mruby-io/mrbgem.rake | 16 + web/server/h2o/libh2o/deps/mruby-io/mrblib/file.rb | 173 ++++ .../libh2o/deps/mruby-io/mrblib/file_constants.rb | 29 + web/server/h2o/libh2o/deps/mruby-io/mrblib/io.rb | 372 ++++++++ .../h2o/libh2o/deps/mruby-io/mrblib/kernel.rb | 15 + web/server/h2o/libh2o/deps/mruby-io/run_test.rb | 25 + web/server/h2o/libh2o/deps/mruby-io/src/file.c | 325 +++++++ .../h2o/libh2o/deps/mruby-io/src/file_test.c | 365 ++++++++ web/server/h2o/libh2o/deps/mruby-io/src/io.c | 941 +++++++++++++++++++++ .../h2o/libh2o/deps/mruby-io/src/mruby_io_gem.c | 20 + web/server/h2o/libh2o/deps/mruby-io/test/file.rb | 108 +++ .../h2o/libh2o/deps/mruby-io/test/file_test.rb | 95 +++ .../h2o/libh2o/deps/mruby-io/test/gc_filedes.sh | 4 + web/server/h2o/libh2o/deps/mruby-io/test/io.rb | 416 +++++++++ .../h2o/libh2o/deps/mruby-io/test/mruby_io_test.c | 141 +++ 19 files changed, 3269 insertions(+) create mode 100644 web/server/h2o/libh2o/deps/mruby-io/.gitignore create mode 100644 web/server/h2o/libh2o/deps/mruby-io/.travis.yml create mode 100644 web/server/h2o/libh2o/deps/mruby-io/README.md create mode 100644 web/server/h2o/libh2o/deps/mruby-io/include/mruby/ext/io.h create mode 100644 web/server/h2o/libh2o/deps/mruby-io/mrbgem.rake create mode 100644 web/server/h2o/libh2o/deps/mruby-io/mrblib/file.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/mrblib/file_constants.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/mrblib/io.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/mrblib/kernel.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/run_test.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/src/file.c create mode 100644 web/server/h2o/libh2o/deps/mruby-io/src/file_test.c create mode 100644 web/server/h2o/libh2o/deps/mruby-io/src/io.c create mode 100644 web/server/h2o/libh2o/deps/mruby-io/src/mruby_io_gem.c create mode 100644 web/server/h2o/libh2o/deps/mruby-io/test/file.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/test/file_test.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/test/gc_filedes.sh create mode 100644 web/server/h2o/libh2o/deps/mruby-io/test/io.rb create mode 100644 web/server/h2o/libh2o/deps/mruby-io/test/mruby_io_test.c (limited to 'web/server/h2o/libh2o/deps/mruby-io') diff --git a/web/server/h2o/libh2o/deps/mruby-io/.gitignore b/web/server/h2o/libh2o/deps/mruby-io/.gitignore new file mode 100644 index 000000000..ceeb05b41 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/.gitignore @@ -0,0 +1 @@ +/tmp diff --git a/web/server/h2o/libh2o/deps/mruby-io/.travis.yml b/web/server/h2o/libh2o/deps/mruby-io/.travis.yml new file mode 100644 index 000000000..ffe227284 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/.travis.yml @@ -0,0 +1,2 @@ +script: + - "ruby run_test.rb all test" diff --git a/web/server/h2o/libh2o/deps/mruby-io/README.md b/web/server/h2o/libh2o/deps/mruby-io/README.md new file mode 100644 index 000000000..d7fa6945b --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/README.md @@ -0,0 +1,185 @@ +mruby-io +======== + +IO, File module for mruby + + +## Implemented methods + +### IO + - http://doc.ruby-lang.org/ja/1.9.3/class/IO.html + +| method | mruby-io | memo | +| ------------------------- | -------- | ---- | +| IO.binread | | | +| IO.binwrite | | | +| IO.copy_stream | | | +| IO.new, IO.for_fd, IO.open | o | | +| IO.foreach | | | +| IO.pipe | | | +| IO.popen | o | | +| IO.read | o | | +| IO.readlines | | | +| IO.select | o | | +| IO.sysopen | o | | +| IO.try_convert | | | +| IO.write | | | +| IO#<< | | | +| IO#advise | | | +| IO#autoclose= | | | +| IO#autoclose? | | | +| IO#binmode | | | +| IO#binmode? | | | +| IO#bytes | | obsolete | +| IO#chars | | obsolete | +| IO#clone, IO#dup | | | +| IO#close | o | | +| IO#close_on_exec= | o | | +| IO#close_on_exec? | o | | +| IO#close_read | | | +| IO#close_write | | | +| IO#closed? | o | | +| IO#codepoints | | obsolete | +| IO#each_byte | o | | +| IO#each_char | o | | +| IO#each_codepoint | | | +| IO#each_line | o | | +| IO#eof, IO#eof? | o | | +| IO#external_encoding | | | +| IO#fcntl | | | +| IO#fdatasync | | | +| IO#fileno, IO#to_i | o | | +| IO#flush | o | | +| IO#fsync | | | +| IO#getbyte | | | +| IO#getc | o | | +| IO#gets | o | | +| IO#internal_encoding | | | +| IO#ioctl | | | +| IO#isatty, IO#tty? | | | +| IO#lineno | | | +| IO#lineno= | | | +| IO#lines | | obsolete | +| IO#pid | o | | +| IO#pos, IO#tell | o | | +| IO#pos= | o | | +| IO#print | o | | +| IO#printf | o | | +| IO#putc | | | +| IO#puts | o | | +| IO#read | o | | +| IO#read_nonblock | | | +| IO#readbyte | | | +| IO#readchar | o | | +| IO#readline | o | | +| IO#readlines | o | | +| IO#readpartial | | | +| IO#reopen | | | +| IO#rewind | | | +| IO#seek | o | | +| IO#set_encoding | | | +| IO#stat | | | +| IO#sync | o | | +| IO#sync= | o | | +| IO#sysread | o | | +| IO#sysseek | o | | +| IO#syswrite | o | | +| IO#to_io | | | +| IO#ungetbyte | | | +| IO#ungetc | o | | +| IO#write | o | | +| IO#write_nonblock | | | + +### File + - http://doc.ruby-lang.org/ja/1.9.3/class/File.html + +| method | mruby-io | memo | +| --------------------------- | -------- | ---- | +| File.absolute_path | | | +| File.atime | | | +| File.basename | o | | +| File.blockdev? | | FileTest | +| File.chardev? | | FileTest | +| File.chmod | | | +| File.chown | | | +| File.ctime | | | +| File.delete, File.unlink | o | | +| File.directory? | o | FileTest | +| File.dirname | o | | +| File.executable? | | FileTest | +| File.executable_real? | | FileTest | +| File.exist?, exists? | o | FileTest | +| File.expand_path | o | | +| File.extname | o | | +| File.file? | o | FileTest | +| File.fnmatch, File.fnmatch? | | | +| File.ftype | | | +| File.grpowned? | | FileTest | +| File.identical? | | FileTest | +| File.join | o | | +| File.lchmod | | | +| File.lchown | | | +| File.link | | | +| File.lstat | | | +| File.mtime | | | +| File.new, File.open | o | | +| File.owned? | | FileTest | +| File.path | | | +| File.pipe? | o | FileTest | +| File.readable? | | FileTest | +| File.readable_real? | | FileTest | +| File.readlink | | | +| File.realdirpath | | | +| File.realpath | o | | +| File.rename | o | | +| File.setgid? | | FileTest | +| File.setuid? | | FileTest | +| File.size | o | | +| File.size? | o | FileTest | +| File.socket? | o | FileTest | +| File.split | | | +| File.stat | | | +| File.sticky? | | FileTest | +| File.symlink | | | +| File.symlink? | o | FileTest | +| File.truncate | | | +| File.umask | o | | +| File.utime | | | +| File.world_readable? | | | +| File.world_writable? | | | +| File.writable? | | FileTest | +| File.writable_real? | | FileTest | +| File.zero? | o | FileTest | +| File#atime | | | +| File#chmod | | | +| File#chown | | | +| File#ctime | | | +| File#flock | o | | +| File#lstat | | | +| File#mtime | | | +| File#path, File#to_path | o | | +| File#size | | | +| File#truncate | | | + + +## License + +Copyright (c) 2013 Internet Initiative Japan Inc. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/web/server/h2o/libh2o/deps/mruby-io/include/mruby/ext/io.h b/web/server/h2o/libh2o/deps/mruby-io/include/mruby/ext/io.h new file mode 100644 index 000000000..3107f1053 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/include/mruby/ext/io.h @@ -0,0 +1,36 @@ +/* +** io.h - IO class +*/ + +#ifndef MRUBY_IO_H +#define MRUBY_IO_H + +#if defined(__cplusplus) +extern "C" { +#endif + +struct mrb_io { + int fd; /* file descriptor, or -1 */ + int fd2; /* file descriptor to write if it's different from fd, or -1 */ + int pid; /* child's pid (for pipes) */ + unsigned int writable:1, + sync:1; +}; + +#define FMODE_READABLE 0x00000001 +#define FMODE_WRITABLE 0x00000002 +#define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE) +#define FMODE_BINMODE 0x00000004 +#define FMODE_APPEND 0x00000040 +#define FMODE_CREATE 0x00000080 +#define FMODE_TRUNC 0x00000800 + +#define E_IO_ERROR (mrb_class_get(mrb, "IOError")) +#define E_EOF_ERROR (mrb_class_get(mrb, "EOFError")) + +mrb_value mrb_io_fileno(mrb_state *mrb, mrb_value io); + +#if defined(__cplusplus) +} /* extern "C" { */ +#endif +#endif /* MRUBY_IO_H */ diff --git a/web/server/h2o/libh2o/deps/mruby-io/mrbgem.rake b/web/server/h2o/libh2o/deps/mruby-io/mrbgem.rake new file mode 100644 index 000000000..8120f7832 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/mrbgem.rake @@ -0,0 +1,16 @@ +MRuby::Gem::Specification.new('mruby-io') do |spec| + spec.license = 'MIT' + spec.authors = 'Internet Initiative Japan Inc.' + + spec.cc.include_paths << "#{build.root}/src" + + case RUBY_PLATFORM + when /mingw|mswin/ + spec.linker.libraries += ['Ws2_32'] + #spec.cc.include_paths += ["C:/Windows/system/include"] + spec.linker.library_paths += ["C:/Windows/system"] + end + if build.kind_of?(MRuby::CrossBuild) && %w(x86_64-w64-mingw32 i686-w64-mingw32).include?(build.host_target) + spec.linker.libraries += ['ws2_32'] + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/mrblib/file.rb b/web/server/h2o/libh2o/deps/mruby-io/mrblib/file.rb new file mode 100644 index 000000000..aa0981930 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/mrblib/file.rb @@ -0,0 +1,173 @@ +class File < IO + include Enumerable + + class FileError < Exception; end + class NoFileError < FileError; end + class UnableToStat < FileError; end + class PermissionError < FileError; end + + attr_accessor :path + + def initialize(fd_or_path, mode = "r", perm = 0666) + if fd_or_path.kind_of? Fixnum + super(fd_or_path, mode) + else + @path = fd_or_path + fd = IO.sysopen(@path, mode, perm) + super(fd, mode) + end + end + + def self.join(*names) + if names.size == 0 + "" + elsif names.size == 1 + names[0] + else + if names[0][-1] == File::SEPARATOR + s = names[0][0..-2] + else + s = names[0].dup + end + (1..names.size-2).each { |i| + t = names[i] + if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR + t = t[1..-2] + elsif t[0] == File::SEPARATOR + t = t[1..-1] + elsif t[-1] == File::SEPARATOR + t = t[0..-2] + end + s += File::SEPARATOR + t if t != "" + } + if names[-1][0] == File::SEPARATOR + s += File::SEPARATOR + names[-1][1..-1] + else + s += File::SEPARATOR + names[-1] + end + s + end + end + + def self.expand_path(path, default_dir = '.') + def concat_path(path, base_path) + if path[0] == "/" || path[1] == ':' # Windows root! + expanded_path = path + elsif path[0] == "~" + if (path[1] == "/" || path[1] == nil) + dir = path[1, path.size] + home_dir = _gethome + + unless home_dir + raise ArgumentError, "couldn't find HOME environment -- expanding '~'" + end + + expanded_path = home_dir + expanded_path += dir if dir + expanded_path += "/" + else + splitted_path = path.split("/") + user = splitted_path[0][1, splitted_path[0].size] + dir = "/" + splitted_path[1, splitted_path.size].join("/") + + home_dir = _gethome(user) + + unless home_dir + raise ArgumentError, "user #{user} doesn't exist" + end + + expanded_path = home_dir + expanded_path += dir if dir + expanded_path += "/" + end + else + expanded_path = concat_path(base_path, _getwd) + expanded_path += "/" + path + end + + expanded_path + end + + expanded_path = concat_path(path, default_dir) + expand_path_array = [] + while expanded_path.include?('//') + expanded_path = expanded_path.gsub('//', '/') + end + + if expanded_path == "/" + expanded_path + else + expanded_path.split('/').each do |path_token| + if path_token == '..' + if expand_path_array.size > 1 + expand_path_array.pop + end + elsif path_token == '.' + # nothing to do. + else + expand_path_array << path_token + end + end + + expand_path = expand_path_array.join("/") + expand_path.empty? ? '/' : expand_path + end + end + + def self.foreach(file) + if block_given? + self.open(file) do |f| + f.each {|l| yield l} + end + else + return self.new(file) + end + end + + def self.directory?(file) + FileTest.directory?(file) + end + + def self.exist?(file) + FileTest.exist?(file) + end + + def self.exists?(file) + FileTest.exists?(file) + end + + def self.file?(file) + FileTest.file?(file) + end + + def self.pipe?(file) + FileTest.pipe?(file) + end + + def self.size(file) + FileTest.size(file) + end + + def self.size?(file) + FileTest.size?(file) + end + + def self.socket?(file) + FileTest.socket?(file) + end + + def self.symlink?(file) + FileTest.symlink?(file) + end + + def self.zero?(file) + FileTest.zero?(file) + end + + def self.extname(filename) + fname = self.basename(filename) + return '' if fname[0] == '.' || fname.index('.').nil? + ext = fname.split('.').last + ext.empty? ? '' : ".#{ext}" + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/mrblib/file_constants.rb b/web/server/h2o/libh2o/deps/mruby-io/mrblib/file_constants.rb new file mode 100644 index 000000000..a68ee2598 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/mrblib/file_constants.rb @@ -0,0 +1,29 @@ +class File + module Constants + RDONLY = 0 + WRONLY = 1 + RDWR = 2 + NONBLOCK = 4 + APPEND = 8 + + BINARY = 0 + SYNC = 128 + NOFOLLOW = 256 + CREAT = 512 + TRUNC = 1024 + EXCL = 2048 + + NOCTTY = 131072 + DSYNC = 4194304 + + FNM_SYSCASE = 0 + FNM_NOESCAPE = 1 + FNM_PATHNAME = 2 + FNM_DOTMATCH = 4 + FNM_CASEFOLD = 8 + end +end + +class File + include File::Constants +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/mrblib/io.rb b/web/server/h2o/libh2o/deps/mruby-io/mrblib/io.rb new file mode 100644 index 000000000..c838b96e5 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/mrblib/io.rb @@ -0,0 +1,372 @@ +## +# IO + +class IOError < StandardError; end +class EOFError < IOError; end + +class IO + SEEK_SET = 0 + SEEK_CUR = 1 + SEEK_END = 2 + + BUF_SIZE = 4096 + + def self.open(*args, &block) + io = self.new(*args) + + return io unless block + + begin + yield io + ensure + begin + io.close unless io.closed? + rescue StandardError + end + end + end + + def self.popen(command, mode = 'r', &block) + io = self._popen(command, mode) + return io unless block + + begin + yield io + ensure + begin + io.close unless io.closed? + rescue IOError + # nothing + end + end + end + + + def self.read(path, length=nil, offset=nil, opt=nil) + if not opt.nil? # 4 arguments + offset ||= 0 + elsif not offset.nil? # 3 arguments + if offset.is_a? Hash + opt = offset + offset = 0 + else + opt = {} + end + elsif not length.nil? # 2 arguments + if length.is_a? Hash + opt = length + offset = 0 + length = nil + else + offset = 0 + opt = {} + end + else # only 1 argument + opt = {} + offset = 0 + length = nil + end + + str = "" + fd = -1 + io = nil + begin + if path[0] == "|" + io = IO.popen(path[1..-1], (opt[:mode] || "r")) + else + fd = IO.sysopen(path) + io = IO.open(fd, opt[:mode] || "r") + end + io.seek(offset) if offset > 0 + str = io.read(length) + ensure + if io + io.close + elsif fd != -1 + IO._sysclose(fd) + end + end + str + end + + def flush + # mruby-io always writes immediately (no output buffer). + raise IOError, "closed stream" if self.closed? + self + end + + def write(string) + str = string.is_a?(String) ? string : string.to_s + return str.size unless str.size > 0 + + len = syswrite(str) + if len != -1 + @pos += len + return len + end + + raise IOError + end + + def eof? + return true if @buf && @buf.size > 0 + + ret = false + char = '' + + begin + char = sysread(1) + rescue EOFError => e + ret = true + ensure + _ungets(char) + end + + ret + end + alias_method :eof, :eof? + + def pos + raise IOError if closed? + @pos + end + alias_method :tell, :pos + + def pos=(i) + seek(i, SEEK_SET) + end + + def seek(i, whence = SEEK_SET) + raise IOError if closed? + @pos = sysseek(i, whence) + @buf = '' + 0 + end + + def _read_buf + return @buf if @buf && @buf.size > 0 + @buf = sysread(BUF_SIZE) + end + + def _ungets(substr) + raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String) + raise IOError if @pos == 0 || @pos.nil? + @pos -= substr.size + if @buf.empty? + @buf = substr + else + @buf = substr + @buf + end + nil + end + + def ungetc(char) + raise IOError if @pos == 0 || @pos.nil? + _ungets(char) + nil + end + + def read(length = nil) + unless length.nil? + unless length.is_a? Fixnum + raise TypeError.new "can't convert #{length.class} into Integer" + end + if length < 0 + raise ArgumentError.new "negative length: #{length} given" + end + if length == 0 + return "" # easy case + end + end + + array = [] + start_pos = @pos + while 1 + begin + _read_buf + rescue EOFError => e + array = nil if array.empty? and (not length.nil?) and length != 0 + break + end + + if length && (@pos - start_pos + @buf.size) >= length + len = length - (@pos - start_pos) + array.push @buf[0, len] + @pos += len + @buf = @buf[len, @buf.size - len] + break + else + array.push @buf + @pos += @buf.size + @buf = '' + end + end + + array && array.join + end + + def readline(arg = $/, limit = nil) + case arg + when String + rs = arg + when Fixnum + rs = $/ + limit = arg + else + raise ArgumentError + end + + if rs.nil? + return read + end + + if rs == "" + rs = $/ + $/ + end + + array = [] + start_pos = @pos + while 1 + begin + _read_buf + rescue EOFError => e + array = nil if array.empty? + break + end + + if limit && (@pos - start_pos + @buf.size) >= limit + len = limit - (@pos - start_pos) + array.push @buf[0, len] + @pos += len + @buf = @buf[len, @buf.size - len] + break + elsif idx = @buf.index(rs) + len = idx + rs.size + array.push @buf[0, len] + @pos += len + @buf = @buf[len, @buf.size - len] + break + else + array.push @buf + @pos += @buf.size + @buf = '' + end + end + + raise EOFError.new "end of file reached" if array.nil? + + array.join + end + + def gets(*args) + begin + readline(*args) + rescue EOFError => e + nil + end + end + + def readchar + _read_buf + c = @buf[0] + @buf = @buf[1, @buf.size] + @pos += 1 + c + end + + def getc + begin + readchar + rescue EOFError => e + nil + end + end + + # 15.2.20.5.3 + def each(&block) + while line = self.gets + block.call(line) + end + self + end + + # 15.2.20.5.4 + def each_byte(&block) + while char = self.getc + block.call(char) + end + self + end + + # 15.2.20.5.5 + alias each_line each + + alias each_char each_byte + + def readlines + ary = [] + while (line = gets) + ary << line + end + ary + end + + def puts(*args) + i = 0 + len = args.size + while i < len + s = args[i].to_s + write s + write "\n" if (s[-1] != "\n") + i += 1 + end + write "\n" if len == 0 + nil + end + + def print(*args) + i = 0 + len = args.size + while i < len + write args[i].to_s + i += 1 + end + end + + def printf(*args) + write sprintf(*args) + nil + end + + alias_method :to_i, :fileno +end + +STDIN = IO.open(0, "r") +STDOUT = IO.open(1, "w") +STDERR = IO.open(2, "w") + +$stdin = STDIN +$stdout = STDOUT +$stderr = STDERR + +module Kernel + def print(*args) + STDOUT.print(*args) + end + + def puts(*args) + STDOUT.puts(*args) + end + + def printf(*args) + STDOUT.printf(*args) + end + + def gets(*args) + STDIN.gets(*args) + end + + def getc(*args) + STDIN.getc(*args) + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/mrblib/kernel.rb b/web/server/h2o/libh2o/deps/mruby-io/mrblib/kernel.rb new file mode 100644 index 000000000..484b50160 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/mrblib/kernel.rb @@ -0,0 +1,15 @@ +module Kernel + def self.`(cmd) + IO.popen(cmd) { |io| io.read } + end + + def open(file, *rest, &block) + raise ArgumentError unless file.is_a?(String) + + if file[0] == "|" + IO.popen(file[1..-1], *rest, &block) + else + File.open(file, *rest, &block) + end + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/run_test.rb b/web/server/h2o/libh2o/deps/mruby-io/run_test.rb new file mode 100644 index 000000000..444d18fda --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/run_test.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +# +# mrbgems test runner +# + +if __FILE__ == $0 + repository, dir = 'https://github.com/mruby/mruby.git', 'tmp/mruby' + build_args = ARGV + + Dir.mkdir 'tmp' unless File.exist?('tmp') + unless File.exist?(dir) + system "git clone #{repository} #{dir}" + end + + exit system(%Q[cd #{dir}; MRUBY_CONFIG=#{File.expand_path __FILE__} ruby minirake #{build_args.join(' ')}]) +end + +MRuby::Build.new do |conf| + toolchain :gcc + conf.gembox 'default' + + conf.gem :git => 'https://github.com/iij/mruby-env.git' + + conf.gem File.expand_path(File.dirname(__FILE__)) +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/src/file.c b/web/server/h2o/libh2o/deps/mruby-io/src/file.c new file mode 100644 index 000000000..feb8558ed --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/src/file.c @@ -0,0 +1,325 @@ +/* +** file.c - File class +*/ + +#include "mruby.h" +#include "mruby/class.h" +#include "mruby/data.h" +#include "mruby/string.h" +#include "mruby/ext/io.h" + +#if MRUBY_RELEASE_NO < 10000 +#include "error.h" +#else +#include "mruby/error.h" +#endif + +#include +#include + +#include +#include + +#include +#include +#include +#if defined(_WIN32) || defined(_WIN64) + #define NULL_FILE "NUL" + #define UNLINK _unlink + #define GETCWD _getcwd + #define CHMOD(a, b) 0 + #define MAXPATHLEN 1024 + #if !defined(PATH_MAX) + #define PATH_MAX _MAX_PATH + #endif + #define realpath(N,R) _fullpath((R),(N),_MAX_PATH) + #include +#else + #define NULL_FILE "/dev/null" + #include + #define UNLINK unlink + #define GETCWD getcwd + #define CHMOD(a, b) chmod(a,b) + #include + #include + #include + #include +#endif + +#if defined(_WIN32) || defined(_WIN64) + #define PATH_SEPARATOR ";" + #define FILE_SEPARATOR "\\" +#else + #define PATH_SEPARATOR ":" + #define FILE_SEPARATOR "/" +#endif + +#ifndef LOCK_SH +#define LOCK_SH 1 +#endif +#ifndef LOCK_EX +#define LOCK_EX 2 +#endif +#ifndef LOCK_NB +#define LOCK_NB 4 +#endif +#ifndef LOCK_UN +#define LOCK_UN 8 +#endif + +#define STAT(p, s) stat(p, s) + + +mrb_value +mrb_file_s_umask(mrb_state *mrb, mrb_value klass) +{ +#if defined(_WIN32) || defined(_WIN64) + /* nothing to do on windows */ + return mrb_fixnum_value(0); + +#else + mrb_int mask, omask; + if (mrb_get_args(mrb, "|i", &mask) == 0) { + omask = umask(0); + umask(omask); + } else { + omask = umask(mask); + } + return mrb_fixnum_value(omask); +#endif +} + +static mrb_value +mrb_file_s_unlink(mrb_state *mrb, mrb_value obj) +{ + mrb_value *argv; + mrb_value pathv; + mrb_int argc, i; + const char *path; + + mrb_get_args(mrb, "*", &argv, &argc); + for (i = 0; i < argc; i++) { + pathv = mrb_convert_type(mrb, argv[i], MRB_TT_STRING, "String", "to_str"); + path = mrb_string_value_cstr(mrb, &pathv); + if (UNLINK(path) < 0) { + mrb_sys_fail(mrb, path); + } + } + return mrb_fixnum_value(argc); +} + +static mrb_value +mrb_file_s_rename(mrb_state *mrb, mrb_value obj) +{ + mrb_value from, to; + const char *src, *dst; + + mrb_get_args(mrb, "SS", &from, &to); + src = mrb_string_value_cstr(mrb, &from); + dst = mrb_string_value_cstr(mrb, &to); + if (rename(src, dst) < 0) { + if (CHMOD(dst, 0666) == 0 && UNLINK(dst) == 0 && rename(src, dst) == 0) { + return mrb_fixnum_value(0); + } + mrb_sys_fail(mrb, mrb_str_to_cstr(mrb, mrb_format(mrb, "(%S, %S)", from, to))); + } + return mrb_fixnum_value(0); +} + +static mrb_value +mrb_file_dirname(mrb_state *mrb, mrb_value klass) +{ + #if defined(_WIN32) || defined(_WIN64) + char dname[_MAX_DIR], vname[_MAX_DRIVE]; + char buffer[_MAX_DRIVE + _MAX_DIR]; + char *path; + mrb_value s; + mrb_get_args(mrb, "S", &s); + path = mrb_str_to_cstr(mrb, s); + _splitpath((const char*)path, vname, dname, NULL, NULL); + snprintf(buffer, _MAX_DRIVE + _MAX_DIR, "%s%s", vname, dname); + return mrb_str_new_cstr(mrb, buffer); + #else + char *dname, *path; + mrb_value s; + mrb_get_args(mrb, "S", &s); + path = mrb_str_to_cstr(mrb, s); + + if ((dname = dirname(path)) == NULL) { + mrb_sys_fail(mrb, "dirname"); + } + #endif + return mrb_str_new_cstr(mrb, dname); +} + +static mrb_value +mrb_file_basename(mrb_state *mrb, mrb_value klass) +{ + #if defined(_WIN32) || defined(_WIN64) + char bname[_MAX_DIR]; + char extname[_MAX_EXT]; + char *path; + char buffer[_MAX_DIR + _MAX_EXT]; + mrb_value s; + mrb_get_args(mrb, "S", &s); + path = mrb_str_to_cstr(mrb, s); + _splitpath((const char*)path, NULL, NULL, bname, extname); + snprintf(buffer, _MAX_DIR + _MAX_EXT, "%s%s", bname, extname); + return mrb_str_new_cstr(mrb, buffer); + #else + char *bname, *path; + mrb_value s; + mrb_get_args(mrb, "S", &s); + path = mrb_str_to_cstr(mrb, s); + if ((bname = basename(path)) == NULL) { + mrb_sys_fail(mrb, "basename"); + } + return mrb_str_new_cstr(mrb, bname); + #endif +} + +static mrb_value +mrb_file_realpath(mrb_state *mrb, mrb_value klass) +{ + mrb_value pathname, dir_string, s, result; + int argc; + char *cpath; + + argc = mrb_get_args(mrb, "S|S", &pathname, &dir_string); + if (argc == 2) { + s = mrb_str_dup(mrb, dir_string); + s = mrb_str_append(mrb, s, mrb_str_new_cstr(mrb, FILE_SEPARATOR)); + s = mrb_str_append(mrb, s, pathname); + pathname = s; + } + cpath = mrb_str_to_cstr(mrb, pathname); + result = mrb_str_buf_new(mrb, PATH_MAX); + if (realpath(cpath, RSTRING_PTR(result)) == NULL) + mrb_sys_fail(mrb, cpath); + mrb_str_resize(mrb, result, strlen(RSTRING_PTR(result))); + return result; +} + +mrb_value +mrb_file__getwd(mrb_state *mrb, mrb_value klass) +{ + mrb_value path; + + path = mrb_str_buf_new(mrb, MAXPATHLEN); + if (GETCWD(RSTRING_PTR(path), MAXPATHLEN) == NULL) { + mrb_sys_fail(mrb, "getcwd(2)"); + } + mrb_str_resize(mrb, path, strlen(RSTRING_PTR(path))); + return path; +} + +static int +mrb_file_is_absolute_path(const char *path) +{ + return (path[0] == '/'); +} + +static mrb_value +mrb_file__gethome(mrb_state *mrb, mrb_value klass) +{ +#ifndef _WIN32 + mrb_value username; + int argc; + char *home; + + argc = mrb_get_args(mrb, "|S", &username); + if (argc == 0) { + home = getenv("HOME"); + if (home == NULL) { + return mrb_nil_value(); + } + if (!mrb_file_is_absolute_path(home)) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home"); + } + } else { + const char *cuser = mrb_str_to_cstr(mrb, username); + struct passwd *pwd = getpwnam(cuser); + if (pwd == NULL) { + return mrb_nil_value(); + } + home = pwd->pw_dir; + if (!mrb_file_is_absolute_path(home)) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "non-absolute home of ~%S", username); + } + } + return mrb_str_new_cstr(mrb, home); +#else + + return mrb_nil_value(); +#endif +} + +#ifndef _WIN32 +mrb_value +mrb_file_flock(mrb_state *mrb, mrb_value self) +{ +#if defined(sun) + mrb_raise(mrb, E_RUNTIME_ERROR, "flock is not supported on Illumos/Solaris"); +#else + mrb_int operation; + int fd; + + mrb_get_args(mrb, "i", &operation); + fd = mrb_fixnum(mrb_io_fileno(mrb, self)); + + while (flock(fd, operation) == -1) { + switch (errno) { + case EINTR: + /* retry */ + break; + case EAGAIN: /* NetBSD */ +#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN + case EWOULDBLOCK: /* FreeBSD OpenBSD Linux */ +#endif + if (operation & LOCK_NB) { + return mrb_false_value(); + } + /* FALLTHRU - should not happen */ + default: + mrb_sys_fail(mrb, "flock failed"); + break; + } + } +#endif + return mrb_fixnum_value(0); +} +#endif + +void +mrb_init_file(mrb_state *mrb) +{ + struct RClass *io, *file, *cnst; + + io = mrb_class_get(mrb, "IO"); + file = mrb_define_class(mrb, "File", io); + MRB_SET_INSTANCE_TT(file, MRB_TT_DATA); + mrb_define_class_method(mrb, file, "umask", mrb_file_s_umask, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, file, "delete", mrb_file_s_unlink, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, file, "unlink", mrb_file_s_unlink, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, file, "rename", mrb_file_s_rename, MRB_ARGS_REQ(2)); + + mrb_define_class_method(mrb, file, "dirname", mrb_file_dirname, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, file, "basename", mrb_file_basename, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, file, "realpath", mrb_file_realpath, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); + mrb_define_class_method(mrb, file, "_getwd", mrb_file__getwd, MRB_ARGS_NONE()); + mrb_define_class_method(mrb, file, "_gethome", mrb_file__gethome, MRB_ARGS_OPT(1)); + + #ifndef _WIN32 + mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1)); + #endif + + cnst = mrb_define_module_under(mrb, file, "Constants"); + mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH)); + mrb_define_const(mrb, cnst, "LOCK_EX", mrb_fixnum_value(LOCK_EX)); + mrb_define_const(mrb, cnst, "LOCK_UN", mrb_fixnum_value(LOCK_UN)); + mrb_define_const(mrb, cnst, "LOCK_NB", mrb_fixnum_value(LOCK_NB)); + mrb_define_const(mrb, cnst, "SEPARATOR", mrb_str_new_cstr(mrb, FILE_SEPARATOR)); + mrb_define_const(mrb, cnst, "PATH_SEPARATOR", mrb_str_new_cstr(mrb, PATH_SEPARATOR)); + mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE)); + +} diff --git a/web/server/h2o/libh2o/deps/mruby-io/src/file_test.c b/web/server/h2o/libh2o/deps/mruby-io/src/file_test.c new file mode 100644 index 000000000..6c380c4a5 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/src/file_test.c @@ -0,0 +1,365 @@ +/* +** file.c - File class +*/ + +#include "mruby.h" +#include "mruby/class.h" +#include "mruby/data.h" +#include "mruby/string.h" +#include "mruby/ext/io.h" + +#if MRUBY_RELEASE_NO < 10000 +#include "error.h" +#else +#include "mruby/error.h" +#endif + +#include +#include + +#if defined(_WIN32) || defined(_WIN64) + #define LSTAT stat + #include +#else + #define LSTAT lstat + #include + #include + #include + #include + #include + #include +#endif + +#include + +#include +#include +#include +#include + +extern struct mrb_data_type mrb_io_type; + +static int +mrb_stat0(mrb_state *mrb, mrb_value obj, struct stat *st, int do_lstat) +{ + mrb_value tmp; + mrb_value io_klass, str_klass; + + io_klass = mrb_obj_value(mrb_class_get(mrb, "IO")); + str_klass = mrb_obj_value(mrb_class_get(mrb, "String")); + + tmp = mrb_funcall(mrb, obj, "is_a?", 1, io_klass); + if (mrb_test(tmp)) { + struct mrb_io *fptr; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, obj, &mrb_io_type); + + if (fptr && fptr->fd >= 0) { + return fstat(fptr->fd, st); + } + + mrb_raise(mrb, E_IO_ERROR, "closed stream"); + return -1; + } + + tmp = mrb_funcall(mrb, obj, "is_a?", 1, str_klass); + if (mrb_test(tmp)) { + if (do_lstat) { + return LSTAT(mrb_str_to_cstr(mrb, obj), st); + } else { + return stat(mrb_str_to_cstr(mrb, obj), st); + } + } + + return -1; +} + +static int +mrb_stat(mrb_state *mrb, mrb_value obj, struct stat *st) +{ + return mrb_stat0(mrb, obj, st, 0); +} + +static int +mrb_lstat(mrb_state *mrb, mrb_value obj, struct stat *st) +{ + return mrb_stat0(mrb, obj, st, 1); +} + +/* + * Document-method: directory? + * + * call-seq: + * File.directory?(file_name) -> true or false + * + * Returns true if the named file is a directory, + * or a symlink that points at a directory, and false + * otherwise. + * + * File.directory?(".") + */ + +mrb_value +mrb_filetest_s_directory_p(mrb_state *mrb, mrb_value klass) +{ +#ifndef S_ISDIR +# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + if (S_ISDIR(st.st_mode)) + return mrb_true_value(); + + return mrb_false_value(); +} + +/* + * call-seq: + * File.pipe?(file_name) -> true or false + * + * Returns true if the named file is a pipe. + */ + +mrb_value +mrb_filetest_s_pipe_p(mrb_state *mrb, mrb_value klass) +{ +#ifdef S_IFIFO +# ifndef S_ISFIFO +# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +# endif + + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + if (S_ISFIFO(st.st_mode)) + return mrb_true_value(); + +#endif + return mrb_false_value(); +} + +/* + * call-seq: + * File.symlink?(file_name) -> true or false + * + * Returns true if the named file is a symbolic link. + */ + +mrb_value +mrb_filetest_s_symlink_p(mrb_state *mrb, mrb_value klass) +{ +#ifndef S_ISLNK +# ifdef _S_ISLNK +# define S_ISLNK(m) _S_ISLNK(m) +# else +# ifdef _S_IFLNK +# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK) +# else +# ifdef S_IFLNK +# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +# endif +# endif +# endif +#endif + +#ifdef S_ISLNK + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_lstat(mrb, obj, &st) == -1) + return mrb_false_value(); + if (S_ISLNK(st.st_mode)) + return mrb_true_value(); +#endif + + return mrb_false_value(); +} + +/* + * call-seq: + * File.socket?(file_name) -> true or false + * + * Returns true if the named file is a socket. + */ + +mrb_value +mrb_filetest_s_socket_p(mrb_state *mrb, mrb_value klass) +{ +#ifndef S_ISSOCK +# ifdef _S_ISSOCK +# define S_ISSOCK(m) _S_ISSOCK(m) +# else +# ifdef _S_IFSOCK +# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK) +# else +# ifdef S_IFSOCK +# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) +# endif +# endif +# endif +#endif + +#ifdef S_ISSOCK + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + if (S_ISSOCK(st.st_mode)) + return mrb_true_value(); +#endif + + return mrb_false_value(); +} + +/* + * call-seq: + * File.exist?(file_name) -> true or false + * File.exists?(file_name) -> true or false + * + * Return true if the named file exists. + */ + +mrb_value +mrb_filetest_s_exist_p(mrb_state *mrb, mrb_value klass) +{ + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + + return mrb_true_value(); +} + +/* + * call-seq: + * File.file?(file_name) -> true or false + * + * Returns true if the named file exists and is a + * regular file. + */ + +mrb_value +mrb_filetest_s_file_p(mrb_state *mrb, mrb_value klass) +{ +#ifndef S_ISREG +# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#endif + + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + if (S_ISREG(st.st_mode)) + return mrb_true_value(); + + return mrb_false_value(); +} + +/* + * call-seq: + * File.zero?(file_name) -> true or false + * + * Returns true if the named file exists and has + * a zero size. + */ + +mrb_value +mrb_filetest_s_zero_p(mrb_state *mrb, mrb_value klass) +{ + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_false_value(); + if (st.st_size == 0) + return mrb_true_value(); + + return mrb_false_value(); +} + +/* + * call-seq: + * File.size(file_name) -> integer + * + * Returns the size of file_name. + * + * _file_name_ can be an IO object. + */ + +mrb_value +mrb_filetest_s_size(mrb_state *mrb, mrb_value klass) +{ + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + mrb_sys_fail(mrb, "mrb_stat"); + + return mrb_fixnum_value(st.st_size); +} + +/* + * call-seq: + * File.size?(file_name) -> Integer or nil + * + * Returns +nil+ if +file_name+ doesn't exist or has zero size, the size of the + * file otherwise. + */ + +mrb_value +mrb_filetest_s_size_p(mrb_state *mrb, mrb_value klass) +{ + struct stat st; + mrb_value obj; + + mrb_get_args(mrb, "o", &obj); + + if (mrb_stat(mrb, obj, &st) < 0) + return mrb_nil_value(); + if (st.st_size == 0) + return mrb_nil_value(); + + return mrb_fixnum_value(st.st_size); +} + +void +mrb_init_file_test(mrb_state *mrb) +{ + struct RClass *f; + + f = mrb_define_class(mrb, "FileTest", mrb->object_class); + + mrb_define_class_method(mrb, f, "directory?", mrb_filetest_s_directory_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "exist?", mrb_filetest_s_exist_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "exists?", mrb_filetest_s_exist_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "file?", mrb_filetest_s_file_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "pipe?", mrb_filetest_s_pipe_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "size", mrb_filetest_s_size, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "size?", mrb_filetest_s_size_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "socket?", mrb_filetest_s_socket_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "symlink?", mrb_filetest_s_symlink_p, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, f, "zero?", mrb_filetest_s_zero_p, MRB_ARGS_REQ(1)); +} diff --git a/web/server/h2o/libh2o/deps/mruby-io/src/io.c b/web/server/h2o/libh2o/deps/mruby-io/src/io.c new file mode 100644 index 000000000..1d2f98f3c --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/src/io.c @@ -0,0 +1,941 @@ +/* +** io.c - IO class +*/ + +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/class.h" +#include "mruby/data.h" +#include "mruby/hash.h" +#include "mruby/string.h" +#include "mruby/variable.h" +#include "mruby/ext/io.h" + +#if MRUBY_RELEASE_NO < 10000 +#include "error.h" +#else +#include "mruby/error.h" +#endif + +#include +#include + +#if defined(_WIN32) || defined(_WIN64) + #include + #include + #define open _open + #define close _close + #define read _read + #define write _write + #define lseek _lseek +#else + #include + #include +#endif + +#include + +#include +#include +#include + + +static int mrb_io_modestr_to_flags(mrb_state *mrb, const char *modestr); +static int mrb_io_flags_to_modenum(mrb_state *mrb, int flags); +static void fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int noraise); + +#if MRUBY_RELEASE_NO < 10000 +static struct RClass * +mrb_module_get(mrb_state *mrb, const char *name) +{ + return mrb_class_get(mrb, name); +} +#endif + +static int +mrb_io_modestr_to_flags(mrb_state *mrb, const char *mode) +{ + int flags = 0; + const char *m = mode; + + switch (*m++) { + case 'r': + flags |= FMODE_READABLE; + break; + case 'w': + flags |= FMODE_WRITABLE | FMODE_CREATE | FMODE_TRUNC; + break; + case 'a': + flags |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE; + break; + default: + mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %S", mrb_str_new_cstr(mrb, mode)); + } + + while (*m) { + switch (*m++) { + case 'b': + flags |= FMODE_BINMODE; + break; + case '+': + flags |= FMODE_READWRITE; + break; + case ':': + /* XXX: PASSTHROUGH*/ + default: + mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %S", mrb_str_new_cstr(mrb, mode)); + } + } + + return flags; +} + +static int +mrb_io_flags_to_modenum(mrb_state *mrb, int flags) +{ + int modenum = 0; + + switch(flags & (FMODE_READABLE|FMODE_WRITABLE|FMODE_READWRITE)) { + case FMODE_READABLE: + modenum = O_RDONLY; + break; + case FMODE_WRITABLE: + modenum = O_WRONLY; + break; + case FMODE_READWRITE: + modenum = O_RDWR; + break; + } + + if (flags & FMODE_APPEND) { + modenum |= O_APPEND; + } + if (flags & FMODE_TRUNC) { + modenum |= O_TRUNC; + } + if (flags & FMODE_CREATE) { + modenum |= O_CREAT; + } +#ifdef O_BINARY + if (flags & FMODE_BINMODE) { + modenum |= O_BINARY; + } +#endif + + return modenum; +} + +void +mrb_fd_cloexec(mrb_state *mrb, int fd) +{ + int flags, flags2; + +#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) + flags = fcntl(fd, F_GETFD); + if (flags == -1) { + mrb_sys_fail(mrb, "fcntl"); + } + if (fd <= 2) { + flags2 = flags & ~FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */ + } + else { + flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */ + } + if (flags != flags2) { + if (fcntl(fd, F_SETFD, flags2) == -1) { + mrb_sys_fail(mrb, "fcntl"); + } + } +#endif +} + +#ifndef _WIN32 +static int +mrb_proc_exec(const char *pname) +{ + const char *s; + s = pname; + + while (*s == ' ' || *s == '\t' || *s == '\n') + s++; + + if (!*s) { + errno = ENOENT; + return -1; + } + + execl("/bin/sh", "sh", "-c", pname, (char *)NULL); + return -1; +} +#endif + +static void +mrb_io_free(mrb_state *mrb, void *ptr) +{ + struct mrb_io *io = (struct mrb_io *)ptr; + if (io != NULL) { + fptr_finalize(mrb, io, TRUE); + mrb_free(mrb, io); + } +} + +struct mrb_data_type mrb_io_type = { "IO", mrb_io_free }; + +static struct mrb_io * +mrb_io_alloc(mrb_state *mrb) +{ + struct mrb_io *fptr; + + fptr = (struct mrb_io *)mrb_malloc(mrb, sizeof(struct mrb_io)); + fptr->fd = -1; + fptr->fd2 = -1; + fptr->pid = 0; + fptr->writable = 0; + fptr->sync = 0; + return fptr; +} + +#ifndef NOFILE +#define NOFILE 64 +#endif + +#ifndef _WIN32 +mrb_value +mrb_io_s_popen(mrb_state *mrb, mrb_value klass) +{ + mrb_value cmd, io, result; + mrb_value mode = mrb_str_new_cstr(mrb, "r"); + mrb_value opt = mrb_hash_new(mrb); + + struct mrb_io *fptr; + const char *pname; + int pid, flags, fd, write_fd = -1; + int pr[2] = { -1, -1 }; + int pw[2] = { -1, -1 }; + int doexec; + int saved_errno; + + mrb_get_args(mrb, "S|SH", &cmd, &mode, &opt); + io = mrb_obj_value(mrb_data_object_alloc(mrb, mrb_class_ptr(klass), NULL, &mrb_io_type)); + + pname = mrb_string_value_cstr(mrb, &cmd); + flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); + + doexec = (strcmp("-", pname) != 0); + + if (flags & FMODE_READABLE) { + if (pipe(pr) == -1) { + mrb_sys_fail(mrb, "pipe"); + } + mrb_fd_cloexec(mrb, pr[0]); + mrb_fd_cloexec(mrb, pr[1]); + } + + if (flags & FMODE_WRITABLE) { + if (pipe(pw) == -1) { + if (pr[0] != -1) close(pr[0]); + if (pr[1] != -1) close(pr[1]); + mrb_sys_fail(mrb, "pipe"); + } + mrb_fd_cloexec(mrb, pw[0]); + mrb_fd_cloexec(mrb, pw[1]); + } + + if (!doexec) { + // XXX + fflush(stdin); + fflush(stdout); + fflush(stderr); + } + + result = mrb_nil_value(); + switch (pid = fork()) { + case 0: /* child */ + if (flags & FMODE_READABLE) { + close(pr[0]); + if (pr[1] != 1) { + dup2(pr[1], 1); + close(pr[1]); + } + } + if (flags & FMODE_WRITABLE) { + close(pw[1]); + if (pw[0] != 0) { + dup2(pw[0], 0); + close(pw[0]); + } + } + if (doexec) { + for (fd = 3; fd < NOFILE; fd++) { + close(fd); + } + mrb_proc_exec(pname); + mrb_raisef(mrb, E_IO_ERROR, "command not found: %S", cmd); + _exit(127); + } + result = mrb_nil_value(); + break; + + default: /* parent */ + if ((flags & FMODE_READABLE) && (flags & FMODE_WRITABLE)) { + close(pr[1]); + fd = pr[0]; + close(pw[0]); + write_fd = pw[1]; + } else if (flags & FMODE_READABLE) { + close(pr[1]); + fd = pr[0]; + } else { + close(pw[0]); + fd = pw[1]; + } + + mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); + mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0)); + + fptr = mrb_io_alloc(mrb); + fptr->fd = fd; + fptr->fd2 = write_fd; + fptr->pid = pid; + fptr->writable = ((flags & FMODE_WRITABLE) != 0); + fptr->sync = 0; + + DATA_TYPE(io) = &mrb_io_type; + DATA_PTR(io) = fptr; + result = io; + break; + + case -1: /* error */ + saved_errno = errno; + if (flags & FMODE_READABLE) { + close(pr[0]); + close(pr[1]); + } + if (flags & FMODE_WRITABLE) { + close(pw[0]); + close(pw[1]); + } + errno = saved_errno; + mrb_sys_fail(mrb, "pipe_open failed."); + break; + } + return result; +} +#endif + +mrb_value +mrb_io_initialize(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + mrb_int fd; + mrb_value mode, opt; + int flags; + + mode = opt = mrb_nil_value(); + + mrb_get_args(mrb, "i|So", &fd, &mode, &opt); + if (mrb_nil_p(mode)) { + mode = mrb_str_new_cstr(mrb, "r"); + } + if (mrb_nil_p(opt)) { + opt = mrb_hash_new(mrb); + } + + flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); + + mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); + mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0)); + + fptr = DATA_PTR(io); + if (fptr != NULL) { + fptr_finalize(mrb, fptr, 0); + mrb_free(mrb, fptr); + } + fptr = mrb_io_alloc(mrb); + + DATA_TYPE(io) = &mrb_io_type; + DATA_PTR(io) = fptr; + + fptr->fd = fd; + fptr->writable = ((flags & FMODE_WRITABLE) != 0); + fptr->sync = 0; + return io; +} + +static void +fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int noraise) +{ + int n = 0; + + if (fptr == NULL) { + return; + } + + if (fptr->fd > 2) { + n = close(fptr->fd); + if (n == 0) { + fptr->fd = -1; + } + } + if (fptr->fd2 > 2) { + n = close(fptr->fd2); + if (n == 0) { + fptr->fd2 = -1; + } + } + +#if !defined(_WIN32) && !defined(_WIN64) + if (fptr->pid != 0) { + pid_t pid; + do { + pid = waitpid(fptr->pid, NULL, 0); + } while (pid == -1 && errno == EINTR); + } +#endif + + if (!noraise && n != 0) { + mrb_sys_fail(mrb, "fptr_finalize failed."); + } +} + +mrb_value +mrb_io_s_for_fd(mrb_state *mrb, mrb_value klass) +{ + struct RClass *c = mrb_class_ptr(klass); + enum mrb_vtype ttype = MRB_INSTANCE_TT(c); + mrb_value obj; + + /* copied from mrb_instance_alloc() */ + if (ttype == 0) ttype = MRB_TT_OBJECT; + obj = mrb_obj_value((struct RObject*)mrb_obj_alloc(mrb, ttype, c)); + return mrb_io_initialize(mrb, obj); +} + +mrb_value +mrb_io_s_sysclose(mrb_state *mrb, mrb_value klass) +{ + mrb_int fd; + mrb_get_args(mrb, "i", &fd); + if (close(fd) == -1) { + mrb_sys_fail(mrb, "close"); + } + return mrb_fixnum_value(0); +} + +int +mrb_cloexec_open(mrb_state *mrb, const char *pathname, mrb_int flags, mrb_int mode) +{ + int fd, retry = FALSE; + +#ifdef O_CLOEXEC + /* O_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */ + flags |= O_CLOEXEC; +#elif defined O_NOINHERIT + flags |= O_NOINHERIT; +#endif +reopen: + fd = open(pathname, flags, mode); + if (fd == -1) { + if (!retry) { + switch (errno) { + case ENFILE: + case EMFILE: + mrb_garbage_collect(mrb); + retry = TRUE; + goto reopen; + } + } + mrb_sys_fail(mrb, "open"); + } + + if (fd <= 2) { + mrb_fd_cloexec(mrb, fd); + } + return fd; +} + +mrb_value +mrb_io_s_sysopen(mrb_state *mrb, mrb_value klass) +{ + mrb_value path = mrb_nil_value(); + mrb_value mode = mrb_nil_value(); + mrb_int fd, flags, perm = -1; + const char *pat; + int modenum; + + mrb_get_args(mrb, "S|Si", &path, &mode, &perm); + if (mrb_nil_p(mode)) { + mode = mrb_str_new_cstr(mrb, "r"); + } + if (perm < 0) { + perm = 0666; + } + + pat = mrb_string_value_cstr(mrb, &path); + flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); + modenum = mrb_io_flags_to_modenum(mrb, flags); + fd = mrb_cloexec_open(mrb, pat, modenum, perm); + return mrb_fixnum_value(fd); +} + +mrb_value +mrb_io_sysread(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + mrb_value buf = mrb_nil_value(); + mrb_int maxlen; + int ret; + + mrb_get_args(mrb, "i|S", &maxlen, &buf); + if (maxlen < 0) { + return mrb_nil_value(); + } + + if (mrb_nil_p(buf)) { + buf = mrb_str_new(mrb, NULL, maxlen); + } + if (RSTRING_LEN(buf) != maxlen) { + buf = mrb_str_resize(mrb, buf, maxlen); + } + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + ret = read(fptr->fd, RSTRING_PTR(buf), maxlen); + switch (ret) { + case 0: /* EOF */ + if (maxlen == 0) { + buf = mrb_str_new_cstr(mrb, ""); + } else { + mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File"); + } + break; + case -1: /* Error */ + mrb_sys_fail(mrb, "sysread failed"); + break; + default: + if (RSTRING_LEN(buf) != ret) { + buf = mrb_str_resize(mrb, buf, ret); + } + break; + } + + return buf; +} + +mrb_value +mrb_io_sysseek(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + int pos; + mrb_int offset, whence = -1; + + mrb_get_args(mrb, "i|i", &offset, &whence); + if (whence < 0) { + whence = 0; + } + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + pos = lseek(fptr->fd, offset, whence); + if (pos < 0) { + mrb_raise(mrb, E_IO_ERROR, "sysseek faield"); + } + + return mrb_fixnum_value(pos); +} + +mrb_value +mrb_io_syswrite(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + mrb_value str, buf; + int fd, length; + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + if (! fptr->writable) { + mrb_raise(mrb, E_IO_ERROR, "not opened for writing"); + } + + mrb_get_args(mrb, "S", &str); + if (mrb_type(str) != MRB_TT_STRING) { + buf = mrb_funcall(mrb, str, "to_s", 0); + } else { + buf = str; + } + + if (fptr->fd2 == -1) { + fd = fptr->fd; + } else { + fd = fptr->fd2; + } + length = write(fd, RSTRING_PTR(buf), RSTRING_LEN(buf)); + + return mrb_fixnum_value(length); +} + +mrb_value +mrb_io_close(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + if (fptr && fptr->fd < 0) { + mrb_raise(mrb, E_IO_ERROR, "closed stream."); + } + fptr_finalize(mrb, fptr, FALSE); + return mrb_nil_value(); +} + +mrb_value +mrb_io_closed(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + if (fptr->fd >= 0) { + return mrb_false_value(); + } + + return mrb_true_value(); +} + +mrb_value +mrb_io_pid(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + + if (fptr->pid > 0) { + return mrb_fixnum_value(fptr->pid); + } + + return mrb_nil_value(); +} + +static struct timeval +time2timeval(mrb_state *mrb, mrb_value time) +{ + struct timeval t = { 0, 0 }; + + switch (mrb_type(time)) { + case MRB_TT_FIXNUM: + t.tv_sec = mrb_fixnum(time); + t.tv_usec = 0; + break; + + case MRB_TT_FLOAT: + t.tv_sec = mrb_float(time); + t.tv_usec = (mrb_float(time) - t.tv_sec) * 1000000.0; + break; + + default: + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); + } + + return t; +} + +static int +mrb_io_read_data_pending(mrb_state *mrb, mrb_value io) +{ + mrb_value buf = mrb_iv_get(mrb, io, mrb_intern_cstr(mrb, "@buf")); + if (mrb_type(buf) == MRB_TT_STRING && RSTRING_LEN(buf) > 0) { + return 1; + } + return 0; +} + +static mrb_value +mrb_io_s_select(mrb_state *mrb, mrb_value klass) +{ + mrb_value *argv; + mrb_int argc; + mrb_value read, read_io, write, except, timeout, list; + struct timeval *tp, timerec; + fd_set pset, rset, wset, eset; + fd_set *rp, *wp, *ep; + struct mrb_io *fptr; + int pending = 0; + mrb_value result; + int max = 0; + int interrupt_flag = 0; + int i, n; + + mrb_get_args(mrb, "*", &argv, &argc); + + if (argc < 1 || argc > 4) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1..4)", mrb_fixnum_value(argc)); + } + + timeout = mrb_nil_value(); + except = mrb_nil_value(); + write = mrb_nil_value(); + if (argc > 3) + timeout = argv[3]; + if (argc > 2) + except = argv[2]; + if (argc > 1) + write = argv[1]; + read = argv[0]; + + if (mrb_nil_p(timeout)) { + tp = NULL; + } else { + timerec = time2timeval(mrb, timeout); + tp = &timerec; + } + + FD_ZERO(&pset); + if (!mrb_nil_p(read)) { + mrb_check_type(mrb, read, MRB_TT_ARRAY); + rp = &rset; + FD_ZERO(rp); + for (i = 0; i < RARRAY_LEN(read); i++) { + read_io = RARRAY_PTR(read)[i]; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, read_io, &mrb_io_type); + FD_SET(fptr->fd, rp); + if (mrb_io_read_data_pending(mrb, read_io)) { + pending++; + FD_SET(fptr->fd, &pset); + } + if (max < fptr->fd) + max = fptr->fd; + } + if (pending) { + timerec.tv_sec = timerec.tv_usec = 0; + tp = &timerec; + } + } else { + rp = NULL; + } + + if (!mrb_nil_p(write)) { + mrb_check_type(mrb, write, MRB_TT_ARRAY); + wp = &wset; + FD_ZERO(wp); + for (i = 0; i < RARRAY_LEN(write); i++) { + fptr = (struct mrb_io *)mrb_get_datatype(mrb, RARRAY_PTR(write)[i], &mrb_io_type); + FD_SET(fptr->fd, wp); + if (max < fptr->fd) + max = fptr->fd; + if (fptr->fd2 >= 0) { + FD_SET(fptr->fd2, wp); + if (max < fptr->fd2) + max = fptr->fd2; + } + } + } else { + wp = NULL; + } + + if (!mrb_nil_p(except)) { + mrb_check_type(mrb, except, MRB_TT_ARRAY); + ep = &eset; + FD_ZERO(ep); + for (i = 0; i < RARRAY_LEN(except); i++) { + fptr = (struct mrb_io *)mrb_get_datatype(mrb, RARRAY_PTR(except)[i], &mrb_io_type); + FD_SET(fptr->fd, ep); + if (max < fptr->fd) + max = fptr->fd; + if (fptr->fd2 >= 0) { + FD_SET(fptr->fd2, ep); + if (max < fptr->fd2) + max = fptr->fd2; + } + } + } else { + ep = NULL; + } + + max++; + +retry: + n = select(max, rp, wp, ep, tp); + if (n < 0) { + if (errno != EINTR) + mrb_sys_fail(mrb, "select failed"); + if (tp == NULL) + goto retry; + interrupt_flag = 1; + } + + if (!pending && n == 0) + return mrb_nil_value(); + + result = mrb_ary_new_capa(mrb, 3); + mrb_ary_push(mrb, result, rp? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); + mrb_ary_push(mrb, result, wp? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); + mrb_ary_push(mrb, result, ep? mrb_ary_new(mrb) : mrb_ary_new_capa(mrb, 0)); + + if (interrupt_flag == 0) { + if (rp) { + list = RARRAY_PTR(result)[0]; + for (i = 0; i < RARRAY_LEN(read); i++) { + fptr = (struct mrb_io *)mrb_get_datatype(mrb, RARRAY_PTR(read)[i], &mrb_io_type); + if (FD_ISSET(fptr->fd, rp) || + FD_ISSET(fptr->fd, &pset)) { + mrb_ary_push(mrb, list, RARRAY_PTR(read)[i]); + } + } + } + + if (wp) { + list = RARRAY_PTR(result)[1]; + for (i = 0; i < RARRAY_LEN(write); i++) { + fptr = (struct mrb_io *)mrb_get_datatype(mrb, RARRAY_PTR(write)[i], &mrb_io_type); + if (FD_ISSET(fptr->fd, wp)) { + mrb_ary_push(mrb, list, RARRAY_PTR(write)[i]); + } else if (fptr->fd2 >= 0 && FD_ISSET(fptr->fd2, wp)) { + mrb_ary_push(mrb, list, RARRAY_PTR(write)[i]); + } + } + } + + if (ep) { + list = RARRAY_PTR(result)[2]; + for (i = 0; i < RARRAY_LEN(except); i++) { + fptr = (struct mrb_io *)mrb_get_datatype(mrb, RARRAY_PTR(except)[i], &mrb_io_type); + if (FD_ISSET(fptr->fd, ep)) { + mrb_ary_push(mrb, list, RARRAY_PTR(except)[i]); + } else if (fptr->fd2 >= 0 && FD_ISSET(fptr->fd2, ep)) { + mrb_ary_push(mrb, list, RARRAY_PTR(except)[i]); + } + } + } + } + + return result; +} + +mrb_value +mrb_io_fileno(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr; + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + return mrb_fixnum_value(fptr->fd); +} + +mrb_value +mrb_io_close_on_exec_p(mrb_state *mrb, mrb_value io) +{ +#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) + struct mrb_io *fptr; + int ret; + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + if (fptr->fd < 0) { + mrb_raise(mrb, E_IO_ERROR, "closed stream"); + } + + if (fptr->fd2 >= 0) { + if ((ret = fcntl(fptr->fd2, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); + if (!(ret & FD_CLOEXEC)) return mrb_false_value(); + } + + if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); + if (!(ret & FD_CLOEXEC)) return mrb_false_value(); + return mrb_true_value(); + +#else + mrb_raise(mrb, E_NOTIMP_ERROR, "IO#close_on_exec? is not supported on the platform"); + return mrb_false_value(); +#endif +} + +mrb_value +mrb_io_set_close_on_exec(mrb_state *mrb, mrb_value io) +{ +#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) + struct mrb_io *fptr; + int flag, ret; + mrb_bool b; + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, io, &mrb_io_type); + if (fptr->fd < 0) { + mrb_raise(mrb, E_IO_ERROR, "closed stream"); + } + + mrb_get_args(mrb, "b", &b); + flag = b ? FD_CLOEXEC : 0; + + if (fptr->fd2 >= 0) { + if ((ret = fcntl(fptr->fd2, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); + if ((ret & FD_CLOEXEC) != flag) { + ret = (ret & ~FD_CLOEXEC) | flag; + ret = fcntl(fptr->fd2, F_SETFD, ret); + + if (ret == -1) mrb_sys_fail(mrb, "F_SETFD failed"); + } + } + + if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) mrb_sys_fail(mrb, "F_GETFD failed"); + if ((ret & FD_CLOEXEC) != flag) { + ret = (ret & ~FD_CLOEXEC) | flag; + ret = fcntl(fptr->fd, F_SETFD, ret); + if (ret == -1) mrb_sys_fail(mrb, "F_SETFD failed"); + } + + return mrb_bool_value(b); +#else + mrb_raise(mrb, E_NOTIMP_ERROR, "IO#close_on_exec= is not supported on the platform"); + return mrb_nil_value(); +#endif +} + +mrb_value +mrb_io_set_sync(mrb_state *mrb, mrb_value self) +{ + struct mrb_io *fptr; + mrb_bool b; + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, self, &mrb_io_type); + if (fptr->fd < 0) { + mrb_raise(mrb, E_IO_ERROR, "closed stream."); + } + + mrb_get_args(mrb, "b", &b); + fptr->sync = b; + return mrb_bool_value(b); +} + +mrb_value +mrb_io_sync(mrb_state *mrb, mrb_value self) +{ + struct mrb_io *fptr; + + fptr = (struct mrb_io *)mrb_get_datatype(mrb, self, &mrb_io_type); + if (fptr->fd < 0) { + mrb_raise(mrb, E_IO_ERROR, "closed stream."); + } + return mrb_bool_value(fptr->sync); +} + +void +mrb_init_io(mrb_state *mrb) +{ + struct RClass *io; + + io = mrb_define_class(mrb, "IO", mrb->object_class); + MRB_SET_INSTANCE_TT(io, MRB_TT_DATA); + + mrb_include_module(mrb, io, mrb_module_get(mrb, "Enumerable")); /* 15.2.20.3 */ +#ifndef _WIN32 + mrb_define_class_method(mrb, io, "_popen", mrb_io_s_popen, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, io, "_sysclose", mrb_io_s_sysclose, MRB_ARGS_REQ(1)); +#endif + mrb_define_class_method(mrb, io, "for_fd", mrb_io_s_for_fd, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, io, "select", mrb_io_s_select, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, io, "sysopen", mrb_io_s_sysopen, MRB_ARGS_ANY()); + + mrb_define_method(mrb, io, "initialize", mrb_io_initialize, MRB_ARGS_ANY()); /* 15.2.20.5.21 (x)*/ + mrb_define_method(mrb, io, "sync", mrb_io_sync, MRB_ARGS_NONE()); + mrb_define_method(mrb, io, "sync=", mrb_io_set_sync, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, io, "sysread", mrb_io_sysread, MRB_ARGS_ANY()); + mrb_define_method(mrb, io, "sysseek", mrb_io_sysseek, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, io, "syswrite", mrb_io_syswrite, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, io, "close", mrb_io_close, MRB_ARGS_NONE()); /* 15.2.20.5.1 */ + mrb_define_method(mrb, io, "close_on_exec=", mrb_io_set_close_on_exec, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, io, "close_on_exec?", mrb_io_close_on_exec_p, MRB_ARGS_NONE()); + mrb_define_method(mrb, io, "closed?", mrb_io_closed, MRB_ARGS_NONE()); /* 15.2.20.5.2 */ + mrb_define_method(mrb, io, "pid", mrb_io_pid, MRB_ARGS_NONE()); /* 15.2.20.5.2 */ + mrb_define_method(mrb, io, "fileno", mrb_io_fileno, MRB_ARGS_NONE()); + + + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$/"), mrb_str_new_cstr(mrb, "\n")); +} diff --git a/web/server/h2o/libh2o/deps/mruby-io/src/mruby_io_gem.c b/web/server/h2o/libh2o/deps/mruby-io/src/mruby_io_gem.c new file mode 100644 index 000000000..6880e6678 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/src/mruby_io_gem.c @@ -0,0 +1,20 @@ +#include "mruby.h" + +void mrb_init_io(mrb_state *mrb); +void mrb_init_file(mrb_state *mrb); +void mrb_init_file_test(mrb_state *mrb); + +#define DONE mrb_gc_arena_restore(mrb, 0) + +void +mrb_mruby_io_gem_init(mrb_state* mrb) +{ + mrb_init_io(mrb); DONE; + mrb_init_file(mrb); DONE; + mrb_init_file_test(mrb); DONE; +} + +void +mrb_mruby_io_gem_final(mrb_state* mrb) +{ +} diff --git a/web/server/h2o/libh2o/deps/mruby-io/test/file.rb b/web/server/h2o/libh2o/deps/mruby-io/test/file.rb new file mode 100644 index 000000000..d6f39ceb1 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/test/file.rb @@ -0,0 +1,108 @@ +## +# IO Test + +assert('File', '15.2.21') do + File.class == Class +end + +assert('File', '15.2.21.2') do + File.superclass == IO +end + +assert('File TEST SETUP') do + MRubyIOTestUtil.io_test_setup +end + +assert('File#initialize', '15.2.21.4.1') do + io = File.open($mrbtest_io_rfname, "r") + assert_nil io.close + assert_raise IOError do + io.close + end +end + +assert('File#path', '15.2.21.4.2') do + io = File.open($mrbtest_io_rfname, "r") + assert_equal $mrbtest_io_msg, io.read + assert_equal $mrbtest_io_rfname, io.path + io.close + assert_equal $mrbtest_io_rfname, io.path + io.closed? +end + +assert('File.basename') do + assert_equal '/', File.basename('//') + assert_equal 'a', File.basename('/a/') + assert_equal 'b', File.basename('/a/b') + assert_equal 'b', File.basename('../a/b') +end + +assert('File.dirname') do + assert_equal '.', File.dirname('') + assert_equal '.', File.dirname('a') + assert_equal '/', File.dirname('/a') + assert_equal 'a', File.dirname('a/b') + assert_equal '/a', File.dirname('/a/b') +end + +assert('File.extname') do + assert_equal '.txt', File.extname('foo/foo.txt') + assert_equal '.gz', File.extname('foo/foo.tar.gz') + assert_equal '', File.extname('foo/bar') + assert_equal '', File.extname('foo/.bar') + assert_equal '', File.extname('foo.txt/bar') + assert_equal '', File.extname('.foo') +end + +assert('IO#flock') do + f = File.open $mrbtest_io_rfname + assert_equal(f.flock(File::LOCK_SH), 0) + assert_equal(f.flock(File::LOCK_UN), 0) + assert_equal(f.flock(File::LOCK_EX | File::LOCK_NB), 0) + assert_equal(f.flock(File::LOCK_UN), 0) + f.close + true +end + +assert('File.join') do + File.join() == "" and + File.join("a") == "a" and + File.join("/a") == "/a" and + File.join("a/") == "a/" and + File.join("a", "b", "c") == "a/b/c" and + File.join("/a", "b", "c") == "/a/b/c" and + File.join("a", "b", "c/") == "a/b/c/" and + File.join("a/", "/b/", "/c") == "a/b/c" +end + +assert('File.realpath') do + usrbin = IO.popen("cd bin; /bin/pwd -P") { |f| f.read.chomp } + assert_equal usrbin, File.realpath("bin") +end + +assert('File TEST CLEANUP') do + assert_nil MRubyIOTestUtil.io_test_cleanup +end + +assert('File.expand_path') do + assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)" + assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)" + + assert_equal "/home", File.expand_path("/home"), "absolute" + assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir" + + assert_equal "/hoge", File.expand_path("/tmp/..//hoge") + assert_equal "/hoge", File.expand_path("////tmp/..///////hoge") + + assert_equal "/", File.expand_path("../../../..", "/") + assert_equal "/", File.expand_path(([".."] * 100).join("/")) +end + +assert('File.expand_path (with ENV)') do + skip unless Object.const_defined?(:ENV) && ENV['HOME'] + + assert_equal ENV['HOME'], File.expand_path("~/"), "home" + assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir" + + assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir" +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/test/file_test.rb b/web/server/h2o/libh2o/deps/mruby-io/test/file_test.rb new file mode 100644 index 000000000..11742f2db --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/test/file_test.rb @@ -0,0 +1,95 @@ +## +# FileTest + +assert('FileTest TEST SETUP') do + MRubyIOTestUtil.io_test_setup +end + +assert("FileTest.directory?") do + assert_equal true, FileTest.directory?("/tmp") + assert_equal false, FileTest.directory?("/bin/sh") +end + +assert("FileTest.exist?") do + assert_equal true, FileTest.exist?($mrbtest_io_rfname), "filename - exist" + assert_equal false, FileTest.exist?($mrbtest_io_rfname + "-"), "filename - not exist" + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + assert_equal true, FileTest.exist?(io), "io obj - exist" + io.close + assert_equal true, io.closed? + assert_raise IOError do + FileTest.exist?(io) + end +end + +assert("FileTest.file?") do + assert_equal false, FileTest.file?("/tmp") + assert_equal true, FileTest.file?("/bin/sh") +end + +assert("FileTest.pipe?") do + io = IO.popen("ls") + assert_equal true, FileTest.pipe?(io) + assert_equal false, FileTest.pipe?("/tmp") +end + +assert('FileTest.size') do + assert_equal FileTest.size($mrbtest_io_rfname), $mrbtest_io_msg.size + assert_equal FileTest.size($mrbtest_io_wfname), 0 +end + +assert("FileTest.size?") do + assert_equal $mrbtest_io_msg.size, FileTest.size?($mrbtest_io_rfname) + assert_equal nil, FileTest.size?($mrbtest_io_wfname) + assert_equal nil, FileTest.size?("not-exist-test-target-file") + + fp1 = File.open($mrbtest_io_rfname) + fp2 = File.open($mrbtest_io_wfname) + assert_equal $mrbtest_io_msg.size, FileTest.size?(fp1) + assert_equal nil, FileTest.size?(fp2) + fp1.close + fp2.close + + assert_raise IOError do + FileTest.size?(fp1) + end + assert_raise IOError do + FileTest.size?(fp2) + end + + fp1.closed? && fp2.closed? +end + +assert("FileTest.socket?") do + assert_true FileTest.socket?($mrbtest_io_socketname) +end + +assert("FileTest.symlink?") do + assert_true FileTest.symlink?($mrbtest_io_symlinkname) +end + +assert("FileTest.zero?") do + assert_equal false, FileTest.zero?($mrbtest_io_rfname) + assert_equal true, FileTest.zero?($mrbtest_io_wfname) + assert_equal false, FileTest.zero?("not-exist-test-target-file") + + fp1 = File.open($mrbtest_io_rfname) + fp2 = File.open($mrbtest_io_wfname) + assert_equal false, FileTest.zero?(fp1) + assert_equal true, FileTest.zero?(fp2) + fp1.close + fp2.close + + assert_raise IOError do + FileTest.zero?(fp1) + end + assert_raise IOError do + FileTest.zero?(fp2) + end + + fp1.closed? && fp2.closed? +end + +assert('FileTest TEST CLEANUP') do + assert_nil MRubyIOTestUtil.io_test_cleanup +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/test/gc_filedes.sh b/web/server/h2o/libh2o/deps/mruby-io/test/gc_filedes.sh new file mode 100644 index 000000000..6e5d1bbf1 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/test/gc_filedes.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +ulimit -n 20 +mruby -e '100.times { File.open "'$0'" }' diff --git a/web/server/h2o/libh2o/deps/mruby-io/test/io.rb b/web/server/h2o/libh2o/deps/mruby-io/test/io.rb new file mode 100644 index 000000000..b828fef49 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/test/io.rb @@ -0,0 +1,416 @@ +## +# IO Test + +assert('IO TEST SETUP') do + MRubyIOTestUtil.io_test_setup +end + +assert('IO', '15.2.20') do + assert_equal(Class, IO.class) +end + +assert('IO', '15.2.20.2') do + assert_equal(Object, IO.superclass) +end + +assert('IO', '15.2.20.3') do + assert_include(IO.included_modules, Enumerable) +end + +assert('IO.open', '15.2.20.4.1') do + fd = IO.sysopen $mrbtest_io_rfname + assert_equal Fixnum, fd.class + io = IO.open fd + assert_equal IO, io.class + assert_equal $mrbtest_io_msg, io.read + io.close + + fd = IO.sysopen $mrbtest_io_rfname + IO.open(fd) do |io| + assert_equal $mrbtest_io_msg, io.read + end + + true +end + +assert('IO#close', '15.2.20.5.1') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + assert_nil io.close +end + +assert('IO#closed?', '15.2.20.5.2') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + assert_false io.closed? + io.close + assert_true io.closed? +end + +#assert('IO#each', '15.2.20.5.3') do +#assert('IO#each_byte', '15.2.20.5.4') do +#assert('IO#each_line', '15.2.20.5.5') do + +assert('IO#eof?', '15.2.20.5.6') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + $mrbtest_io_msg.each_char { |ch| + # XXX + #assert_false io.eof? + io.getc + } + assert_true io.eof? + io.close + true +end + +assert('IO#flush', '15.2.20.5.7') do + # Note: mruby-io does not have any buffer to be flushed now. + io = IO.new(IO.sysopen($mrbtest_io_wfname)) + assert_equal io, io.flush + io.close + assert_raise(IOError) do + io.flush + end +end + +assert('IO#getc', '15.2.20.5.8') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + $mrbtest_io_msg.each_char { |ch| + assert_equal ch, io.getc + } + assert_equal nil, io.getc + io.close + true +end + +#assert('IO#gets', '15.2.20.5.9') do +#assert('IO#initialize_copy', '15.2.20.5.10') do +#assert('IO#print', '15.2.20.5.11') do +#assert('IO#putc', '15.2.20.5.12') do +#assert('IO#puts', '15.2.20.5.13') do + +assert('IO#read', '15.2.20.5.14') do + IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| + assert_raise(ArgumentError) { io.read(-5) } + assert_raise(TypeError) { io.read("str") } + + len = $mrbtest_io_msg.length + assert_equal '', io.read(0) + assert_equal 'mruby', io.read(5) + assert_equal $mrbtest_io_msg[5,len], io.read(len) + + assert_equal "", io.read + assert_nil io.read(1) + end + + IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| + assert_equal $mrbtest_io_msg, io.read + end +end + +assert('IO#readchar', '15.2.20.5.15') do + # almost same as IO#getc + IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| + $mrbtest_io_msg.each_char { |ch| + assert_equal ch, io.readchar + } + assert_raise(EOFError) do + io.readchar + end + end +end + +#assert('IO#readline', '15.2.20.5.16') do +#assert('IO#readlines', '15.2.20.5.17') do + +assert('IO#sync', '15.2.20.5.18') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + s = io.sync + assert_true(s == true || s == false) + io.close + assert_raise(IOError) do + io.sync + end +end + +assert('IO#sync=', '15.2.20.5.19') do + io = IO.new(IO.sysopen($mrbtest_io_rfname)) + io.sync = true + assert_true io.sync + io.sync = false + assert_false io.sync + io.close + assert_raise(IOError) do + io.sync = true + end +end + +assert('IO#write', '15.2.20.5.20') do + io = IO.open(IO.sysopen($mrbtest_io_wfname)) + assert_equal 0, io.write("") + io.close + true +end + +assert('IO.for_fd') do + fd = IO.sysopen($mrbtest_io_rfname) + io = IO.for_fd(fd) + assert_equal $mrbtest_io_msg, io.read + io.close + true +end + +assert('IO.new') do + io = IO.new(0) + io.close + true +end + +assert('IO gc check') do + 100.times { IO.new(0) } +end + +assert('IO.sysopen("./nonexistent")') do + if Object.const_defined? :Errno + eclass = Errno::ENOENT + else + eclass = RuntimeError + end + assert_raise eclass do + fd = IO.sysopen "./nonexistent" + IO._sysclose fd + end +end + +assert('IO.sysopen, IO#sysread') do + fd = IO.sysopen $mrbtest_io_rfname + io = IO.new fd + str1 = " " + str2 = io.sysread(5, str1) + assert_equal $mrbtest_io_msg[0,5], str1 + assert_equal $mrbtest_io_msg[0,5], str2 + assert_raise EOFError do + io.sysread(10000) + io.sysread(10000) + end + io.close + io.closed? +end + +assert('IO.sysopen, IO#syswrite') do + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + str = "abcdefg" + len = io.syswrite(str) + assert_equal str.size, len + io.close + + io = IO.new(IO.sysopen($mrbtest_io_rfname), "r") + assert_raise(IOError) { io.syswrite("a") } + io.close + + true +end + +assert('IO#_read_buf') do + fd = IO.sysopen $mrbtest_io_rfname + io = IO.new fd + def io._buf + @buf + end + msg_len = $mrbtest_io_msg.size + assert_equal '', io._buf + assert_equal $mrbtest_io_msg, io._read_buf + assert_equal $mrbtest_io_msg, io._buf + assert_equal 'mruby', io.read(5) + assert_equal 5, io.pos + assert_equal msg_len - 5, io._buf.size + assert_equal $mrbtest_io_msg[5,100], io.read + assert_equal 0, io._buf.size + assert_raise EOFError do + io._read_buf + end + assert_equal true, io.eof + assert_equal true, io.eof? + io.close + io.closed? +end + +assert('IO#pos=, IO#seek') do + fd = IO.sysopen $mrbtest_io_rfname + io = IO.new fd + def io._buf + @buf + end + assert_equal 'm', io.getc + assert_equal 1, io.pos + assert_equal 0, io.seek(0) + assert_equal 0, io.pos + io.close + io.closed? +end + +assert('IO#gets') do + fd = IO.sysopen $mrbtest_io_rfname + io = IO.new fd + + # gets without arguments + assert_equal $mrbtest_io_msg, io.gets, "gets without arguments" + assert_equal nil, io.gets, "gets returns nil, when EOF" + + # gets with limit + io.pos = 0 + assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit" + + # gets with rs + io.pos = 0 + assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs" + + # gets with rs, limit + io.pos = 0 + assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit" + io.close + assert_equal true, io.closed?, "close success" + + # reading many-lines file. + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + io.write "0123456789" * 2 + "\na" + assert_equal 22, io.pos + io.close + assert_equal true, io.closed? + + fd = IO.sysopen $mrbtest_io_wfname + io = IO.new fd + line = io.gets + + # gets first line + assert_equal "0123456789" * 2 + "\n", line, "gets first line" + assert_equal 21, line.size + assert_equal 21, io.pos + + # gets second line + assert_equal "a", io.gets, "gets second line" + + # gets third line + assert_equal nil, io.gets, "gets third line; returns nil" + + io.close + io.closed? +end + +assert('IO#gets - paragraph mode') do + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + io.write "0" * 10 + "\n" + io.write "1" * 10 + "\n\n" + io.write "2" * 10 + "\n" + assert_equal 34, io.pos + io.close + assert_equal true, io.closed? + + fd = IO.sysopen $mrbtest_io_wfname + io = IO.new fd + para1 = "#{'0' * 10}\n#{'1' * 10}\n\n" + text1 = io.gets("") + assert_equal para1, text1 + para2 = "#{'2' * 10}\n" + text2 = io.gets("") + assert_equal para2, text2 + io.close + io.closed? +end + +assert('IO.popen') do + io = IO.popen("ls") + assert_true io.close_on_exec? + assert_equal Fixnum, io.pid.class + ls = io.read + assert_equal ls.class, String + assert_include ls, 'AUTHORS' + assert_include ls, 'mrblib' + io.close + io.closed? +end + +assert('IO.read') do + # empty file + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + io.close + assert_equal "", IO.read($mrbtest_io_wfname) + assert_equal nil, IO.read($mrbtest_io_wfname, 1) + + # one byte file + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + io.write "123" + io.close + assert_equal "123", IO.read($mrbtest_io_wfname) + assert_equal "", IO.read($mrbtest_io_wfname, 0) + assert_equal "1", IO.read($mrbtest_io_wfname, 1) + assert_equal "", IO.read($mrbtest_io_wfname, 0, 10) + assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1) + assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1) + assert_equal "", IO.read($mrbtest_io_wfname, nil, 10) + assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10) +end + +assert('IO#fileno') do + fd = IO.sysopen $mrbtest_io_rfname + io = IO.new fd + assert_equal io.fileno, fd + assert_equal io.to_i, fd + io.close + io.closed? +end + +assert('IO#close_on_exec') do + fd = IO.sysopen $mrbtest_io_wfname, "w" + io = IO.new fd, "w" + begin + # IO.sysopen opens a file descripter with O_CLOEXEC flag. + assert_true io.close_on_exec? + rescue ScriptError + skip "IO\#close_on_exec is not implemented." + end + + io.close_on_exec = false + assert_equal(false, io.close_on_exec?) + io.close_on_exec = true + assert_equal(true, io.close_on_exec?) + io.close_on_exec = false + assert_equal(false, io.close_on_exec?) + + io.close + io.closed? + + # # Use below when IO.pipe is implemented. + # begin + # r, w = IO.pipe + # assert_equal(false, r.close_on_exec?) + # r.close_on_exec = true + # assert_equal(true, r.close_on_exec?) + # r.close_on_exec = false + # assert_equal(false, r.close_on_exec?) + # r.close_on_exec = true + # assert_equal(true, r.close_on_exec?) + + # assert_equal(false, w.close_on_exec?) + # w.close_on_exec = true + # assert_equal(true, w.close_on_exec?) + # w.close_on_exec = false + # assert_equal(false, w.close_on_exec?) + # w.close_on_exec = true + # assert_equal(true, w.close_on_exec?) + # ensure + # r.close unless r.closed? + # w.close unless w.closed? + # end +end + +assert('`cmd`') do + assert_equal `echo foo`, "foo\n" +end + +assert('IO TEST CLEANUP') do + assert_nil MRubyIOTestUtil.io_test_cleanup +end diff --git a/web/server/h2o/libh2o/deps/mruby-io/test/mruby_io_test.c b/web/server/h2o/libh2o/deps/mruby-io/test/mruby_io_test.c new file mode 100644 index 000000000..f58f9ff8c --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-io/test/mruby_io_test.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include "mruby/variable.h" + +static mrb_value +mrb_io_test_io_setup(mrb_state *mrb, mrb_value self) +{ + char rfname[] = "tmp.mruby-io-test.XXXXXXXX"; + char wfname[] = "tmp.mruby-io-test.XXXXXXXX"; + char symlinkname[] = "tmp.mruby-io-test.XXXXXXXX"; + char socketname[] = "tmp.mruby-io-test.XXXXXXXX"; + char msg[] = "mruby io test\n"; + mode_t mask; + int fd0, fd1, fd2, fd3; + FILE *fp; + struct sockaddr_un sun0; + + mask = umask(077); + fd0 = mkstemp(rfname); + fd1 = mkstemp(wfname); + fd2 = mkstemp(symlinkname); + fd3 = mkstemp(socketname); + if (fd0 == -1 || fd1 == -1 || fd2 == -1 || fd3 == -1) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't create temporary file"); + return mrb_nil_value(); + } + umask(mask); + + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"), mrb_str_new_cstr(mrb, rfname)); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"), mrb_str_new_cstr(mrb, wfname)); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"), mrb_str_new_cstr(mrb, symlinkname)); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_str_new_cstr(mrb, socketname)); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_str_new_cstr(mrb, msg)); + + fp = fopen(rfname, "w"); + if (fp == NULL) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file"); + return mrb_nil_value(); + } + fputs(msg, fp); + fclose(fp); + + fp = fopen(wfname, "w"); + if (fp == NULL) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file"); + return mrb_nil_value(); + } + fclose(fp); + + unlink(symlinkname); + close(fd2); + if (symlink("hoge", symlinkname) == -1) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link"); + } + + unlink(socketname); + close(fd3); + fd3 = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd3 == -1) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a socket"); + } + sun0.sun_family = AF_UNIX; + snprintf(sun0.sun_path, sizeof(sun0.sun_path), "%s", socketname); + if (bind(fd3, (struct sockaddr *)&sun0, sizeof(sun0)) == -1) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a socket bi"); + } + close(fd3); + + return mrb_true_value(); +} + +static mrb_value +mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self) +{ + mrb_value rfname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname")); + mrb_value wfname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname")); + mrb_value symlinkname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname")); + mrb_value socketname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname")); + + if (mrb_type(rfname) == MRB_TT_STRING) { + remove(RSTRING_PTR(rfname)); + } + if (mrb_type(wfname) == MRB_TT_STRING) { + remove(RSTRING_PTR(wfname)); + } + if (mrb_type(symlinkname) == MRB_TT_STRING) { + remove(RSTRING_PTR(symlinkname)); + } + if (mrb_type(socketname) == MRB_TT_STRING) { + remove(RSTRING_PTR(socketname)); + } + + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"), mrb_nil_value()); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"), mrb_nil_value()); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"), mrb_nil_value()); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_nil_value()); + mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_nil_value()); + + return mrb_nil_value(); +} + +static mrb_value +mrb_io_test_file_setup(mrb_state *mrb, mrb_value self) +{ + mrb_value ary = mrb_io_test_io_setup(mrb, self); + if (symlink("/usr/bin", "test-bin") == -1) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link"); + } + + return ary; +} + +static mrb_value +mrb_io_test_file_cleanup(mrb_state *mrb, mrb_value self) +{ + mrb_io_test_io_cleanup(mrb, self); + remove("test-bin"); + + return mrb_nil_value(); +} + +void +mrb_mruby_io_gem_test(mrb_state* mrb) +{ + struct RClass *io_test = mrb_define_module(mrb, "MRubyIOTestUtil"); + mrb_define_class_method(mrb, io_test, "io_test_setup", mrb_io_test_io_setup, MRB_ARGS_NONE()); + mrb_define_class_method(mrb, io_test, "io_test_cleanup", mrb_io_test_io_cleanup, MRB_ARGS_NONE()); + + mrb_define_class_method(mrb, io_test, "file_test_setup", mrb_io_test_file_setup, MRB_ARGS_NONE()); + mrb_define_class_method(mrb, io_test, "file_test_cleanup", mrb_io_test_file_cleanup, MRB_ARGS_NONE()); + +} -- cgit v1.2.3