Skip to content

Commit

Permalink
* lib/fileutils.rb (mkdir): remove trailing `/' from pathes.
Browse files Browse the repository at this point in the history
* lib/fileutils.rb (rmdir): ditto. [ruby-dev:22238]
* lib/fileutils.rb (rmdir_r): ditto.
* lib/fileutils.rb (fu_copy_dir): check if it is a directory after mkdir(2).


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5176 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
aamine committed Dec 12, 2003
1 parent 6de4020 commit 232b4c2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Fri Dec 12 19:33:06 2003 Minero Aoki <[email protected]>

* lib/fileutils.rb (mkdir): remove trailing `/' from pathes.

* lib/fileutils.rb (rmdir): ditto. [ruby-dev:22238]

* lib/fileutils.rb (rmdir_r): ditto.

* lib/fileutils.rb (fu_copy_dir): check if it is a directory after
mkdir(2).

Fri Dec 12 06:06:09 2003 Nobuyoshi Nakada <[email protected]>

* eval.c (proc_invoke): fix class name in warning message for
Expand Down
53 changes: 25 additions & 28 deletions lib/fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def mkdir(list, options = {})

mode = options[:mode] || (0777 & ~File.umask)
list.each do |dir|
Dir.mkdir dir, mode
Dir.mkdir dir.sub(%r</\z>, ''), mode
end
end

Expand Down Expand Up @@ -209,7 +209,7 @@ def rmdir(list, options = {})
return if options[:noop]

list.each do |dir|
Dir.rmdir dir
Dir.rmdir dir.sub(%r</\z>, '')
end
end

Expand Down Expand Up @@ -357,8 +357,11 @@ def cp_r(src, dest, options = {})

def fu_copy_dir(src, dest, rel, preserve) #:nodoc:
fu_preserve_attr(preserve, "#{src}/#{rel}", "#{dest}/#{rel}") {|s,d|
dir = File.expand_path(d) # to remove '/./'
Dir.mkdir dir unless File.directory?(dir)
begin
Dir.mkdir File.expand_path(d)
rescue => err
raise unless File.directory?(d)
end
}
Dir.entries("#{src}/#{rel}").each do |fname|
if File.directory?(File.join(src,rel,fname))
Expand Down Expand Up @@ -585,7 +588,7 @@ def remove_dir(dir, force = false) #:nodoc:
end
end
begin
Dir.rmdir dir
Dir.rmdir dir.sub(%r</\z>, '')
rescue Errno::ENOENT
raise unless force
end
Expand All @@ -601,9 +604,10 @@ def remove_dir(dir, force = false) #:nodoc:
def compare_file(a, b)
return false unless File.size(a) == File.size(b)
File.open(a, 'rb') {|fa|
File.open(b, 'rb') {|fb|
File.open(b, 'rb') {|fb|
return compare_stream(fa, fb)
} }
}
}
end

alias identical? compare_file
Expand Down Expand Up @@ -644,11 +648,11 @@ def install(src, dest, options = {})

fu_each_src_dest(src, dest) do |s,d|
unless File.exist?(d) and compare_file(s,d)
remove_file d, true
st = File.stat(s) if options[:preserve]
copy_file s, d
File.utime st.atime, st.mtime, d if options[:preserve]
File.chmod options[:mode], d if options[:mode]
remove_file d, true
st = File.stat(s) if options[:preserve]
copy_file s, d
File.utime st.atime, st.mtime, d if options[:preserve]
File.chmod options[:mode], d if options[:mode]
end
end
end
Expand Down Expand Up @@ -694,7 +698,7 @@ def touch(list, options = {})
File.utime(t, t, fname)
rescue Errno::ENOENT
File.open(fname, 'a') {
;
;
}
end
end
Expand Down Expand Up @@ -722,23 +726,16 @@ def fu_each_src_dest(src, dest)
end

def fu_each_src_dest0(src, dest)
unless src.is_a?(Array)
yield src.to_str, fu_dest_filename(src.to_str, dest.to_str)
else
dir = dest.to_str
#raise ArgumentError, "not a directory: #{dir}" unless File.directory?(dir)
dir += (dir[-1,1] == '/') ? '' : '/'
src.map {|s| s.to_str }.each do |fname|
yield fname, dir + File.basename(fname)
if src.is_a?(Array)
src.each do |s|
yield s.to_str, File.join(dest, File.basename(s))
end
end
end

def fu_dest_filename(src, dest)
if File.directory?(dest)
(dest[-1,1] == '/' ? dest : dest + '/') + File.basename(src)
else
dest
if File.directory?(dest)
yield src.to_str, File.join(dest, File.basename(src))
else
yield src.to_str, dest.to_str
end
end
end

Expand Down
19 changes: 17 additions & 2 deletions test/fileutils/test_fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ def test_cp
cp fname, 'tmp'
assert_same_file fname, 'tmp/' + File.basename(fname)

cp fname, 'tmp/'
assert_same_file fname, 'tmp/' + File.basename(fname)

cp fname, 'tmp/preserve', :preserve => true
assert_same_file fname, 'tmp/preserve'
a = File.stat(fname)
Expand Down Expand Up @@ -221,10 +224,17 @@ def test_cp_r
end

def test_mv
mkdir 'tmp/dest'
TARGETS.each do |fname|
cp fname, 'tmp/mvsrc'
mv 'tmp/mvsrc', 'tmp/mvdest'
assert_same_file fname, 'tmp/mvdest'

mv 'tmp/mvdest', 'tmp/dest/'
assert_same_file fname, 'tmp/dest/mvdest'

mv 'tmp/dest/mvdest', 'tmp'
assert_same_file fname, 'tmp/mvdest'
end

# src==dest (1) same path
Expand Down Expand Up @@ -474,6 +484,10 @@ def test_mkdir
assert_directory 'tmpdatadir'
Dir.rmdir 'tmpdatadir'

mkdir 'tmpdatadir/'
assert_directory 'tmpdatadir'
Dir.rmdir 'tmpdatadir'

mkdir 'tmp/mkdirdest'
assert_directory 'tmp/mkdirdest'
Dir.rmdir 'tmp/mkdirdest'
Expand All @@ -485,7 +499,8 @@ def test_mkdir

# pathname
assert_nothing_raised {
mkdir 'tmp/tmpdirtmp'
mkdir Pathname.new('tmp/tmpdirtmp')
mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
}
end

Expand Down Expand Up @@ -518,7 +533,7 @@ def test_mkdir_p
end
rm_rf 'tmpdir'
dirs.each do |d|
mkdir_p File.expand_path(d)
mkdir_p "#{Dir.pwd}/#{d}"
assert_directory d
end
rm_rf 'tmpdir'
Expand Down

0 comments on commit 232b4c2

Please sign in to comment.