Escape shellwords for file operations
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
module Dunlop::FileGrep
|
||||
|
||||
def self.egrep( input_filename, output_filename, regexp )
|
||||
%x[egrep '#{regexp}' #{input_filename} > #{output_filename}]
|
||||
%x[egrep '#{regexp}' #{Shellwords.escape(input_filename)} > #{Shellwords.escape(output_filename)}]
|
||||
end
|
||||
|
||||
def self.egrep_v( input_filename, output_filename, regexp )
|
||||
%x[egrep -v '#{regexp}' #{input_filename} > #{output_filename}]
|
||||
%x[egrep -v '#{regexp}' #{Shellwords.escape(input_filename)} > #{Shellwords.escape(output_filename)}]
|
||||
end
|
||||
|
||||
def self.select(input_filename, regexp_string)
|
||||
|
||||
@@ -4,20 +4,20 @@ module Dunlop::FileSed
|
||||
expression_list = Array.wrap(sed_expressions).map { |exp| "-e '#{exp}'" }.join(' ')
|
||||
if /Darwin/i =~ %x[uname]
|
||||
#BSD
|
||||
%x[ LC_CTYPE=C LANG=C gsed -i'' -r #{expression_list} #{filename} ]
|
||||
%x[ LC_CTYPE=C LANG=C gsed -i'' -r #{expression_list} #{Shellwords.escape(filename)} ]
|
||||
raise 'error in sed command line' unless $?.exitstatus == 0
|
||||
else
|
||||
#GNU
|
||||
%x[ LC_CTYPE=C LANG=C sed -i'' -r #{expression_list} #{filename} ]
|
||||
%x[ LC_CTYPE=C LANG=C sed -i'' -r #{expression_list} #{Shellwords.escape(filename)} ]
|
||||
raise 'error in sed command line' unless $?.exitstatus == 0
|
||||
end
|
||||
end
|
||||
|
||||
def self.fix_line_endings(filename)
|
||||
return if filename.to_s.end_with?('.xlsx')
|
||||
res = %x[dos2unix -f #{filename} > /dev/null 2>&1]
|
||||
res = %x[dos2unix -f #{Shellwords.escape(filename)} > /dev/null 2>&1]
|
||||
raise "error in dos2unix command line (#{res})" unless $?.exitstatus == 0
|
||||
res = %x[dos2unix -f -c mac #{filename} > /dev/null 2>&1]
|
||||
res = %x[dos2unix -f -c mac #{Shellwords.escape(filename)} > /dev/null 2>&1]
|
||||
raise "error in dos2unix command line mac (#{res})" unless $?.exitstatus == 0
|
||||
end
|
||||
|
||||
|
||||
@@ -4,12 +4,12 @@ module Dunlop::FileZip
|
||||
case self.mime_type(zipped_filename)
|
||||
when "application/zip"
|
||||
if zipped_filename.to_s.end_with?('.xlsx') # .xlsx IS a zipfile # .to_s is because it can be a Pathname
|
||||
%x[ cp #{zipped_filename} #{unzipped_filename} ]
|
||||
%x[ cp #{Shellwords.escape(zipped_filename)} #{Shellwords.escape(unzipped_filename)} ]
|
||||
else
|
||||
%x[ unzip -p #{zipped_filename} > #{unzipped_filename} ]
|
||||
%x[ unzip -p #{Shellwords.escape(zipped_filename)} > #{Shellwords.escape(unzipped_filename)} ]
|
||||
end
|
||||
when "application/x-gzip"
|
||||
%x[ gzip -c -d #{zipped_filename} > #{unzipped_filename} ]
|
||||
%x[ gzip -c -d #{Shellwords.escape(zipped_filename)} > #{Shellwords.escape(unzipped_filename)} ]
|
||||
when "text/plain"
|
||||
FileUtils.cp(zipped_filename, unzipped_filename)
|
||||
else
|
||||
@@ -20,7 +20,7 @@ module Dunlop::FileZip
|
||||
def self.unzip_to_directory_and_junk_paths(zipped_filename, destination_directory)
|
||||
case self.mime_type(zipped_filename)
|
||||
when "application/zip"
|
||||
%x[ unzip -j #{zipped_filename} -d #{destination_directory} ]
|
||||
%x[ unzip -j #{Shellwords.escape(zipped_filename)} -d #{Shellwords.escape(destination_directory)} ]
|
||||
else
|
||||
raise "unsupported mime-type"
|
||||
end
|
||||
@@ -28,9 +28,9 @@ module Dunlop::FileZip
|
||||
|
||||
def self.mime_type(filename)
|
||||
command = if /Darwin/i =~ %x[uname]
|
||||
"file -bI #{filename}"
|
||||
"file -bI #{Shellwords.escape(filename)}"
|
||||
else
|
||||
"file -bi #{filename}"
|
||||
"file -bi #{Shellwords.escape(filename)}"
|
||||
end
|
||||
|
||||
case %x[#{command}]
|
||||
@@ -45,7 +45,7 @@ module Dunlop::FileZip
|
||||
|
||||
def self.gzip(unzipped_filename, gzipped_filename)
|
||||
return if mime_type(unzipped_filename) == "application/x-gzip" #TODO: maybe the application using is is expecti a copy at the target location in this case?
|
||||
%x[cat #{unzipped_filename} | gzip -c > #{gzipped_filename}]
|
||||
%x[cat #{Shellwords.escape(unzipped_filename)} | gzip -c > #{Shellwords.escape(gzipped_filename)}]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user