TVA van Hesteren
TVA van Hesteren

Reputation: 1251

C++ Monitoring filechange with ReadDirectoryChangesW not triggering all Actions?

I have an application which has to monitor any changes in a particular directory. I use the following code, however when I rename a file for instance, the Action FILE_ACTION_RENAMED_NEW_NAME is not triggered. Actually, the UNDISCOVERED ACTION get's triggered which results in unexpected behavior? What am I doing wrong here?

char Dir[] = "DIRPATH";
HANDLE hDir = CreateFile(
    Dir,
    FILE_LIST_DIRECTORY,
    FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_BACKUP_SEMANTICS,
    NULL);
int nCounter = 0;
FILE_NOTIFY_INFORMATION strFileNotifyInfo[1024];
FILE_NOTIFY_INFORMATION *fni = NULL;

while (TRUE)
{
    //strFileNotifyInfo = NULL;
    DWORD dwBytesReturned = 0;
    if (ReadDirectoryChangesW(hDir, (LPVOID)&strFileNotifyInfo, sizeof(strFileNotifyInfo), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, &dwBytesReturned, NULL, NULL) == 0)
    {
        Exit(GetLastErrorAsString());
    }
    else
    {
        char fileName[MAX_PATH] = ""; 
        DWORD offset = 0;
        do {
            fni = (FILE_NOTIFY_INFORMATION*)(&strFileNotifyInfo[offset]);
            int ret = ::WideCharToMultiByte(CP_ACP, 0, fni->FileName, fni->FileNameLength / sizeof(WCHAR), fileName, sizeof(fileName), NULL, NULL);
            string Test = Dir;
            Test += "\\";
            Test += fileName;
            switch (fni->Action) {
                case FILE_ACTION_ADDED:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory added: " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File added: " << Dir << "\\" << fileName << endl;
                        }
                    break;
                case FILE_ACTION_MODIFIED:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory modified: " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File modified: " << Dir << "\\" << fileName << endl;
                        }
                    break;
                case FILE_ACTION_REMOVED:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory removed: " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File removed: " << Dir << "\\" << fileName << endl;
                        }
                    break;
                case FILE_ACTION_RENAMED_NEW_NAME:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory renamend (NEW): " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File renamed (NEW): " << Dir << "\\" << fileName << endl;
                        }
                    break;
                case FILE_ACTION_RENAMED_OLD_NAME:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory renamed (OLD): " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File renamed (OLD): " << Dir << "\\" << fileName << endl;
                        }
                    break;
                default:
                    if (boost::filesystem::is_directory(Test)) {
                        cout << "Directory UNDISCOVERED ACTION: " << Dir << "\\" << fileName << endl;
                        }
                    else {
                        cout << "File UNDISCOVERED ACTION: " << Dir << "\\" << fileName << endl;
                        }
                    break;
                }
            ::memset(fileName, '\0', sizeof(fileName));
            offset += fni->NextEntryOffset;
            }
        while (fni->NextEntryOffset != 0);
        cout << "Loop: " << nCounter++ << endl;
    }
}

Some sample output on renaming a file from file.txt to file2.txt in the map named: MAP:

File renamed (OLD): DIRPATH\MAP\file.txt
Directory UNDISCOVERED ACTION: DIRPATH
Loop: 0
Directory modified: DIRPATH\MAP
Loop: 1

Upvotes: 1

Views: 2735

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52561

FILE_NOTIFY_INFORMATION is not a fixed-size structure. It represents a fixed-size header followed by the file name - a string of variable size.

&strFileNotifyInfo[1] points to some offset in the middle of the file name that follows strFileNotifyInfo[0]. Its values are simply mis-interpreted chunks of memory filled with file name characters; essentially random garbage.

Instead, you are supposed to use FILE_NOTIFY_INFORMATION::NextEntryOffset to locate the following instance of FILE_NOTIFY_INFORMATION in the buffer.

Your code would look something like this:

BYTE buffer[4096];
ReadDirectoryChangesW(hDir, buffer, sizeof(buffer), ...);
BYTE* p = buffer;
for (;;) {
  FILE_NOTIFY_INFORMATION* info =
      reinterpret_cast<FILE_NOTIFY_INFORMATION*>(p);

  // Work with `info` as necessary

  if (!info->NextEntryOffset) break;  // this was last entry
  p += info->NextEntryOffset;
}

Upvotes: 2

Related Questions