blob: 3629f5bbe5e281ca0690869815f1998522e161cf [file] [log] [blame] [raw]
/* A part of the Native C Library for Windows NT
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <windows.h>
#include <nt.h>
#include <errno.h>
#include <unistd.h>
#include <pathname.h>
#include <ntstatus.h>
#include <stdio.h>
int access(const char *pathname, int mode) {
if(mode & ~(R_OK | W_OK | X_OK | F_OK)) {
errno = EINVAL;
return -1;
}
if(!pathname) {
errno = EFAULT;
return -1;
}
if(!*pathname) {
errno = ENOENT;
return -1;
}
if(*pathname != '/') {
errno = ENOSYS;
return -1;
}
int request_dir = 0;
UNICODE_STRING ntpathname;
if(!RtlCreateUnicodeStringFromAsciiz(&ntpathname, pathname)) {
errno = ENOMEM;
return -1;
}
if(ntpathname.Length > sizeof(wchar_t) && ntpathname.Buffer[ntpathname.Length / sizeof(wchar_t) - 1] == L'/') {
request_dir = 1;
ntpathname.Buffer[ntpathname.Length / sizeof(wchar_t) - 1] = 0;
ntpathname.Length -= sizeof(wchar_t);
ntpathname.MaximumLength -= sizeof(wchar_t);
}
PATHNAME_UNIX2NT_UTF16_STRUCT(ntpathname);
void *handle;
OBJECT_ATTRIBUTES object_attrib = { sizeof(OBJECT_ATTRIBUTES), NULL, &ntpathname, 0, NULL, NULL };
unsigned long int access_mask = DIRECTORY_QUERY;
//if(mode & R_OK) access_mask |= DIRECTORY_QUERY;
if(mode & W_OK) access_mask |= DIRECTORY_CREATE_OBJECT | DIRECTORY_CREATE_SUBDIRECTORY;
if(mode & X_OK) access_mask |= DIRECTORY_TRAVERSE;
long int status = NtOpenDirectoryObject(&handle, access_mask, &object_attrib);
printf("nativelibc debug: access: status = 0x%lx\n", status);
// F_OK tests for the existence of the file
if(mode == F_OK) switch(status) {
case STATUS_INVALID_PARAMETER:
case STATUS_NO_SUCH_DEVICE:
case STATUS_NO_SUCH_FILE:
case STATUS_WRONG_VOLUME:
case STATUS_NO_MEDIA_IN_DEVICE:
case STATUS_UNRECOGNIZED_MEDIA:
case STATUS_NO_MEMORY:
case STATUS_DISK_CORRUPT_ERROR:
case STATUS_OBJECT_TYPE_MISMATCH: // If the path is in FS, this status is returned
case STATUS_OBJECT_NAME_INVALID:
case STATUS_OBJECT_NAME_NOT_FOUND:
case STATUS_OBJECT_PATH_INVALID:
case STATUS_OBJECT_PATH_NOT_FOUND:
case STATUS_OBJECT_PATH_SYNTAX_BAD:
//case STATUS_OBJECT_NO_LONGER_EXISTS:
break;
default:
RtlFreeUnicodeString(&ntpathname);
if(status >= 0) NtClose(handle);
return 0;
}
if(status != STATUS_OBJECT_TYPE_MISMATCH) {
RtlFreeUnicodeString(&ntpathname);
if(status >= 0) NtClose(handle);
return __set_errno_from_ntstatus(status) ? -1 : 0;
}
if(request_dir) {
ntpathname.Buffer[ntpathname.Length / sizeof(wchar_t)] = L'\\';
ntpathname.Length += sizeof(wchar_t);
ntpathname.MaximumLength += sizeof(wchar_t);
}
IO_STATUS_BLOCK io_status;
unsigned long int file_share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
unsigned long int options = FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT;
access_mask = SYNCHRONIZE;
if(mode & R_OK) access_mask |= GENERIC_READ | FILE_LIST_DIRECTORY;
if(mode & W_OK) access_mask |= GENERIC_WRITE;
if(mode & X_OK) access_mask |= FILE_TRAVERSE; // Directory only
status = NtOpenFile(&handle, access_mask, &object_attrib, &io_status, file_share, options);
printf("nativelibc debug: access: status = 0x%lx\n", status);
RtlFreeUnicodeString(&ntpathname);
if(status >= 0) NtClose(handle);
return __set_errno_from_ntstatus(status) ? -1 : 0;
}