Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include<Windows.h>
  3. using namespace std;
  4.  
  5. VOID WINAPI asyncReading(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV);
  6. VOID WINAPI asyncWrite(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV);
  7. void asynccopy();
  8.  
  9. const int CLUSTER = 4096;//4 KB
  10. HANDLE infile, outfile;
  11. CHAR** buffers;
  12. int multiplier;
  13. int bytestouse;
  14. OVERLAPPED* OverLapIn, * OverLapOut;//структуры овелапд
  15. LONGLONG counter;
  16. LARGE_INTEGER fileSize;
  17. int main()
  18. {
  19.     asynccopy();
  20.     system("pause");
  21.     return 0;
  22. }
  23. void asynccopy()
  24. {
  25.     puts("Enter filepath. Spaces are not allowed");
  26.     char path[MAX_PATH];
  27.     cin >> path;
  28.     infile = CreateFileA(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED| FILE_FLAG_NO_BUFFERING, NULL);
  29.     if (infile == INVALID_HANDLE_VALUE)
  30.         cout << "Error " << GetLastError() << endl;
  31.     else
  32.     {
  33.         puts("File opened successfully");
  34.         puts("Enter filepath where to copy file. Spaces are not allowed");
  35.         char newpath[MAX_PATH];
  36.         cin >> newpath;
  37.         outfile = CreateFileA(newpath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,  FILE_FLAG_OVERLAPPED| FILE_FLAG_NO_BUFFERING  , NULL);
  38.         if (outfile == INVALID_HANDLE_VALUE)
  39.             cout << "Error " << GetLastError() << endl;
  40.         else
  41.         {
  42.             puts("File created successfully");
  43.             puts("Enter cluster multipltier and number of overlaps");
  44.             int num_ovrlp;
  45.             cin >> multiplier >> num_ovrlp;
  46.             bytestouse = multiplier * CLUSTER;
  47.             OverLapIn = new OVERLAPPED[num_ovrlp];
  48.             OverLapOut = new OVERLAPPED[num_ovrlp];
  49.             buffers = new CHAR * [num_ovrlp];//буфферы
  50.             for (int i = 0; i < num_ovrlp; i++)
  51.                 buffers[i] = new CHAR[bytestouse];
  52.             int clustersInFile;
  53.             LARGE_INTEGER posIn;
  54.             GetFileSizeEx(infile, &fileSize);
  55.             clustersInFile = fileSize.QuadPart / bytestouse;
  56.             if (fileSize.QuadPart % bytestouse != 0)
  57.                 clustersInFile++;
  58.             cout << "File Size: " << fileSize.QuadPart << " Bytes" << endl;
  59.             cout << "Copying with given buffer will take " << clustersInFile*2 << " operations"<<endl;;
  60.             posIn.QuadPart = 0;
  61.             DWORD before = GetTickCount64();
  62.             counter = 0;
  63.             for (int i = 0; i < num_ovrlp; i++)
  64.             {
  65.                 OverLapIn[i].hEvent = (HANDLE)i;
  66.                 OverLapOut[i].hEvent = (HANDLE)i;
  67.                 OverLapIn[i].Offset = posIn.LowPart;
  68.                 OverLapIn[i].OffsetHigh = posIn.HighPart;
  69.                 if (posIn.QuadPart < fileSize.QuadPart)
  70.                     ReadFileEx(infile, buffers[i], bytestouse, &OverLapIn[i],asyncReading);
  71.                 posIn.QuadPart += (LONGLONG)bytestouse;
  72.             }
  73.             while (counter < 2 * clustersInFile)
  74.                 SleepEx(INFINITE, true);
  75.             DWORD after = GetTickCount64();
  76.             cout << "Copying took " << float(after - before) << " miliseconds /n Overlaps: " << num_ovrlp << " Buffer size per operation (Bytes) : " << bytestouse << endl;
  77.             delete[] OverLapIn;
  78.             delete[]OverLapOut;
  79.             for (int i = 0; i < num_ovrlp; i++)
  80.                 delete[] buffers[i];
  81.             delete[] buffers;
  82.             SetFilePointerEx(outfile, fileSize, 0, FILE_BEGIN);
  83.             SetEndOfFile(outfile);
  84.             CloseHandle(outfile);
  85.         }
  86.     }
  87.     CloseHandle(infile);
  88. }
  89. VOID WINAPI asyncReading(DWORD code,DWORD nBytes,LPOVERLAPPED lpOV) {
  90.     ++counter;
  91.     LARGE_INTEGER posIn, posOut;
  92.     DWORD i = (DWORD)(lpOV->hEvent);
  93.     posIn.LowPart = OverLapIn[i].Offset;
  94.     posIn.HighPart = OverLapIn[i].OffsetHigh;
  95.     posOut.QuadPart = posIn.QuadPart;
  96.     OverLapOut[i].Offset = posOut.LowPart;
  97.     OverLapOut[i].OffsetHigh = posOut.HighPart;
  98.     WriteFileEx(outfile, buffers[i], bytestouse, &OverLapOut[i], asyncWrite);
  99.     posIn.QuadPart += bytestouse;
  100.     OverLapIn[i].Offset = posIn.LowPart;
  101.     OverLapIn[i].OffsetHigh = posIn.HighPart;
  102. }
  103. VOID WINAPI asyncWrite(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV) {
  104.     ++counter;
  105.     LARGE_INTEGER posIn;
  106.     DWORD i = (DWORD)(lpOV->hEvent);
  107.     posIn.LowPart = OverLapIn[i].Offset;
  108.     posIn.HighPart = OverLapIn[i].OffsetHigh;
  109.     if (posIn.QuadPart < fileSize.QuadPart)
  110.         ReadFileEx(infile, buffers[i], bytestouse, &OverLapIn[i], asyncReading);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement