blob: fbe9f8b75313cb7442f3e0c851a25b4d18ebcbd5 [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 <pathname.h>
#include <ntstatus.h>
#include <stdio.h>
int rmdir(const char *pathname) {
if(!pathname) {
errno = EFAULT;
return -1;
}
if(!*pathname) {
errno = ENOENT;
return -1;
}
if(*pathname != '/') {
errno = ENOSYS;
return -1;
}
if(!pathname[1]) {
// Want to remove / ??
errno = EPERM;
return -1;
}
UNICODE_STRING ntpathname;
RtlCreateUnicodeStringFromAsciiz(&ntpathname, pathname);
if(ntpathname.Length > sizeof(wchar_t) && ntpathname.Buffer[ntpathname.Length / sizeof(wchar_t) - 1] == L'/') {
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 };
long int status = NtOpenSymbolicLinkObject(&handle, 0, &object_attrib);
//printf("nativelibc debug: rmdir: check sym link: status = 0x%lx\n", status);
if(status != STATUS_OBJECT_TYPE_MISMATCH) {
// The rmdir will failed on symbolic link even the pathname contents a '/' at end
RtlFreeUnicodeString(&ntpathname);
if(status == STATUS_OBJECT_NAME_NOT_FOUND) errno = ENOENT;
else {
if(status >= 0) NtClose(handle);
errno = ENOTDIR;
}
return -1;
}
status = NtOpenDirectoryObject(&handle, DIRECTORY_QUERY | DELETE, &object_attrib);
//printf("nativelibc debug: rmdir check dir object: status = 0x%lx\n", status);
if(status != STATUS_OBJECT_TYPE_MISMATCH) {
RtlFreeUnicodeString(&ntpathname);
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
unsigned long int context = 0;
status = NtQueryDirectoryObject(handle, NULL, 0, 0, 1, &context, NULL);
//printf("nativelibc debug: rmdir check dir object contents: status = 0x%lx\n", status);
if(status != STATUS_NO_MORE_ENTRIES) {
if(status < 0) __set_errno_from_ntstatus(status);
else {
NtClose(handle);
errno = ENOTEMPTY;
}
return -1;
}
status = NtMakeTemporaryObject(handle);
//printf("nativelibc debug: rmdir: status = 0x%lx\n", status);
NtClose(handle);
return __set_errno_from_ntstatus(status) ? -1 : 0;
}
IO_STATUS_BLOCK io_status;
unsigned long int file_share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
unsigned long int options = FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT;
status = NtOpenFile(&handle, FILE_LIST_DIRECTORY | DELETE | SYNCHRONIZE, &object_attrib, &io_status, file_share, options);
printf("nativelibc debug: rmdir: status = 0x%lx\n", status);
if(status < 0) {
RtlFreeUnicodeString(&ntpathname);
__set_errno_from_ntstatus(status);
return -1;
}
// TODO: Remove a EMPTY directory on a file system
RtlFreeUnicodeString(&ntpathname);
NtClose(handle);
return -1;
}