SHARE
TWEET

os32

a guest Nov 15th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX_LEN 255
  6.  
  7. typedef struct {
  8.     OVERLAPPED overlap;
  9.     LPVOID buf;
  10. } BUF, *LPBUF;
  11.  
  12. VOID WINAPI CompletionWrite(DWORD, DWORD, LPOVERLAPPED);
  13. VOID WINAPI CompletionRead(DWORD, DWORD, LPOVERLAPPED);
  14.  
  15. HANDLE exFile, newFile;
  16. DWORD cluster = 4096;
  17. int counter = 0;
  18.  
  19. VOID WINAPI CompletionWrite(DWORD err, DWORD bytes, LPOVERLAPPED lpOverlap) {
  20.     ++counter;
  21.     LPBUF lpBuf;
  22.     lpBuf = (LPBUF)lpOverlap;
  23.     if (err == ERROR_SUCCESS) {
  24.         LARGE_INTEGER temp;
  25.         temp.LowPart = lpBuf -> overlap.Offset;
  26.         temp.HighPart = lpBuf -> overlap.OffsetHigh;
  27.         temp.QuadPart += cluster;
  28.         lpBuf -> overlap.Offset = temp.LowPart;
  29.         lpBuf -> overlap.OffsetHigh = temp.HighPart;
  30.         ReadFileEx(exFile, lpBuf -> buf, cluster, (LPOVERLAPPED)lpBuf, (LPOVERLAPPED_COMPLETION_ROUTINE)CompletionRead);
  31.     }
  32. }
  33.  
  34. VOID WINAPI CompletionRead(DWORD err, DWORD bytes, LPOVERLAPPED lpOverlap) {
  35.     ++counter;
  36.     LPBUF lpBuf;
  37.     lpBuf = (LPBUF)lpOverlap;
  38.     if (err == ERROR_SUCCESS)
  39.         WriteFileEx(newFile, lpBuf -> buf, cluster, (LPOVERLAPPED)lpBuf, (LPOVERLAPPED_COMPLETION_ROUTINE)CompletionWrite);
  40. }
  41.  
  42. HANDLE fileCreate() {
  43.     char filePath[MAX_LEN];
  44.     HANDLE fileHandle = NULL;
  45.     if (fgets(filePath, MAX_LEN, stdin)){
  46.         fflush(stdin);
  47.         filePath[strlen(filePath) - 1] = '\0';
  48.         SECURITY_ATTRIBUTES secAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  49.         fileHandle = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, &secAttr,
  50.                                         OPEN_ALWAYS, FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING | FILE_ATTRIBUTE_NORMAL, NULL);
  51.         if (fileHandle == INVALID_HANDLE_VALUE) {
  52.             DWORD err = GetLastError();
  53.             if (err == ERROR_PATH_NOT_FOUND)
  54.                 printf("Ошибка: путь к файлу не существует\n");
  55.             else
  56.                 printf("Ошибка: код ошибки %ld\n", err);
  57.         }
  58.         else if (GetLastError() == ERROR_ALREADY_EXISTS)
  59.             printf("Файл открыт\n");
  60.         else
  61.             printf("Файл создан\n");
  62.  
  63.     }
  64.     else {
  65.         fflush(stdin);
  66.         printf("Ошибка при считывании ввода");
  67.     }
  68.     return fileHandle;
  69. }
  70.  
  71. int main()
  72. {
  73.     printf("Введите путь к копируемому файлу\nНапример: \"C:\\Tools\\NewFile\" \".\\NewFile\" (в текущем каталоге)\n");
  74.     exFile = fileCreate();
  75.     printf("Введите путь к новому файлу\nНапример: \"C:\\Tools\\NewFile\" \".\\NewFile\" (в текущем каталоге)\n");
  76.     newFile = fileCreate();
  77.    
  78.     printf("Введите кратность размера блока (1 кластер, 2 кластера и т.п.): ");
  79.     DWORD num;
  80.     scanf("%ld", &num);
  81.     fflush(stdin);
  82.     cluster = cluster * num;
  83.    
  84.     printf("Введите число операций ввода-вывода: ");
  85.     scanf("%ld", &num);
  86.     fflush(stdin);
  87.    
  88.     LPBUF lpBuf = (LPBUF)malloc(sizeof *lpBuf);
  89.     PLARGE_INTEGER fileSize = (PLARGE_INTEGER)malloc(sizeof *fileSize);
  90.     if (!GetFileSizeEx(exFile, fileSize)) {
  91.         DWORD err = GetLastError();
  92.         printf ("Ошибка: код ошибки %ld\n", err);
  93.     }
  94.     else {
  95.         int opnumber = 2*(fileSize->QuadPart/cluster + (fileSize->QuadPart % cluster != 0));
  96.         LARGE_INTEGER temp;
  97.         temp.QuadPart = 0;
  98.         LARGE_INTEGER t1, t2, dt, freq;
  99.         QueryPerformanceFrequency(&freq);
  100.         QueryPerformanceCounter(&t1);
  101.         for (int i = 0; i < num; ++i, temp.QuadPart += cluster) {
  102.             lpBuf -> overlap.Offset = temp.LowPart;
  103.             lpBuf -> overlap.OffsetHigh = temp.HighPart;
  104.             lpBuf -> buf = (char *)malloc(cluster * sizeof * (lpBuf -> buf));
  105.             ReadFileEx(exFile, lpBuf -> buf, cluster, (LPOVERLAPPED)lpBuf, (LPOVERLAPPED_COMPLETION_ROUTINE)CompletionRead);
  106.         }
  107.         while (counter < opnumber)
  108.             SleepEx(INFINITE, TRUE);
  109.         QueryPerformanceCounter(&t2);
  110.         dt.QuadPart = t2.QuadPart - t1.QuadPart;
  111.         dt.QuadPart *= 1000000;
  112.         dt.QuadPart /= freq.QuadPart;
  113.         printf("Время работы: %I64d мкс", dt.QuadPart);
  114.         SetFilePointerEx(newFile, *fileSize, NULL, FILE_BEGIN);
  115.         SetEndOfFile(newFile);
  116.     }
  117.     CloseHandle(exFile);
  118.     CloseHandle(newFile);
  119.     return 0;
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top