1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-19 02:00:26 +02:00

add `rename' gnulib module

This should fix `rename' on mingw32.
This commit is contained in:
Andy Wingo 2011-06-16 23:43:44 +02:00
parent 7505c6e024
commit a67f2fce54
13 changed files with 1103 additions and 2 deletions

View file

@ -9,7 +9,7 @@
# the same distribution terms as the rest of that program. # the same distribution terms as the rest of that program.
# #
# Generated by gnulib-tool. # Generated by gnulib-tool.
# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil close connect duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen locale log1p maintainer-makefile malloc-gnu malloca nproc open pipe2 putenv recv recvfrom send sendto setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar # Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil close connect duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen locale log1p maintainer-makefile malloc-gnu malloca nproc open pipe2 putenv recv recvfrom rename send sendto setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar
AUTOMAKE_OPTIONS = 1.5 gnits subdir-objects AUTOMAKE_OPTIONS = 1.5 gnits subdir-objects
@ -277,6 +277,14 @@ EXTRA_libgnu_la_SOURCES += connect.c
## end gnulib module connect ## end gnulib module connect
## begin gnulib module dirname-lgpl
libgnu_la_SOURCES += dirname-lgpl.c basename-lgpl.c stripslash.c
EXTRA_DIST += dirname.h
## end gnulib module dirname-lgpl
## begin gnulib module dosname ## begin gnulib module dosname
@ -1111,6 +1119,24 @@ EXTRA_libgnu_la_SOURCES += recvfrom.c
## end gnulib module recvfrom ## end gnulib module recvfrom
## begin gnulib module rename
EXTRA_DIST += rename.c
EXTRA_libgnu_la_SOURCES += rename.c
## end gnulib module rename
## begin gnulib module rmdir
EXTRA_DIST += rmdir.c
EXTRA_libgnu_la_SOURCES += rmdir.c
## end gnulib module rmdir
## begin gnulib module safe-read ## begin gnulib module safe-read
libgnu_la_SOURCES += safe-read.c libgnu_la_SOURCES += safe-read.c
@ -1129,6 +1155,13 @@ EXTRA_libgnu_la_SOURCES += safe-read.c
## end gnulib module safe-write ## end gnulib module safe-write
## begin gnulib module same-inode
EXTRA_DIST += same-inode.h
## end gnulib module same-inode
## begin gnulib module send ## begin gnulib module send

75
lib/basename-lgpl.c Normal file
View file

@ -0,0 +1,75 @@
/* basename.c -- return the last element in a file name
Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "dirname.h"
#include <string.h>
/* Return the address of the last file name component of NAME. If
NAME has no relative file name components because it is a file
system root, return the empty string. */
char *
last_component (char const *name)
{
char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
char const *p;
bool saw_slash = false;
while (ISSLASH (*base))
base++;
for (p = base; *p; p++)
{
if (ISSLASH (*p))
saw_slash = true;
else if (saw_slash)
{
base = p;
saw_slash = false;
}
}
return (char *) base;
}
/* Return the length of the basename NAME. Typically NAME is the
value returned by base_name or last_component. Act like strlen
(NAME), except omit all trailing slashes. */
size_t
base_len (char const *name)
{
size_t len;
size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
continue;
if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
&& ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
return 2;
if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
&& len == prefix_len && ISSLASH (name[prefix_len]))
return prefix_len + 1;
return len;
}

86
lib/dirname-lgpl.c Normal file
View file

@ -0,0 +1,86 @@
/* dirname.c -- return all but the last element in a file name
Copyright (C) 1990, 1998, 2000-2001, 2003-2006, 2009-2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "dirname.h"
#include <stdlib.h>
#include <string.h>
/* Return the length of the prefix of FILE that will be used by
dir_name. If FILE is in the working directory, this returns zero
even though `dir_name (FILE)' will return ".". Works properly even
if there are trailing slashes (by effectively ignoring them). */
size_t
dir_len (char const *file)
{
size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
size_t length;
/* Advance prefix_length beyond important leading slashes. */
prefix_length += (prefix_length != 0
? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
&& ISSLASH (file[prefix_length]))
: (ISSLASH (file[0])
? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
&& ISSLASH (file[1]) && ! ISSLASH (file[2])
? 2 : 1))
: 0));
/* Strip the basename and any redundant slashes before it. */
for (length = last_component (file) - file;
prefix_length < length; length--)
if (! ISSLASH (file[length - 1]))
break;
return length;
}
/* In general, we can't use the builtin `dirname' function if available,
since it has different meanings in different environments.
In some environments the builtin `dirname' modifies its argument.
Return the leading directories part of FILE, allocated with malloc.
Works properly even if there are trailing slashes (by effectively
ignoring them). Return NULL on failure.
If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
lstat (base_name (FILE)); } will access the same file. Likewise,
if the sequence { chdir (dir_name (FILE));
rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
to "foo" in the same directory FILE was in. */
char *
mdir_name (char const *file)
{
size_t length = dir_len (file);
bool append_dot = (length == 0
|| (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
&& length == FILE_SYSTEM_PREFIX_LEN (file)
&& file[2] != '\0' && ! ISSLASH (file[2])));
char *dir = malloc (length + append_dot + 1);
if (!dir)
return NULL;
memcpy (dir, file, length);
if (append_dot)
dir[length++] = '.';
dir[length] = '\0';
return dir;
}

46
lib/dirname.h Normal file
View file

@ -0,0 +1,46 @@
/* Take file names apart into directory and base names.
Copyright (C) 1998, 2001, 2003-2006, 2009-2011 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef DIRNAME_H_
# define DIRNAME_H_ 1
# include <stdbool.h>
# include <stddef.h>
# include "dosname.h"
# ifndef DIRECTORY_SEPARATOR
# define DIRECTORY_SEPARATOR '/'
# endif
# ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
# define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
# endif
# if GNULIB_DIRNAME
char *base_name (char const *file);
char *dir_name (char const *file);
# endif
char *mdir_name (char const *file);
size_t base_len (char const *file);
size_t dir_len (char const *file);
char *last_component (char const *file);
bool strip_trailing_slashes (char *file);
#endif /* not DIRNAME_H_ */

473
lib/rename.c Normal file
View file

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

53
lib/rmdir.c Normal file
View file

@ -0,0 +1,53 @@
/* Work around rmdir bugs.
Copyright (C) 1988, 1990, 1999, 2003-2006, 2009-2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "dosname.h"
#undef rmdir
/* Remove directory DIR.
Return 0 if successful, -1 if not. */
int
rpl_rmdir (char const *dir)
{
/* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds. */
size_t len = strlen (dir);
int result;
while (len && ISSLASH (dir[len - 1]))
len--;
if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2])))
{
errno = EINVAL;
return -1;
}
result = rmdir (dir);
/* Work around mingw bug, where rmdir("file/") fails with EINVAL
instead of ENOTDIR. We've already filtered out trailing ., the
only reason allowed by POSIX for EINVAL. */
if (result == -1 && errno == EINVAL)
errno = ENOTDIR;
return result;
}

25
lib/same-inode.h Normal file
View file

@ -0,0 +1,25 @@
/* Determine whether two stat buffers refer to the same file.
Copyright (C) 2006, 2009-2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef SAME_INODE_H
# define SAME_INODE_H 1
# define SAME_INODE(Stat_buf_1, Stat_buf_2) \
((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
&& (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
#endif

45
lib/stripslash.c Normal file
View file

@ -0,0 +1,45 @@
/* stripslash.c -- remove redundant trailing slashes from a file name
Copyright (C) 1990, 2001, 2003-2006, 2009-2011 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "dirname.h"
/* Remove trailing slashes from FILE. Return true if a trailing slash
was removed. This is useful when using file name completion from a
shell that adds a "/" after directory names (such as tcsh and
bash), because on symlinks to directories, several system calls
have different semantics according to whether a trailing slash is
present. */
bool
strip_trailing_slashes (char *file)
{
char *base = last_component (file);
char *base_lim;
bool had_slash;
/* last_component returns "" for file system roots, but we need to turn
`///' into `/'. */
if (! *base)
base = file;
base_lim = base + base_len (base);
had_slash = (*base_lim != '\0');
*base_lim = '\0';
return had_slash;
}

19
m4/dirname.m4 Normal file
View file

@ -0,0 +1,19 @@
#serial 10 -*- autoconf -*-
dnl Copyright (C) 2002-2006, 2009-2011 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_DIRNAME],
[
AC_REQUIRE([gl_DIRNAME_LGPL])
])
AC_DEFUN([gl_DIRNAME_LGPL],
[
dnl Prerequisites of lib/dirname.h.
AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
dnl No prerequisites of lib/basename-lgpl.c, lib/dirname-lgpl.c,
dnl lib/stripslash.c.
])

View file

@ -15,7 +15,7 @@
# Specification in the form of a command-line invocation: # Specification in the form of a command-line invocation:
# gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil close connect duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen locale log1p maintainer-makefile malloc-gnu malloca nproc open pipe2 putenv recv recvfrom send sendto setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar # gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil close connect duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen locale log1p maintainer-makefile malloc-gnu malloca nproc open pipe2 putenv recv recvfrom rename send sendto setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar
# Specification in the form of a few gnulib-tool.m4 macro invocations: # Specification in the form of a few gnulib-tool.m4 macro invocations:
gl_LOCAL_DIR([]) gl_LOCAL_DIR([])
@ -72,6 +72,7 @@ gl_MODULES([
putenv putenv
recv recv
recvfrom recvfrom
rename
send send
sendto sendto
setsockopt setsockopt

View file

@ -46,7 +46,9 @@ AC_DEFUN([gl_EARLY],
# Code from module ceil: # Code from module ceil:
# Code from module close: # Code from module close:
# Code from module connect: # Code from module connect:
# Code from module dirname-lgpl:
# Code from module dosname: # Code from module dosname:
# Code from module double-slash-root:
# Code from module duplocale: # Code from module duplocale:
# Code from module environ: # Code from module environ:
# Code from module errno: # Code from module errno:
@ -130,8 +132,11 @@ AC_DEFUN([gl_EARLY],
# Code from module readlink: # Code from module readlink:
# Code from module recv: # Code from module recv:
# Code from module recvfrom: # Code from module recvfrom:
# Code from module rename:
# Code from module rmdir:
# Code from module safe-read: # Code from module safe-read:
# Code from module safe-write: # Code from module safe-write:
# Code from module same-inode:
# Code from module send: # Code from module send:
# Code from module sendto: # Code from module sendto:
# Code from module servent: # Code from module servent:
@ -230,6 +235,8 @@ if test "$ac_cv_header_winsock2_h" = yes; then
AC_LIBOBJ([connect]) AC_LIBOBJ([connect])
fi fi
gl_SYS_SOCKET_MODULE_INDICATOR([connect]) gl_SYS_SOCKET_MODULE_INDICATOR([connect])
gl_DIRNAME_LGPL
gl_DOUBLE_SLASH_ROOT
gl_FUNC_DUPLOCALE gl_FUNC_DUPLOCALE
if test $REPLACE_DUPLOCALE = 1; then if test $REPLACE_DUPLOCALE = 1; then
AC_LIBOBJ([duplocale]) AC_LIBOBJ([duplocale])
@ -472,6 +479,16 @@ if test "$ac_cv_header_winsock2_h" = yes; then
AC_LIBOBJ([recvfrom]) AC_LIBOBJ([recvfrom])
fi fi
gl_SYS_SOCKET_MODULE_INDICATOR([recvfrom]) gl_SYS_SOCKET_MODULE_INDICATOR([recvfrom])
gl_FUNC_RENAME
if test $REPLACE_RENAME = 1; then
AC_LIBOBJ([rename])
fi
gl_STDIO_MODULE_INDICATOR([rename])
gl_FUNC_RMDIR
if test $REPLACE_RMDIR = 1; then
AC_LIBOBJ([rmdir])
fi
gl_UNISTD_MODULE_INDICATOR([rmdir])
gl_PREREQ_SAFE_READ gl_PREREQ_SAFE_READ
gl_PREREQ_SAFE_WRITE gl_PREREQ_SAFE_WRITE
AC_REQUIRE([gl_HEADER_SYS_SOCKET]) AC_REQUIRE([gl_HEADER_SYS_SOCKET])
@ -736,6 +753,7 @@ AC_DEFUN([gl_FILE_LIST], [
lib/alloca.in.h lib/alloca.in.h
lib/arpa_inet.in.h lib/arpa_inet.in.h
lib/asnprintf.c lib/asnprintf.c
lib/basename-lgpl.c
lib/binary-io.h lib/binary-io.h
lib/bind.c lib/bind.c
lib/byteswap.in.h lib/byteswap.in.h
@ -749,6 +767,8 @@ AC_DEFUN([gl_FILE_LIST], [
lib/ceil.c lib/ceil.c
lib/close.c lib/close.c
lib/connect.c lib/connect.c
lib/dirname-lgpl.c
lib/dirname.h
lib/dosname.h lib/dosname.h
lib/duplocale.c lib/duplocale.c
lib/errno.in.h lib/errno.in.h
@ -825,10 +845,13 @@ AC_DEFUN([gl_FILE_LIST], [
lib/readlink.c lib/readlink.c
lib/recv.c lib/recv.c
lib/recvfrom.c lib/recvfrom.c
lib/rename.c
lib/rmdir.c
lib/safe-read.c lib/safe-read.c
lib/safe-read.h lib/safe-read.h
lib/safe-write.c lib/safe-write.c
lib/safe-write.h lib/safe-write.h
lib/same-inode.h
lib/send.c lib/send.c
lib/sendto.c lib/sendto.c
lib/setsockopt.c lib/setsockopt.c
@ -851,6 +874,7 @@ AC_DEFUN([gl_FILE_LIST], [
lib/striconveh.c lib/striconveh.c
lib/striconveh.h lib/striconveh.h
lib/string.in.h lib/string.in.h
lib/stripslash.c
lib/sys_file.in.h lib/sys_file.in.h
lib/sys_socket.in.h lib/sys_socket.in.h
lib/sys_stat.in.h lib/sys_stat.in.h
@ -888,6 +912,7 @@ AC_DEFUN([gl_FILE_LIST], [
m4/ceil.m4 m4/ceil.m4
m4/check-math-lib.m4 m4/check-math-lib.m4
m4/close.m4 m4/close.m4
m4/dirname.m4
m4/double-slash-root.m4 m4/double-slash-root.m4
m4/duplocale.m4 m4/duplocale.m4
m4/eealloc.m4 m4/eealloc.m4
@ -961,6 +986,8 @@ AC_DEFUN([gl_FILE_LIST], [
m4/putenv.m4 m4/putenv.m4
m4/read.m4 m4/read.m4
m4/readlink.m4 m4/readlink.m4
m4/rename.m4
m4/rmdir.m4
m4/safe-read.m4 m4/safe-read.m4
m4/safe-write.m4 m4/safe-write.m4
m4/servent.m4 m4/servent.m4

184
m4/rename.m4 Normal file
View file

@ -0,0 +1,184 @@
# serial 24
# Copyright (C) 2001, 2003, 2005-2006, 2009-2011 Free Software Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
dnl From Volker Borchert.
dnl Determine whether rename works for source file names with a trailing slash.
dnl The rename from SunOS 4.1.1_U1 doesn't.
dnl
dnl If it doesn't, then define RENAME_TRAILING_SLASH_BUG and arrange
dnl to compile the wrapper function.
dnl
AC_DEFUN([gl_FUNC_RENAME],
[
AC_REQUIRE([AC_CANONICAL_HOST])
AC_REQUIRE([gl_STDIO_H_DEFAULTS])
AC_CHECK_FUNCS_ONCE([lstat])
dnl Solaris 10, AIX 7.1 mistakenly allow rename("file","name/").
dnl NetBSD 1.6 mistakenly forbids rename("dir","name/").
dnl FreeBSD 7.2 mistakenly allows rename("file","link-to-file/").
dnl The Solaris bug can be worked around without stripping
dnl trailing slash, while the NetBSD bug requires stripping;
dnl the two conditions can be distinguished by whether hard
dnl links are also broken.
AC_CACHE_CHECK([whether rename honors trailing slash on destination],
[gl_cv_func_rename_slash_dst_works],
[rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
AC_MSG_ERROR([cannot create temporary files])
# Assume that if we have lstat, we can also check symlinks.
if test $ac_cv_func_lstat = yes; then
ln -s conftest.f conftest.lnk
fi
AC_RUN_IFELSE(
[AC_LANG_PROGRAM([[
# include <stdio.h>
# include <stdlib.h>
]],
[[int result = 0;
if (rename ("conftest.f1", "conftest.f2/") == 0)
result |= 1;
if (rename ("conftest.d1", "conftest.d2/") != 0)
result |= 2;
#if HAVE_LSTAT
if (rename ("conftest.f", "conftest.lnk/") == 0)
result |= 4;
#endif
return result;
]])],
[gl_cv_func_rename_slash_dst_works=yes],
[gl_cv_func_rename_slash_dst_works=no],
dnl When crosscompiling, assume rename is broken.
[gl_cv_func_rename_slash_dst_works="guessing no"])
rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
])
if test "x$gl_cv_func_rename_slash_dst_works" != xyes; then
REPLACE_RENAME=1
AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
[Define if rename does not correctly handle slashes on the destination
argument, such as on Solaris 10 or NetBSD 1.6.])
fi
dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
dnl Solaris 9 mistakenly allows rename("file/","name").
dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name").
dnl These bugs require stripping trailing slash to avoid corrupting
dnl symlinks with a trailing slash.
AC_CACHE_CHECK([whether rename honors trailing slash on source],
[gl_cv_func_rename_slash_src_works],
[rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
AC_MSG_ERROR([cannot create temporary files])
# Assume that if we have lstat, we can also check symlinks.
if test $ac_cv_func_lstat = yes; then
ln -s conftest.f conftest.lnk
fi
AC_RUN_IFELSE(
[AC_LANG_PROGRAM([[
# include <stdio.h>
# include <stdlib.h>
]],
[[int result = 0;
if (rename ("conftest.f1/", "conftest.d3") == 0)
result |= 1;
if (rename ("conftest.d1/", "conftest.d2") != 0)
result |= 2;
#if HAVE_LSTAT
if (rename ("conftest.lnk/", "conftest.f") == 0)
result |= 4;
#endif
return result;
]])],
[gl_cv_func_rename_slash_src_works=yes],
[gl_cv_func_rename_slash_src_works=no],
dnl When crosscompiling, assume rename is broken.
[gl_cv_func_rename_slash_src_works="guessing no"])
rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
])
if test "x$gl_cv_func_rename_slash_src_works" != xyes; then
REPLACE_RENAME=1
AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
[Define if rename does not correctly handle slashes on the source
argument, such as on Solaris 9 or cygwin 1.5.])
fi
dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
dnl on rename("h1","h2").
dnl This bug requires stat'ting targets prior to attempting rename.
AC_CACHE_CHECK([whether rename manages hard links correctly],
[gl_cv_func_rename_link_works],
[rm -rf conftest.f conftest.f1
if touch conftest.f && ln conftest.f conftest.f1 &&
set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
AC_RUN_IFELSE(
[AC_LANG_PROGRAM([[
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
]],
[[int result = 0;
if (rename ("conftest.f", "conftest.f1"))
result |= 1;
if (unlink ("conftest.f1"))
result |= 2;
if (rename ("conftest.f", "conftest.f"))
result |= 4;
if (rename ("conftest.f1", "conftest.f1") == 0)
result |= 8;
return result;
]])],
[gl_cv_func_rename_link_works=yes],
[gl_cv_func_rename_link_works=no],
dnl When crosscompiling, assume rename is broken.
[gl_cv_func_rename_link_works="guessing no"])
else
gl_cv_func_rename_link_works="guessing no"
fi
rm -rf conftest.f conftest.f1
])
if test "x$gl_cv_func_rename_link_works" != xyes; then
REPLACE_RENAME=1
AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
[Define if rename fails to leave hard links alone, as on NetBSD 1.6
or Cygwin 1.5.])
fi
dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
dnl mingw mistakenly forbids rename("dir1","dir2").
dnl These bugs require stripping trailing slash to avoid corrupting
dnl symlinks with a trailing slash.
AC_CACHE_CHECK([whether rename manages existing destinations correctly],
[gl_cv_func_rename_dest_works],
[rm -rf conftest.f conftest.d1 conftest.d2
touch conftest.f && mkdir conftest.d1 conftest.d2 ||
AC_MSG_ERROR([cannot create temporary files])
AC_RUN_IFELSE(
[AC_LANG_PROGRAM([[
# include <stdio.h>
# include <stdlib.h>
]],
[[int result = 0;
if (rename ("conftest.d1", "conftest.d2") != 0)
result |= 1;
if (rename ("conftest.d2", "conftest.f") == 0)
result |= 2;
return result;
]])],
[gl_cv_func_rename_dest_works=yes],
[gl_cv_func_rename_dest_works=no],
dnl When crosscompiling, assume rename is broken.
[gl_cv_func_rename_dest_works="guessing no"])
rm -rf conftest.f conftest.d1 conftest.d2
])
if test "x$gl_cv_func_rename_dest_works" != xyes; then
REPLACE_RENAME=1
AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
[Define if rename does not work when the destination file exists,
as on Cygwin 1.5 or Windows.])
fi
])

34
m4/rmdir.m4 Normal file
View file

@ -0,0 +1,34 @@
# rmdir.m4 serial 11
dnl Copyright (C) 2002, 2005, 2009-2011 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_FUNC_RMDIR],
[
AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
dnl Detect cygwin 1.5.x bug.
AC_CACHE_CHECK([whether rmdir works], [gl_cv_func_rmdir_works],
[mkdir conftest.dir
touch conftest.file
AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[[#include <stdio.h>
#include <errno.h>
#include <unistd.h>
]], [[int result = 0;
if (!rmdir ("conftest.file/"))
result |= 1;
else if (errno != ENOTDIR)
result |= 2;
if (!rmdir ("conftest.dir/./"))
result |= 4;
return result;
]])],
[gl_cv_func_rmdir_works=yes], [gl_cv_func_rmdir_works=no],
[gl_cv_func_rmdir_works="guessing no"])
rm -rf conftest.dir conftest.file])
if test x"$gl_cv_func_rmdir_works" != xyes; then
REPLACE_RMDIR=1
fi
])