mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-02-02 20:40:24 +08:00
9e5f45ed6f
* dir.cc (__opendir_with_d_ino): Just call opendir. (opendir): Remove CYGWIN_VERSION_CHECK_FOR_NEEDS_D_INO handling. (readdir_worker): Only try generating d_ino if it's 0. Utilize namehash of directories fhandler. Call readdir_get_ino to generate d_ino for "..". (seekdir64): Keep dirent_set_d_ino flag. * fhandler.h (enum dirent_states): Add dirent_get_d_ino. (class fhandler_disk_file): Declare new private methods readdir_helper and readdir_9x. * fhandler_disk_file.cc (path_conv::hasgood_inode): New method to evaluate if a filesystem has reliable inode numbers. (fhandler_base::fstat_by_handle): Accomodate structure member name change from IndexNumber to FileId. (fhandler_base::fstat_helper): Call hasgood_inode here. (fhandler_disk_file::opendir): Call fhaccess only for real files. Don't append '*' to __d_dirname here, move to readdir_9x. On NT, open directory handle here. Set dirent_get_d_ino and dirent_set_d_ino flags according to wincap and filesystem. (fhandler_disk_file::readdir_helper): New method to implement readdir postprocessing only once. (readdir_get_ino_by_handle): New static function. (readdir_get_ino): New function to centralize inode number evaluation in case inode number hasn't been returned by NtQueryDirectoryFile. (fhandler_disk_file::readdir): Move old functionality to readdir_9x. Call readdir_9x when on 9x/Me. Implement NT specific readdir here. (fhandler_disk_file::readdir_9x): Move 9x specific readdir here. (fhandler_disk_file::seekdir): Accomodate new NT readdir method. (fhandler_disk_file::closedir): Ditto. (fhandler_cygdrive::fstat): Set d_ino to namehash. Add comment. (fhandler_cygdrive::opendir): Call get_namehash to prepare later correct evaluation of d_ino. (fhandler_cygdrive::readdir): Replace recursion with loop. Evaluate drive's d_ino by calling readdir_get_ino. * fhandler_proc.cc (fhandler_proc::readdir): Set dirent_saw_dot and dirent_saw_dot_dot to avoid seeing . and .. entries twice. * fhandler_process.cc (fhandler_process::readdir): Ditto. * fhandler_registry.cc (fhandler_registry::readdir): Ditto. * ntdll.h (STATUS_INVALID_PARAMETER): New define. (STATUS_INVALID_LEVEL): New define. (struct _FILE_INTERNAL_INFORMATION): Rename member IndexNumber to FileId (as in Nebbitt). * path.h (path_conv::hasgood_inode): Now implemented in fhandler_disk_file.cc. * wincap.h (wincaps::has_fileid_dirinfo): New element. * wincap.cc: Implement above element throughout. * winsup.h (readdir_get_ino): Add declaration. * include/sys/dirent.h (struct dirent): Slightly rename structure members to accomodate changes. Remove __USE_EXPENSIVE_CYGWIN_D_INO handling and declaration of __opendir_with_d_ino.
322 lines
6.3 KiB
C++
322 lines
6.3 KiB
C++
/* dir.cc: Posix directory-related routines
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006 Red Hat, Inc.
|
|
|
|
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. */
|
|
|
|
#include "winsup.h"
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <sys/stat.h>
|
|
|
|
#define _COMPILING_NEWLIB
|
|
#include <dirent.h>
|
|
|
|
#include "pinfo.h"
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
#include "cygtls.h"
|
|
#include "perprocess.h"
|
|
#include "cygwin/version.h"
|
|
|
|
extern "C" int
|
|
dirfd (DIR *dir)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return -1;
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
{
|
|
set_errno (EBADF);
|
|
syscall_printf ("-1 = dirfd (%p)", dir);
|
|
return -1;
|
|
}
|
|
return dir->__d_fd;
|
|
}
|
|
|
|
/* Symbol kept for backward compatibility. Don't remove. Don't declare
|
|
in public header file. */
|
|
extern "C" DIR *
|
|
__opendir_with_d_ino (const char *name)
|
|
{
|
|
return opendir (name);
|
|
}
|
|
|
|
/* opendir: POSIX 5.1.2.1 */
|
|
extern "C" DIR *
|
|
opendir (const char *name)
|
|
{
|
|
fhandler_base *fh;
|
|
DIR *res;
|
|
|
|
fh = build_fh_name (name, NULL, PC_SYM_FOLLOW);
|
|
if (!fh)
|
|
res = NULL;
|
|
else if (fh->exists ())
|
|
res = fh->opendir ();
|
|
else
|
|
{
|
|
set_errno (ENOENT);
|
|
res = NULL;
|
|
}
|
|
|
|
if (!res && fh)
|
|
delete fh;
|
|
return res;
|
|
}
|
|
|
|
static int
|
|
readdir_worker (DIR *dir, dirent *de)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted ())
|
|
return EFAULT;
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
{
|
|
syscall_printf ("%p = readdir (%p)", NULL, dir);
|
|
return EBADF;
|
|
}
|
|
|
|
de->d_ino = 0;
|
|
int res = ((fhandler_base *) dir->__fh)->readdir (dir, de);
|
|
|
|
if (res == ENMFILE)
|
|
{
|
|
if (!(dir->__flags & dirent_saw_dot))
|
|
{
|
|
strcpy (de->d_name, ".");
|
|
dir->__flags |= dirent_saw_dot;
|
|
dir->__d_position++;
|
|
res = 0;
|
|
}
|
|
else if (!(dir->__flags & dirent_saw_dot_dot))
|
|
{
|
|
strcpy (de->d_name, "..");
|
|
dir->__flags |= dirent_saw_dot_dot;
|
|
dir->__d_position++;
|
|
res = 0;
|
|
}
|
|
}
|
|
|
|
if (!res && !de->d_ino)
|
|
{
|
|
bool is_dot = false;
|
|
bool is_dot_dot = false;
|
|
|
|
if (de->d_name[0] == '.')
|
|
{
|
|
if (de->d_name[1] == '\0')
|
|
is_dot = true;
|
|
else if (de->d_name[1] == '.' && de->d_name[2] == '\0')
|
|
is_dot_dot = true;
|
|
}
|
|
|
|
if (is_dot_dot && !(dir->__flags & dirent_isroot))
|
|
de->d_ino = readdir_get_ino (dir,
|
|
((fhandler_base *) dir->__fh)->get_name (),
|
|
true);
|
|
else
|
|
{
|
|
/* Compute d_ino by combining filename hash with directory hash. */
|
|
de->d_ino = ((fhandler_base *) dir->__fh)->get_namehash ();
|
|
if (!is_dot && !is_dot_dot)
|
|
{
|
|
const char *w32name = ((fhandler_base *) dir->__fh)->get_win32_name ();
|
|
/* A drive's root dir has a trailing backslash already. */
|
|
if (w32name[1] != ':' || w32name[2] != '\\' || w32name[3])
|
|
de->d_ino = hash_path_name (de->d_ino, "\\");
|
|
de->d_ino = hash_path_name (de->d_ino, de->d_name);
|
|
}
|
|
}
|
|
}
|
|
de->__d_internal1 = de->d_ino;
|
|
|
|
return res;
|
|
}
|
|
|
|
/* readdir: POSIX 5.1.2.1 */
|
|
extern "C" struct dirent *
|
|
readdir (DIR *dir)
|
|
{
|
|
int res = readdir_worker (dir, dir->__d_dirent);
|
|
if (res == 0)
|
|
return dir->__d_dirent;
|
|
if (res != ENMFILE)
|
|
set_errno (res);
|
|
return NULL;
|
|
}
|
|
|
|
extern "C" int
|
|
readdir_r (DIR *dir, dirent *de, dirent **ode)
|
|
{
|
|
int res = readdir_worker (dir, de);
|
|
if (!res)
|
|
*ode = de;
|
|
else
|
|
{
|
|
*ode = NULL;
|
|
if (res == ENMFILE)
|
|
res = 0;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
extern "C" _off64_t
|
|
telldir64 (DIR *dir)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return -1;
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
return 0;
|
|
return ((fhandler_base *) dir->__fh)->telldir (dir);
|
|
}
|
|
|
|
/* telldir */
|
|
extern "C" _off_t
|
|
telldir (DIR *dir)
|
|
{
|
|
return telldir64 (dir);
|
|
}
|
|
|
|
extern "C" void
|
|
seekdir64 (DIR *dir, _off64_t loc)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return;
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
return;
|
|
dir->__flags &= (dirent_isroot | dirent_get_d_ino | dirent_set_d_ino);
|
|
return ((fhandler_base *) dir->__fh)->seekdir (dir, loc);
|
|
}
|
|
|
|
/* seekdir */
|
|
extern "C" void
|
|
seekdir (DIR *dir, _off_t loc)
|
|
{
|
|
seekdir64 (dir, (_off64_t)loc);
|
|
}
|
|
|
|
/* rewinddir: POSIX 5.1.2.1 */
|
|
extern "C" void
|
|
rewinddir (DIR *dir)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return;
|
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
|
return;
|
|
dir->__flags &= dirent_isroot;
|
|
return ((fhandler_base *) dir->__fh)->rewinddir (dir);
|
|
}
|
|
|
|
/* closedir: POSIX 5.1.2.1 */
|
|
extern "C" int
|
|
closedir (DIR *dir)
|
|
{
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return -1;
|
|
|
|
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;
|
|
|
|
int res = ((fhandler_base *) dir->__fh)->closedir (dir);
|
|
|
|
cygheap->fdtab.release (dir->__d_fd);
|
|
|
|
free (dir->__d_dirname);
|
|
free (dir->__d_dirent);
|
|
free (dir);
|
|
syscall_printf ("%d = closedir (%p)", res);
|
|
return res;
|
|
}
|
|
|
|
/* mkdir: POSIX 5.4.1.1 */
|
|
extern "C" int
|
|
mkdir (const char *dir, mode_t mode)
|
|
{
|
|
int res = -1;
|
|
fhandler_base *fh = NULL;
|
|
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return -1;
|
|
|
|
if (has_dot_last_component (dir))
|
|
{
|
|
set_errno (ENOENT);
|
|
return -1;
|
|
}
|
|
|
|
if (!(fh = build_fh_name (dir, NULL, PC_SYM_NOFOLLOW)))
|
|
goto done; /* errno already set */;
|
|
|
|
if (fh->error ())
|
|
{
|
|
debug_printf ("got %d error from build_fh_name", fh->error ());
|
|
set_errno (fh->error ());
|
|
}
|
|
else if (!fh->mkdir (mode))
|
|
res = 0;
|
|
delete fh;
|
|
|
|
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;
|
|
fhandler_base *fh = NULL;
|
|
|
|
myfault efault;
|
|
if (efault.faulted (EFAULT))
|
|
return -1;
|
|
|
|
if (has_dot_last_component (dir))
|
|
{
|
|
set_errno (EINVAL);
|
|
return -1;
|
|
}
|
|
|
|
if (!(fh = build_fh_name (dir, NULL, PC_SYM_NOFOLLOW)))
|
|
goto done; /* errno already set */;
|
|
|
|
if (fh->error ())
|
|
{
|
|
debug_printf ("got %d error from build_fh_name", fh->error ());
|
|
set_errno (fh->error ());
|
|
}
|
|
else if (!fh->rmdir ())
|
|
res = 0;
|
|
delete fh;
|
|
|
|
done:
|
|
syscall_printf ("%d = rmdir (%s)", res, dir);
|
|
return res;
|
|
}
|