2000-02-18 03:38:33 +08:00
|
|
|
/* dir.cc: Posix directory-related routines
|
|
|
|
|
2002-01-14 04:03:03 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define _COMPILING_NEWLIB
|
2000-08-22 13:10:20 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "pinfo.h"
|
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "dtable.h"
|
2000-11-14 13:53:32 +08:00
|
|
|
#include "cygheap.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
/* Return whether the directory of a file is writable. Return 1 if it
|
|
|
|
is. Otherwise, return 0, and set errno appropriately. */
|
|
|
|
int __stdcall
|
|
|
|
writable_directory (const char *file)
|
|
|
|
{
|
2000-05-25 04:09:43 +08:00
|
|
|
#if 0
|
2000-02-18 03:38:33 +08:00
|
|
|
char dir[strlen (file) + 1];
|
|
|
|
|
|
|
|
strcpy (dir, file);
|
|
|
|
|
|
|
|
const char *usedir;
|
|
|
|
char *slash = strrchr (dir, '\\');
|
|
|
|
if (slash == NULL)
|
|
|
|
usedir = ".";
|
2000-05-24 03:25:18 +08:00
|
|
|
else if (slash == dir)
|
|
|
|
{
|
|
|
|
usedir = "\\";
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*slash = '\0';
|
|
|
|
usedir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
int acc = access (usedir, W_OK);
|
|
|
|
|
|
|
|
return acc == 0;
|
2000-05-25 04:09:43 +08:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-09-04 00:36:54 +08:00
|
|
|
extern "C" int
|
|
|
|
dirfd (DIR *dir)
|
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return -1;
|
2001-09-04 00:36:54 +08:00
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
syscall_printf ("-1 = dirfd (%p)", dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return dir->__d_dirent->d_fd;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* opendir: POSIX 5.1.2.1 */
|
|
|
|
extern "C" DIR *
|
2001-11-21 14:47:57 +08:00
|
|
|
opendir (const char *name)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
fhandler_base *fh;
|
|
|
|
path_conv pc;
|
2001-12-06 07:05:15 +08:00
|
|
|
DIR *res;
|
2001-11-21 14:47:57 +08:00
|
|
|
|
|
|
|
fh = cygheap->fdtab.build_fhandler_from_name (-1, name, NULL, pc,
|
|
|
|
PC_SYM_FOLLOW | PC_FULL, NULL);
|
2001-12-06 07:05:15 +08:00
|
|
|
if (!fh)
|
|
|
|
res = NULL;
|
2002-01-06 11:32:32 +08:00
|
|
|
else if (pc.exists ())
|
|
|
|
res = fh->opendir (pc);
|
2001-12-06 07:05:15 +08:00
|
|
|
else
|
|
|
|
{
|
2002-01-06 11:32:32 +08:00
|
|
|
set_errno (ENOENT);
|
|
|
|
res = NULL;
|
2001-12-06 07:05:15 +08:00
|
|
|
}
|
2002-01-06 11:32:32 +08:00
|
|
|
|
|
|
|
if (!res && fh)
|
|
|
|
delete fh;
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readdir: POSIX 5.1.2.1 */
|
|
|
|
extern "C" struct dirent *
|
2001-11-21 14:47:57 +08:00
|
|
|
readdir (DIR *dir)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
2001-11-21 14:47:57 +08:00
|
|
|
syscall_printf ("%p = readdir (%p)", NULL, dir);
|
|
|
|
return NULL;
|
2001-02-22 05:49:37 +08:00
|
|
|
}
|
|
|
|
|
2002-06-01 10:45:38 +08:00
|
|
|
dirent *res = ((fhandler_base *) dir->__d_u.__d_data.__fh)->readdir (dir);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
/* Compute d_ino by combining filename hash with the directory hash
|
|
|
|
(which was stored in dir->__d_dirhash when opendir was called). */
|
|
|
|
if (res->d_name[0] == '.')
|
|
|
|
{
|
|
|
|
if (res->d_name[1] == '\0')
|
|
|
|
dir->__d_dirent->d_ino = dir->__d_dirhash;
|
|
|
|
else if (res->d_name[1] != '.' || res->d_name[2] != '\0')
|
|
|
|
goto hashit;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *p, up[strlen (dir->__d_dirname) + 1];
|
|
|
|
strcpy (up, dir->__d_dirname);
|
|
|
|
if (!(p = strrchr (up, '\\')))
|
|
|
|
goto hashit;
|
|
|
|
*p = '\0';
|
|
|
|
if (!(p = strrchr (up, '\\')))
|
|
|
|
dir->__d_dirent->d_ino = hash_path_name (0, ".");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*p = '\0';
|
|
|
|
dir->__d_dirent->d_ino = hash_path_name (0, up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hashit:
|
2003-05-12 05:52:09 +08:00
|
|
|
__ino64_t dino = hash_path_name (dir->__d_dirhash, "\\");
|
2002-06-01 10:45:38 +08:00
|
|
|
dir->__d_dirent->d_ino = hash_path_name (dino, res->d_name);
|
|
|
|
}
|
|
|
|
}
|
2003-05-11 08:10:11 +08:00
|
|
|
dir->__d_dirent->old_d_ino = dir->__d_dirent->d_ino; // just truncate
|
2002-06-01 10:45:38 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-04-02 00:11:41 +08:00
|
|
|
extern "C" _off64_t
|
2002-02-26 01:47:51 +08:00
|
|
|
telldir64 (DIR *dir)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return -1;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
return 0;
|
2001-11-21 14:47:57 +08:00
|
|
|
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->telldir (dir);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
/* telldir */
|
2003-04-02 00:11:41 +08:00
|
|
|
extern "C" _off_t
|
2002-02-26 01:47:51 +08:00
|
|
|
telldir (DIR *dir)
|
|
|
|
{
|
|
|
|
return telldir64 (dir);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
2003-04-02 00:11:41 +08:00
|
|
|
seekdir64 (DIR *dir, _off64_t loc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
return;
|
2001-11-21 14:47:57 +08:00
|
|
|
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->seekdir (dir, loc);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-02-26 01:47:51 +08:00
|
|
|
/* seekdir */
|
|
|
|
extern "C" void
|
2003-04-02 00:11:41 +08:00
|
|
|
seekdir (DIR *dir, _off_t loc)
|
2002-02-26 01:47:51 +08:00
|
|
|
{
|
2003-04-02 00:11:41 +08:00
|
|
|
seekdir64 (dir, (_off64_t)loc);
|
2002-02-26 01:47:51 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* rewinddir: POSIX 5.1.2.1 */
|
|
|
|
extern "C" void
|
2001-11-21 14:47:57 +08:00
|
|
|
rewinddir (DIR *dir)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
return;
|
2001-11-21 14:47:57 +08:00
|
|
|
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->rewinddir (dir);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* closedir: POSIX 5.1.2.1 */
|
|
|
|
extern "C" int
|
2001-11-21 14:47:57 +08:00
|
|
|
closedir (DIR *dir)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-11-21 14:47:57 +08:00
|
|
|
if (check_null_invalid_struct_errno (dir))
|
|
|
|
return -1;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
syscall_printf ("-1 = closedir (%p)", dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the marker in case the caller tries to use `dir' again. */
|
|
|
|
dir->__d_cookie = 0;
|
|
|
|
|
2001-11-21 14:47:57 +08:00
|
|
|
int res = ((fhandler_base *) dir->__d_u.__d_data.__fh)->closedir (dir);
|
|
|
|
|
|
|
|
cygheap->fdtab.release (dir->__d_dirent->d_fd);
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
free (dir->__d_dirname);
|
|
|
|
free (dir->__d_dirent);
|
|
|
|
free (dir);
|
2001-11-21 14:47:57 +08:00
|
|
|
syscall_printf ("%d = closedir (%p)", res);
|
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mkdir: POSIX 5.4.1.1 */
|
|
|
|
extern "C" int
|
|
|
|
mkdir (const char *dir, mode_t mode)
|
|
|
|
{
|
|
|
|
int res = -1;
|
2001-08-07 23:09:54 +08:00
|
|
|
SECURITY_ATTRIBUTES sa = sec_none_nih;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-18 03:18:21 +08:00
|
|
|
path_conv real_dir (dir, PC_SYM_NOFOLLOW);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (real_dir.error)
|
|
|
|
{
|
2001-04-13 05:21:37 +08:00
|
|
|
set_errno (real_dir.case_clash ? ECASECLASH : real_dir.error);
|
2000-02-18 03:38:33 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-09-23 08:31:31 +08:00
|
|
|
nofinalslash (real_dir.get_win32 (), real_dir.get_win32 ());
|
2000-02-18 03:38:33 +08:00
|
|
|
if (! writable_directory (real_dir.get_win32 ()))
|
|
|
|
goto done;
|
|
|
|
|
2001-08-07 23:09:54 +08:00
|
|
|
if (allow_ntsec && real_dir.has_acls ())
|
2001-08-08 00:14:59 +08:00
|
|
|
set_security_attribute (S_IFDIR | ((mode & 07777) & ~cygheap->umask),
|
2001-09-06 11:39:18 +08:00
|
|
|
&sa, alloca (4096), 4096);
|
2001-08-07 23:09:54 +08:00
|
|
|
|
|
|
|
if (CreateDirectoryA (real_dir.get_win32 (), &sa))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-08-07 23:09:54 +08:00
|
|
|
if (!allow_ntsec && allow_ntea)
|
|
|
|
set_file_attribute (real_dir.has_acls (), real_dir.get_win32 (),
|
2001-08-08 00:14:59 +08:00
|
|
|
S_IFDIR | ((mode & 07777) & ~cygheap->umask));
|
2001-11-14 05:49:06 +08:00
|
|
|
#ifdef HIDDEN_DOT_FILES
|
|
|
|
char *c = strrchr (real_dir.get_win32 (), '\\');
|
|
|
|
if ((c && c[1] == '.') || *real_dir.get_win32 () == '.')
|
2002-05-28 09:55:40 +08:00
|
|
|
SetFileAttributes (real_dir.get_win32 (), FILE_ATTRIBUTE_HIDDEN);
|
2001-11-14 05:49:06 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__seterrno ();
|
|
|
|
|
|
|
|
done:
|
|
|
|
syscall_printf ("%d = mkdir (%s, %d)", res, dir, mode);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rmdir: POSIX 5.5.2.1 */
|
|
|
|
extern "C" int
|
|
|
|
rmdir (const char *dir)
|
|
|
|
{
|
|
|
|
int res = -1;
|
2002-06-06 00:01:55 +08:00
|
|
|
DWORD devn;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-02-22 22:51:16 +08:00
|
|
|
path_conv real_dir (dir, PC_SYM_NOFOLLOW);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (real_dir.error)
|
2002-06-06 00:01:55 +08:00
|
|
|
set_errno (real_dir.error);
|
|
|
|
else if ((devn = real_dir.get_devn ()) == FH_PROC || devn == FH_REGISTRY
|
|
|
|
|| devn == FH_PROCESS)
|
|
|
|
set_errno (EROFS);
|
2001-10-01 12:10:07 +08:00
|
|
|
else if (!real_dir.exists ())
|
2002-06-06 00:01:55 +08:00
|
|
|
set_errno (ENOENT);
|
2001-10-01 12:10:07 +08:00
|
|
|
else if (!real_dir.isdir ())
|
2002-06-06 00:01:55 +08:00
|
|
|
set_errno (ENOTDIR);
|
2001-05-13 00:24:05 +08:00
|
|
|
else
|
2000-05-23 01:15:47 +08:00
|
|
|
{
|
2001-10-01 12:10:07 +08:00
|
|
|
/* Even own directories can't be removed if R/O attribute is set. */
|
|
|
|
if (real_dir.has_attribute (FILE_ATTRIBUTE_READONLY))
|
|
|
|
SetFileAttributes (real_dir,
|
2001-11-05 14:09:15 +08:00
|
|
|
(DWORD) real_dir & ~FILE_ATTRIBUTE_READONLY);
|
2001-10-01 12:10:07 +08:00
|
|
|
|
|
|
|
if (RemoveDirectory (real_dir))
|
2001-09-18 02:10:02 +08:00
|
|
|
{
|
2001-10-01 12:10:07 +08:00
|
|
|
/* RemoveDirectory on a samba drive doesn't return an error if the
|
|
|
|
directory can't be removed because it's not empty. Checking for
|
|
|
|
existence afterwards keeps us informed about success. */
|
2002-01-15 04:39:59 +08:00
|
|
|
if (GetFileAttributes (real_dir) != INVALID_FILE_ATTRIBUTES)
|
2001-10-01 12:10:07 +08:00
|
|
|
set_errno (ENOTEMPTY);
|
2001-09-18 02:10:02 +08:00
|
|
|
else
|
2001-10-01 12:10:07 +08:00
|
|
|
res = 0;
|
2001-09-18 02:10:02 +08:00
|
|
|
}
|
2001-10-01 12:10:07 +08:00
|
|
|
else
|
2001-09-08 05:32:07 +08:00
|
|
|
{
|
2001-10-01 12:10:07 +08:00
|
|
|
/* This kludge detects if we are attempting to remove the current working
|
|
|
|
directory. If so, we will move elsewhere to potentially allow the
|
|
|
|
rmdir to succeed. This means that cygwin's concept of the current working
|
|
|
|
directory != Windows concept but, hey, whaddaregonnado?
|
|
|
|
Note that this will not cause something like the following to work:
|
|
|
|
$ cd foo
|
|
|
|
$ rmdir .
|
|
|
|
since the shell will have foo "open" in the above case and so Windows will
|
|
|
|
not allow the deletion.
|
|
|
|
FIXME: A potential workaround for this is for cygwin apps to *never* call
|
|
|
|
SetCurrentDirectory. */
|
|
|
|
if (strcasematch (real_dir, cygheap->cwd.win32)
|
|
|
|
&& !strcasematch ("c:\\", cygheap->cwd.win32))
|
|
|
|
{
|
|
|
|
DWORD err = GetLastError ();
|
|
|
|
if (!SetCurrentDirectory ("c:\\"))
|
|
|
|
SetLastError (err);
|
|
|
|
else if ((res = rmdir (dir)))
|
|
|
|
SetCurrentDirectory (cygheap->cwd.win32);
|
|
|
|
}
|
2002-06-06 00:01:55 +08:00
|
|
|
if (res)
|
2001-10-01 12:10:07 +08:00
|
|
|
{
|
2002-06-06 00:01:55 +08:00
|
|
|
if (GetLastError () != ERROR_ACCESS_DENIED
|
|
|
|
|| !wincap.access_denied_on_delete ())
|
2001-10-01 12:10:07 +08:00
|
|
|
__seterrno ();
|
2002-06-06 00:01:55 +08:00
|
|
|
else
|
|
|
|
set_errno (ENOTEMPTY); /* On 9X ERROR_ACCESS_DENIED is
|
|
|
|
returned if you try to remove a
|
|
|
|
non-empty directory. */
|
2001-05-13 00:24:05 +08:00
|
|
|
|
2002-06-06 00:01:55 +08:00
|
|
|
/* If directory still exists, restore R/O attribute. */
|
|
|
|
if (real_dir.has_attribute (FILE_ATTRIBUTE_READONLY))
|
|
|
|
SetFileAttributes (real_dir, real_dir);
|
|
|
|
}
|
2001-10-01 12:10:07 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
syscall_printf ("%d = rmdir (%s)", res, dir);
|
|
|
|
return res;
|
|
|
|
}
|