diff --git a/fileutils.gemspec b/fileutils.gemspec index a72b878..7212883 100644 --- a/fileutils.gemspec +++ b/fileutils.gemspec @@ -1,13 +1,20 @@ # frozen_string_literal: true +begin + require_relative "lib/fileutils/version" +rescue LoadError + # for Ruby core repository + require_relative "version" +end + Gem::Specification.new do |s| s.name = "fileutils" - s.version = '1.1.0' + s.version = FileUtils::VERSION s.summary = "Several file utility methods for copying, moving, removing, etc." s.description = "Several file utility methods for copying, moving, removing, etc." s.require_path = %w{lib} - s.files = [".gitignore", ".travis.yml", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "fileutils.gemspec", "lib/fileutils.rb"] + s.files = [".gitignore", ".travis.yml", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "fileutils.gemspec", "lib/fileutils.rb", "lib/fileutils/version.rb"] s.required_ruby_version = ">= 2.3.0" s.authors = ["Minero Aoki"] diff --git a/lib/fileutils.rb b/lib/fileutils.rb index 7cbc6f4..6332fcd 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -1,4 +1,13 @@ # frozen_string_literal: true + +begin + require 'rbconfig' +rescue LoadError + # for make mjit-headers +end + +require "fileutils/version" + # # = fileutils.rb # @@ -56,7 +65,7 @@ # # There are some `low level' methods, which do not accept any option: # -# FileUtils.copy_entry(src, dest, preserve = false, dereference = false) +# FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) # FileUtils.copy_file(src, dest, preserve = false, dereference = true) # FileUtils.copy_stream(srcstream, deststream) # FileUtils.remove_entry(path, force = false) @@ -84,13 +93,8 @@ # files/directories. This equates to passing the :noop and # :verbose flags to methods in FileUtils. # - -require 'rbconfig' - module FileUtils - VERSION = "1.1.0" - def self.private_module_function(name) #:nodoc: module_function name private_class_method name @@ -519,8 +523,6 @@ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil) if destent.exist? if destent.directory? raise Errno::EEXIST, d - else - destent.remove_file if rename_cannot_overwrite_file? end end begin @@ -543,11 +545,6 @@ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil) alias move mv module_function :move - def rename_cannot_overwrite_file? #:nodoc: - /emx/ =~ RbConfig::CONFIG['host_os'] - end - private_module_function :rename_cannot_overwrite_file? - # # Remove file(s) specified in +list+. This method cannot remove directories. # All StandardErrors are ignored when the :force option is set. @@ -698,7 +695,7 @@ def remove_entry_secure(path, force = false) f.chown euid, -1 f.chmod 0700 } - rescue EISDIR # JRuby in non-native mode can't open files as dirs + rescue Errno::EISDIR # JRuby in non-native mode can't open files as dirs File.lstat(dot_file).tap {|fstat| unless fu_stat_identical_entry?(st, fstat) # symlink (TOC-to-TOU attack?) @@ -1148,8 +1145,11 @@ def touch(list, noop: nil, verbose: nil, mtime: nil, nocreate: nil) module StreamUtils_ private - def fu_windows? - /mswin|mingw|bccwin|emx/ =~ RbConfig::CONFIG['host_os'] + case (defined?(::RbConfig) ? ::RbConfig::CONFIG['host_os'] : ::RUBY_PLATFORM) + when /mswin|mingw/ + def fu_windows?; true end + else + def fu_windows?; false end end def fu_copy_stream0(src, dest, blksize = nil) #:nodoc: @@ -1274,8 +1274,7 @@ def door? def entries opts = {} opts[:encoding] = ::Encoding::UTF_8 if fu_windows? - Dir.entries(path(), opts)\ - .reject {|n| n == '.' or n == '..' }\ + Dir.children(path, opts)\ .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) } end @@ -1321,6 +1320,7 @@ def chmod(mode) else File.chmod mode, path() end + rescue Errno::EOPNOTSUPP end def chown(uid, gid) @@ -1412,7 +1412,7 @@ def copy_metadata(path) if st.symlink? begin File.lchmod mode, path - rescue NotImplementedError + rescue NotImplementedError, Errno::EOPNOTSUPP end else File.chmod mode, path diff --git a/lib/fileutils/version.rb b/lib/fileutils/version.rb new file mode 100644 index 0000000..e82734d --- /dev/null +++ b/lib/fileutils/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module FileUtils + VERSION = "1.1.0" +end diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index f260000..c81f5b4 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -740,6 +740,17 @@ def test_remove_entry_secure remove_entry_secure 'tmp/tmpdir/c', true assert_file_not_exist 'tmp/tmpdir/a' assert_file_not_exist 'tmp/tmpdir/c' + + unless root_in_posix? + File.chmod(01777, 'tmp/tmpdir') + if File.sticky?('tmp/tmpdir') + Dir.mkdir 'tmp/tmpdir/d', 0 + assert_raise(Errno::EACCES) {remove_entry_secure 'tmp/tmpdir/d'} + File.chmod 0777, 'tmp/tmpdir/d' + Dir.rmdir 'tmp/tmpdir/d' + end + end + Dir.rmdir 'tmp/tmpdir' end