Chinaunix首页 | 论坛 | 博客
  • 博客访问: 567634
  • 博文数量: 158
  • 博客积分: 4380
  • 博客等级: 上校
  • 技术积分: 2356
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-21 10:45
文章分类

全部博文(158)

文章存档

2012年(158)

我的朋友

分类: C/C++

2012-11-14 16:11:37

因为文章已经在上发表过,故尔这里仅给出链接
http://www.vckbase.com/document/viewdoc.asp?id=814

// 2007-12-14 整理(使用VC2005)

@FileSystemWatcher.hpp

#ifndef __FILESYSTEMWATCHER_HPP__
#define __FILESYSTEMWATCHER_HPP__

#if(_WIN32_WINNT < 0x0400)
#define _WIN32_WINNT 0x0400
#endif
#include

class FileSystemWatcher
{
public:
    enum Filter
    {
        FILTER_FILE_NAME        = 0x00000001, // add/remove/rename
        FILTER_DIR_NAME         = 0x00000002, // add/remove/rename
        FILTER_ATTR_NAME        = 0x00000004,
        FILTER_SIZE_NAME        = 0x00000008,
        FILTER_LAST_WRITE_NAME  = 0x00000010, // timestamp
        FILTER_LAST_ACCESS_NAME = 0x00000020, // timestamp
        FILTER_CREATION_NAME    = 0x00000040, // timestamp
        FILTER_SECURITY_NAME    = 0x00000100
    };
    enum ACTION
    {
        ACTION_ERRSTOP          = -1,
        ACTION_ADDED            = 0x00000001,
        ACTION_REMOVED          = 0x00000002,
        ACTION_MODIFIED         = 0x00000003,
        ACTION_RENAMED_OLD      = 0x00000004,
        ACTION_RENAMED_NEW      = 0x00000005
    };

    typedef void (__stdcall *LPDEALFUNCTION)( ACTION act, LPCWSTR filename, LPVOID lParam );

    FileSystemWatcher();
    ~FileSystemWatcher();

    // LPCTSTR dir: ended-with "\\"
    bool Run( LPCTSTR dir, bool bWatchSubtree, DWORD dwNotifyFilter, LPDEALFUNCTION dealfun, LPVOID lParam );
    void Close( DWORD dwMilliseconds=INFINITE );

private: // no-impl
    FileSystemWatcher( const FileSystemWatcher& );
    FileSystemWatcher operator=( const FileSystemWatcher );

private:
    HANDLE m_hDir;
    DWORD m_dwNotifyFilter;
    bool m_bWatchSubtree;
    HANDLE m_hThread;
    volatile bool m_bRequestStop;
    LPDEALFUNCTION m_DealFun;
    LPVOID m_DealFunParam;
    static DWORD WINAPI Routine( LPVOID lParam );
};

#endif // __FILESYSTEMWATCHER_HPP__


@FileSystemWatcher.cpp

#include
#include "FileSystemWatcher.hpp"

FileSystemWatcher::FileSystemWatcher() : m_hDir(INVALID_HANDLE_VALUE), m_hThread(NULL)
{
    assert( FILTER_FILE_NAME        == FILE_NOTIFY_CHANGE_FILE_NAME   );
    assert( FILTER_DIR_NAME         == FILE_NOTIFY_CHANGE_DIR_NAME    );
    assert( FILTER_ATTR_NAME        == FILE_NOTIFY_CHANGE_ATTRIBUTES  );
    assert( FILTER_SIZE_NAME        == FILE_NOTIFY_CHANGE_SIZE        );
    assert( FILTER_LAST_WRITE_NAME  == FILE_NOTIFY_CHANGE_LAST_WRITE  );
    assert( FILTER_LAST_ACCESS_NAME == FILE_NOTIFY_CHANGE_LAST_ACCESS );
    assert( FILTER_CREATION_NAME    == FILE_NOTIFY_CHANGE_CREATION    );
    assert( FILTER_SECURITY_NAME    == FILE_NOTIFY_CHANGE_SECURITY    );

    assert( ACTION_ADDED            == FILE_ACTION_ADDED              );
    assert( ACTION_REMOVED          == FILE_ACTION_REMOVED            );
    assert( ACTION_MODIFIED         == FILE_ACTION_MODIFIED           );
    assert( ACTION_RENAMED_OLD      == FILE_ACTION_RENAMED_OLD_NAME   );
    assert( ACTION_RENAMED_NEW      == FILE_ACTION_RENAMED_NEW_NAME   );
}

FileSystemWatcher::~FileSystemWatcher()
{
    Close();
}

bool FileSystemWatcher::Run( LPCTSTR dir, bool bWatchSubtree, DWORD dwNotifyFilter, LPDEALFUNCTION dealfun, LPVOID lParam )
{
    Close();

    m_hDir = CreateFile(
        dir,
        GENERIC_READ,
        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS,
        NULL
    );
    if( INVALID_HANDLE_VALUE == m_hDir ) return false;

    m_bWatchSubtree = bWatchSubtree;
    m_dwNotifyFilter = dwNotifyFilter;
    m_DealFun = dealfun;
    m_DealFunParam = lParam;
    m_bRequestStop = false;

    DWORD ThreadId;
    m_hThread = CreateThread( NULL,0,Routine,this,0,&ThreadId );
    if( NULL == m_hThread )
    {
        CloseHandle( m_hDir );
        m_hDir = INVALID_HANDLE_VALUE;
    }

    return NULL!=m_hThread;
}

void FileSystemWatcher::Close( DWORD dwMilliseconds )
{
    if( NULL != m_hThread )
    {
        m_bRequestStop = true;
        if( WAIT_TIMEOUT == WaitForSingleObject(m_hThread,dwMilliseconds) )
            TerminateThread( m_hThread, 0 );
        CloseHandle( m_hThread );
        m_hThread = NULL;
    }
    if( INVALID_HANDLE_VALUE != m_hDir )
    {
        CloseHandle( m_hDir );
        m_hDir = INVALID_HANDLE_VALUE;
    }
}

DWORD WINAPI FileSystemWatcher::Routine( LPVOID lParam )
{
    FileSystemWatcher& obj = *(FileSystemWatcher*)lParam;

    BYTE buf[ 2*(sizeof(FILE_NOTIFY_INFORMATION)+2*MAX_PATH)+2 ];
    FILE_NOTIFY_INFORMATION* pNotify=(FILE_NOTIFY_INFORMATION *)buf;
    DWORD BytesReturned;
    while( !obj.m_bRequestStop )
    {
        if( ReadDirectoryChangesW( obj.m_hDir,
            pNotify,
            sizeof(buf)-2,
            obj.m_bWatchSubtree,
            obj.m_dwNotifyFilter,
            &BytesReturned,
            NULL,
            NULL ) ) // 无限等待,应当使用异步方式
        {
            for( FILE_NOTIFY_INFORMATION* p=pNotify; p; )
            {
                WCHAR c = p->FileName[p->FileNameLength/2];
                p->FileName[p->FileNameLength/2] = L'\0';

                obj.m_DealFun( (ACTION)p->Action, p->FileName, obj.m_DealFunParam );

                p->FileName[p->FileNameLength/2] = c;

                if( p->NextEntryOffset )
                    p  = (PFILE_NOTIFY_INFORMATION)( (BYTE*)p + p->NextEntryOffset );
                else
                    p = 0;
            }
        }
        else
        {
            obj.m_DealFun( (ACTION)ACTION_ERRSTOP, 0, obj.m_DealFunParam );
            break;
        }
    }

    return 0;
}


@test.cpp

#include "FileSystemWatcher.hpp"
#include
#include
#include
#include
#include

void __stdcall MyDeal( FileSystemWatcher::ACTION act, LPCWSTR filename, LPVOID lParam )
{
    static FileSystemWatcher::ACTION pre = FileSystemWatcher::ACTION_ERRSTOP;
    switch( act )
    {
    case FileSystemWatcher::ACTION_ADDED:
        wprintf_s( L"Added     - %s\n", filename );
        break;
    case FileSystemWatcher::ACTION_REMOVED:
        wprintf_s( L"Removed   - %s\n", filename );
        break;
    case FileSystemWatcher::ACTION_MODIFIED:
        wprintf_s( L"Modified  - %s\n", filename );
        break;
    case FileSystemWatcher::ACTION_RENAMED_OLD:
        wprintf_s( L"Rename(O) - %s\n", filename );
        break;
    case FileSystemWatcher::ACTION_RENAMED_NEW:
        assert( pre == FileSystemWatcher::ACTION_RENAMED_OLD );
        wprintf_s( L"Rename(N) - %s\n", filename );
        break;
    case FileSystemWatcher::ACTION_ERRSTOP:
    default:
        wprintf_s( L"---ERROR---%s\n", filename );
        break;
    }
    pre = act;
}

int main()
{
    LPCTSTR sDir= TEXT("F:\\temp\\cpp06\\test\\");
    DWORD dwNotifyFilter = FileSystemWatcher::FILTER_FILE_NAME|FileSystemWatcher::FILTER_DIR_NAME;

    FileSystemWatcher fsw;
    bool r = fsw.Run( sDir, true, dwNotifyFilter, &MyDeal, 0 );
    if( !r ) return -1;

    _tsetlocale( LC_CTYPE, TEXT("chs") );
    _tprintf_s( TEXT("Watch%s\n"), sDir );
    _tprintf_s( TEXT("Press to quit.\n") );

    while( _getch() != 'q' );

    fsw.Close();

    return 0;
}

 

阅读(1832) | 评论(11) | 转发(0) |
给主人留下些什么吧!~~

网友评论2012-11-14 16:22:54

richard
嗯,谢谢你,虽然我还不会异步方式。
msdn上说这个函数有漏洞,就是当大量操作文件时,一部分文件信息会丢失。
最好是用文件过滤驱动了。(还不会,学习中)
还是谢谢你写的这篇文章了

网友评论2012-11-14 16:22:34

周星星
我没有使用异步方式
但使用异步方式是最好的

网友评论2012-11-14 16:22:15

richard
当然您也没有指定这个参数,还有您在函数ReadDirectoryChangesW()最后说的应当异步调用,我也没发现您用的是异步调用的方法。请问如何真正实现异步调用。

网友评论2012-11-14 16:21:59

richard
不好意思说错了,是指定CreateFile()的倒数第二个参数为FILE_FLAG_OVERLAPPED属性

网友评论2012-11-14 16:21:42

richard
您好,我想请问您用的是异步检测的么?
如果是,在msdn上解释说:为了指定异步完成,需要指定ReadDirectoryChangesW()中倒数第二个参数为FILE_FLAG_OVERLAPPED属性,然后调用ReadDirectoryChangesW时,指定一个OVERLAPPED结构。

可是您并没有指定这个参数。

请问如何用异步调用这个ReadDirectoryChangesW函数;