@@ -226,7 +226,6 @@ static int replace_running(sr_conn_ctx_t *conn, sr_session_ctx_t *sess,
226226static int copy (const char * src , const char * dst , const char * remote_user )
227227{
228228 struct infix_ds * srcds = NULL , * dstds = NULL ;
229- char temp_file [20 ] = "/tmp/copy.XXXXXX" ;
230229 const char * tmpfn = NULL ;
231230 sr_session_ctx_t * sess ;
232231 const char * fn = NULL ;
@@ -253,9 +252,8 @@ static int copy(const char *src, const char *dst, const char *remote_user)
253252
254253 user = getuser ();
255254
256- /* 1. Regular ds copy */
255+ /* 1. Regular ds to ds copy */
257256 if (srcds && dstds ) {
258- /* Ensure the dst ds is writable */
259257 if (!dstds -> rw ) {
260258 fprintf (stderr , ERRMSG "not possible to write to \"%s\", skipping.\n" , dst );
261259 rc = 1 ;
@@ -311,12 +309,14 @@ static int copy(const char *src, const char *dst, const char *remote_user)
311309
312310 if (srcds ) {
313311 /* 2. Export from a datastore somewhere else */
312+
314313 if (strstr (dst , "://" )) {
315314 if (srcds -> path )
316315 fn = srcds -> path ;
317316 else {
318317 snprintf (adjust , sizeof (adjust ), "/tmp/%s.cfg" , srcds -> name );
319318 fn = tmpfn = adjust ;
319+ remove (tmpfn );
320320 rc = systemf ("sysrepocfg -d %s -X%s -f json" , srcds -> sysrepocfg , fn );
321321 }
322322
@@ -357,6 +357,8 @@ static int copy(const char *src, const char *dst, const char *remote_user)
357357 else
358358 set_owner (fn , user );
359359 } else if (dstds ) {
360+ /* 3. Import from somewhere to a datastore */
361+
360362 if (!dstds -> sysrepocfg ) {
361363 fprintf (stderr , ERRMSG "not possible to import to this datastore.\n" );
362364 rc = 1 ;
@@ -367,10 +369,16 @@ static int copy(const char *src, const char *dst, const char *remote_user)
367369 goto err ;
368370 }
369371
370- /* 3. Import from somewhere to a datastore */
371372 if (strstr (src , "://" )) {
372- tmpfn = mktemp (temp_file );
373- fn = tmpfn ;
373+ fn = basenm (src );
374+ if (fn [0 ] == 0 ) {
375+ fprintf (stderr , ERRMSG "missing filename in sorce URI.\n" );
376+ rc = 1 ;
377+ goto err ;
378+ }
379+ snprintf (adjust , sizeof (adjust ), "/tmp/%s" , fn );
380+ fn = tmpfn = adjust ;
381+ remove (tmpfn );
374382 } else {
375383 fn = cfg_adjust (src , NULL , adjust , sizeof (adjust ), sanitize );
376384 if (!fn ) {
@@ -385,7 +393,6 @@ static int copy(const char *src, const char *dst, const char *remote_user)
385393 if (rc ) {
386394 fprintf (stderr , ERRMSG "failed downloading %s" , src );
387395 } else {
388- /* Use replace_config() for running-config (triggers callbacks), sysrepocfg for others */
389396 if (dstds -> datastore == SR_DS_RUNNING ) {
390397 if (sr_connect (SR_CONN_DEFAULT , & conn )) {
391398 fprintf (stderr , ERRMSG "connection to datastore failed\n" );
@@ -418,6 +425,8 @@ static int copy(const char *src, const char *dst, const char *remote_user)
418425 }
419426 }
420427 } else {
428+ /* 4. regular copy file -> file, either may be remote */
429+
421430 if (strstr (src , "://" ) && strstr (dst , "://" )) {
422431 fprintf (stderr , ERRMSG "copy from remote to remote is not supported.\n" );
423432 goto err ;
@@ -448,17 +457,34 @@ static int copy(const char *src, const char *dst, const char *remote_user)
448457 }
449458
450459 if (access (fn , F_OK ))
451- fprintf (stderr , ERRMSG "no such file %s, aborting ." , fn );
460+ fprintf (stderr , ERRMSG "no such file %s." , fn );
452461 else
453462 rc = systemf ("curl %s -LT %s %s" , remote_user , fn , dst );
454463 } else {
455- if (!access (dst , F_OK )) {
456- if (!yorn ("Overwrite existing file %s" , dst )) {
464+ char bufa [256 ], bufb [256 ];
465+ const char * from , * to ;
466+
467+ from = cfg_adjust (src , NULL , bufa , sizeof (bufa ), sanitize );
468+ if (!from ) {
469+ fprintf (stderr , ERRMSG "no such file %s." , src );
470+ rc = 1 ;
471+ goto err ;
472+ }
473+
474+ to = cfg_adjust (dst , src , bufb , sizeof (bufb ), sanitize );
475+ if (!to ) {
476+ fprintf (stderr , ERRMSG "no such file %s." , dst );
477+ rc = 1 ;
478+ goto err ;
479+ }
480+
481+ if (!access (to , F_OK )) {
482+ if (!yorn ("Overwrite existing file %s" , to )) {
457483 fprintf (stderr , "OK, aborting.\n" );
458484 return 0 ;
459485 }
460486 }
461- rc = systemf ("cp %s %s" , src , dst );
487+ rc = systemf ("cp %s %s" , from , to );
462488 }
463489 }
464490
0 commit comments