@@ -46,8 +46,7 @@ class RbPhoto
46
46
47
47
def initialize
48
48
@args = RbPhoto ::Args . new
49
- @fileopts = { :force => @args [ 'force' ] ,
50
- :verbose => @args [ 'verbose' ] ,
49
+ @fileopts = { :verbose => @args [ 'verbose' ] ,
51
50
:noop => @args [ 'no_act' ] }
52
51
@postfix = '-' + ENV [ 'USER' ]
53
52
if ( @args [ 'version' ] )
@@ -77,27 +76,43 @@ class RbPhoto
77
76
78
77
def copy
79
78
@target . each do |file |
80
- if ( @args [ 'without_rename' ] )
81
- target_file = file
82
- else
83
- target_file = self . rename ( file )
84
- end
85
-
86
- target_dir = File . dirname ( target_file )
87
- if ( ! File . directory? ( target_dir ) )
88
- FileUtils . makedirs ( target_dir , @fileopts )
89
- end
90
-
91
- begin
92
- if ( @args [ 'move' ] )
93
- FileUtils . mv ( file , target_file , @fileopts )
94
- else
95
- FileUtils . cp ( file , target_file , @fileopts )
96
- end
97
- FileUtils . chmod ( 0444 , target_file , @fileopts )
98
- rescue => e
99
- STDERR . puts "ERROR: execution failed: #{ e . to_s } "
100
- end
79
+ if ( @args [ 'without_rename' ] )
80
+ target_file = file
81
+ else
82
+ target_file = self . rename ( file )
83
+ end
84
+
85
+ target_dir = File . dirname ( target_file )
86
+ if ( ! File . directory? ( target_dir ) )
87
+ begin
88
+ FileUtils . makedirs ( target_dir , @fileopts )
89
+ rescue => e
90
+ STDERR . puts "ERROR: mkdir failed: #{ e . to_s } "
91
+ end
92
+ end
93
+
94
+ if ( @args [ 'move' ] )
95
+ begin
96
+ FileUtils . mv ( file , target_file , @fileopts )
97
+ rescue => e
98
+ if ( e . is_a? ( Errno ::EACCES ) && @args [ 'force' ] && !File . writable? ( target_file ) )
99
+ FileUtils . chmod ( 0644 , target_file , @fileopts )
100
+ retry
101
+ end
102
+ STDERR . puts "ERROR: mv failed: #{ e . to_s } "
103
+ end
104
+ else
105
+ begin
106
+ FileUtils . cp ( file , target_file , @fileopts )
107
+ rescue => e
108
+ STDERR . puts "ERROR: cp failed: #{ e . to_s } "
109
+ end
110
+ end
111
+ begin
112
+ FileUtils . chmod ( 0444 , target_file , @fileopts )
113
+ rescue => e
114
+ STDERR . puts "ERROR: chmod failed: #{ e . to_s } "
115
+ end
101
116
end
102
117
end
103
118
0 commit comments