blob: 5c21c903a70de76893d7013852a76a4da792600e [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>
int link(const char *oldpath, const char *newpath) {
if(!oldpath || !newpath) {
errno = EFAULT;
return -1;
}
void *handle;
IO_STATUS_BLOCK io_status;
unsigned long int file_share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
unsigned long int options = FILE_NON_DIRECTORY_FILE | FILE_OPEN_REPARSE_POINT | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT;
UNICODE_STRING old_path;
UNICODE_STRING new_path;
RtlCreateUnicodeStringFromAsciiz(&old_path, oldpath);
RtlCreateUnicodeStringFromAsciiz(&new_path, newpath);
PATHNAME_UNIX2NT_UTF16_STRUCT(old_path);
PATHNAME_UNIX2NT_UTF16_STRUCT(new_path);
OBJECT_ATTRIBUTES old_object = { sizeof(OBJECT_ATTRIBUTES), NULL, &old_path, 0, NULL, NULL };
long int status = NtOpenFile(&handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &old_object, &io_status, file_share, options);
RtlFreeUnicodeString(&old_path);
if(__set_errno_from_ntstatus(status)) goto failed;
FILE_LINK_INFORMATION *new_link = malloc(sizeof(FILE_LINK_INFORMATION) + new_path.MaximumLength);
if(!new_link) {
NtClose(handle);
errno = ENOMEM;
//return -1;
goto failed;
}
memcpy(new_link->FileName, new_path.Buffer, new_path.MaximumLength);
new_link->ReplaceIfExists = 0;
new_link->RootDirectory = NULL;
new_link->FileNameLength = new_path.Length;
status = NtSetInformationFile(handle, &io_status, new_link, sizeof(FILE_LINK_INFORMATION) + new_path.MaximumLength, FileLinkInformation);
//printf("nativelibc debug: status = 0x%lx\n", status);
RtlFreeUnicodeString(&new_path);
free(new_link);
NtClose(handle);
return __set_errno_from_ntstatus(status) ? -1 : 0;
failed:
RtlFreeUnicodeString(&new_path);
return -1;
}