@@ -155,6 +155,30 @@ static bool is_uri(const char *str)
155155 return strstr (str , "://" ) != NULL ;
156156}
157157
158+ static char * mktmp (void )
159+ {
160+ mode_t oldmask ;
161+ char * path ;
162+ int fd ;
163+
164+ path = strdup ("/tmp/copy-XXXXXX" );
165+ if (!path )
166+ goto err ;
167+
168+ oldmask = umask (0077 );
169+ fd = mkstemp (path );
170+ umask (oldmask );
171+
172+ if (fd < 0 )
173+ goto err ;
174+
175+ close (fd );
176+ return path ;
177+ err :
178+ free (path );
179+ return NULL ;
180+ }
181+
158182static void rmtmp (const char * path )
159183{
160184 if (remove (path )) {
@@ -338,7 +362,7 @@ static int curl(char *op, const char *path, const char *uri)
338362 char * argv [] = {
339363 "curl" , "-L" , op , NULL , NULL , NULL , NULL , NULL ,
340364 };
341- int err ;
365+ int err = 1 ;
342366
343367 argv [3 ] = strdup (path );
344368 argv [4 ] = strdup (uri );
@@ -390,7 +414,7 @@ static int cp(const char *srcpath, const char *dstpath)
390414 char * argv [] = {
391415 "cp" , NULL , NULL , NULL ,
392416 };
393- int err ;
417+ int err = 1 ;
394418
395419 argv [1 ] = strdup (srcpath );
396420 argv [2 ] = strdup (dstpath );
@@ -445,7 +469,7 @@ static int resolve_src(const char **src, const struct infix_ds **ds, char **path
445469 * src = infix_ds (* src , ds );
446470
447471 if (* ds || is_uri (* src )) {
448- * path = tempnam ( NULL , NULL );
472+ * path = mktmp ( );
449473 if (!* path )
450474 return 1 ;
451475
@@ -477,7 +501,7 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
477501 if (!(* ds )-> path )
478502 return 0 ;
479503
480- * path = ( * ds )-> path ;
504+ * path = strdup (( * ds )-> path ) ;
481505 } else if (is_uri (* dst )) {
482506 return 0 ;
483507 } else {
@@ -499,8 +523,8 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
499523
500524static int copy (const char * src , const char * dst )
501525{
526+ char * srcpath = NULL , * dstpath = NULL ;
502527 const struct infix_ds * srcds , * dstds ;
503- char * srcpath , * dstpath ;
504528 bool rmsrc = false;
505529 mode_t oldmask ;
506530 int err = 1 ;
@@ -534,6 +558,9 @@ static int copy(const char *src, const char *dst)
534558 if (rmsrc )
535559 rmtmp (srcpath );
536560
561+ free (dstpath );
562+ free (srcpath );
563+
537564 sync ();
538565 umask (oldmask );
539566 return err ;
0 commit comments