lib/fileutils.rb in fileutils-1.4.1 vs lib/fileutils.rb in fileutils-1.5.0

- old
+ new

@@ -100,11 +100,11 @@ # This module has all methods of FileUtils module, but never changes # files/directories. This equates to passing the <tt>:noop</tt> and # <tt>:verbose</tt> flags to methods in FileUtils. # module FileUtils - VERSION = "1.4.1" + VERSION = "1.5.0" def self.private_module_function(name) #:nodoc: module_function name private_class_method name end @@ -206,11 +206,13 @@ def mkdir_p(list, mode: nil, noop: nil, verbose: nil) list = fu_list(list) fu_output_message "mkdir -p #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose return *list if noop - list.map {|path| remove_trailing_slash(path)}.each do |path| + list.each do |item| + path = remove_trailing_slash(item) + # optimize for the most common case begin fu_mkdir path, mode next rescue SystemCallError @@ -219,12 +221,13 @@ stack = [] until path == stack.last # dirname("/")=="/", dirname("C:/")=="C:/" stack.push path path = File.dirname(path) + break if File.directory?(path) end - stack.pop # root directory should exist + stack.pop if path == stack.last # root directory should exist stack.reverse_each do |dir| begin fu_mkdir dir, mode rescue SystemCallError raise unless File.directory?(dir) @@ -915,15 +918,12 @@ end end private_module_function :apply_mask def symbolic_modes_to_i(mode_sym, path) #:nodoc: - mode = if File::Stat === path - path.mode - else - File.stat(path).mode - end + path = File.stat(path) unless File::Stat === path + mode = path.mode mode_sym.split(/,/).inject(mode & 07777) do |current_mode, clause| target, *actions = clause.split(/([=+-])/) raise ArgumentError, "invalid file mode: #{mode_sym}" if actions.empty? target = 'a' if target.empty? user_mask = user_mask(target) @@ -936,11 +936,11 @@ when "w" mask | 0222 when "x" mask | 0111 when "X" - if FileTest.directory? path + if path.directory? mask | 0111 else mask end when "s" @@ -1288,11 +1288,11 @@ s and (s.mode & 0xF000 == S_IF_DOOR) end def entries opts = {} - opts[:encoding] = ::Encoding::UTF_8 if fu_windows? + opts[:encoding] = fu_windows? ? ::Encoding::UTF_8 : path.encoding files = if Dir.respond_to?(:children) Dir.children(path, **opts) else Dir.entries(path(), **opts) @@ -1343,10 +1343,11 @@ if symlink? File.lchmod mode, path() if have_lchmod? else File.chmod mode, path() end + rescue Errno::EOPNOTSUPP end def chown(uid, gid) if symlink? File.lchown uid, gid, path() if have_lchown? @@ -1437,11 +1438,11 @@ mode &= 01777 end if st.symlink? begin File.lchmod mode, path - rescue NotImplementedError + rescue NotImplementedError, Errno::EOPNOTSUPP end else File.chmod mode, path end end @@ -1557,11 +1558,19 @@ end def join(dir, base) return File.path(dir) if not base or base == '.' return File.path(base) if not dir or dir == '.' - File.join(dir, base) + begin + File.join(dir, base) + rescue EncodingError + if fu_windows? + File.join(dir.encode(::Encoding::UTF_8), base.encode(::Encoding::UTF_8)) + else + raise + end + end end if File::ALT_SEPARATOR DIRECTORY_TERM = "(?=[/#{Regexp.quote(File::ALT_SEPARATOR)}]|\\z)" else @@ -1612,10 +1621,10 @@ end private_module_function :fu_same? def fu_output_message(msg) #:nodoc: output = @fileutils_output if defined?(@fileutils_output) - output ||= $stderr + output ||= $stdout if defined?(@fileutils_label) msg = @fileutils_label + msg end output.puts msg end