Cygwin: open: cleanup code in preparation of O_TMPFILE

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2017-11-14 16:28:44 +01:00
parent 9cf0c4a012
commit f94fe74aad
1 changed files with 55 additions and 57 deletions

View File

@ -1361,6 +1361,7 @@ open (const char *unix_path, int flags, ...)
int res = -1; int res = -1;
va_list ap; va_list ap;
mode_t mode = 0; mode_t mode = 0;
fhandler_base *fh = NULL;
pthread_testcancel (); pthread_testcancel ();
@ -1368,19 +1369,21 @@ open (const char *unix_path, int flags, ...)
{ {
syscall_printf ("open(%s, %y)", unix_path, flags); syscall_printf ("open(%s, %y)", unix_path, flags);
if (!*unix_path) if (!*unix_path)
set_errno (ENOENT);
else
{ {
set_errno (ENOENT);
__leave;
}
/* check for optional mode argument */ /* check for optional mode argument */
va_start (ap, flags); va_start (ap, flags);
mode = va_arg (ap, mode_t); mode = va_arg (ap, mode_t);
va_end (ap); va_end (ap);
fhandler_base *fh;
cygheap_fdnew fd; cygheap_fdnew fd;
if (fd >= 0) if (fd < 0)
{ __leave; /* errno already set */
/* This is a temporary kludge until all utilities can catch up /* This is a temporary kludge until all utilities can catch up
with a change in behavior that implements linux functionality: with a change in behavior that implements linux functionality:
opening a tty should not automatically cause it to become the opening a tty should not automatically cause it to become the
@ -1394,43 +1397,38 @@ open (const char *unix_path, int flags, ...)
of being a controlling terminal if /dev/tty is opened. */ of being a controlling terminal if /dev/tty is opened. */
opt |= PC_CTTY; opt |= PC_CTTY;
} }
if (!(fh = build_fh_name (unix_path, opt, stat_suffixes))) if (!(fh = build_fh_name (unix_path, opt, stat_suffixes)))
; // errno already set __leave; /* errno already set */
else if ((flags & O_NOFOLLOW) && fh->issymlink ()) if ((flags & O_NOFOLLOW) && fh->issymlink ())
{ {
delete fh;
set_errno (ELOOP); set_errno (ELOOP);
__leave;
} }
else if ((flags & O_DIRECTORY) && fh->exists () if ((flags & O_DIRECTORY) && fh->exists () && !fh->pc.isdir ())
&& !fh->pc.isdir ())
{ {
delete fh;
set_errno (ENOTDIR); set_errno (ENOTDIR);
__leave;
} }
else if (((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) if (((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) && fh->exists ())
&& fh->exists ())
{ {
delete fh;
set_errno (EEXIST); set_errno (EEXIST);
__leave;
} }
else if ((fh->is_fs_special () if ((fh->is_fs_special () && fh->device_access_denied (flags))
&& fh->device_access_denied (flags))
|| !fh->open_with_arch (flags, mode & 07777)) || !fh->open_with_arch (flags, mode & 07777))
delete fh; __leave; /* errno already set */
else
{
fd = fh; fd = fh;
if (fd <= 2) if (fd <= 2)
set_std_handle (fd); set_std_handle (fd);
res = fd; res = fd;
} }
}
}
syscall_printf ("%R = open(%s, %y)", res, unix_path, flags);
}
__except (EFAULT) {} __except (EFAULT) {}
__endtry __endtry
if (res < 0 && fh)
delete fh;
syscall_printf ("%R = open(%s, %y)", res, unix_path, flags);
return res; return res;
} }