#include "bfd.h"
#include "bucomm.h"
#include <sys/stat.h>
#ifdef HAVE_GOOD_UTIME_H
#include <utime.h>
#else
#ifdef HAVE_UTIMES
#include <sys/time.h>
#endif
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if ! defined (_WIN32) || defined (__CYGWIN32__)
static int simple_copy (const char *, const char *);
#define COPY_BUF 8192
static int
simple_copy (const char *from, const char *to)
{
int fromfd, tofd, nread;
int saved;
char buf[COPY_BUF];
fromfd = open (from, O_RDONLY | O_BINARY);
if (fromfd < 0)
return -1;
#ifdef O_CREAT
tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
#else
tofd = creat (to, 0777);
#endif
if (tofd < 0)
{
saved = errno;
close (fromfd);
errno = saved;
return -1;
}
while ((nread = read (fromfd, buf, sizeof buf)) > 0)
{
if (write (tofd, buf, nread) != nread)
{
saved = errno;
close (fromfd);
close (tofd);
errno = saved;
return -1;
}
}
saved = errno;
close (fromfd);
close (tofd);
if (nread < 0)
{
errno = saved;
return -1;
}
return 0;
}
#endif
void
set_times (const char *destination, const struct stat *statbuf)
{
int result;
{
#ifdef HAVE_GOOD_UTIME_H
struct utimbuf tb;
tb.actime = statbuf->st_atime;
tb.modtime = statbuf->st_mtime;
result = utime (destination, &tb);
#else
#ifndef HAVE_UTIMES
long tb[2];
tb[0] = statbuf->st_atime;
tb[1] = statbuf->st_mtime;
result = utime (destination, tb);
#else
struct timeval tv[2];
tv[0].tv_sec = statbuf->st_atime;
tv[0].tv_usec = 0;
tv[1].tv_sec = statbuf->st_mtime;
tv[1].tv_usec = 0;
result = utimes (destination, tv);
#endif
#endif
}
if (result != 0)
non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
}
#ifndef S_ISLNK
#ifdef S_IFLNK
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#else
#define S_ISLNK(m) 0
#define lstat stat
#endif
#endif
int
smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
{
bfd_boolean exists;
struct stat s;
int ret = 0;
exists = lstat (to, &s) == 0;
#if defined (_WIN32) && !defined (__CYGWIN32__)
if (exists)
remove (to);
ret = rename (from, to);
if (ret != 0)
{
non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
unlink (from);
}
#else
if (! exists
|| (!S_ISLNK (s.st_mode)
&& S_ISREG (s.st_mode)
&& (s.st_mode & S_IWUSR)
&& s.st_nlink == 1)
)
{
ret = rename (from, to);
if (ret == 0)
{
if (exists)
{
chmod (to, s.st_mode & 0777);
if (chown (to, s.st_uid, s.st_gid) >= 0)
chmod (to, s.st_mode & 07777);
}
}
else
{
non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
unlink (from);
}
}
else
{
ret = simple_copy (from, to);
if (ret != 0)
non_fatal (_("unable to copy file '%s' reason: %s"), to, strerror (errno));
if (preserve_dates)
set_times (to, &s);
unlink (from);
}
#endif
return ret;
}