* Makefile.in (DLL_OFILES): Add fhandler_disk_file.o.

* cygheap.h (cygheap_fdnew::operator =): New operator.
* dir.cc: Add invalid struct checking throughout.  Use methods for all
directory manipulation throughout.
* fhandler.cc: Move fhandler_disk_file stuff to own file.
(fhandler_base::opendir): New method.
(fhandler_base::readdir): New method.
(fhandler_base::telldir): New method.
(fhandler_base::seekdir): New method.
(fhandler_base::rewinddir): New method.
(fhandler_base::closedir): New method.
* fhandler_disk_file.cc: New file.
* fhandler.h (fhandler_base): Declare new virtual methods.
(fhandler_disk_file): Ditto.
(fhandler_cygdrive): New class.
* path.cc (conv_path_list): Use strccpy to break apart path.
This commit is contained in:
Christopher Faylor 2001-11-21 06:47:57 +00:00
parent f6a6c2a358
commit 7903ee6955
8 changed files with 895 additions and 743 deletions

View File

@ -1,3 +1,23 @@
2001-11-21 Christopher Faylor <cgf@redhat.com>
* Makefile.in (DLL_OFILES): Add fhandler_disk_file.o.
* cygheap.h (cygheap_fdnew::operator =): New operator.
* dir.cc: Add invalid struct checking throughout. Use methods for all
directory manipulation throughout.
* fhandler.cc: Move fhandler_disk_file stuff to own file.
(fhandler_base::opendir): New method.
(fhandler_base::readdir): New method.
(fhandler_base::telldir): New method.
(fhandler_base::seekdir): New method.
(fhandler_base::rewinddir): New method.
(fhandler_base::closedir): New method.
* fhandler_disk_file.cc: New file.
* fhandler.h (fhandler_base): Declare new virtual methods.
(fhandler_disk_file): Ditto.
(fhandler_cygdrive): New class.
* path.cc (conv_path_list): Use strccpy to break apart path.
2001-11-17 Nick Duffek <nick@duffek.com>
* path.cc (conv_path_list): Copy source paths before modifying them.

View File

@ -119,16 +119,16 @@ DLL_IMPORTS:=$(w32api_lib)/libkernel32.a
DLL_OFILES:=assert.o autoload.o cygheap.o dcrt0.o debug.o delqueue.o dir.o \
dlfcn.o dll_init.o dtable.o environ.o errno.o exceptions.o exec.o \
external.o fcntl.o fhandler.o fhandler_clipboard.o fhandler_console.o \
fhandler_dsp.o fhandler_floppy.o fhandler_mem.o fhandler_random.o \
fhandler_raw.o fhandler_serial.o fhandler_socket.o fhandler_tape.o \
fhandler_termios.o fhandler_tty.o fhandler_windows.o fhandler_zero.o \
fork.o glob.o grp.o heap.o init.o ioctl.o localtime.o malloc.o \
miscfuncs.o mmap.o net.o ntea.o passwd.o path.o pinfo.o pipe.o poll.o \
pthread.o regexp.o regerror.o regsub.o registry.o resource.o scandir.o \
sched.o sec_acl.o sec_helper.o security.o select.o shared.o shortcut.o signal.o sigproc.o \
smallprint.o spawn.o strace.o strsep.o sync.o syscalls.o sysconf.o \
syslog.o termios.o thread.o times.o tty.o uinfo.o uname.o wait.o \
wincap.o window.o \
fhandler_disk_file.o fhandler_dsp.o fhandler_floppy.o fhandler_mem.o \
fhandler_random.o fhandler_raw.o fhandler_serial.o fhandler_socket.o \
fhandler_tape.o fhandler_termios.o fhandler_tty.o fhandler_windows.o \
fhandler_zero.o fork.o glob.o grp.o heap.o init.o ioctl.o localtime.o \
malloc.o miscfuncs.o mmap.o net.o ntea.o passwd.o path.o pinfo.o pipe.o \
poll.o pthread.o regexp.o regerror.o regsub.o registry.o resource.o \
scandir.o sched.o sec_acl.o sec_helper.o security.o select.o shared.o \
shortcut.o signal.o sigproc.o smallprint.o spawn.o strace.o strsep.o \
sync.o syscalls.o sysconf.o syslog.o termios.o thread.o times.o tty.o \
uinfo.o uname.o wait.o wincap.o window.o \
$(EXTRA_DLL_OFILES) $(EXTRA_OFILES) $(MALLOC_OFILES) $(MT_SAFE_OBJECTS)
GMON_OFILES:= gmon.o mcount.o profil.o

View File

@ -231,6 +231,7 @@ class cygheap_fdnew : public cygheap_fdmanip
locked = false;
}
}
void operator = (fhandler_base *fh) {*this->fh = fh;}
};
class cygheap_fdget : public cygheap_fdmanip

View File

@ -65,6 +65,8 @@ writable_directory (const char *file)
extern "C" int
dirfd (DIR *dir)
{
if (check_null_invalid_struct_errno (dir))
return -1;
if (dir->__d_cookie != __DIRENT_COOKIE)
{
set_errno (EBADF);
@ -76,214 +78,80 @@ dirfd (DIR *dir)
/* opendir: POSIX 5.1.2.1 */
extern "C" DIR *
opendir (const char *dirname)
opendir (const char *name)
{
int len;
DIR *dir;
DIR *res = 0;
struct stat statbuf;
fhandler_base *fh;
path_conv pc;
DIR *res = NULL;
path_conv real_dirname;
if (stat_worker (dirname, &statbuf, 0, &real_dirname) == -1)
goto failed;
if (!(statbuf.st_mode & S_IFDIR))
{
set_errno (ENOTDIR);
goto failed;
}
len = strlen (real_dirname);
if (len > MAX_PATH - 3)
{
set_errno (ENAMETOOLONG);
goto failed;
}
if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
{
set_errno (ENOMEM);
goto failed;
}
if ((dir->__d_dirname = (char *) malloc (len + 3)) == NULL)
{
free (dir);
set_errno (ENOMEM);
goto failed;
}
if ((dir->__d_dirent =
(struct dirent *) malloc (sizeof (struct dirent))) == NULL)
{
free (dir->__d_dirname);
free (dir);
set_errno (ENOMEM);
goto failed;
}
strcpy (dir->__d_dirname, real_dirname.get_win32 ());
dir->__d_dirent->d_version = __DIRENT_VERSION;
dir->__d_dirent->d_fd = open (dir->__d_dirname, O_RDONLY | O_DIROPEN);
/* FindFirstFile doesn't seem to like duplicate /'s. */
len = strlen (dir->__d_dirname);
if (len == 0 || SLASH_P (dir->__d_dirname[len - 1]))
strcat (dir->__d_dirname, "*");
else
strcat (dir->__d_dirname, "\\*"); /**/
dir->__d_cookie = __DIRENT_COOKIE;
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
dir->__d_position = 0;
dir->__d_dirhash = statbuf.st_ino;
res = dir;
failed:
syscall_printf ("%p = opendir (%s)", res, dirname);
fh = cygheap->fdtab.build_fhandler_from_name (-1, name, NULL, pc,
PC_SYM_FOLLOW | PC_FULL, NULL);
res = fh->opendir (name, pc);
if (!res)
delete fh;
return res;
}
/* readdir: POSIX 5.1.2.1 */
extern "C" struct dirent *
readdir (DIR * dir)
readdir (DIR *dir)
{
WIN32_FIND_DATA buf;
HANDLE handle;
struct dirent *res = NULL;
if (check_null_invalid_struct_errno (dir))
return NULL;
if (dir->__d_cookie != __DIRENT_COOKIE)
{
set_errno (EBADF);
syscall_printf ("%p = readdir (%p)", res, dir);
return res;
syscall_printf ("%p = readdir (%p)", NULL, dir);
return NULL;
}
if (dir->__d_u.__d_data.__handle == INVALID_HANDLE_VALUE
&& dir->__d_position == 0)
{
handle = FindFirstFileA (dir->__d_dirname, &buf);
DWORD lasterr = GetLastError ();
dir->__d_u.__d_data.__handle = handle;
if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES))
{
seterrno_from_win_error (__FILE__, __LINE__, lasterr);
return res;
}
}
else if (dir->__d_u.__d_data.__handle == INVALID_HANDLE_VALUE)
{
return res;
}
else if (!FindNextFileA (dir->__d_u.__d_data.__handle, &buf))
{
DWORD lasterr = GetLastError ();
(void) FindClose (dir->__d_u.__d_data.__handle);
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
/* POSIX says you shouldn't set errno when readdir can't
find any more files; so, if another error we leave it set. */
if (lasterr != ERROR_NO_MORE_FILES)
seterrno_from_win_error (__FILE__, __LINE__, lasterr);
syscall_printf ("%p = readdir (%p)", res, dir);
return res;
}
/* We get here if `buf' contains valid data. */
strcpy (dir->__d_dirent->d_name, buf.cFileName);
/* Check for Windows shortcut. If it's a Cygwin or U/WIN
symlink, drop the .lnk suffix. */
if (buf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
{
char *c = dir->__d_dirent->d_name;
int len = strlen (c);
if (strcasematch (c + len - 4, ".lnk"))
{
char fbuf[MAX_PATH + 1];
strcpy (fbuf, dir->__d_dirname);
strcpy (fbuf + strlen (fbuf) - 1, dir->__d_dirent->d_name);
path_conv fpath (fbuf, PC_SYM_NOFOLLOW);
if (fpath.issymlink ())
c[len - 4] = '\0';
}
}
/* Compute d_ino by combining filename hash with the directory hash
(which was stored in dir->__d_dirhash when opendir was called). */
if (buf.cFileName[0] == '.')
{
if (buf.cFileName[1] == '\0')
dir->__d_dirent->d_ino = dir->__d_dirhash;
else if (buf.cFileName[1] != '.' || buf.cFileName[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:
ino_t dino = hash_path_name (dir->__d_dirhash, "\\");
dir->__d_dirent->d_ino = hash_path_name (dino, buf.cFileName);
}
++dir->__d_position;
res = dir->__d_dirent;
syscall_printf ("%p = readdir (%p) (%s)",
&dir->__d_dirent, dir, buf.cFileName);
return res;
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->readdir (dir);
}
/* telldir */
extern "C" off_t
telldir (DIR * dir)
telldir (DIR *dir)
{
if (check_null_invalid_struct_errno (dir))
return -1;
if (dir->__d_cookie != __DIRENT_COOKIE)
return 0;
return dir->__d_position;
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->telldir (dir);
}
/* seekdir */
extern "C" void
seekdir (DIR * dir, off_t loc)
seekdir (DIR *dir, off_t loc)
{
if (check_null_invalid_struct_errno (dir))
return;
if (dir->__d_cookie != __DIRENT_COOKIE)
return;
rewinddir (dir);
while (loc > dir->__d_position)
if (! readdir (dir))
break;
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->seekdir (dir, loc);
}
/* rewinddir: POSIX 5.1.2.1 */
extern "C" void
rewinddir (DIR * dir)
rewinddir (DIR *dir)
{
syscall_printf ("rewinddir (%p)", dir);
if (check_null_invalid_struct_errno (dir))
return;
if (dir->__d_cookie != __DIRENT_COOKIE)
return;
if (dir->__d_u.__d_data.__handle != INVALID_HANDLE_VALUE)
{
(void) FindClose (dir->__d_u.__d_data.__handle);
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
}
dir->__d_position = 0;
return ((fhandler_base *) dir->__d_u.__d_data.__fh)->rewinddir (dir);
}
/* closedir: POSIX 5.1.2.1 */
extern "C" int
closedir (DIR * dir)
closedir (DIR *dir)
{
if (check_null_invalid_struct_errno (dir))
return -1;
if (dir->__d_cookie != __DIRENT_COOKIE)
{
set_errno (EBADF);
@ -291,25 +159,18 @@ closedir (DIR * dir)
return -1;
}
if (dir->__d_u.__d_data.__handle != INVALID_HANDLE_VALUE &&
FindClose (dir->__d_u.__d_data.__handle) == 0)
{
__seterrno ();
syscall_printf ("-1 = closedir (%p)", dir);
return -1;
}
if (dir->__d_dirent->d_fd >= 0)
close (dir->__d_dirent->d_fd);
/* Reset the marker in case the caller tries to use `dir' again. */
dir->__d_cookie = 0;
int res = ((fhandler_base *) dir->__d_u.__d_data.__fh)->closedir (dir);
cygheap->fdtab.release (dir->__d_dirent->d_fd);
free (dir->__d_dirname);
free (dir->__d_dirent);
free (dir);
syscall_printf ("0 = closedir (%p)", dir);
return 0;
syscall_printf ("%d = closedir (%p)", res);
return res;
}
/* mkdir: POSIX 5.4.1.1 */

View File

@ -886,313 +886,6 @@ fhandler_base::fstat (struct stat *buf, path_conv *)
return 0;
}
static int
num_entries (const char *win32_name)
{
WIN32_FIND_DATA buf;
HANDLE handle;
char buf1[MAX_PATH];
int count = 0;
strcpy (buf1, win32_name);
int len = strlen (buf1);
if (len == 0 || isdirsep (buf1[len - 1]))
strcat (buf1, "*");
else
strcat (buf1, "/*"); /* */
handle = FindFirstFileA (buf1, &buf);
if (handle == INVALID_HANDLE_VALUE)
return 0;
count ++;
while (FindNextFileA (handle, &buf))
{
if ((buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
count ++;
}
FindClose (handle);
return count;
}
int
fhandler_disk_file::fstat (struct stat *buf, path_conv *pc)
{
int res = -1;
int oret;
uid_t uid;
gid_t gid;
int open_flags = O_RDONLY | O_BINARY | O_DIROPEN;
if (!pc)
return fstat_helper (buf);
if ((oret = open (pc, open_flags, 0)))
/* ok */;
else
{
int ntsec_atts = 0;
/* If we couldn't open the file, try a "query open" with no permissions.
This will allow us to determine *some* things about the file, at least. */
set_query_open (TRUE);
if ((oret = open (pc, open_flags, 0)))
/* ok */;
else if (allow_ntsec && pc->has_acls () && get_errno () == EACCES
&& !get_file_attribute (TRUE, get_win32_name (), &ntsec_atts, &uid, &gid)
&& !ntsec_atts && uid == myself->uid && gid == myself->gid)
{
/* Check a special case here. If ntsec is ON it happens
that a process creates a file using mode 000 to disallow
other processes access. In contrast to UNIX, this results
in a failing open call in the same process. Check that
case. */
set_file_attribute (TRUE, get_win32_name (), 0400);
oret = open (pc, open_flags, 0);
set_file_attribute (TRUE, get_win32_name (), ntsec_atts);
}
}
if (oret)
{
res = fstat_helper (buf);
/* The number of links to a directory includes the
number of subdirectories in the directory, since all
those subdirectories point to it.
This is too slow on remote drives, so we do without it and
set the number of links to 2. */
/* Unfortunately the count of 2 confuses `find (1)' command. So
let's try it with `1' as link count. */
if (pc->isdir ())
buf->st_nlink = (pc->isremote ()
? 1 : num_entries (pc->get_win32 ()));
close ();
}
else if (pc->exists ())
{
/* Unfortunately, the above open may fail if the file exists, though.
So we have to care for this case here, too. */
WIN32_FIND_DATA wfd;
HANDLE handle;
buf->st_nlink = 1;
if (pc->isdir () && pc->isremote ())
buf->st_nlink = num_entries (pc->get_win32 ());
buf->st_dev = FHDEVN (FH_DISK) << 8;
buf->st_ino = hash_path_name (0, pc->get_win32 ());
if (pc->isdir ())
buf->st_mode = S_IFDIR;
else if (pc->issymlink ())
buf->st_mode = S_IFLNK;
else if (pc->issocket ())
buf->st_mode = S_IFSOCK;
else
buf->st_mode = S_IFREG;
if (!pc->has_acls ()
|| get_file_attribute (TRUE, pc->get_win32 (),
&buf->st_mode,
&buf->st_uid, &buf->st_gid))
{
buf->st_mode |= STD_RBITS | STD_XBITS;
if (!(pc->has_attribute (FILE_ATTRIBUTE_READONLY)))
buf->st_mode |= STD_WBITS;
if (pc->issymlink ())
buf->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
get_file_attribute (FALSE, pc->get_win32 (),
NULL, &buf->st_uid, &buf->st_gid);
}
if ((handle = FindFirstFile (pc->get_win32 (), &wfd))
!= INVALID_HANDLE_VALUE)
{
/* This is for FAT filesystems, which don't support atime/ctime */
if (wfd.ftLastAccessTime.dwLowDateTime == 0
&& wfd.ftLastAccessTime.dwHighDateTime == 0)
wfd.ftLastAccessTime = wfd.ftLastWriteTime;
if (wfd.ftCreationTime.dwLowDateTime == 0
&& wfd.ftCreationTime.dwHighDateTime == 0)
wfd.ftCreationTime = wfd.ftLastWriteTime;
buf->st_atime = to_time_t (&wfd.ftLastAccessTime);
buf->st_mtime = to_time_t (&wfd.ftLastWriteTime);
buf->st_ctime = to_time_t (&wfd.ftCreationTime);
buf->st_size = wfd.nFileSizeLow;
buf->st_blksize = S_BLKSIZE;
buf->st_blocks = ((unsigned long) buf->st_size +
S_BLKSIZE-1) / S_BLKSIZE;
FindClose (handle);
}
res = 0;
}
return res;
}
int
fhandler_disk_file::fstat_helper (struct stat *buf)
{
int res = 0; // avoid a compiler warning
BY_HANDLE_FILE_INFORMATION local;
save_errno saved_errno;
/* NT 3.51 seems to have a bug when attempting to get vol serial
numbers. This loop gets around this. */
for (int i = 0; i < 2; i++)
{
if (!(res = GetFileInformationByHandle (get_handle (), &local)))
break;
if (local.dwVolumeSerialNumber && (long) local.dwVolumeSerialNumber != -1)
break;
}
debug_printf ("%d = GetFileInformationByHandle (%s, %d)",
res, get_win32_name (), get_handle ());
if (res == 0)
{
/* GetFileInformationByHandle will fail if it's given stdin/out/err
or a pipe*/
DWORD lsize, hsize;
if (GetFileType (get_handle ()) != FILE_TYPE_DISK)
buf->st_mode = S_IFCHR;
lsize = GetFileSize (get_handle (), &hsize);
if (lsize == 0xffffffff && GetLastError () != NO_ERROR)
buf->st_mode = S_IFCHR;
else
buf->st_size = lsize;
/* We expect these to fail! */
buf->st_mode |= STD_RBITS | STD_WBITS;
buf->st_blksize = S_BLKSIZE;
buf->st_ino = get_namehash ();
syscall_printf ("0 = fstat (, %p)", buf);
return 0;
}
if (!get_win32_name ())
{
saved_errno.set (ENOENT);
return -1;
}
/* This is for FAT filesystems, which don't support atime/ctime */
if (local.ftLastAccessTime.dwLowDateTime == 0
&& local.ftLastAccessTime.dwHighDateTime == 0)
local.ftLastAccessTime = local.ftLastWriteTime;
if (local.ftCreationTime.dwLowDateTime == 0
&& local.ftCreationTime.dwHighDateTime == 0)
local.ftCreationTime = local.ftLastWriteTime;
buf->st_atime = to_time_t (&local.ftLastAccessTime);
buf->st_mtime = to_time_t (&local.ftLastWriteTime);
buf->st_ctime = to_time_t (&local.ftCreationTime);
buf->st_nlink = local.nNumberOfLinks;
buf->st_dev = local.dwVolumeSerialNumber;
buf->st_size = local.nFileSizeLow;
/* Allocate some place to determine the root directory. Need to allocate
enough so that rootdir can add a trailing slash if path starts with \\. */
char root[strlen (get_win32_name ()) + 3];
strcpy (root, get_win32_name ());
/* Assume that if a drive has ACL support it MAY have valid "inodes".
It definitely does not have valid inodes if it does not have ACL
support. */
switch (has_acls () ? GetDriveType (rootdir (root)) : DRIVE_UNKNOWN)
{
case DRIVE_FIXED:
case DRIVE_REMOVABLE:
case DRIVE_CDROM:
case DRIVE_RAMDISK:
/* Although the documentation indicates otherwise, it seems like
"inodes" on these devices are persistent, at least across reboots. */
buf->st_ino = local.nFileIndexHigh | local.nFileIndexLow;
break;
default:
/* Either the nFileIndex* fields are unreliable or unavailable. Use the
next best alternative. */
buf->st_ino = get_namehash ();
break;
}
buf->st_blksize = S_BLKSIZE;
buf->st_blocks = ((unsigned long) buf->st_size + S_BLKSIZE-1) / S_BLKSIZE;
buf->st_mode = 0;
/* Using a side effect: get_file_attibutes checks for
directory. This is used, to set S_ISVTX, if needed. */
if (local.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
buf->st_mode = S_IFDIR;
else if (get_symlink_p ())
buf->st_mode = S_IFLNK;
else if (get_socket_p ())
buf->st_mode = S_IFSOCK;
if (get_file_attribute (has_acls (), get_win32_name (), &buf->st_mode,
&buf->st_uid, &buf->st_gid) == 0)
{
/* If read-only attribute is set, modify ntsec return value */
if ((local.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
&& !get_symlink_p ())
buf->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
if (!(buf->st_mode & S_IFMT))
buf->st_mode |= S_IFREG;
}
else
{
buf->st_mode |= STD_RBITS;
if (!(local.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
buf->st_mode |= STD_WBITS;
/* | S_IWGRP | S_IWOTH; we don't give write to group etc */
if (buf->st_mode & S_IFDIR)
buf->st_mode |= S_IFDIR | STD_XBITS;
else if (buf->st_mode & S_IFMT)
/* nothing */;
else if (get_socket_p ())
buf->st_mode |= S_IFSOCK;
else
switch (GetFileType (get_handle ()))
{
case FILE_TYPE_CHAR:
case FILE_TYPE_UNKNOWN:
buf->st_mode |= S_IFCHR;
break;
case FILE_TYPE_DISK:
buf->st_mode |= S_IFREG;
if (!dont_care_if_execable () && !get_execable_p ())
{
DWORD cur, done;
char magic[3];
/* First retrieve current position, set to beginning
of file if not already there. */
cur = SetFilePointer (get_handle(), 0, NULL, FILE_CURRENT);
if (cur != INVALID_SET_FILE_POINTER &&
(!cur ||
SetFilePointer (get_handle(), 0, NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER))
{
/* FIXME should we use /etc/magic ? */
magic[0] = magic[1] = magic[2] = '\0';
if (ReadFile (get_handle (), magic, 3, &done, NULL) &&
has_exec_chars (magic, done))
set_execable_p ();
SetFilePointer (get_handle(), cur, NULL, FILE_BEGIN);
}
}
if (get_execable_p ())
buf->st_mode |= STD_XBITS;
break;
case FILE_TYPE_PIPE:
buf->st_mode |= S_IFSOCK;
break;
}
}
syscall_printf ("0 = fstat (, %p) st_atime=%x st_size=%d, st_mode=%p, st_ino=%d, sizeof=%d",
buf, buf->st_atime, buf->st_size, buf->st_mode,
(int) buf->st_ino, sizeof (*buf));
return 0;
}
void
fhandler_base::init (HANDLE f, DWORD a, mode_t bin)
{
@ -1388,234 +1081,6 @@ fhandler_base::~fhandler_base (void)
unix_path_name = win32_path_name = NULL;
}
/**********************************************************************/
/* fhandler_disk_file */
fhandler_disk_file::fhandler_disk_file () :
fhandler_base (FH_DISK)
{
}
int
fhandler_disk_file::open (path_conv *real_path, int flags, mode_t mode)
{
if (real_path->case_clash && flags & O_CREAT)
{
debug_printf ("Caseclash detected.");
set_errno (ECASECLASH);
return 0;
}
if (real_path->isbinary ())
{
set_r_binary (1);
set_w_binary (1);
}
set_has_acls (real_path->has_acls ());
set_isremote (real_path->isremote ());
if (real_path->isdir ())
flags |= O_DIROPEN;
int res = this->fhandler_base::open (real_path, flags, mode);
if (!res)
goto out;
/* This is for file systems known for having a buggy CreateFile call
which might return a valid HANDLE without having actually opened
the file.
The only known file system to date is the SUN NFS Solstice Client 3.1
which returns a valid handle when trying to open a file in a nonexistent
directory. */
if (real_path->has_buggy_open ()
&& GetFileAttributes (win32_path_name) == (DWORD) -1)
{
debug_printf ("Buggy open detected.");
close ();
set_errno (ENOENT);
return 0;
}
if (flags & O_APPEND)
SetFilePointer (get_handle(), 0, 0, FILE_END);
set_symlink_p (real_path->issymlink ());
set_execable_p (real_path->exec_state ());
set_socket_p (real_path->issocket ());
out:
syscall_printf ("%d = fhandler_disk_file::open (%s, %p)", res,
get_win32_name (), flags);
return res;
}
int
fhandler_disk_file::close ()
{
int res = this->fhandler_base::close ();
if (!res)
cygwin_shared->delqueue.process_queue ();
return res;
}
/*
* FIXME !!!
* The correct way to do this to get POSIX locking
* semantics is to keep a linked list of posix lock
* requests and map them into Win32 locks. The problem
* is that Win32 does not deal correctly with overlapping
* lock requests. Also another pain is that Win95 doesn't do
* non-blocking or non exclusive locks at all. For '95 just
* convert all lock requests into blocking,exclusive locks.
* This shouldn't break many apps but denying all locking
* would.
* For now just convert to Win32 locks and hope for the best.
*/
int
fhandler_disk_file::lock (int cmd, struct flock *fl)
{
int win32_start;
int win32_len;
DWORD win32_upper;
DWORD startpos;
/*
* We don't do getlck calls yet.
*/
if (cmd == F_GETLK)
{
set_errno (ENOSYS);
return -1;
}
/*
* Calculate where in the file to start from,
* then adjust this by fl->l_start.
*/
switch (fl->l_whence)
{
case SEEK_SET:
startpos = 0;
break;
case SEEK_CUR:
if ((off_t) (startpos = lseek (0, SEEK_CUR)) == (off_t)-1)
return -1;
break;
case SEEK_END:
{
BY_HANDLE_FILE_INFORMATION finfo;
if (GetFileInformationByHandle (get_handle(), &finfo) == 0)
{
__seterrno ();
return -1;
}
startpos = finfo.nFileSizeLow; /* Nowhere to keep high word */
break;
}
default:
set_errno (EINVAL);
return -1;
}
/*
* Now the fun starts. Adjust the start and length
* fields until they make sense.
*/
win32_start = startpos + fl->l_start;
if (fl->l_len < 0)
{
win32_start -= fl->l_len;
win32_len = -fl->l_len;
}
else
win32_len = fl->l_len;
if (win32_start < 0)
{
/* watch the signs! */
win32_len -= -win32_start;
if (win32_len <= 0)
{
/* Failure ! */
set_errno (EINVAL);
return -1;
}
win32_start = 0;
}
/*
* Special case if len == 0 for POSIX means lock
* to the end of the entire file (and all future extensions).
*/
if (win32_len == 0)
{
win32_len = 0xffffffff;
win32_upper = wincap.lock_file_highword ();
}
else
win32_upper = 0;
BOOL res;
if (wincap.has_lock_file_ex ())
{
DWORD lock_flags = (cmd == F_SETLK) ? LOCKFILE_FAIL_IMMEDIATELY : 0;
lock_flags |= (fl->l_type == F_WRLCK) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
OVERLAPPED ov;
ov.Internal = 0;
ov.InternalHigh = 0;
ov.Offset = (DWORD)win32_start;
ov.OffsetHigh = 0;
ov.hEvent = (HANDLE) 0;
if (fl->l_type == F_UNLCK)
{
res = UnlockFileEx (get_handle (), 0, (DWORD)win32_len, win32_upper, &ov);
}
else
{
res = LockFileEx (get_handle (), lock_flags, 0, (DWORD)win32_len,
win32_upper, &ov);
/* Deal with the fail immediately case. */
/*
* FIXME !! I think this is the right error to check for
* but I must admit I haven't checked....
*/
if ((res == 0) && (lock_flags & LOCKFILE_FAIL_IMMEDIATELY) &&
(GetLastError () == ERROR_LOCK_FAILED))
{
set_errno (EAGAIN);
return -1;
}
}
}
else
{
/* Windows 95 -- use primitive lock call */
if (fl->l_type == F_UNLCK)
res = UnlockFile (get_handle (), (DWORD)win32_start, 0, (DWORD)win32_len,
win32_upper);
else
res = LockFile (get_handle (), (DWORD)win32_start, 0, (DWORD)win32_len, win32_upper);
}
if (res == 0)
{
__seterrno ();
return -1;
}
return 0;
}
/**********************************************************************/
/* /dev/null */
@ -1699,3 +1164,45 @@ fhandler_base::set_nonblocking (int yes)
int new_flags = yes ? (!current ? O_NONBLOCK : current) : 0;
openflags = (openflags & ~O_NONBLOCK_MASK) | new_flags;
}
DIR *
fhandler_base::opendir (const char *, path_conv&)
{
set_errno (ENOTDIR);
return NULL;
}
struct dirent *
fhandler_base::readdir (DIR *)
{
set_errno (ENOTDIR);
return NULL;
}
off_t
fhandler_base::telldir (DIR *)
{
set_errno (ENOTDIR);
return -1;
}
void
fhandler_base::seekdir (DIR *, off_t)
{
set_errno (ENOTDIR);
return;
}
void
fhandler_base::rewinddir (DIR *)
{
set_errno (ENOTDIR);
return;
}
int
fhandler_base::closedir (DIR *)
{
set_errno (ENOTDIR);
return -1;
}

View File

@ -103,6 +103,8 @@ extern struct __cygwin_perfile *perfile_table;
class select_record;
class path_conv;
class fhandler_disk_file;
typedef struct __DIR DIR;
struct dirent;
enum bg_check_types
{
@ -338,6 +340,12 @@ class fhandler_base
void operator delete (void *);
virtual HANDLE get_guard () const {return NULL;}
virtual void set_eof () {}
virtual DIR *opendir (const char *dirname, path_conv& pc);
virtual dirent *readdir (DIR *);
virtual off_t telldir (DIR *);
virtual void seekdir (DIR *, off_t);
virtual void rewinddir (DIR *);
virtual int closedir (DIR *);
};
class fhandler_socket: public fhandler_base
@ -532,6 +540,12 @@ class fhandler_disk_file: public fhandler_base
int msync (HANDLE h, caddr_t addr, size_t len, int flags);
BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
DWORD size, void *address);
DIR *opendir (const char *dirname, path_conv& pc);
struct dirent *readdir (DIR *);
off_t telldir (DIR *);
void seekdir (DIR *, off_t);
void rewinddir (DIR *);
int closedir (DIR *);
};
class fhandler_serial: public fhandler_base
@ -577,6 +591,12 @@ class fhandler_serial: public fhandler_base
select_record *select_except (select_record *s);
};
class fhandler_cygdrive: public fhandler_disk_file
{
public:
fhandler_cygdrive ();
};
#define acquire_output_mutex(ms) \
__acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms);

View File

@ -0,0 +1,750 @@
/* fhandler_disk_file.cc
Copyright 1996, 1997, 1998, 1999, 2000, 2001 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 <sys/fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/cygwin.h>
#include <signal.h>
#include "cygerrno.h"
#include "perprocess.h"
#include "security.h"
#include "cygwin/version.h"
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
#include "cygheap.h"
#include "shared_info.h"
#include "sigproc.h"
#include "pinfo.h"
#include <assert.h>
#define _COMPILING_NEWLIB
#include <dirent.h>
static int
num_entries (const char *win32_name)
{
WIN32_FIND_DATA buf;
HANDLE handle;
char buf1[MAX_PATH];
int count = 0;
strcpy (buf1, win32_name);
int len = strlen (buf1);
if (len == 0 || isdirsep (buf1[len - 1]))
strcat (buf1, "*");
else
strcat (buf1, "/*"); /* */
handle = FindFirstFileA (buf1, &buf);
if (handle == INVALID_HANDLE_VALUE)
return 0;
count ++;
while (FindNextFileA (handle, &buf))
{
if ((buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
count ++;
}
FindClose (handle);
return count;
}
int
fhandler_disk_file::fstat (struct stat *buf, path_conv *pc)
{
int res = -1;
int oret;
uid_t uid;
gid_t gid;
int open_flags = O_RDONLY | O_BINARY | O_DIROPEN;
if (!pc)
return fstat_helper (buf);
if ((oret = open (pc, open_flags, 0)))
/* ok */;
else
{
int ntsec_atts = 0;
/* If we couldn't open the file, try a "query open" with no permissions.
This will allow us to determine *some* things about the file, at least. */
set_query_open (TRUE);
if ((oret = open (pc, open_flags, 0)))
/* ok */;
else if (allow_ntsec && pc->has_acls () && get_errno () == EACCES
&& !get_file_attribute (TRUE, get_win32_name (), &ntsec_atts, &uid, &gid)
&& !ntsec_atts && uid == myself->uid && gid == myself->gid)
{
/* Check a special case here. If ntsec is ON it happens
that a process creates a file using mode 000 to disallow
other processes access. In contrast to UNIX, this results
in a failing open call in the same process. Check that
case. */
set_file_attribute (TRUE, get_win32_name (), 0400);
oret = open (pc, open_flags, 0);
set_file_attribute (TRUE, get_win32_name (), ntsec_atts);
}
}
if (oret)
{
res = fstat_helper (buf);
/* The number of links to a directory includes the
number of subdirectories in the directory, since all
those subdirectories point to it.
This is too slow on remote drives, so we do without it and
set the number of links to 2. */
/* Unfortunately the count of 2 confuses `find (1)' command. So
let's try it with `1' as link count. */
if (pc->isdir ())
buf->st_nlink = (pc->isremote ()
? 1 : num_entries (pc->get_win32 ()));
close ();
}
else if (pc->exists ())
{
/* Unfortunately, the above open may fail if the file exists, though.
So we have to care for this case here, too. */
WIN32_FIND_DATA wfd;
HANDLE handle;
buf->st_nlink = 1;
if (pc->isdir () && pc->isremote ())
buf->st_nlink = num_entries (pc->get_win32 ());
buf->st_dev = FHDEVN (FH_DISK) << 8;
buf->st_ino = hash_path_name (0, pc->get_win32 ());
if (pc->isdir ())
buf->st_mode = S_IFDIR;
else if (pc->issymlink ())
buf->st_mode = S_IFLNK;
else if (pc->issocket ())
buf->st_mode = S_IFSOCK;
else
buf->st_mode = S_IFREG;
if (!pc->has_acls ()
|| get_file_attribute (TRUE, pc->get_win32 (),
&buf->st_mode,
&buf->st_uid, &buf->st_gid))
{
buf->st_mode |= STD_RBITS | STD_XBITS;
if (!(pc->has_attribute (FILE_ATTRIBUTE_READONLY)))
buf->st_mode |= STD_WBITS;
if (pc->issymlink ())
buf->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
get_file_attribute (FALSE, pc->get_win32 (),
NULL, &buf->st_uid, &buf->st_gid);
}
if ((handle = FindFirstFile (pc->get_win32 (), &wfd))
!= INVALID_HANDLE_VALUE)
{
/* This is for FAT filesystems, which don't support atime/ctime */
if (wfd.ftLastAccessTime.dwLowDateTime == 0
&& wfd.ftLastAccessTime.dwHighDateTime == 0)
wfd.ftLastAccessTime = wfd.ftLastWriteTime;
if (wfd.ftCreationTime.dwLowDateTime == 0
&& wfd.ftCreationTime.dwHighDateTime == 0)
wfd.ftCreationTime = wfd.ftLastWriteTime;
buf->st_atime = to_time_t (&wfd.ftLastAccessTime);
buf->st_mtime = to_time_t (&wfd.ftLastWriteTime);
buf->st_ctime = to_time_t (&wfd.ftCreationTime);
buf->st_size = wfd.nFileSizeLow;
buf->st_blksize = S_BLKSIZE;
buf->st_blocks = ((unsigned long) buf->st_size +
S_BLKSIZE-1) / S_BLKSIZE;
FindClose (handle);
}
res = 0;
}
return res;
}
int
fhandler_disk_file::fstat_helper (struct stat *buf)
{
int res = 0; // avoid a compiler warning
BY_HANDLE_FILE_INFORMATION local;
save_errno saved_errno;
/* NT 3.51 seems to have a bug when attempting to get vol serial
numbers. This loop gets around this. */
for (int i = 0; i < 2; i++)
{
if (!(res = GetFileInformationByHandle (get_handle (), &local)))
break;
if (local.dwVolumeSerialNumber && (long) local.dwVolumeSerialNumber != -1)
break;
}
debug_printf ("%d = GetFileInformationByHandle (%s, %d)",
res, get_win32_name (), get_handle ());
if (res == 0)
{
/* GetFileInformationByHandle will fail if it's given stdin/out/err
or a pipe*/
DWORD lsize, hsize;
if (GetFileType (get_handle ()) != FILE_TYPE_DISK)
buf->st_mode = S_IFCHR;
lsize = GetFileSize (get_handle (), &hsize);
if (lsize == 0xffffffff && GetLastError () != NO_ERROR)
buf->st_mode = S_IFCHR;
else
buf->st_size = lsize;
/* We expect these to fail! */
buf->st_mode |= STD_RBITS | STD_WBITS;
buf->st_blksize = S_BLKSIZE;
buf->st_ino = get_namehash ();
syscall_printf ("0 = fstat (, %p)", buf);
return 0;
}
if (!get_win32_name ())
{
saved_errno.set (ENOENT);
return -1;
}
/* This is for FAT filesystems, which don't support atime/ctime */
if (local.ftLastAccessTime.dwLowDateTime == 0
&& local.ftLastAccessTime.dwHighDateTime == 0)
local.ftLastAccessTime = local.ftLastWriteTime;
if (local.ftCreationTime.dwLowDateTime == 0
&& local.ftCreationTime.dwHighDateTime == 0)
local.ftCreationTime = local.ftLastWriteTime;
buf->st_atime = to_time_t (&local.ftLastAccessTime);
buf->st_mtime = to_time_t (&local.ftLastWriteTime);
buf->st_ctime = to_time_t (&local.ftCreationTime);
buf->st_nlink = local.nNumberOfLinks;
buf->st_dev = local.dwVolumeSerialNumber;
buf->st_size = local.nFileSizeLow;
/* Allocate some place to determine the root directory. Need to allocate
enough so that rootdir can add a trailing slash if path starts with \\. */
char root[strlen (get_win32_name ()) + 3];
strcpy (root, get_win32_name ());
/* Assume that if a drive has ACL support it MAY have valid "inodes".
It definitely does not have valid inodes if it does not have ACL
support. */
switch (has_acls () ? GetDriveType (rootdir (root)) : DRIVE_UNKNOWN)
{
case DRIVE_FIXED:
case DRIVE_REMOVABLE:
case DRIVE_CDROM:
case DRIVE_RAMDISK:
/* Although the documentation indicates otherwise, it seems like
"inodes" on these devices are persistent, at least across reboots. */
buf->st_ino = local.nFileIndexHigh | local.nFileIndexLow;
break;
default:
/* Either the nFileIndex* fields are unreliable or unavailable. Use the
next best alternative. */
buf->st_ino = get_namehash ();
break;
}
buf->st_blksize = S_BLKSIZE;
buf->st_blocks = ((unsigned long) buf->st_size + S_BLKSIZE-1) / S_BLKSIZE;
buf->st_mode = 0;
/* Using a side effect: get_file_attibutes checks for
directory. This is used, to set S_ISVTX, if needed. */
if (local.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
buf->st_mode = S_IFDIR;
else if (get_symlink_p ())
buf->st_mode = S_IFLNK;
else if (get_socket_p ())
buf->st_mode = S_IFSOCK;
if (get_file_attribute (has_acls (), get_win32_name (), &buf->st_mode,
&buf->st_uid, &buf->st_gid) == 0)
{
/* If read-only attribute is set, modify ntsec return value */
if ((local.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
&& !get_symlink_p ())
buf->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
if (!(buf->st_mode & S_IFMT))
buf->st_mode |= S_IFREG;
}
else
{
buf->st_mode |= STD_RBITS;
if (!(local.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
buf->st_mode |= STD_WBITS;
/* | S_IWGRP | S_IWOTH; we don't give write to group etc */
if (buf->st_mode & S_IFDIR)
buf->st_mode |= S_IFDIR | STD_XBITS;
else if (buf->st_mode & S_IFMT)
/* nothing */;
else if (get_socket_p ())
buf->st_mode |= S_IFSOCK;
else
switch (GetFileType (get_handle ()))
{
case FILE_TYPE_CHAR:
case FILE_TYPE_UNKNOWN:
buf->st_mode |= S_IFCHR;
break;
case FILE_TYPE_DISK:
buf->st_mode |= S_IFREG;
if (!dont_care_if_execable () && !get_execable_p ())
{
DWORD cur, done;
char magic[3];
/* First retrieve current position, set to beginning
of file if not already there. */
cur = SetFilePointer (get_handle(), 0, NULL, FILE_CURRENT);
if (cur != INVALID_SET_FILE_POINTER &&
(!cur ||
SetFilePointer (get_handle(), 0, NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER))
{
/* FIXME should we use /etc/magic ? */
magic[0] = magic[1] = magic[2] = '\0';
if (ReadFile (get_handle (), magic, 3, &done, NULL) &&
has_exec_chars (magic, done))
set_execable_p ();
SetFilePointer (get_handle(), cur, NULL, FILE_BEGIN);
}
}
if (get_execable_p ())
buf->st_mode |= STD_XBITS;
break;
case FILE_TYPE_PIPE:
buf->st_mode |= S_IFSOCK;
break;
}
}
syscall_printf ("0 = fstat (, %p) st_atime=%x st_size=%d, st_mode=%p, st_ino=%d, sizeof=%d",
buf, buf->st_atime, buf->st_size, buf->st_mode,
(int) buf->st_ino, sizeof (*buf));
return 0;
}
fhandler_disk_file::fhandler_disk_file () :
fhandler_base (FH_DISK)
{
}
int
fhandler_disk_file::open (path_conv *real_path, int flags, mode_t mode)
{
if (real_path->case_clash && flags & O_CREAT)
{
debug_printf ("Caseclash detected.");
set_errno (ECASECLASH);
return 0;
}
if (real_path->isbinary ())
{
set_r_binary (1);
set_w_binary (1);
}
set_has_acls (real_path->has_acls ());
set_isremote (real_path->isremote ());
if (real_path->isdir ())
flags |= O_DIROPEN;
int res = this->fhandler_base::open (real_path, flags, mode);
if (!res)
goto out;
/* This is for file systems known for having a buggy CreateFile call
which might return a valid HANDLE without having actually opened
the file.
The only known file system to date is the SUN NFS Solstice Client 3.1
which returns a valid handle when trying to open a file in a nonexistent
directory. */
if (real_path->has_buggy_open ()
&& GetFileAttributes (win32_path_name) == (DWORD) -1)
{
debug_printf ("Buggy open detected.");
close ();
set_errno (ENOENT);
return 0;
}
if (flags & O_APPEND)
SetFilePointer (get_handle(), 0, 0, FILE_END);
set_symlink_p (real_path->issymlink ());
set_execable_p (real_path->exec_state ());
set_socket_p (real_path->issocket ());
out:
syscall_printf ("%d = fhandler_disk_file::open (%s, %p)", res,
get_win32_name (), flags);
return res;
}
int
fhandler_disk_file::close ()
{
int res = this->fhandler_base::close ();
if (!res)
cygwin_shared->delqueue.process_queue ();
return res;
}
/*
* FIXME !!!
* The correct way to do this to get POSIX locking
* semantics is to keep a linked list of posix lock
* requests and map them into Win32 locks. The problem
* is that Win32 does not deal correctly with overlapping
* lock requests. Also another pain is that Win95 doesn't do
* non-blocking or non exclusive locks at all. For '95 just
* convert all lock requests into blocking,exclusive locks.
* This shouldn't break many apps but denying all locking
* would.
* For now just convert to Win32 locks and hope for the best.
*/
int
fhandler_disk_file::lock (int cmd, struct flock *fl)
{
int win32_start;
int win32_len;
DWORD win32_upper;
DWORD startpos;
/*
* We don't do getlck calls yet.
*/
if (cmd == F_GETLK)
{
set_errno (ENOSYS);
return -1;
}
/*
* Calculate where in the file to start from,
* then adjust this by fl->l_start.
*/
switch (fl->l_whence)
{
case SEEK_SET:
startpos = 0;
break;
case SEEK_CUR:
if ((off_t) (startpos = lseek (0, SEEK_CUR)) == (off_t)-1)
return -1;
break;
case SEEK_END:
{
BY_HANDLE_FILE_INFORMATION finfo;
if (GetFileInformationByHandle (get_handle(), &finfo) == 0)
{
__seterrno ();
return -1;
}
startpos = finfo.nFileSizeLow; /* Nowhere to keep high word */
break;
}
default:
set_errno (EINVAL);
return -1;
}
/*
* Now the fun starts. Adjust the start and length
* fields until they make sense.
*/
win32_start = startpos + fl->l_start;
if (fl->l_len < 0)
{
win32_start -= fl->l_len;
win32_len = -fl->l_len;
}
else
win32_len = fl->l_len;
if (win32_start < 0)
{
/* watch the signs! */
win32_len -= -win32_start;
if (win32_len <= 0)
{
/* Failure ! */
set_errno (EINVAL);
return -1;
}
win32_start = 0;
}
/*
* Special case if len == 0 for POSIX means lock
* to the end of the entire file (and all future extensions).
*/
if (win32_len == 0)
{
win32_len = 0xffffffff;
win32_upper = wincap.lock_file_highword ();
}
else
win32_upper = 0;
BOOL res;
if (wincap.has_lock_file_ex ())
{
DWORD lock_flags = (cmd == F_SETLK) ? LOCKFILE_FAIL_IMMEDIATELY : 0;
lock_flags |= (fl->l_type == F_WRLCK) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
OVERLAPPED ov;
ov.Internal = 0;
ov.InternalHigh = 0;
ov.Offset = (DWORD)win32_start;
ov.OffsetHigh = 0;
ov.hEvent = (HANDLE) 0;
if (fl->l_type == F_UNLCK)
{
res = UnlockFileEx (get_handle (), 0, (DWORD)win32_len, win32_upper, &ov);
}
else
{
res = LockFileEx (get_handle (), lock_flags, 0, (DWORD)win32_len,
win32_upper, &ov);
/* Deal with the fail immediately case. */
/*
* FIXME !! I think this is the right error to check for
* but I must admit I haven't checked....
*/
if ((res == 0) && (lock_flags & LOCKFILE_FAIL_IMMEDIATELY) &&
(GetLastError () == ERROR_LOCK_FAILED))
{
set_errno (EAGAIN);
return -1;
}
}
}
else
{
/* Windows 95 -- use primitive lock call */
if (fl->l_type == F_UNLCK)
res = UnlockFile (get_handle (), (DWORD)win32_start, 0, (DWORD)win32_len,
win32_upper);
else
res = LockFile (get_handle (), (DWORD)win32_start, 0, (DWORD)win32_len, win32_upper);
}
if (res == 0)
{
__seterrno ();
return -1;
}
return 0;
}
DIR *
fhandler_disk_file::opendir (const char *name, path_conv& real_name)
{
struct stat statbuf;
DIR *dir;
DIR *res = NULL;
size_t len;
if (fstat (&statbuf, &real_name) ||!(statbuf.st_mode & S_IFDIR))
set_errno (ENOTDIR);
else if ((len = strlen (real_name))> MAX_PATH - 3)
set_errno (ENAMETOOLONG);
else if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
set_errno (ENOMEM);
else if ((dir->__d_dirname = (char *) malloc (len + 3)) == NULL)
{
free (dir);
set_errno (ENOMEM);
}
else if ((dir->__d_dirent =
(struct dirent *) malloc (sizeof (struct dirent))) == NULL)
{
free (dir->__d_dirname);
free (dir);
set_errno (ENOMEM);
}
else
{
strcpy (dir->__d_dirname, real_name.get_win32 ());
dir->__d_dirent->d_version = __DIRENT_VERSION;
cygheap_fdnew fd;
fd = this;
dir->__d_dirent->d_fd = fd;
dir->__d_u.__d_data.__fh = this;
/* FindFirstFile doesn't seem to like duplicate /'s. */
len = strlen (dir->__d_dirname);
if (len == 0 || SLASH_P (dir->__d_dirname[len - 1]))
strcat (dir->__d_dirname, "*");
else
strcat (dir->__d_dirname, "\\*"); /**/
dir->__d_cookie = __DIRENT_COOKIE;
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
dir->__d_position = 0;
dir->__d_dirhash = statbuf.st_ino;
res = dir;
}
syscall_printf ("%p = opendir (%s)", res, name);
return res;
}
struct dirent *
fhandler_disk_file::readdir (DIR *dir)
{
WIN32_FIND_DATA buf;
HANDLE handle;
struct dirent *res = NULL;
if (dir->__d_u.__d_data.__handle == INVALID_HANDLE_VALUE
&& dir->__d_position == 0)
{
handle = FindFirstFileA (dir->__d_dirname, &buf);
DWORD lasterr = GetLastError ();
dir->__d_u.__d_data.__handle = handle;
if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES))
{
seterrno_from_win_error (__FILE__, __LINE__, lasterr);
return res;
}
}
else if (dir->__d_u.__d_data.__handle == INVALID_HANDLE_VALUE)
{
return res;
}
else if (!FindNextFileA (dir->__d_u.__d_data.__handle, &buf))
{
DWORD lasterr = GetLastError ();
(void) FindClose (dir->__d_u.__d_data.__handle);
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
/* POSIX says you shouldn't set errno when readdir can't
find any more files; so, if another error we leave it set. */
if (lasterr != ERROR_NO_MORE_FILES)
seterrno_from_win_error (__FILE__, __LINE__, lasterr);
syscall_printf ("%p = readdir (%p)", res, dir);
return res;
}
/* We get here if `buf' contains valid data. */
strcpy (dir->__d_dirent->d_name, buf.cFileName);
/* Check for Windows shortcut. If it's a Cygwin or U/WIN
symlink, drop the .lnk suffix. */
if (buf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
{
char *c = dir->__d_dirent->d_name;
int len = strlen (c);
if (strcasematch (c + len - 4, ".lnk"))
{
char fbuf[MAX_PATH + 1];
strcpy (fbuf, dir->__d_dirname);
strcpy (fbuf + strlen (fbuf) - 1, dir->__d_dirent->d_name);
path_conv fpath (fbuf, PC_SYM_NOFOLLOW);
if (fpath.issymlink ())
c[len - 4] = '\0';
}
}
/* Compute d_ino by combining filename hash with the directory hash
(which was stored in dir->__d_dirhash when opendir was called). */
if (buf.cFileName[0] == '.')
{
if (buf.cFileName[1] == '\0')
dir->__d_dirent->d_ino = dir->__d_dirhash;
else if (buf.cFileName[1] != '.' || buf.cFileName[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:
ino_t dino = hash_path_name (dir->__d_dirhash, "\\");
dir->__d_dirent->d_ino = hash_path_name (dino, buf.cFileName);
}
++dir->__d_position;
res = dir->__d_dirent;
syscall_printf ("%p = readdir (%p) (%s)",
&dir->__d_dirent, dir, buf.cFileName);
return res;
}
off_t
fhandler_disk_file::telldir (DIR *dir)
{
return dir->__d_position;
}
void
fhandler_disk_file::seekdir (DIR *dir, off_t loc)
{
rewinddir (dir);
while (loc > dir->__d_position)
if (! readdir (dir))
break;
}
void
fhandler_disk_file::rewinddir (DIR *dir)
{
if (dir->__d_u.__d_data.__handle != INVALID_HANDLE_VALUE)
{
(void) FindClose (dir->__d_u.__d_data.__handle);
dir->__d_u.__d_data.__handle = INVALID_HANDLE_VALUE;
}
dir->__d_position = 0;
}
int
fhandler_disk_file::closedir (DIR *dir)
{
int res = 0;
if (dir->__d_u.__d_data.__handle != INVALID_HANDLE_VALUE &&
FindClose (dir->__d_u.__d_data.__handle) == 0)
{
__seterrno ();
res = -1;
}
syscall_printf ("%d = closedir (%p)", res, dir);
return 0;
}

View File

@ -1234,31 +1234,24 @@ conv_path_list (const char *src, char *dst, int to_posix_p)
int (*conv_fn) (const char *, char *) = (to_posix_p
? cygwin_conv_to_posix_path
: cygwin_conv_to_win32_path);
char srcbuf[MAX_PATH];
int len;
char *srcbuf = (char *) alloca (strlen (src) + 1);
do
{
s = strchr (src, src_delim);
if (s)
s = strccpy (srcbuf, &src, src_delim);
int len = s - srcbuf;
if (len >= MAX_PATH)
srcbuf[MAX_PATH - 1] = '\0';
(*conv_fn) (len ? srcbuf : ".", d);
src += len;
if (*src)
{
len = s - src;
if (len >= MAX_PATH)
len = MAX_PATH - 1;
memcpy (srcbuf, src, len);
srcbuf[len] = 0;
(*conv_fn) (len ? srcbuf : ".", d);
d += strlen (d);
d = strchr (d, '\0');
*d++ = dst_delim;
src = s + 1;
}
else
{
/* Last one. */
(*conv_fn) (src[0] != 0 ? src : ".", d);
}
}
while (s != NULL);
while (*src++);
}
/* init: Initialize the mount table. */