neongm

kurs_os_root

Nov 29th, 2021
756
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char usage[] = ""
  2. "notify - print a name of changed object if something is is changed in a directory subtree.\n"
  3. "version 2.00, 2011-06-08\n"
  4. "(C) Dmitry Koterov (dmitry@koterov.ru)\n"
  5. "dkLab RealSync project, http://en.dklab.ru/lib/dklab_realsync/\n\n"
  6. "Usage:\n"
  7. "  notify.exe <any-path-to-watch>\n"
  8. ;
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <cstdint>
  13. #include <cassert>
  14. #include <locale>
  15.  
  16. // EXE-file type.
  17. #pragma comment(linker, "/SUBSYSTEM:console")
  18.  
  19. int main(int argc, char* argv[]) {
  20.     setlocale(LC_ALL, "ru-RU");
  21.     _wsetlocale(LC_ALL, L"ru-RU");
  22.  
  23.     if (!argv[1] || !*argv[1]) {
  24.         fprintf(stderr, "%s", usage);
  25.         ExitProcess(1);
  26.     }
  27.    
  28.     char* path = argv[1];
  29.  
  30.  
  31.     HANDLE file = CreateFile(path,
  32.         FILE_LIST_DIRECTORY,
  33.         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  34.         NULL,
  35.         OPEN_EXISTING,
  36.         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
  37.         NULL);
  38.     assert(file != INVALID_HANDLE_VALUE);
  39.     OVERLAPPED overlapped;
  40.     overlapped.hEvent = CreateEvent(NULL, FALSE, 0, NULL);
  41.  
  42.     uint8_t change_buf[1024];
  43.     BOOL success = ReadDirectoryChangesW(
  44.         file, change_buf, 1024, TRUE,
  45.         FILE_NOTIFY_CHANGE_FILE_NAME |
  46.         FILE_NOTIFY_CHANGE_DIR_NAME |
  47.         FILE_NOTIFY_CHANGE_LAST_WRITE,
  48.         NULL, &overlapped, NULL);
  49.  
  50.     while (true) {
  51.         DWORD result = WaitForSingleObject(overlapped.hEvent, 0);
  52.  
  53.         if (result == WAIT_OBJECT_0) {
  54.             DWORD bytes_transferred;
  55.             GetOverlappedResult(file, &overlapped, &bytes_transferred, FALSE);
  56.  
  57.             FILE_NOTIFY_INFORMATION* event = (FILE_NOTIFY_INFORMATION*)change_buf;
  58.  
  59.             for (;;) {
  60.                 DWORD name_len = event->FileNameLength / sizeof(wchar_t);
  61.  
  62.                 switch (event->Action) {
  63.                 case FILE_ACTION_ADDED: {
  64.                     wprintf(L"       Added: %.*s\n", name_len, event->FileName);
  65.                 } break;
  66.  
  67.                 case FILE_ACTION_REMOVED: {
  68.                     wprintf(L"     Removed: %.*s\n", name_len, event->FileName);
  69.                 } break;
  70.  
  71.                 case FILE_ACTION_MODIFIED: {
  72.                     wprintf(L"    Modified: %.*s\n", name_len, event->FileName);
  73.                 } break;
  74.  
  75.                 case FILE_ACTION_RENAMED_OLD_NAME: {
  76.                     wprintf(L"Renamed from: %.*s\n", name_len, event->FileName);
  77.                 } break;
  78.  
  79.                 case FILE_ACTION_RENAMED_NEW_NAME: {
  80.                     wprintf(L"          to: %.*s\n", name_len, event->FileName);
  81.                 } break;
  82.  
  83.                 default: {
  84.                     printf("Unknown action!\n");
  85.                 } break;
  86.                 }
  87.  
  88.                 // Are there more events to handle?
  89.                 if (event->NextEntryOffset) {
  90.                     *((uint8_t**)&event) += event->NextEntryOffset;
  91.                 }
  92.                 else {
  93.                     break;
  94.                 }
  95.             }
  96.  
  97.             // Queue the next event
  98.             BOOL success = ReadDirectoryChangesW(
  99.                 file, change_buf, 1024, TRUE,
  100.                 FILE_NOTIFY_CHANGE_FILE_NAME |
  101.                 FILE_NOTIFY_CHANGE_DIR_NAME |
  102.                 FILE_NOTIFY_CHANGE_LAST_WRITE,
  103.                 NULL, &overlapped, NULL);
  104.  
  105.         }
  106.  
  107.         // Do other loop stuff here...
  108.     }
  109. }
RAW Paste Data