| #include <windows.h> |
| #include <nt.h> |
| //#include <errno.h> |
| #include <pathname.h> |
| |
| int link(const char *oldpath, const char *newpath) { |
| 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_FLAG_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, DELETE | SYNCHRONIZE, &old_object, &io_status, file_share, options); |
| RtlFreeUnicodeString(&old_path); |
| //printf("nativelibc debug: status = 0x%lx\n",status); |
| //if(ntstatus_to_errno(status)) goto failed; |
| if(status < 0) goto failed; // TODO: implemente the errno |
| |
| FILE_LINK_INFORMATION *new_link = malloc(sizeof(FILE_LINK_INFORMATION) + new_path.MaximumLength); |
| if(!new_link) { |
| NtClose(handle); |
| //errno = ENOMEM; // TODO: implemente the errno |
| //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 ntstatus_to_errno(status) ? -1 : 0; |
| return status < 0 ? -1 : 0; // TODO: implemente the errno |
| |
| failed: |
| RtlFreeUnicodeString(&new_path); |
| return -1; |
| } |