blob: 4eef5a2bc9e862225cd8ee9406a5faffbf5e8a0a [file] [log] [blame] [raw]
/* A part of the Native C Library for Windows NT
Copyright 2007-2015 PC GO Ld.
Copyright 2026 Rivoreo
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 <fcntl.h>
#include <ntstatus.h>
int linkat(int old_dir_fd, const char *oldpath, int new_dir_fd, const char *newpath, int flags) {
if(old_dir_fd == -1 || new_dir_fd == -1) {
errno = EBADF;
return -1;
}
if(!oldpath || !newpath) {
errno = EFAULT;
return -1;
}
if(flags & ~AT_SYMLINK_FOLLOW) {
errno = EINVAL;
return -1;
}
void *handle;
IO_STATUS_BLOCK io_status;
unsigned long int file_access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
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 obj_attr = { .Length = sizeof(OBJECT_ATTRIBUTES), .ObjectName = &old_path };
if(*oldpath != '/') {
if(old_dir_fd == AT_FDCWD) {
RtlFreeUnicodeString(&old_path);
errno = EPERM;
return -1;
}
obj_attr.RootDirectory = (void *)old_dir_fd;
}
if(!(flags & AT_SYMLINK_FOLLOW)) {
long int status = NtOpenSymbolicLinkObject(&handle, 0, &obj_attr);
if(status != STATUS_OBJECT_TYPE_MISMATCH) {
if(status >= 0) {
NtClose(handle);
errno = EOPNOTSUPP;
} else {
__set_errno_from_ntstatus(status);
}
return -1;
}
}
long int status = NtOpenFile(&handle, file_access, &obj_attr, &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 = *newpath == '/' && new_dir_fd != AT_FDCWD ? NULL : (void *)new_dir_fd;
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;
}
int link(const char *oldpath, const char *newpath) {
return linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, AT_SYMLINK_FOLLOW);
}