Tizen 2.0 Release
[external/tizen-coreutils.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 2, or (at your option)
14    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, write to the Free Software Foundation,
23    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
24
25 /* written by Jim Meyering */
26
27 #include <config.h>
28 #undef rename
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "dirname.h"
39 #include "xalloc.h"
40
41 static bool
42 has_trailing_slash (char const *file)
43 {
44   /* Don't count "/", "//", etc., as having a trailing slash.  */
45   bool has_non_slash = false;
46   bool ends_in_slash = false;
47
48   for (file += FILE_SYSTEM_PREFIX_LEN (file); *file; file++)
49     {
50       ends_in_slash = ISSLASH (*file);
51       has_non_slash |= ~ ends_in_slash;
52     }
53
54   return has_non_slash & ends_in_slash;
55 }
56
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.  */
64 int
65 rpl_rename_dest_slash (char const *src, char const *dst)
66 {
67   int ret_val = rename (src, dst);
68
69   if (ret_val != 0 && errno == ENOENT && has_trailing_slash (dst))
70     {
71       int rename_errno = ENOENT;
72
73       /* Fail now, unless SRC is a directory.  */
74       struct stat sb;
75       if (lstat (src, &sb) == 0 && S_ISDIR (sb.st_mode))
76         {
77           char *dst_temp = xstrdup (dst);
78           strip_trailing_slashes (dst_temp);
79           ret_val = rename (src, dst_temp);
80           rename_errno = errno;
81           free (dst_temp);
82         }
83
84       errno = rename_errno;
85     }
86
87   return ret_val;
88 }