@@ -42,8 +42,6 @@ extern int deterministic;
4242
4343static bfd * obfd ;
4444static char * real_name ;
45- static char * temp_name ;
46- static int real_ofd ;
4745static FILE * outfile ;
4846
4947static void
@@ -151,24 +149,27 @@ maybequit (void)
151149void
152150ar_open (char * name , int t )
153151{
154- real_name = xstrdup (name );
155- temp_name = make_tempname (real_name , & real_ofd );
152+ char * tname ;
153+ const char * bname = lbasename (name );
154+ real_name = name ;
156155
157- if (temp_name == NULL )
156+ /* Prepend tmp- to the beginning, to avoid file-name clashes after
157+ truncation on filesystems with limited namespaces (DOS). */
158+ if (asprintf (& tname , "%.*stmp-%s" , (int ) (bname - name ), name , bname ) == -1 )
158159 {
159- fprintf (stderr , _ ("%s: Can't open temporary file (%s)\n" ),
160+ fprintf (stderr , _ ("%s: Can't allocate memory for temp name (%s)\n" ),
160161 program_name , strerror (errno ));
161162 maybequit ();
162163 return ;
163164 }
164165
165- obfd = bfd_fdopenw ( temp_name , NULL , real_ofd );
166+ obfd = bfd_openw ( tname , NULL );
166167
167168 if (!obfd )
168169 {
169170 fprintf (stderr ,
170171 _ ("%s: Can't open output archive %s\n" ),
171- program_name , temp_name );
172+ program_name , tname );
172173
173174 maybequit ();
174175 }
@@ -343,41 +344,28 @@ ar_save (void)
343344 }
344345 else
345346 {
347+ char * ofilename = xstrdup (bfd_get_filename (obfd ));
346348 bfd_boolean skip_stat = FALSE;
347349 struct stat target_stat ;
348- int ofd = real_ofd ;
350+ int ofd = -1 ;
349351
350352 if (deterministic > 0 )
351353 obfd -> flags |= BFD_DETERMINISTIC_OUTPUT ;
352354
353355#if !defined (_WIN32 ) || defined (__CYGWIN32__ )
354356 /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
355357 copy fallback to write the output. */
356- ofd = dup (ofd );
358+ ofd = dup (fileno ((FILE * ) obfd -> iostream ));
359+ if (lstat (real_name , & target_stat ) != 0 )
360+ skip_stat = TRUE;
357361#endif
358- bfd_close (obfd );
359362
360- if (lstat (real_name , & target_stat ) != 0 )
361- {
362- /* The temp file created in ar_open has mode 0600 as per mkstemp.
363- Create the real empty output file here so smart_rename will
364- update the mode according to the process umask. */
365- obfd = bfd_openw (real_name , NULL );
366- if (obfd == NULL
367- || bfd_stat (obfd , & target_stat ) != 0 )
368- skip_stat = TRUE;
369- if (obfd != NULL )
370- {
371- bfd_set_format (obfd , bfd_archive );
372- bfd_close (obfd );
373- }
374- }
363+ bfd_close (obfd );
375364
376- smart_rename (temp_name , real_name , ofd ,
365+ smart_rename (ofilename , real_name , ofd ,
377366 skip_stat ? NULL : & target_stat , 0 );
378367 obfd = 0 ;
379- free (temp_name );
380- free (real_name );
368+ free (ofilename );
381369 }
382370}
383371
0 commit comments