Skip to content

Commit 958a4ed

Browse files
committed
Keep the trailing slash if the user wishes
1 parent 3590095 commit 958a4ed

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/rex/file.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def self.normalize_win_path(*strs)
3535
# Filter out double slashes
3636
s = s.gsub(/\\\\/, '\\') while s.index('\\\\')
3737

38+
# Keep the trailing slash if exists
39+
trailing_s = ('\\' if s =~ /\\$/) || ''
40+
3841
# Check the items (fie/dir) individually
3942
s = s.split(/\\/)
4043

4144
# Parse the path prefix
4245
prefix = (s[0] || '').gsub(/[\*<>\?\/]/, '')
43-
if prefix =~ /^\w:$/ and s.length == 1
44-
prefix += '\\'
45-
end
4646

4747
# Delete the original prefix. We want the new one later.
4848
s.delete_at(0)
@@ -55,6 +55,9 @@ def self.normalize_win_path(*strs)
5555

5656
# And then safely join the items
5757
s *= '\\'
58+
59+
# Add the trailing slash back if exists
60+
s << trailing_s
5861
end
5962

6063
#

spec/lib/rex/file_utils_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
described_class.normalize_win_path('\\temp').should eq("\\temp")
1818
end
1919

20+
it "should keep the trailing slash if exists" do
21+
described_class.normalize_win_path('/', 'test', 'me\\').should eq("\\test\\me\\")
22+
described_class.normalize_win_path('\\temp\\').should eq("\\temp\\")
23+
end
24+
2025
it "should convert a path without reserved characters" do
2126
described_class.normalize_win_path('C:\\', 'Windows:').should eq("C:\\Windows")
2227
described_class.normalize_win_path('C:\\Windows???\\test').should eq("C:\\Windows\\test")
@@ -26,6 +31,7 @@
2631
described_class.normalize_win_path('C:\\\\\\', 'Windows').should eq("C:\\Windows")
2732
described_class.normalize_win_path('C:\\\\\\Hello World\\\\whatever.txt').should eq("C:\\Hello World\\whatever.txt")
2833
described_class.normalize_win_path('C:\\\\').should eq("C:\\")
34+
described_class.normalize_win_path('\\test\\\\test\\\\').should eq("\\test\\test\\")
2935
end
3036
end
3137

0 commit comments

Comments
 (0)