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
9 Copyright (C) 2006 Free Software Foundation, Inc.
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 2, or (at your option)
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.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /* written by Jim Meyering */
30 #include <sys/types.h>
42 has_trailing_slash (char const *file)
44 /* Don't count "/", "//", etc., as having a trailing slash. */
45 bool has_non_slash = false;
46 bool ends_in_slash = false;
48 for (file += FILE_SYSTEM_PREFIX_LEN (file); *file; file++)
50 ends_in_slash = ISSLASH (*file);
51 has_non_slash |= ~ ends_in_slash;
54 return has_non_slash & ends_in_slash;
57 /* This is a rename wrapper for systems where the rename syscall
58 works differently than desired when SRC is a directory and DST does
59 not exist but is specified with a trailing slash. On NetBSD 6.1,
60 rename fails in that case. On Linux and Solaris systems, it succeeds.
61 This wrapper makes it succeed on NetBSD by running the originally
62 requested rename, and if it fails due to the above scenario, calling
63 it again with DST's trailing slashes removed. */
65 rpl_rename_dest_slash (char const *src, char const *dst)
67 int ret_val = rename (src, dst);
69 if (ret_val != 0 && errno == ENOENT && has_trailing_slash (dst))
71 int rename_errno = ENOENT;
73 /* Fail now, unless SRC is a directory. */
75 if (lstat (src, &sb) == 0 && S_ISDIR (sb.st_mode))
77 char *dst_temp = xstrdup (dst);
78 strip_trailing_slashes (dst_temp);
79 ret_val = rename (src, dst_temp);