2000-02-18 03:38:33 +08:00
|
|
|
/* dir.cc: Posix directory-related routines
|
|
|
|
|
|
|
|
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 <stdlib.h>
|
2012-02-02 21:58:20 +08:00
|
|
|
#include <unistd.h>
|
2024-01-24 04:49:48 +08:00
|
|
|
#include <sys/stat.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2021-11-09 11:22:27 +08:00
|
|
|
#define _LIBC
|
2000-08-22 13:10:20 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "dtable.h"
|
2000-11-14 13:53:32 +08:00
|
|
|
#include "cygheap.h"
|
2005-07-03 10:40:30 +08:00
|
|
|
#include "cygtls.h"
|
2009-09-26 23:51:53 +08:00
|
|
|
#include "tls_pbuf.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-09-04 00:36:54 +08:00
|
|
|
extern "C" int
|
|
|
|
dirfd (DIR *dir)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2001-09-04 00:36:54 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (dir->__d_cookie == __DIRENT_COOKIE)
|
|
|
|
return dir->__d_fd;
|
2001-09-04 00:36:54 +08:00
|
|
|
syscall_printf ("-1 = dirfd (%p)", dir);
|
2014-08-22 17:21:33 +08:00
|
|
|
set_errno (EINVAL);
|
2001-09-04 00:36:54 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2006-01-21 10:24:17 +08:00
|
|
|
}
|
|
|
|
|
2006-01-28 05:50:42 +08:00
|
|
|
/* Symbol kept for backward compatibility. Don't remove. Don't declare
|
|
|
|
in public header file. */
|
2006-01-21 10:24:17 +08:00
|
|
|
extern "C" DIR *
|
|
|
|
__opendir_with_d_ino (const char *name)
|
|
|
|
{
|
2006-01-28 05:50:42 +08:00
|
|
|
return opendir (name);
|
2001-09-04 00:36:54 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2001-12-06 07:05:15 +08:00
|
|
|
DIR *res;
|
2001-11-21 14:47:57 +08:00
|
|
|
|
2010-01-12 18:14:59 +08:00
|
|
|
fh = build_fh_name (name, PC_SYM_FOLLOW);
|
2001-12-06 07:05:15 +08:00
|
|
|
if (!fh)
|
|
|
|
res = NULL;
|
2014-03-10 02:39:14 +08:00
|
|
|
else if (fh->error ())
|
|
|
|
{
|
|
|
|
set_errno (fh->error ());
|
|
|
|
res = NULL;
|
|
|
|
}
|
2024-03-20 19:34:51 +08:00
|
|
|
else if (!fh->exists ())
|
2001-12-06 07:05:15 +08:00
|
|
|
{
|
2002-01-06 11:32:32 +08:00
|
|
|
set_errno (ENOENT);
|
|
|
|
res = NULL;
|
2001-12-06 07:05:15 +08:00
|
|
|
}
|
2024-03-20 19:34:51 +08:00
|
|
|
else if (!fh->pc.isdir ())
|
|
|
|
{
|
|
|
|
set_errno (ENOTDIR);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = fh->opendir (-1);
|
2002-01-06 11:32:32 +08:00
|
|
|
|
2006-01-28 05:50:42 +08:00
|
|
|
if (!res && fh)
|
2002-01-06 11:32:32 +08:00
|
|
|
delete fh;
|
2013-10-30 17:44:47 +08:00
|
|
|
/* Applications calling flock(2) on dirfd(fd) need this... */
|
2014-02-01 03:27:26 +08:00
|
|
|
if (res && !fh->nohandle ())
|
2013-10-30 17:44:47 +08:00
|
|
|
fh->set_unique_id ();
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-06-29 23:13:01 +08:00
|
|
|
extern "C" DIR *
|
|
|
|
fdopendir (int fd)
|
|
|
|
{
|
|
|
|
DIR *res = NULL;
|
|
|
|
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd >= 0)
|
|
|
|
res = cfd->opendir (fd);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-08-23 11:58:09 +08:00
|
|
|
static int
|
2005-08-20 14:19:55 +08:00
|
|
|
readdir_worker (DIR *dir, dirent *de)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
int res = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (dir->__d_cookie != __DIRENT_COOKIE)
|
2003-09-08 12:04:19 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
syscall_printf ("%p = readdir (%p)", NULL, dir);
|
|
|
|
res = EBADF;
|
|
|
|
__leave;
|
2003-09-08 12:04:19 +08:00
|
|
|
}
|
2002-06-01 10:45:38 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
de->d_ino = 0;
|
|
|
|
de->d_type = DT_UNKNOWN;
|
2024-01-22 02:51:54 +08:00
|
|
|
de->d_reclen = sizeof *de;
|
2014-08-22 17:21:33 +08:00
|
|
|
memset (&de->__d_unused1, 0, sizeof (de->__d_unused1));
|
2006-01-28 05:50:42 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
res = ((fhandler_base *) dir->__fh)->readdir (dir, de);
|
|
|
|
|
|
|
|
if (res == ENMFILE)
|
2006-01-04 01:44:26 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
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;
|
|
|
|
}
|
2006-01-28 05:50:42 +08:00
|
|
|
}
|
2006-05-28 23:50:14 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!res && !de->d_ino)
|
2006-05-28 23:50:14 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
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 *)
|
2020-03-11 20:23:55 +08:00
|
|
|
dir->__fh)->get_name (),
|
2014-08-22 17:21:33 +08:00
|
|
|
true);
|
|
|
|
else
|
2006-01-04 01:44:26 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2006-01-04 01:44:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (NO_ERROR)
|
|
|
|
{
|
|
|
|
res = EFAULT;
|
|
|
|
}
|
|
|
|
__endtry
|
2005-08-20 14:19:55 +08:00
|
|
|
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
|
2013-11-25 19:38:08 +08:00
|
|
|
readdir_r (DIR *__restrict dir, dirent *__restrict de, dirent **__restrict ode)
|
2005-08-20 14:19:55 +08:00
|
|
|
{
|
|
|
|
int res = readdir_worker (dir, de);
|
|
|
|
if (!res)
|
|
|
|
*ode = de;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ode = NULL;
|
2005-08-29 00:30:48 +08:00
|
|
|
if (res == ENMFILE)
|
2005-08-20 14:19:55 +08:00
|
|
|
res = 0;
|
2002-06-01 10:45:38 +08:00
|
|
|
}
|
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 04:49:48 +08:00
|
|
|
extern "C"
|
|
|
|
ssize_t posix_getdents(int fd, void *buf, size_t nbytes, int flags)
|
|
|
|
{
|
|
|
|
struct posix_dent *dent_buf, *src;
|
|
|
|
ssize_t cnt = 0;
|
|
|
|
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
/* Valid descriptor? */
|
|
|
|
if (cfd < 0)
|
|
|
|
return -1;
|
|
|
|
/* Valid flags? Right now only DT_FORCE_TYPE is defined */
|
|
|
|
if ((flags & ~DT_FORCE_TYPE) != 0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Create type-safe buffer pointer */
|
|
|
|
dent_buf = (struct posix_dent *) buf;
|
|
|
|
/* Check if nbytes is big enough to fit at least one struct posix_dent */
|
|
|
|
if (nbytes < sizeof (struct posix_dent))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Create internal DIR * on first invocation */
|
|
|
|
if (!cfd->getdents_dir ())
|
|
|
|
{
|
|
|
|
cfd->getdents_dir (cfd->opendir (fd));
|
|
|
|
if (!cfd->getdents_dir ())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Now loop until EOF or buf is full */
|
|
|
|
while (nbytes >= sizeof (struct posix_dent))
|
|
|
|
{
|
|
|
|
/* Our struct posix_dent is identical to struct dirent */
|
|
|
|
src = (struct posix_dent *) readdir (cfd->getdents_dir ());
|
|
|
|
if (!src)
|
|
|
|
break;
|
|
|
|
/* Handle the suggested DT_FORCE_TYPE flag */
|
|
|
|
if (src->d_type == DT_UNKNOWN && (flags & DT_FORCE_TYPE))
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!fstatat (fd, src->d_name, &st, AT_SYMLINK_NOFOLLOW))
|
|
|
|
src->d_type = IFTODT (st.st_mode);
|
|
|
|
}
|
|
|
|
*dent_buf++ = *src;
|
|
|
|
++cnt;
|
|
|
|
nbytes -= sizeof (struct posix_dent);
|
|
|
|
}
|
|
|
|
return cnt * sizeof (struct posix_dent);
|
|
|
|
}
|
|
|
|
|
2010-07-06 00:59:56 +08:00
|
|
|
/* telldir */
|
|
|
|
extern "C" long
|
|
|
|
telldir (DIR *dir)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2014-08-20 03:25:54 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (dir->__d_cookie == __DIRENT_COOKIE)
|
|
|
|
return ((fhandler_base *) dir->__fh)->telldir (dir);
|
2014-08-20 03:25:54 +08:00
|
|
|
set_errno (EBADF);
|
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-07-06 00:59:56 +08:00
|
|
|
/* telldir was never defined using off_t in POSIX, only in early versions
|
|
|
|
of glibc. We have to keep the function in as entry point for backward
|
|
|
|
compatibility. */
|
2013-04-23 17:44:36 +08:00
|
|
|
extern "C" off_t
|
2010-07-06 00:59:56 +08:00
|
|
|
telldir64 (DIR *dir)
|
2002-02-26 01:47:51 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
return (off_t) telldir (dir);
|
2002-02-26 01:47:51 +08:00
|
|
|
}
|
|
|
|
|
2010-07-06 00:59:56 +08:00
|
|
|
/* seekdir */
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
2010-07-06 00:59:56 +08:00
|
|
|
seekdir (DIR *dir, long loc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (dir->__d_cookie == __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
dir->__flags &= dirent_info_mask;
|
|
|
|
((fhandler_base *) dir->__fh)->seekdir (dir, loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-07-06 00:59:56 +08:00
|
|
|
/* seekdir was never defined using off_t in POSIX, only in early versions
|
|
|
|
of glibc. We have to keep the function in as entry point for backward
|
|
|
|
compatibility. */
|
2002-02-26 01:47:51 +08:00
|
|
|
extern "C" void
|
2013-04-23 17:44:36 +08:00
|
|
|
seekdir64 (DIR *dir, off_t loc)
|
2002-02-26 01:47:51 +08:00
|
|
|
{
|
2010-07-06 00:59:56 +08:00
|
|
|
seekdir (dir, (long) 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
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (dir->__d_cookie == __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
dir->__flags &= dirent_info_mask;
|
|
|
|
((fhandler_base *) dir->__fh)->rewinddir (dir);
|
|
|
|
}
|
|
|
|
set_errno (EINVAL); /* Diagnosis */
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 20:41:14 +08:00
|
|
|
static int
|
|
|
|
closedir_worker (DIR *dir, int *fdret)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (dir->__d_cookie == __DIRENT_COOKIE)
|
|
|
|
{
|
|
|
|
/* Reset the marker in case the caller tries to use `dir' again. */
|
|
|
|
dir->__d_cookie = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
int res = ((fhandler_base *) dir->__fh)->closedir (dir);
|
2001-11-21 14:47:57 +08:00
|
|
|
|
2024-01-23 20:41:14 +08:00
|
|
|
if (fdret)
|
|
|
|
*fdret = dir->__d_fd;
|
|
|
|
else
|
|
|
|
close (dir->__d_fd);
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
free (dir->__d_dirname);
|
|
|
|
free (dir->__d_dirent);
|
|
|
|
free (dir);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
set_errno (EBADF);
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 20:41:14 +08:00
|
|
|
/* closedir: POSIX 5.1.2.1 */
|
|
|
|
extern "C" int
|
|
|
|
closedir (DIR *dir)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = closedir_worker (dir, NULL);
|
|
|
|
syscall_printf ("%R = closedir(%p)", res, dir);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
fdclosedir (DIR *dir)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
closedir_worker (dir, &fd);
|
|
|
|
syscall_printf ("%d = fdclosedir(%p)", fd, dir);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
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;
|
2005-05-25 12:32:59 +08:00
|
|
|
fhandler_base *fh = NULL;
|
2009-09-26 23:51:53 +08:00
|
|
|
tmp_pathbuf tp;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2009-09-26 23:51:53 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!*dir)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
__leave;
|
|
|
|
}
|
2019-09-22 23:33:34 +08:00
|
|
|
/* Following Linux, and intentionally ignoring POSIX, do not
|
|
|
|
resolve the last component of DIR if it is a symlink, even if
|
|
|
|
DIR has a trailing slash. Achieve this by stripping trailing
|
2019-09-28 02:00:52 +08:00
|
|
|
slashes or backslashes.
|
|
|
|
|
|
|
|
Exception: If DIR == 'x:' followed by one or more slashes or
|
|
|
|
backslashes, and if there's at least one backslash, assume
|
|
|
|
that the user is referring to the root directory of drive x.
|
|
|
|
Retain one backslash in this case. */
|
2014-08-22 17:21:33 +08:00
|
|
|
if (isdirsep (dir[strlen (dir) - 1]))
|
|
|
|
{
|
|
|
|
/* This converts // to /, but since both give EEXIST, we're okay. */
|
|
|
|
char *buf;
|
|
|
|
char *p = stpcpy (buf = tp.c_get (), dir) - 1;
|
2019-09-28 02:00:52 +08:00
|
|
|
bool msdos = false;
|
2014-08-22 17:21:33 +08:00
|
|
|
dir = buf;
|
|
|
|
while (p > dir && isdirsep (*p))
|
2019-09-28 02:00:52 +08:00
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
msdos = true;
|
|
|
|
*p-- = '\0';
|
|
|
|
}
|
|
|
|
if (msdos && p == dir + 1 && isdrive (dir))
|
|
|
|
p[1] = '\\';
|
2014-08-22 17:21:33 +08:00
|
|
|
}
|
|
|
|
if (!(fh = build_fh_name (dir, PC_SYM_NOFOLLOW)))
|
|
|
|
__leave; /* errno already set */;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (fh->error ())
|
|
|
|
{
|
|
|
|
debug_printf ("got %d error from build_fh_name", fh->error ());
|
|
|
|
set_errno (fh->error ());
|
|
|
|
}
|
2019-06-04 02:15:50 +08:00
|
|
|
else if (fh->exists ())
|
|
|
|
set_errno (EEXIST);
|
2014-08-22 17:21:33 +08:00
|
|
|
else if (has_dot_last_component (dir, true))
|
2019-06-04 02:15:50 +08:00
|
|
|
set_errno (ENOENT);
|
2014-08-22 17:21:33 +08:00
|
|
|
else if (!fh->mkdir (mode))
|
|
|
|
res = 0;
|
|
|
|
delete fh;
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%R = mkdir(%s, %d)", res, dir, mode);
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rmdir: POSIX 5.5.2.1 */
|
|
|
|
extern "C" int
|
|
|
|
rmdir (const char *dir)
|
|
|
|
{
|
|
|
|
int res = -1;
|
2005-05-25 12:32:59 +08:00
|
|
|
fhandler_base *fh = NULL;
|
2019-09-22 23:33:34 +08:00
|
|
|
tmp_pathbuf tp;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2000-05-23 01:15:47 +08:00
|
|
|
{
|
2019-09-22 23:33:34 +08:00
|
|
|
if (!*dir)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
/* Following Linux, and intentionally ignoring POSIX, do not
|
|
|
|
resolve the last component of DIR if it is a symlink, even if
|
|
|
|
DIR has a trailing slash. Achieve this by stripping trailing
|
2019-09-28 02:00:52 +08:00
|
|
|
slashes or backslashes.
|
|
|
|
|
|
|
|
Exception: If DIR == 'x:' followed by one or more slashes or
|
|
|
|
backslashes, and if there's at least one backslash, assume
|
|
|
|
that the user is referring to the root directory of drive x.
|
|
|
|
Retain one backslash in this case. */
|
2019-09-22 23:33:34 +08:00
|
|
|
if (isdirsep (dir[strlen (dir) - 1]))
|
|
|
|
{
|
|
|
|
/* This converts // to /, but since both give ENOTEMPTY,
|
|
|
|
we're okay. */
|
|
|
|
char *buf;
|
|
|
|
char *p = stpcpy (buf = tp.c_get (), dir) - 1;
|
2019-09-28 02:00:52 +08:00
|
|
|
bool msdos = false;
|
2019-09-22 23:33:34 +08:00
|
|
|
dir = buf;
|
|
|
|
while (p > dir && isdirsep (*p))
|
2019-09-28 02:00:52 +08:00
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
msdos = true;
|
|
|
|
*p-- = '\0';
|
|
|
|
}
|
|
|
|
if (msdos && p == dir + 1 && isdrive (dir))
|
|
|
|
p[1] = '\\';
|
2019-09-22 23:33:34 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!(fh = build_fh_name (dir, PC_SYM_NOFOLLOW)))
|
|
|
|
__leave; /* 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;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%R = rmdir(%s)", res, dir);
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|