mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-15 02:09:19 +08:00
636c94d853
Revamp advisory file locking to avoid cross reference pointers as well as to allow BSD flock semantics. More agressively delete unused nodes and sync objects. * fhandler.h (fhandler_base::ino): Rename from namehash. Fix comment. (fhandler_base::node): Remove. (fhandler_base::unique_id): Add. (fhandler_base::del_my_locks): New method. (get_ino): Rename from get_namehash. Change usage throughout Cygwin. (get_unique_id): New method. * fhandler.cc (fhandler_base::close): Call own del_my_locks method. Fix comment. (fhandler_base::fhandler_base): Accommodate new and changed members. (fhandler_base::fixup_after_fork): Call del_my_locks. (fhandler_base::fixup_after_exec): Ditto for files with close-on-exec flag set. * fhandler_disk_file.cc (get_ino_by_handle): Rename from readdir_get_ino_by_handle. Accommodate throughout. (fhandler_base::open_fs): Fill ino with inode number if FS has good inodes. Allocate a LUID and store in unique_id to recognize file descriptors referencing the same file object. * flock.cc: Drop flock TODO comments. Use explicit types __dev32_t and __ino64_t instead of dev_t and ino_t. (LOCK_OBJ_NAME_LEN): Change to reflect longer lf_id length. (get_obj_handle_count): New method. (lockf_t::lf_id): Change type to long long. (inode_t::get_lock_obj_handle_count): Drop in favor of static function get_obj_handle_count. (inode_t::del_locks): Remove. (inode_t::get): Add create_if_missing flag argument. (inode_t::del_my_locks): Reimplement to handle POSIX and BSD flock locks. Return if node can be deleted or not. (inode_t::~inode_t): Ditto. Close handles to i_dir and i_mtx. (fixup_lockf_after_fork): Remove. (fhandler_base::del_my_locks): New method. (fixup_lockf_after_exec): Check if node can be deleted. (inode_t::get): Only create node if create_if_missing is set. Lock the returned node here before unlocking the node list. (inode_t::get_all_locks_list): Accommodate new lf_id length. (inode_t::create_lock_obj): Ditto. (lockf_t::open_lock_obj): Ditto. Change return type to bool. De-const. Set lf_obj instead of returning a handle. (lockf_t::del_lock_obj): Call SetEvent only if new incoming parameters allow it. Explain how it's supposed to work. (fhandler_disk_file::lock): Only fetch file length in SEEK_END case. Use NtQueryInformationFile(FileStandardInformation) instead of calling fstat_by_handle. Always unlock node before returning. Use fhandler's unique id to create lf_id for BSD flock locks. Rely on node lock from inode_t::get. Call del_lock_obj on removed locks here to allow explicit unlocking. Delete node if no lock exists on the file anymore. (lf_setlock): Get file handle as additional parameter. Handle the fact that lf_getblock now always opens the attached event object. Reactivate erroneously applied patch which deactivates setting thread priority. Additionally handle blocking on BSD flock locks. (lf_clearlock): Get file handle as additional parameter. (lf_getlock): Close event handle opened by lf_getblock. (lf_getblock): Open potentially blocking event object here and check its signal state if it's a BSD flock lock. (lf_wakelock): Get file handle as additional parameter. * fork.cc (frok::child): Drop call to fixup_lockf_after_fork. * ntdll.h (struct _EVENT_BASIC_INFORMATION): Define. (enum _EVENT_INFORMATION_CLASS): Define. (NtQueryEvent): Declare. * fhandler.h (fhandler_base::fs_flags): Remove. (fhandler_base::set_fs_flags): Remove. (fhandler_base::get_fs_flags): Remove. * fhandler.cc (fhandler_base::write): Check for sparse file using pc.fs_flags(). * fhandler_disk_file.cc (fhandler_disk_file::ftruncate): Ditto. The return of the volume serial number in fs_info. * fhandler.h (get_dev): New method. * fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Drop call to NtQueryVolumeInformationFile(FileFsVolumeInformation). Just use get_dev() method. * fhandler_fifo.cc (fhandler_fifo::open) Use device ID and inode number to generate fifo name. * path.h (fs_info::sernum): New member. (fs_info::serial_number): New method. (path_conv::fs_serial_number): New method. * path.cc (fs_info::update): Fetch volume serial number and store in sernum.
334 lines
6.7 KiB
C++
334 lines
6.7 KiB
C++
/* dir.cc: Posix directory-related routines
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006, 2007 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 (-1);
|
|
else
|
|
{
|
|
set_errno (ENOENT);
|
|
res = NULL;
|
|
}
|
|
|
|
if (!res && fh)
|
|
delete fh;
|
|
return res;
|
|
}
|
|
|
|
extern "C" DIR *
|
|
fdopendir (int fd)
|
|
{
|
|
DIR *res = NULL;
|
|
|
|
cygheap_fdget cfd (fd);
|
|
if (cfd >= 0)
|
|
res = cfd->opendir (fd);
|
|
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 (((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_ino ();
|
|
if (!is_dot && !is_dot_dot)
|
|
{
|
|
PUNICODE_STRING w32name =
|
|
((fhandler_base *) dir->__fh)->pc.get_nt_native_path ();
|
|
DWORD devn = ((fhandler_base *) dir->__fh)->get_device ();
|
|
/* Paths below /proc don't have a Win32 pendant. */
|
|
if (isproc_dev (devn))
|
|
de->d_ino = hash_path_name (de->d_ino, L"/");
|
|
else if (w32name->Buffer[w32name->Length / sizeof (WCHAR) - 1]
|
|
!= L'\\')
|
|
de->d_ino = hash_path_name (de->d_ino, L"\\");
|
|
de->d_ino = hash_path_name (de->d_ino, de->d_name);
|
|
}
|
|
}
|
|
}
|
|
/* This fills out the old 32 bit d_ino field for old applications,
|
|
build under Cygwin before 1.5.x. */
|
|
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_info_mask;
|
|
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_info_mask;
|
|
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 (!(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 (has_dot_last_component (dir, true))
|
|
set_errno (fh->exists () ? EEXIST : ENOENT);
|
|
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 (!(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->exists ())
|
|
set_errno (ENOENT);
|
|
else if (has_dot_last_component (dir, false))
|
|
set_errno (EINVAL);
|
|
else if (!fh->rmdir ())
|
|
res = 0;
|
|
|
|
delete fh;
|
|
|
|
done:
|
|
syscall_printf ("%d = rmdir (%s)", res, dir);
|
|
return res;
|
|
}
|