@@ -112,6 +112,13 @@ struct work_data {
112112 struct entry_data ed ;
113113};
114114
115+ #define print_error_and_goto (msg , path , label ) \
116+ const int err = errno; \
117+ fprintf(stderr, "Error: %s \"%s\": %s (%d)\n", \
118+ msg, path, strerror(err), err); \
119+ rc = 1; \
120+ goto label
121+
115122/* copy a single file in a single thread */
116123/* TODO: if file is too big, split up copy */
117124static int cpr_file (QPTPool_t * ctx , const size_t id , void * data , void * args ) {
@@ -127,39 +134,23 @@ static int cpr_file(QPTPool_t *ctx, const size_t id, void *data, void *args) {
127134
128135 struct stat st ;
129136 if (lstat (work -> name , & st ) != 0 ) {
130- const int err = errno ;
131- fprintf (stderr , "Error: Could not lstat file \"%s\": %s (%d)\n" ,
132- work -> name , strerror (err ), err );
133- rc = 1 ;
134- goto cleanup ;
137+ print_error_and_goto ("Could not lstat file" , work -> name , cleanup );
135138 }
136139
137140 const int src_fd = open (work -> name , O_RDONLY );
138141 if (src_fd < 0 ) {
139- const int err = errno ;
140- fprintf (stderr , "Error: Could not open file \"%s\": %s (%d)\n" ,
141- work -> name , strerror (err ), err );
142- rc = 1 ;
143- goto cleanup ;
142+ print_error_and_goto ("Could not open file" , work -> name , cleanup );
144143 }
145144
146145 str_t dst = create_dst_name (in , work );
147146
148147 const int dst_fd = open (dst .data , O_CREAT | O_WRONLY , st .st_mode );
149148 if (dst_fd < 0 ) {
150- const int err = errno ;
151- fprintf (stderr , "Error: Could not open file \"%s\": %s (%d)\n" ,
152- dst .data , strerror (err ), err );
153- rc = 1 ;
154- goto free_name ;
149+ print_error_and_goto ("Could not opent file" , dst .data , free_name );
155150 }
156151
157152 if (copyfd (src_fd , 0 , dst_fd , 0 , st .st_size ) != st .st_size ) {
158- const int err = errno ;
159- fprintf (stderr , "Error: Could not copy file \"%s\": %s (%d)\n" ,
160- work -> name , strerror (err ), err );
161- rc = 1 ;
162- goto free_name ;
153+ print_error_and_goto ("Could not copy file" , work -> name , free_name );
163154 }
164155
165156 rc = set_xattrs (dst .data , & ed -> xattrs );
@@ -182,36 +173,20 @@ static int cpr_file(QPTPool_t *ctx, const size_t id, void *data, void *args) {
182173static int cpr_link (struct work * work , struct entry_data * ed , struct input * in ) {
183174 int rc = 0 ;
184175
185- struct stat st ;
186- if (lstat (work -> name , & st ) != 0 ) {
187- const int err = errno ;
188- fprintf (stderr , "Error: Could not lstat link \"%s\": %s (%d)\n" ,
189- work -> name , strerror (err ), err );
190- rc = 1 ;
191- goto cleanup ;
192- }
193-
194176 /* need to give users ability to force overwriting of links */
195177
196178 str_t dst = create_dst_name (in , work );
197179
198180 /* see man 2 readlink */
199181 const ssize_t len = readlink (work -> name , ed -> linkname , sizeof (ed -> linkname ));
200182 if ((len < 0 ) || (len == sizeof (ed -> linkname ))) {
201- const int err = errno ;
202- fprintf (stderr , "Error: Could not readlink \"%s\": %s (%d)\n" ,
203- work -> name , strerror (err ), err );
204- rc = 1 ;
205- goto cleanup ;
183+ print_error_and_goto ("Could not readlink" , work -> name , cleanup );
206184 }
207185
208186 ed -> linkname [len ] = '\0' ;
209187
210188 if (symlink (ed -> linkname , dst .data ) != 0 ) {
211- const int err = errno ;
212- fprintf (stderr , "Error: Could not create link \"%s\": %s (%d)\n" ,
213- dst .data , strerror (err ), err );
214- rc = 1 ;
189+ print_error_and_goto ("Could not create link" , dst .data , cleanup );
215190 }
216191
217192 cleanup :
@@ -261,20 +236,12 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
261236
262237 DIR * dir = opendir (work -> name );
263238 if (!dir ) {
264- const int err = errno ;
265- fprintf (stderr , "Error: Could not open directory \"%s\": %s (%d)\n" ,
266- work -> name , strerror (err ), err );
267- rc = 1 ;
268- goto cleanup ;
239+ print_error_and_goto ("Could not open_directory" , work -> name , cleanup );
269240 }
270241
271242 struct stat st ;
272243 if (lstat (work -> name , & st ) != 0 ) {
273- const int err = errno ;
274- fprintf (stderr , "Error: Could not lstat directory \"%s\": %s (%d)\n" ,
275- work -> name , strerror (err ), err );
276- rc = 1 ;
277- goto cleanup ;
244+ print_error_and_goto ("Could not lstat directory" , work -> name , cleanup );
278245 }
279246
280247 str_t dst = create_dst_name (in , work );
@@ -291,11 +258,7 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
291258 }
292259
293260 if (chown (dst .data , st .st_uid , st .st_gid ) != 0 ) {
294- const int err = errno ;
295- fprintf (stderr , "Error: Could not chown directory \"%s\": %s (%d)\n" ,
296- dst .data , strerror (err ), err );
297- rc = 1 ;
298- goto cleanup ;
261+ print_error_and_goto ("Could not chown directory" , dst .data , cleanup );
299262 }
300263
301264 struct QPTPool_vals qptp_vals = {
0 commit comments