-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfotify.cpp
More file actions
179 lines (156 loc) · 5.6 KB
/
infotify.cpp
File metadata and controls
179 lines (156 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// MonitorTest.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#include<io.h>
#include<time.h>
#include <sstream>
using namespace std;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
LPCTSTR pDirtory = (char*)lpParam;
BOOL bRet = FALSE;
BYTE Buffer[1024] = { 0 };
FILE_NOTIFY_INFORMATION *pBuffer = (FILE_NOTIFY_INFORMATION *)Buffer;
DWORD BytesReturned = 0;
HANDLE hFile = CreateFile(pDirtory,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if ( INVALID_HANDLE_VALUE == hFile )
{
return 1;
}
printf("monitor... \r\n");
fflush(stdout);
while ( TRUE )
{
ZeroMemory(Buffer, 1024);
//子线程一直在读取目录的改变
//调用系统ReadDirectoryChangesW API
bRet = ReadDirectoryChangesW(hFile,
&Buffer,
sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME | // 修改文件名
FILE_NOTIFY_CHANGE_ATTRIBUTES | // 修改文件属性
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_DIR_NAME, // 最后一次写入
&BytesReturned,
NULL, NULL);
if ( bRet == TRUE )
{
time_t ChangeTime;
time(&ChangeTime);
char szFileName[MAX_PATH] = { 0 };
// 宽字符转换多字节
WideCharToMultiByte(CP_ACP,
0,
pBuffer->FileName,
pBuffer->FileNameLength / 2,
szFileName,
MAX_PATH,
NULL,
NULL);
switch(pBuffer->Action)
{
// 添加
case FILE_ACTION_ADDED:
{
//printf("添加 : %s\r\n", szFileName);
cout<<ctime(&ChangeTime)<<"添加:"<<szFileName<<"\n"<<flush;
break;
}
// 删除
case FILE_ACTION_REMOVED:
{
//printf("删除 : %s\r\n", szFileName);
cout<<ctime(&ChangeTime)<<"删除:"<<szFileName<<"\n"<<flush;
break;
}
// 修改
case FILE_ACTION_MODIFIED:
{
//修改为文件夹
_finddata_t file;
string sdirfile;
sdirfile = string(pDirtory);
if(sdirfile.find_last_of("\\") != sdirfile.length()){
sdirfile = string(pDirtory)+"\\"+szFileName;
}else{
sdirfile = string(pDirtory)+szFileName;
}
long lf;
if((lf = _findfirst(sdirfile.c_str(), &file))== -1l){
cout<<"目录不存在:"<<sdirfile<<"\n"<<flush;
return 1;
}
if((file.attrib & _A_SUBDIR) == 0){
//printf("修改 : %s\r\n", szFileName);
cout<<ctime(&ChangeTime)<<"修改:"<<szFileName<<"\n"<<flush;
}
break;
}
// 重命名
case FILE_ACTION_RENAMED_OLD_NAME:
{
//printf("重命名 : %s", szFileName);
cout<<ctime(&ChangeTime)<<"重命名:"<<szFileName;
if ( pBuffer->NextEntryOffset != 0 )
{
FILE_NOTIFY_INFORMATION *tmpBuffer = (FILE_NOTIFY_INFORMATION *)((DWORD)pBuffer + pBuffer->NextEntryOffset);
switch ( tmpBuffer->Action )
{
case FILE_ACTION_RENAMED_NEW_NAME:
{
ZeroMemory(szFileName, MAX_PATH);
WideCharToMultiByte(CP_ACP,
0,
tmpBuffer->FileName,
tmpBuffer->FileNameLength / 2,
szFileName,
MAX_PATH,
NULL,
NULL);
//printf(" -> : %s \r\n", szFileName);
cout<<"->"<<szFileName<<"\n"<<flush;
break;
}
}
}
break;
}
case FILE_ACTION_RENAMED_NEW_NAME:
{
//printf("重命名(new) : %s\r\n", szFileName);
cout<<ctime(&ChangeTime)<<"重命名(new):"<<szFileName<<"\n"<<flush;
}
}
}
fflush(stdout);
}
CloseHandle(hFile);
return 0;
}
int main(int argc, char* argv[])
{
if(argc == 1){
return 0;
}
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, argv[1], 0, NULL);
if ( hThread == NULL )
{
return -1;
}
//等待线程结束
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}