Skip to content
This repository
Browse code

Escape filenames when using the shell.

  • Loading branch information...
commit e84130bf15dde42a0fa4647215b8916aa2c488eb 1 parent 9b0547a
janfri authored February 10, 2011
6  Changelog
... ...
@@ -1,6 +1,10 @@
  1
+Version 1.1.0
  2
+- Escaping filenames in shell commands
  3
+  Thanks to Michael Hoy for the hint and implementing a patch which was
  4
+  the base for this fix.
1 5
 Version 1.0.2
2 6
 - Fixing warings
3  
-  thanks to Peter-Hinrich Krogmann for the hint.
  7
+  Thanks to Peter-Hinrich Krogmann for the hint.
4 8
 Version 1.0.1
5 9
 - Fixing bug [#22726]
6 10
   Making MiniExiftool::Error public.
7  lib/mini_exiftool.rb
@@ -16,6 +16,7 @@
16 16
 require 'tempfile'
17 17
 require 'pstore'
18 18
 require 'set'
  19
+require 'shellwords'
19 20
 
20 21
 # Simple OO access to the Exiftool command-line application.
21 22
 class MiniExiftool
@@ -29,7 +30,7 @@ class MiniExiftool
29 30
   attr_reader :filename
30 31
   attr_accessor :numerical, :composite, :convert_encoding, :errors, :timestamps
31 32
 
32  
-  VERSION = '1.0.2'
  33
+  VERSION = '1.1.0'
33 34
 
34 35
   # +opts+ support at the moment
35 36
   # * <code>:numerical</code> for numerical values, default is +false+
@@ -80,7 +81,7 @@ def load filename
80 81
     opt_params << (@numerical ? '-n ' : '')
81 82
     opt_params << (@composite ? '' : '-e ')
82 83
     opt_params << (@convert_encoding ? '-L ' : '')
83  
-    cmd = %Q(#@@cmd -q -q -s -t #{opt_params} #{@@sep_op} "#{filename}")
  84
+    cmd = %Q(#@@cmd -q -q -s -t #{opt_params} #{@@sep_op} #{Shellwords.escape(filename)})
84 85
     if run(cmd)
85 86
       parse_output
86 87
     else
@@ -156,7 +157,7 @@ def save
156 157
       opt_params = ''
157 158
       opt_params << (arr_val.detect {|x| x.kind_of?(Numeric)} ? '-n ' : '')
158 159
       opt_params << (@convert_encoding ? '-L ' : '')
159  
-      cmd = %Q(#@@cmd -q -P -overwrite_original #{opt_params} #{tag_params} "#{temp_filename}")
  160
+      cmd = %Q(#@@cmd -q -P -overwrite_original #{opt_params} #{tag_params} #{temp_filename})
160 161
       result = run(cmd)
161 162
       unless result
162 163
         all_ok = false
BIN  test/data/test 36"Bench.jpg
25  test/test_escape_filename.rb
... ...
@@ -0,0 +1,25 @@
  1
+require 'helpers_for_test'
  2
+
  3
+class TestEscapeFilename < TestCase
  4
+
  5
+  def setup
  6
+    @temp_file = Tempfile.new('test')
  7
+    @temp_file.close
  8
+    @temp_filename = @temp_file.path
  9
+    @org_filename = File.dirname(__FILE__) + '/data/test 36"Bench.jpg'
  10
+    FileUtils.cp(@org_filename, @temp_filename)
  11
+    @mini_exiftool = MiniExiftool.new @temp_filename
  12
+  end
  13
+
  14
+  def test_access
  15
+    assert_equal '36" Bench', @mini_exiftool['description']
  16
+  end
  17
+
  18
+  def test_save
  19
+    desc = 'another bench'
  20
+    @mini_exiftool.description = desc
  21
+    assert @mini_exiftool.save
  22
+    assert_equal desc, @mini_exiftool.description
  23
+  end
  24
+
  25
+end

0 notes on commit e84130b

Please sign in to comment.
Something went wrong with that request. Please try again.