Blame lib/rename.c

Packit 9e4112
/* Work around rename bugs in some systems.
Packit 9e4112
Packit 9e4112
   Copyright (C) 2001-2003, 2005-2006, 2009-2018 Free Software Foundation, Inc.
Packit 9e4112
Packit 9e4112
   This program is free software: you can redistribute it and/or modify
Packit 9e4112
   it under the terms of the GNU General Public License as published by
Packit 9e4112
   the Free Software Foundation; either version 3 of the License, or
Packit 9e4112
   (at your option) any later version.
Packit 9e4112
Packit 9e4112
   This program is distributed in the hope that it will be useful,
Packit 9e4112
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
   GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
   You should have received a copy of the GNU General Public License
Packit 9e4112
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 9e4112
Packit 9e4112
/* Written by Volker Borchert, Eric Blake.  */
Packit 9e4112
Packit 9e4112
#include <config.h>
Packit 9e4112
Packit 9e4112
#include <stdio.h>
Packit 9e4112
Packit 9e4112
#undef rename
Packit 9e4112
Packit 9e4112
#if defined _WIN32 && ! defined __CYGWIN__
Packit 9e4112
/* The mingw rename has problems with trailing slashes; it also
Packit 9e4112
   requires use of native Windows calls to allow atomic renames over
Packit 9e4112
   existing files.  */
Packit 9e4112
Packit 9e4112
# include <errno.h>
Packit 9e4112
# include <stdbool.h>
Packit 9e4112
# include <stdlib.h>
Packit 9e4112
# include <sys/stat.h>
Packit 9e4112
# include <unistd.h>
Packit 9e4112
Packit 9e4112
# define WIN32_LEAN_AND_MEAN
Packit 9e4112
# include <windows.h>
Packit 9e4112
Packit 9e4112
# include "dirname.h"
Packit 9e4112
Packit 9e4112
/* Rename the file SRC to DST.  This replacement is necessary on
Packit 9e4112
   Windows, on which the system rename function will not replace
Packit 9e4112
   an existing DST.  */
Packit 9e4112
int
Packit 9e4112
rpl_rename (char const *src, char const *dst)
Packit 9e4112
{
Packit 9e4112
  int error;
Packit 9e4112
  size_t src_len = strlen (src);
Packit 9e4112
  size_t dst_len = strlen (dst);
Packit 9e4112
  char *src_base = last_component (src);
Packit 9e4112
  char *dst_base = last_component (dst);
Packit 9e4112
  bool src_slash;
Packit 9e4112
  bool dst_slash;
Packit 9e4112
  bool dst_exists;
Packit 9e4112
  struct stat src_st;
Packit 9e4112
  struct stat dst_st;
Packit 9e4112
Packit 9e4112
  /* Filter out dot as last component.  */
Packit 9e4112
  if (!src_len || !dst_len)
Packit 9e4112
    {
Packit 9e4112
      errno = ENOENT;
Packit 9e4112
      return -1;
Packit 9e4112
    }
Packit 9e4112
  if (*src_base == '.')
Packit 9e4112
    {
Packit 9e4112
      size_t len = base_len (src_base);
Packit 9e4112
      if (len == 1 || (len == 2 && src_base[1] == '.'))
Packit 9e4112
        {
Packit 9e4112
          errno = EINVAL;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
    }
Packit 9e4112
  if (*dst_base == '.')
Packit 9e4112
    {
Packit 9e4112
      size_t len = base_len (dst_base);
Packit 9e4112
      if (len == 1 || (len == 2 && dst_base[1] == '.'))
Packit 9e4112
        {
Packit 9e4112
          errno = EINVAL;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
  /* Presence of a trailing slash requires directory semantics.  If
Packit 9e4112
     the source does not exist, or if the destination cannot be turned
Packit 9e4112
     into a directory, give up now.  Otherwise, strip trailing slashes
Packit 9e4112
     before calling rename.  There are no symlinks on mingw, so stat
Packit 9e4112
     works instead of lstat.  */
Packit 9e4112
  src_slash = ISSLASH (src[src_len - 1]);
Packit 9e4112
  dst_slash = ISSLASH (dst[dst_len - 1]);
Packit 9e4112
  if (stat (src, &src_st))
Packit 9e4112
    return -1;
Packit 9e4112
  if (stat (dst, &dst_st))
Packit 9e4112
    {
Packit 9e4112
      if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
Packit 9e4112
        return -1;
Packit 9e4112
      dst_exists = false;
Packit 9e4112
    }
Packit 9e4112
  else
Packit 9e4112
    {
Packit 9e4112
      if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
Packit 9e4112
        {
Packit 9e4112
          errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
      dst_exists = true;
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
  /* There are no symlinks, so if a file existed with a trailing
Packit 9e4112
     slash, it must be a directory, and we don't have to worry about
Packit 9e4112
     stripping strip trailing slash.  However, mingw refuses to
Packit 9e4112
     replace an existing empty directory, so we have to help it out.
Packit 9e4112
     And canonicalize_file_name is not yet ported to mingw; however,
Packit 9e4112
     for directories, getcwd works as a viable alternative.  Ensure
Packit 9e4112
     that we can get back to where we started before using it; later
Packit 9e4112
     attempts to return are fatal.  Note that we can end up losing a
Packit 9e4112
     directory if rename then fails, but it was empty, so not much
Packit 9e4112
     damage was done.  */
Packit 9e4112
  if (dst_exists && S_ISDIR (dst_st.st_mode))
Packit 9e4112
    {
Packit 9e4112
      char *cwd = getcwd (NULL, 0);
Packit 9e4112
      char *src_temp;
Packit 9e4112
      char *dst_temp;
Packit 9e4112
      if (!cwd || chdir (cwd))
Packit 9e4112
        return -1;
Packit 9e4112
      if (IS_ABSOLUTE_FILE_NAME (src))
Packit 9e4112
        {
Packit 9e4112
          dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
Packit 9e4112
          src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
Packit 9e4112
        }
Packit 9e4112
      else
Packit 9e4112
        {
Packit 9e4112
          src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
Packit 9e4112
          if (!IS_ABSOLUTE_FILE_NAME (dst) && chdir (cwd))
Packit 9e4112
            abort ();
Packit 9e4112
          dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
Packit 9e4112
        }
Packit 9e4112
      if (chdir (cwd))
Packit 9e4112
        abort ();
Packit 9e4112
      free (cwd);
Packit 9e4112
      if (!src_temp || !dst_temp)
Packit 9e4112
        {
Packit 9e4112
          free (src_temp);
Packit 9e4112
          free (dst_temp);
Packit 9e4112
          errno = ENOMEM;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
      src_len = strlen (src_temp);
Packit 9e4112
      if (strncmp (src_temp, dst_temp, src_len) == 0
Packit 9e4112
          && (ISSLASH (dst_temp[src_len]) || dst_temp[src_len] == '\0'))
Packit 9e4112
        {
Packit 9e4112
          error = dst_temp[src_len];
Packit 9e4112
          free (src_temp);
Packit 9e4112
          free (dst_temp);
Packit 9e4112
          if (error)
Packit 9e4112
            {
Packit 9e4112
              errno = EINVAL;
Packit 9e4112
              return -1;
Packit 9e4112
            }
Packit 9e4112
          return 0;
Packit 9e4112
        }
Packit 9e4112
      if (rmdir (dst))
Packit 9e4112
        {
Packit 9e4112
          error = errno;
Packit 9e4112
          free (src_temp);
Packit 9e4112
          free (dst_temp);
Packit 9e4112
          errno = error;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
      free (src_temp);
Packit 9e4112
      free (dst_temp);
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
  /* MoveFileEx works if SRC is a directory without any flags, but
Packit 9e4112
     fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
Packit 9e4112
     Thankfully, MoveFileEx handles hard links correctly, even though
Packit 9e4112
     rename() does not.  */
Packit 9e4112
  if (MoveFileEx (src, dst, 0))
Packit 9e4112
    return 0;
Packit 9e4112
Packit 9e4112
  /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
Packit 9e4112
     due to the destination already existing.  */
Packit 9e4112
  error = GetLastError ();
Packit 9e4112
  if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
Packit 9e4112
    {
Packit 9e4112
      if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
Packit 9e4112
        return 0;
Packit 9e4112
Packit 9e4112
      error = GetLastError ();
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
  switch (error)
Packit 9e4112
    {
Packit 9e4112
    case ERROR_FILE_NOT_FOUND:
Packit 9e4112
    case ERROR_PATH_NOT_FOUND:
Packit 9e4112
    case ERROR_BAD_PATHNAME:
Packit 9e4112
    case ERROR_DIRECTORY:
Packit 9e4112
      errno = ENOENT;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_ACCESS_DENIED:
Packit 9e4112
    case ERROR_SHARING_VIOLATION:
Packit 9e4112
      errno = EACCES;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_OUTOFMEMORY:
Packit 9e4112
      errno = ENOMEM;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_CURRENT_DIRECTORY:
Packit 9e4112
      errno = EBUSY;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_NOT_SAME_DEVICE:
Packit 9e4112
      errno = EXDEV;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_WRITE_PROTECT:
Packit 9e4112
      errno = EROFS;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_WRITE_FAULT:
Packit 9e4112
    case ERROR_READ_FAULT:
Packit 9e4112
    case ERROR_GEN_FAILURE:
Packit 9e4112
      errno = EIO;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_HANDLE_DISK_FULL:
Packit 9e4112
    case ERROR_DISK_FULL:
Packit 9e4112
    case ERROR_DISK_TOO_FRAGMENTED:
Packit 9e4112
      errno = ENOSPC;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_FILE_EXISTS:
Packit 9e4112
    case ERROR_ALREADY_EXISTS:
Packit 9e4112
      errno = EEXIST;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_BUFFER_OVERFLOW:
Packit 9e4112
    case ERROR_FILENAME_EXCED_RANGE:
Packit 9e4112
      errno = ENAMETOOLONG;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    case ERROR_INVALID_NAME:
Packit 9e4112
    case ERROR_DELETE_PENDING:
Packit 9e4112
      errno = EPERM;        /* ? */
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
# ifndef ERROR_FILE_TOO_LARGE
Packit 9e4112
/* This value is documented but not defined in all versions of windows.h.  */
Packit 9e4112
#  define ERROR_FILE_TOO_LARGE 223
Packit 9e4112
# endif
Packit 9e4112
    case ERROR_FILE_TOO_LARGE:
Packit 9e4112
      errno = EFBIG;
Packit 9e4112
      break;
Packit 9e4112
Packit 9e4112
    default:
Packit 9e4112
      errno = EINVAL;
Packit 9e4112
      break;
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
  return -1;
Packit 9e4112
}
Packit 9e4112
Packit 9e4112
#else /* ! W32 platform */
Packit 9e4112
Packit 9e4112
# include <errno.h>
Packit 9e4112
# include <stdio.h>
Packit 9e4112
# include <stdlib.h>
Packit 9e4112
# include <string.h>
Packit 9e4112
# include <sys/stat.h>
Packit 9e4112
# include <unistd.h>
Packit 9e4112
Packit 9e4112
# include "dirname.h"
Packit 9e4112
# include "same-inode.h"
Packit 9e4112
Packit 9e4112
/* Rename the file SRC to DST, fixing any trailing slash bugs.  */
Packit 9e4112
Packit 9e4112
int
Packit 9e4112
rpl_rename (char const *src, char const *dst)
Packit 9e4112
{
Packit 9e4112
  size_t src_len = strlen (src);
Packit 9e4112
  size_t dst_len = strlen (dst);
Packit 9e4112
  char *src_temp = (char *) src;
Packit 9e4112
  char *dst_temp = (char *) dst;
Packit 9e4112
  bool src_slash;
Packit 9e4112
  bool dst_slash;
Packit 9e4112
  bool dst_exists _GL_UNUSED;
Packit 9e4112
  int ret_val = -1;
Packit 9e4112
  int rename_errno = ENOTDIR;
Packit 9e4112
  struct stat src_st;
Packit 9e4112
  struct stat dst_st;
Packit 9e4112
Packit 9e4112
  if (!src_len || !dst_len)
Packit 9e4112
    return rename (src, dst); /* Let strace see the ENOENT failure.  */
Packit 9e4112
Packit 9e4112
# if RENAME_DEST_EXISTS_BUG
Packit 9e4112
  {
Packit 9e4112
    char *src_base = last_component (src);
Packit 9e4112
    char *dst_base = last_component (dst);
Packit 9e4112
    if (*src_base == '.')
Packit 9e4112
      {
Packit 9e4112
        size_t len = base_len (src_base);
Packit 9e4112
        if (len == 1 || (len == 2 && src_base[1] == '.'))
Packit 9e4112
          {
Packit 9e4112
            errno = EINVAL;
Packit 9e4112
            return -1;
Packit 9e4112
          }
Packit 9e4112
      }
Packit 9e4112
    if (*dst_base == '.')
Packit 9e4112
      {
Packit 9e4112
        size_t len = base_len (dst_base);
Packit 9e4112
        if (len == 1 || (len == 2 && dst_base[1] == '.'))
Packit 9e4112
          {
Packit 9e4112
            errno = EINVAL;
Packit 9e4112
            return -1;
Packit 9e4112
          }
Packit 9e4112
      }
Packit 9e4112
  }
Packit 9e4112
# endif /* RENAME_DEST_EXISTS_BUG */
Packit 9e4112
Packit 9e4112
  src_slash = src[src_len - 1] == '/';
Packit 9e4112
  dst_slash = dst[dst_len - 1] == '/';
Packit 9e4112
Packit 9e4112
# if !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG
Packit 9e4112
  /* If there are no trailing slashes, then trust the native
Packit 9e4112
     implementation unless we also suspect issues with hard link
Packit 9e4112
     detection or file/directory conflicts.  */
Packit 9e4112
  if (!src_slash && !dst_slash)
Packit 9e4112
    return rename (src, dst);
Packit 9e4112
# endif /* !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG */
Packit 9e4112
Packit 9e4112
  /* Presence of a trailing slash requires directory semantics.  If
Packit 9e4112
     the source does not exist, or if the destination cannot be turned
Packit 9e4112
     into a directory, give up now.  Otherwise, strip trailing slashes
Packit 9e4112
     before calling rename.  */
Packit 9e4112
  if (lstat (src, &src_st))
Packit 9e4112
    return -1;
Packit 9e4112
  if (lstat (dst, &dst_st))
Packit 9e4112
    {
Packit 9e4112
      if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
Packit 9e4112
        return -1;
Packit 9e4112
      dst_exists = false;
Packit 9e4112
    }
Packit 9e4112
  else
Packit 9e4112
    {
Packit 9e4112
      if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
Packit 9e4112
        {
Packit 9e4112
          errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
Packit 9e4112
          return -1;
Packit 9e4112
        }
Packit 9e4112
# if RENAME_HARD_LINK_BUG
Packit 9e4112
      if (SAME_INODE (src_st, dst_st))
Packit 9e4112
        return 0;
Packit 9e4112
# endif /* RENAME_HARD_LINK_BUG */
Packit 9e4112
      dst_exists = true;
Packit 9e4112
    }
Packit 9e4112
Packit 9e4112
# if (RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG        \
Packit 9e4112
      || RENAME_HARD_LINK_BUG)
Packit 9e4112
  /* If the only bug was that a trailing slash was allowed on a
Packit 9e4112
     non-existing file destination, as in Solaris 10, then we've
Packit 9e4112
     already covered that situation.  But if there is any problem with
Packit 9e4112
     a trailing slash on an existing source or destination, as in
Packit 9e4112
     Solaris 9, or if a directory can overwrite a symlink, as on
Packit 9e4112
     Cygwin 1.5, or if directories cannot be created with trailing
Packit 9e4112
     slash, as on NetBSD 1.6, then we must strip the offending slash
Packit 9e4112
     and check that we have not encountered a symlink instead of a
Packit 9e4112
     directory.
Packit 9e4112
Packit 9e4112
     Stripping a trailing slash interferes with POSIX semantics, where
Packit 9e4112
     rename behavior on a symlink with a trailing slash operates on
Packit 9e4112
     the corresponding target directory.  We prefer the GNU semantics
Packit 9e4112
     of rejecting any use of a symlink with trailing slash, but do not
Packit 9e4112
     enforce them, since Solaris 10 is able to obey POSIX semantics
Packit 9e4112
     and there might be clients expecting it, as counter-intuitive as
Packit 9e4112
     those semantics are.
Packit 9e4112
Packit 9e4112
     Technically, we could also follow the POSIX behavior by chasing a
Packit 9e4112
     readlink trail, but that is harder to implement.  */
Packit 9e4112
  if (src_slash)
Packit 9e4112
    {
Packit 9e4112
      src_temp = strdup (src);
Packit 9e4112
      if (!src_temp)
Packit 9e4112
        {
Packit 9e4112
          /* Rather than rely on strdup-posix, we set errno ourselves.  */
Packit 9e4112
          rename_errno = ENOMEM;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      strip_trailing_slashes (src_temp);
Packit 9e4112
      if (lstat (src_temp, &src_st))
Packit 9e4112
        {
Packit 9e4112
          rename_errno = errno;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      if (S_ISLNK (src_st.st_mode))
Packit 9e4112
        goto out;
Packit 9e4112
    }
Packit 9e4112
  if (dst_slash)
Packit 9e4112
    {
Packit 9e4112
      dst_temp = strdup (dst);
Packit 9e4112
      if (!dst_temp)
Packit 9e4112
        {
Packit 9e4112
          rename_errno = ENOMEM;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      strip_trailing_slashes (dst_temp);
Packit 9e4112
      if (lstat (dst_temp, &dst_st))
Packit 9e4112
        {
Packit 9e4112
          if (errno != ENOENT)
Packit 9e4112
            {
Packit 9e4112
              rename_errno = errno;
Packit 9e4112
              goto out;
Packit 9e4112
            }
Packit 9e4112
        }
Packit 9e4112
      else if (S_ISLNK (dst_st.st_mode))
Packit 9e4112
        goto out;
Packit 9e4112
    }
Packit 9e4112
# endif /* RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG
Packit 9e4112
           || RENAME_HARD_LINK_BUG */
Packit 9e4112
Packit 9e4112
# if RENAME_DEST_EXISTS_BUG
Packit 9e4112
  /* Cygwin 1.5 sometimes behaves oddly when moving a non-empty
Packit 9e4112
     directory on top of an empty one (the old directory name can
Packit 9e4112
     reappear if the new directory tree is removed).  Work around this
Packit 9e4112
     by removing the target first, but don't remove the target if it
Packit 9e4112
     is a subdirectory of the source.  Note that we can end up losing
Packit 9e4112
     a directory if rename then fails, but it was empty, so not much
Packit 9e4112
     damage was done.  */
Packit 9e4112
  if (dst_exists && S_ISDIR (dst_st.st_mode))
Packit 9e4112
    {
Packit 9e4112
      if (src_st.st_dev != dst_st.st_dev)
Packit 9e4112
        {
Packit 9e4112
          rename_errno = EXDEV;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      if (src_temp != src)
Packit 9e4112
        free (src_temp);
Packit 9e4112
      src_temp = canonicalize_file_name (src);
Packit 9e4112
      if (dst_temp != dst)
Packit 9e4112
        free (dst_temp);
Packit 9e4112
      dst_temp = canonicalize_file_name (dst);
Packit 9e4112
      if (!src_temp || !dst_temp)
Packit 9e4112
        {
Packit 9e4112
          rename_errno = ENOMEM;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      src_len = strlen (src_temp);
Packit 9e4112
      if (strncmp (src_temp, dst_temp, src_len) == 0
Packit 9e4112
          && dst_temp[src_len] == '/')
Packit 9e4112
        {
Packit 9e4112
          rename_errno = EINVAL;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
      if (rmdir (dst))
Packit 9e4112
        {
Packit 9e4112
          rename_errno = errno;
Packit 9e4112
          goto out;
Packit 9e4112
        }
Packit 9e4112
    }
Packit 9e4112
# endif /* RENAME_DEST_EXISTS_BUG */
Packit 9e4112
Packit 9e4112
  ret_val = rename (src_temp, dst_temp);
Packit 9e4112
  rename_errno = errno;
Packit 9e4112
Packit 9e4112
 out: _GL_UNUSED_LABEL;
Packit 9e4112
Packit 9e4112
  if (src_temp != src)
Packit 9e4112
    free (src_temp);
Packit 9e4112
  if (dst_temp != dst)
Packit 9e4112
    free (dst_temp);
Packit 9e4112
  errno = rename_errno;
Packit 9e4112
  return ret_val;
Packit 9e4112
}
Packit 9e4112
#endif /* ! W32 platform */