| 866 | } |
| 867 | |
| 868 | static NTSTATUS DOKAN_CALLBACK CryptWriteFile(LPCWSTR FileName, LPCVOID Buffer, |
| 869 | DWORD NumberOfBytesToWrite, |
| 870 | LPDWORD NumberOfBytesWritten, |
| 871 | LONGLONG Offset, |
| 872 | PDOKAN_FILE_INFO DokanFileInfo) { |
| 873 | FileNameEnc filePath(DokanFileInfo, FileName); |
| 874 | HANDLE handle = (HANDLE)DokanFileInfo->Context; |
| 875 | BOOL opened = FALSE; |
| 876 | NTSTATUS ret_status = STATUS_SUCCESS; |
| 877 | |
| 878 | DbgPrint(L"WriteFile : %s, offset %I64d, length %d - paging io %u\n", |
| 879 | FileName, Offset, NumberOfBytesToWrite, DokanFileInfo->PagingIo); |
| 880 | |
| 881 | if (DokanFileInfo->WriteToEndOfFile) { |
| 882 | if (DokanFileInfo->PagingIo) { |
| 883 | DbgPrint(L"paging io to end of file. doing nothing\n"); |
| 884 | *NumberOfBytesWritten = 0; |
| 885 | return STATUS_SUCCESS; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // reopen the file |
| 890 | if (!handle || handle == INVALID_HANDLE_VALUE) { |
| 891 | DbgPrint(L"\tinvalid handle, cleanuped?\n"); |
| 892 | handle = CreateFile(filePath, GENERIC_WRITE | GENERIC_READ, |
| 893 | FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 894 | 0, NULL); |
| 895 | if (handle == INVALID_HANDLE_VALUE) { |
| 896 | DWORD error = GetLastError(); |
| 897 | DbgPrint(L"\tCreateFile error : %d\n\n", error); |
| 898 | return ToNtStatus(error); |
| 899 | } |
| 900 | GetContext()->m_openfiles.OpenFile(FileName, handle); |
| 901 | opened = TRUE; |
| 902 | } |
| 903 | |
| 904 | CryptFile *file = CryptFile::NewInstance(GetContext()); |
| 905 | |
| 906 | if (file->Associate(GetContext(), handle, FileName, true)) { |
| 907 | if (!file->Write((const unsigned char *)Buffer, NumberOfBytesToWrite, |
| 908 | NumberOfBytesWritten, Offset, |
| 909 | DokanFileInfo->WriteToEndOfFile, |
| 910 | DokanFileInfo->PagingIo)) { |
| 911 | DWORD error = GetLastError(); |
| 912 | DbgPrint(L"\twrite error = %u, buffer length = %d, write length = %d\n", |
| 913 | error, NumberOfBytesToWrite, *NumberOfBytesWritten); |
| 914 | ret_status = ToNtStatus(error); |
| 915 | } else { |
| 916 | DbgPrint(L"\twrote nbytes = %u\n", *NumberOfBytesWritten); |
| 917 | } |
| 918 | } else { |
| 919 | ret_status = STATUS_ACCESS_DENIED; |
| 920 | } |
| 921 | |
| 922 | delete file; |
| 923 | |
| 924 | // close the file when it is reopened |
| 925 | if (opened) { |