Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / rename-dest-slash.c
1 /* A rename wrapper to make tools like mv -- that would normally rely
2    on the underlying rename syscall -- work more consistently.
3    On at least NetBSD 1.6, `rename ("dir", "B/")' fails when B doesn't
4    exist, whereas it succeeds on Linux-2.6.x and Solaris 10.  This wrapper
5    provides an interface for systems like the former so that the tools
6    (namely mv) relying on the rename syscall have more consistent
7    semantics.
8
9    Copyright (C) 2006 Free Software Foundation, Inc.
10
11    This program is free software: you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 /* written by Jim Meyering */
25
26 #include <config.h>
27 #undef rename
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "dirname.h"
38 #include "xalloc.h"
39
40 static bool
41 has_trailing_slash (char const *file)
42 {
43   /* Don't count "/", "//", etc., as having a trailing slash.  */
44   bool has_non_slash = false;
45   bool ends_in_slash = false;
46
47   for (file += FILE_SYSTEM_PREFIX_LEN (file); *file; file++)
48     {
49       ends_in_slash = ISSLASH (*file);
50       has_non_slash |= ~ ends_in_slash;
51     }
52
53   return has_non_slash & ends_in_slash;
54 }
55
56 /* This is a rename wrapper for systems where the rename syscall
57    works differently than desired when SRC is a directory and DST does
58    not exist but is specified with a trailing slash.  On NetBSD 6.1,
59    rename fails in that case.  On Linux and Solaris systems, it succeeds.
60    This wrapper makes it succeed on NetBSD by running the originally
61    requested rename, and if it fails due to the above scenario, calling
62    it again with DST's trailing slashes removed.  */
63 int
64 rpl_rename_dest_slash (char const *src, char const *dst)
65 {
66   int ret_val = rename (src, dst);
67
68   if (ret_val != 0 && errno == ENOENT && has_trailing_slash (dst))
69     {
70       int rename_errno = ENOENT;
71
72       /* Fail now, unless SRC is a directory.  */
73       struct stat sb;
74       if (lstat (src, &sb) == 0 && S_ISDIR (sb.st_mode))
75         {
76           char *dst_temp = xstrdup (dst);
77           strip_trailing_slashes (dst_temp);
78           ret_val = rename (src, dst_temp);
79           rename_errno = errno;
80           free (dst_temp);
81         }
82
83       errno = rename_errno;
84     }
85
86   return ret_val;
87 }