Skip to content

Commit

Permalink
Reinstate various pieces backed out from smart_rename changes
Browse files Browse the repository at this point in the history
In the interests of a stable release various last minute smart_rename
patches were backed out of the 2.36 branch.  The main reason to
reinstate some of those backed out changes here is to make necessary
followup fixes to commit 8e03235 simple cherry-picks from
mainline.  A secondary reason is that ar -M support isn't fixed for
pr26945 without this patch.

	PR 26945
	* ar.c: Don't include libbfd.h.
	(write_archive): Replace xmalloc+strcpy with xstrdup.
	* arsup.c (temp_name, real_ofd): New static variables.
	(ar_open): Use make_tempname and bfd_fdopenw.
	(ar_save): Adjust to suit ar_open changes.
	* objcopy.c: Don't include libbfd.h.
	* rename.c: Rename and reorder variables.

(cherry picked from commit 95b91a0)
  • Loading branch information
amodra committed Feb 26, 2021
1 parent d5eb492 commit d3edaa9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
13 changes: 13 additions & 0 deletions binutils/ChangeLog
@@ -1,3 +1,16 @@
2021-02-26 Alan Modra <amodra@gmail.com>

Backport from mainline
2021-02-03 Alan Modra <amodra@gmail.com>
PR 26945
* ar.c: Don't include libbfd.h.
(write_archive): Replace xmalloc+strcpy with xstrdup.
* arsup.c (temp_name, real_ofd): New static variables.
(ar_open): Use make_tempname and bfd_fdopenw.
(ar_save): Adjust to suit ar_open changes.
* objcopy.c: Don't include libbfd.h.
* rename.c: Rename and reorder variables.

2021-02-22 Siddhesh Poyarekar <siddhesh@gotplt.org>

* ar.c (write_archive): Adjust call to SMART_RENAME.
Expand Down
4 changes: 1 addition & 3 deletions binutils/ar.c
Expand Up @@ -25,7 +25,6 @@

#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "libiberty.h"
#include "progress.h"
#include "getopt.h"
Expand Down Expand Up @@ -1255,8 +1254,7 @@ write_archive (bfd *iarch)
bfd *contents_head = iarch->archive_next;
int ofd = -1;

old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
strcpy (old_name, bfd_get_filename (iarch));
old_name = xstrdup (bfd_get_filename (iarch));
new_name = make_tempname (old_name, &ofd);

if (new_name == NULL)
Expand Down
37 changes: 25 additions & 12 deletions binutils/arsup.c
Expand Up @@ -42,6 +42,8 @@ extern int deterministic;

static bfd *obfd;
static char *real_name;
static char *temp_name;
static int real_ofd;
static FILE *outfile;

static void
Expand Down Expand Up @@ -149,27 +151,24 @@ maybequit (void)
void
ar_open (char *name, int t)
{
char *tname;
const char *bname = lbasename (name);
real_name = name;
real_name = xstrdup (name);
temp_name = make_tempname (real_name, &real_ofd);

/* Prepend tmp- to the beginning, to avoid file-name clashes after
truncation on filesystems with limited namespaces (DOS). */
if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
if (temp_name == NULL)
{
fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
program_name, strerror(errno));
maybequit ();
return;
}

obfd = bfd_openw (tname, NULL);
obfd = bfd_fdopenw (temp_name, NULL, real_ofd);

if (!obfd)
{
fprintf (stderr,
_("%s: Can't open output archive %s\n"),
program_name, tname);
program_name, temp_name);

maybequit ();
}
Expand Down Expand Up @@ -344,16 +343,30 @@ ar_save (void)
}
else
{
char *ofilename = xstrdup (bfd_get_filename (obfd));
struct stat target_stat;

if (deterministic > 0)
obfd->flags |= BFD_DETERMINISTIC_OUTPUT;

bfd_close (obfd);

smart_rename (ofilename, real_name, NULL);
if (stat (real_name, &target_stat) != 0)
{
/* The temp file created in ar_open has mode 0600 as per mkstemp.
Create the real empty output file here so smart_rename will
update the mode according to the process umask. */
obfd = bfd_openw (real_name, NULL);
if (obfd != NULL)
{
bfd_set_format (obfd, bfd_archive);
bfd_close (obfd);
}
}

smart_rename (temp_name, real_name, NULL);
obfd = 0;
free (ofilename);
free (temp_name);
free (real_name);
}
}

Expand Down
1 change: 0 additions & 1 deletion binutils/objcopy.c
Expand Up @@ -20,7 +20,6 @@

#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "progress.h"
#include "getopt.h"
#include "libiberty.h"
Expand Down
6 changes: 3 additions & 3 deletions binutils/rename.c
Expand Up @@ -130,11 +130,11 @@ int
smart_rename (const char *from, const char *to,
struct stat *target_stat ATTRIBUTE_UNUSED)
{
bfd_boolean exists;
struct stat s;
int ret = 0;
struct stat to_stat;
bfd_boolean exists;

exists = lstat (to, &s) == 0;
exists = lstat (to, &to_stat) == 0;

#if defined (_WIN32) && !defined (__CYGWIN32__)
/* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
Expand Down

0 comments on commit d3edaa9

Please sign in to comment.