Module: Rex::FileUtils

Defined in:
lib/rex/file.rb

Overview

This class provides helper methods for dealing with files that are not supplied by the standard ruby API.

Defined Under Namespace

Classes: UnitTest

Class Method Summary collapse

Class Method Details

.clean_path(old) ⇒ Object

This methods cleans the supplied path of directory traversal sequences It must accept path/with/..a/folder../starting/or/ending/in/two/dots but clean ../something as well as path/with/..traversal



21
22
23
24
25
26
27
28
29
# File 'lib/rex/file.rb', line 21

def self.clean_path(old)
	path = old
	while(path.index(/\/..\/|\/..\\|\\..\\|\\..\/|\A..\\|\A..\//) != nil)
		path.gsub!(/\A..\\|\A..\//,'') #eliminate starting ..\ or ../
		path.gsub!(/\/..\/|\/..\\/,'/') #clean linux style
		path.gsub!(/\\..\\|\\..\//,'\\') #clean windows style
	end
	path
end

.find_full_path(file_name) ⇒ Object

This method searches the PATH environment variable for a fully qualified path to the supplied file name.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/file.rb', line 35

def self.find_full_path(file_name)

	# Check for the absolute fast first
	if (file_name[0,1] == "/" and ::File.exists?(file_name) and ::File::Stat.new(file_name))
		return file_name
	end

	path = Rex::Compat.getenv('PATH')
	if (path)
		path.split(::File::PATH_SEPARATOR).each { |base|
			begin
				# Deal with Windows paths surrounded by quotes.  Prevents
				# silliness like trying to look for
				# '"C:\\framework\\nmap"\\nmap.exe' which will always fail.
				base = $1 if base =~ /^"(.*)"$/
				path = base + ::File::SEPARATOR + file_name
				if (::File::Stat.new(path) and not ::File.directory?(path))
					return path
				end
			rescue
			end
		}
	end
	return nil
end