root/maint/gnulib/lib/renameatu.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. errno_fail
  2. rename_noreplace
  3. renameatu

   1 /* Rename a file relative to open directories.
   2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* written by Eric Blake and Paul Eggert */
  18 
  19 #include <config.h>
  20 
  21 #include "renameatu.h"
  22 
  23 #include <errno.h>
  24 #include <stdio.h>
  25 #include <sys/stat.h>
  26 #include <unistd.h>
  27 
  28 #ifdef __linux__
  29 # include <sys/syscall.h>
  30 #endif
  31 
  32 static int
  33 errno_fail (int e)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35   errno = e;
  36   return -1;
  37 }
  38 
  39 #if HAVE_RENAMEAT
  40 
  41 # include <stdbool.h>
  42 # include <stdlib.h>
  43 # include <string.h>
  44 
  45 # include "dirname.h"
  46 # include "openat.h"
  47 
  48 #else
  49 # include "openat-priv.h"
  50 
  51 static int
  52 rename_noreplace (char const *src, char const *dst)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54   /* This has a race between the call to lstat and the call to rename.  */
  55   struct stat st;
  56   return (lstat (dst, &st) == 0 || errno == EOVERFLOW ? errno_fail (EEXIST)
  57           : errno == ENOENT ? rename (src, dst)
  58           : -1);
  59 }
  60 #endif
  61 
  62 #undef renameat
  63 
  64 /* Rename FILE1, in the directory open on descriptor FD1, to FILE2, in
  65    the directory open on descriptor FD2.  If possible, do it without
  66    changing the working directory.  Otherwise, resort to using
  67    save_cwd/fchdir, then rename/restore_cwd.  If either the save_cwd or
  68    the restore_cwd fails, then give a diagnostic and exit nonzero.
  69 
  70    Obey FLAGS when doing the renaming.  If FLAGS is zero, this
  71    function is equivalent to renameat (FD1, SRC, FD2, DST).
  72    Otherwise, attempt to implement FLAGS even if the implementation is
  73    not atomic; this differs from the GNU/Linux native renameat2,
  74    which fails if it cannot guarantee atomicity.  */
  75 
  76 int
  77 renameatu (int fd1, char const *src, int fd2, char const *dst,
     /* [previous][next][first][last][top][bottom][index][help] */
  78            unsigned int flags)
  79 {
  80   int ret_val = -1;
  81   int err = EINVAL;
  82 
  83 #ifdef HAVE_RENAMEAT2
  84   ret_val = renameat2 (fd1, src, fd2, dst, flags);
  85   err = errno;
  86 #elif defined SYS_renameat2
  87   ret_val = syscall (SYS_renameat2, fd1, src, fd2, dst, flags);
  88   err = errno;
  89 #endif
  90 
  91   if (! (ret_val < 0 && (err == EINVAL || err == ENOSYS || err == ENOTSUP)))
  92     return ret_val;
  93 
  94 #if HAVE_RENAMEAT
  95   {
  96 # if defined RENAME_EXCL                /* macOS */
  97   unsigned int uflags;
  98 # endif
  99   size_t src_len;
 100   size_t dst_len;
 101   char *src_temp = (char *) src;
 102   char *dst_temp = (char *) dst;
 103   bool src_slash;
 104   bool dst_slash;
 105   int rename_errno = ENOTDIR;
 106   struct stat src_st;
 107   struct stat dst_st;
 108   bool dst_found_nonexistent = false;
 109 
 110   /* Check the flags.  */
 111 # if defined RENAME_EXCL
 112   /* We can support RENAME_EXCHANGE and RENAME_NOREPLACE.  */
 113   if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
 114 # else
 115   /* RENAME_NOREPLACE is the only flag currently supported.  */
 116   if (flags & ~RENAME_NOREPLACE)
 117 # endif
 118     return errno_fail (ENOTSUP);
 119 
 120 # if defined RENAME_EXCL
 121   uflags = ((flags & RENAME_EXCHANGE ? RENAME_SWAP : 0)
 122             | (flags & RENAME_NOREPLACE ? RENAME_EXCL : 0));
 123 # endif
 124 
 125   if ((flags & RENAME_NOREPLACE) != 0)
 126     {
 127       /* This has a race between the call to lstatat and the calls to
 128          renameat below.  This lstatat is needed even if RENAME_EXCL
 129          is defined, because RENAME_EXCL is buggy on macOS 11.2:
 130          renameatx_np (fd, "X", fd, "X", RENAME_EXCL) incorrectly
 131          succeeds when X exists.  */
 132       if (lstatat (fd2, dst, &dst_st) == 0 || errno == EOVERFLOW)
 133         return errno_fail (EEXIST);
 134       if (errno != ENOENT)
 135         return -1;
 136       dst_found_nonexistent = true;
 137     }
 138 
 139   /* Let strace see any ENOENT failure.  */
 140   src_len = strlen (src);
 141   dst_len = strlen (dst);
 142   if (!src_len || !dst_len)
 143 # if defined RENAME_EXCL
 144     return renameatx_np (fd1, src, fd2, dst, uflags);
 145 # else
 146     return renameat (fd1, src, fd2, dst);
 147 # endif
 148 
 149   src_slash = src[src_len - 1] == '/';
 150   dst_slash = dst[dst_len - 1] == '/';
 151   if (!src_slash && !dst_slash)
 152 # if defined RENAME_EXCL
 153     return renameatx_np (fd1, src, fd2, dst, uflags);
 154 # else
 155     return renameat (fd1, src, fd2, dst);
 156 # endif
 157 
 158   /* Presence of a trailing slash requires directory semantics.  If
 159      the source does not exist, or if the destination cannot be turned
 160      into a directory, give up now.  Otherwise, strip trailing slashes
 161      before calling rename.  */
 162   if (lstatat (fd1, src, &src_st))
 163     return -1;
 164   if (dst_found_nonexistent)
 165     {
 166       if (!S_ISDIR (src_st.st_mode))
 167         return errno_fail (ENOENT);
 168     }
 169   else if (lstatat (fd2, dst, &dst_st))
 170     {
 171       if (errno != ENOENT || !S_ISDIR (src_st.st_mode))
 172         return -1;
 173     }
 174   else if (!S_ISDIR (dst_st.st_mode))
 175     return errno_fail (ENOTDIR);
 176   else if (!S_ISDIR (src_st.st_mode))
 177     return errno_fail (EISDIR);
 178 
 179 # if RENAME_TRAILING_SLASH_SOURCE_BUG
 180   /* See the lengthy comment in rename.c why Solaris 9 is forced to
 181      GNU behavior, while Solaris 10 is left with POSIX behavior,
 182      regarding symlinks with trailing slash.  */
 183   ret_val = -1;
 184   if (src_slash)
 185     {
 186       src_temp = strdup (src);
 187       if (!src_temp)
 188         {
 189           /* Rather than rely on strdup-posix, we set errno ourselves.  */
 190           rename_errno = ENOMEM;
 191           goto out;
 192         }
 193       strip_trailing_slashes (src_temp);
 194       if (lstatat (fd1, src_temp, &src_st))
 195         {
 196           rename_errno = errno;
 197           goto out;
 198         }
 199       if (S_ISLNK (src_st.st_mode))
 200         goto out;
 201     }
 202   if (dst_slash)
 203     {
 204       dst_temp = strdup (dst);
 205       if (!dst_temp)
 206         {
 207           rename_errno = ENOMEM;
 208           goto out;
 209         }
 210       strip_trailing_slashes (dst_temp);
 211       if (lstatat (fd2, dst_temp, &dst_st))
 212         {
 213           if (errno != ENOENT)
 214             {
 215               rename_errno = errno;
 216               goto out;
 217             }
 218         }
 219       else if (S_ISLNK (dst_st.st_mode))
 220         goto out;
 221     }
 222 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG */
 223 
 224   /* renameat does not honor trailing / on Solaris 10.  Solve it in a
 225      similar manner to rename.  No need to worry about bugs not present
 226      on Solaris, since all other systems either lack renameat or honor
 227      trailing slash correctly.  */
 228 
 229 # if defined RENAME_EXCL
 230   ret_val = renameatx_np (fd1, src_temp, fd2, dst_temp, uflags);
 231 # else
 232   ret_val = renameat (fd1, src_temp, fd2, dst_temp);
 233 # endif
 234   rename_errno = errno;
 235   goto out;
 236  out:
 237   if (src_temp != src)
 238     free (src_temp);
 239   if (dst_temp != dst)
 240     free (dst_temp);
 241   errno = rename_errno;
 242   return ret_val;
 243   }
 244 #else /* !HAVE_RENAMEAT */
 245 
 246   /* RENAME_NOREPLACE is the only flag currently supported.  */
 247   if (flags & ~RENAME_NOREPLACE)
 248     return errno_fail (ENOTSUP);
 249   return at_func2 (fd1, src, fd2, dst, flags ? rename_noreplace : rename);
 250 
 251 #endif /* !HAVE_RENAMEAT */
 252 }

/* [previous][next][first][last][top][bottom][index][help] */