CreateFile, ReadDirectoryChanges проблема

Я использую ReadDirectoryChangesW шпионить папку, которую я открыл CreateFileпосле добавления файла я вызываю функцию (OnFileChanged), который читает размер и открывает его для чтения, мое приложение работает нормально для файла небольшого размера, но проблема возникает, когда я пытаюсь скопировать большой файл в свою папку (7,24 МБ), и я получаю Permission denied ошибка после вызова fopen, чтобы прочитать его.

процесс наблюдения основан на этот

    void Init(const QString FullPath)
{    ...
hDir = CreateFile(
dirPath, // pointer to the directory containing the tex files
FILE_LIST_DIRECTORY|GENERIC_READ,                // access (read-write) mode
FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE,  // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS  | FILE_FLAG_OVERLAPPED , // file attributes
NULL); // file with attributes to copySecureZeroMemory (&overl, sizeof(overl));
SecureZeroMemory(buffer, sizeof(buffer));
overl.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

// watch the directory
BOOL res= ReadDirectoryChangesW(
hDir, /* handle to directory */
&buffer[curBuffer], /* read results buffer */
sizeof(buffer[curBuffer]), /* length of buffer */
FALSE, /* monitoring option */
FILE_NOTIFY_CHANGE_FILE_NAME ,
//FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
NULL, /* bytes returned */
&overl, /* overlapped buffer */
NULL); /* completion routine */

}

void StartWatchThread()
{
// if the thread already exists then stop it
if (IsThreadRunning())
SynchronousAbort();

//CrashIf(!hDir);
if(!hDir)
{ qDebug()<<" handle "<<hDir<<" last error"<<GetLastError();
exit(-1);}

else
{// reset the hEvtStopWatching event so that it can be set if
// some thread requires the watching thread to stop
ResetEvent(hEvtStopWatching);

DWORD watchingthreadID;
qDebug()<<"befrore creating thread";
hWatchingThread = CreateThread(NULL, 0, WatchingThread, this, 0, &watchingthreadID);
qDebug()<<"watchingthreadID"<<watchingthreadID;
}
}DWORD WINAPI WatchingThread(void *param)
{
//qDebug()<<"in WatchingThread";
FileWatcher *fw = (FileWatcher *)param;

HANDLE hp[2] = { fw->hEvtStopWatching, fw->overl.hEvent };
for (;;)
{

DWORD dwObj = WaitForMultipleObjects((sizeof(hp)/(sizeof(hp[0])))
, hp, FALSE, INFINITE);
if (dwObj == WAIT_OBJECT_0) // the user asked to quit the program
{
qDebug()<<"in WatchingThread the user asked to quit the program";
//exit(-1);
break;
}
if (dwObj != WAIT_OBJECT_0 + 1)
{
// BUG!
//assert(0);
qDebug()<<"dwObj "<<dwObj<<" last error "<<GetLastError();
break;
}
//qDebug()<<"WatchingThread fw->NotifyChange() ";

//if (fw->wakeup)
fw->NotifyChange();

}
return 0;
}bool NotifyChange()
{
//qDebug()<<"in NotifyChange";

GetOverlappedResult(hDir, &overl, &dwNumberbytes, FALSE);

FILE_NOTIFY_INFORMATION *pFileNotify = (FILE_NOTIFY_INFORMATION *)buffer[curBuffer];
// Switch the 2 buffers
curBuffer = (curBuffer + 1) % (sizeof(buffer)/(sizeof(buffer[0])));
SecureZeroMemory(buffer[curBuffer], sizeof(buffer[curBuffer]));
// start a new asynchronous call to ReadDirectory in the alternate buffer
ReadDirectoryChangesW(
hDir, /* handle to directory */
&buffer[curBuffer], /* read results buffer */
sizeof(buffer[curBuffer]), /* length of buffer */
FALSE, /* monitoring option */
FILE_NOTIFY_CHANGE_FILE_NAME  ,
//FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
NULL, /* bytes returned */
&overl, /* overlapped buffer */
NULL); /* completion routine */

// Note: the ReadDirectoryChangesW API fills the buffer with WCHAR strings.
for (;;) {

if (pFileNotify->Action == FILE_ACTION_ADDED)
{
qDebug()<<"in NotifyChange if ";
char szAction[42];
char szFilename[MAX_PATH] ;
memset(szFilename,'\0',sizeof( szFilename));
strcpy(szAction,"added");
wcstombs( szFilename, pFileNotify->FileName, MAX_PATH);

OnFileChanged(szFilename,szAction);
qDebug()<<"in NotifyChange after OnFileChanged ";

}

// step to the next entry if there is one
if (!pFileNotify->NextEntryOffset)
return false;
pFileNotify = (FILE_NOTIFY_INFORMATION *)((PBYTE)pFileNotify + pFileNotify->NextEntryOffset);
}
pFileNotify=NULL;
return true;
}

Я не мог понять, как обрабатывать файл и не дал мне прочитать его?
Я пытался вызвать Sleep () после вызова OnFileChanged и просыпаться, когда это закончится, но напрасно.
Любая идея, пожалуйста.

1

Решение

Я вызываю функцию (OnFileChanged), которая читает размер и открывает его для чтения

Это в значительной степени гарантированно провалится рано или поздно. Файл-измененные уведомления генерируются в то время как процесс пишет в файл. Что мешает вам открыть файл, если процесс еще не завершил запись или иным образом сохраняет файл открытым. И не разрешил совместное чтение, очень распространенное для программ, которые пишут файл. Это, безусловно, связано с размером файла: чем больше размер файла, тем больше времени требуется для записи, и тем выше вероятность того, что вы пытаетесь открыть его, пока другой процесс еще не закрыл его.

Вам нужно будет справиться с этим возможным несчастным случаем. Вы ничего не можете сделать, кроме как сохранить путь в очереди, чтобы вы могли попробовать позже.

1

Другие решения

Других решений пока нет …