-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMacFSEvents.lpr
150 lines (125 loc) · 4.11 KB
/
MacFSEvents.lpr
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
(*
* Project: FSEvents ..
* User : Coldzer0 ..
* Date : 25/05/2017 ..
*)
program MacFSEvents;
{$mode Delphi}{$H+}
{$SMARTLINK ON}
{$IFDEF DARWIN}
{$modeswitch objectivec1} // this is must be ON .... fo Cocoa ..under Darwin <<<<<
{.$modeswitch objectivec2} // this is for the for..in loop .
//{$linkframework ApplicationServices}
{$ENDIF}
uses
cthreads,cmem,ctypes,sysutils,MacOSAll,CocoaAll;
{$MACRO ON}
const
CRLF = #10#13;
function GetFileType( flag : integer) : string;
begin
Result := '';
if (flag and kFSEventStreamEventFlagItemIsFile) <> 0 then
Result := 'IsFile';
if (flag and kFSEventStreamEventFlagItemIsDir) <> 0 then
Result := 'IsDir';
if (flag and kFSEventStreamEventFlagItemIsSymlink) <> 0 then
Result := 'IsSymlink';
if (Result = '') then
Result := 'Unknown FileType Flag';
end;
function GetFileChange( flag : integer) : string;
begin
Result := '';
if (flag and kFSEventStreamEventFlagItemInodeMetaMod) <> 0 then
Result := 'Inode - MetaMod';
if (flag and kFSEventStreamEventFlagItemFinderInfoMod) <> 0 then
Result := 'Finder - InfoMod';
if (flag and kFSEventStreamEventFlagItemChangeOwner) <> 0 then
Result := 'ChangeOwner - access';
if (flag and kFSEventStreamEventFlagItemXattrMod) <> 0 then
Result := 'Xattrs - Mod';
if (Result = '') then
Result := 'Unknown FileChange Flag';
end;
function GetEventType( flag : integer ): string;
begin
Result := '';
if (flag and kFSEventStreamEventFlagItemRemoved) <> 0 then
begin
Result := 'Deleted';
exit;
end;
if (flag and kFSEventStreamEventFlagItemRenamed) <> 0 then
begin
Result := 'moved - deleted or moved or renamed X_X';
exit;
end;
if (flag and kFSEventStreamEventFlagItemCreated) <> 0 then
begin
Result := 'Created';
exit;
end;
if (flag and kFSEventStreamEventFlagItemModified) <> 0 then
begin
Result := 'Modified';
exit;
end;
if (flag and kFSEventStreamEventFlagRootChanged) <> 0 then
begin
Result := 'Root-Changed - the main monitored root dir';
exit;
end;
if (flag and kFSEventStreamEventFlagOwnEvent) <> 0 then
Result := 'OwnEvent - idk what is this :P';
Result := 'Unknown EventType Flag';
end;
procedure EventsCallback(
streamRef: ConstFSEventStreamRef;
clientCallBackInfo: UnivPtr;
numEvents: size_t;
eventPaths: UnivPtr;
{const} eventFlags: FSEventStreamEventFlagsPtr;
{const} eventIds : FSEventStreamEventIdPtr ); cdecl;
var
i : integer;
paths : PPCharArray;
Flags : PIntegerArray;
begin
writeln('====================================================================');
writeln('numEvents : ',numEvents);
paths := PPCharArray(eventPaths);
Flags := PIntegerArray(eventFlags);
for i := 0 to numEvents -1 do
begin
writeln('Change in '+ PChar(paths[i]) +' - Flag : '+ IntToHex(Flags[i],8) + CRLF
+' FileEvent : '+ GetEventType(Flags[i]) + CRLF
+' FileType : '+ GetFileType(Flags[i]) + CRLF
+' FileChange : '+ GetFileChange(Flags[i]) + CRLF
+'--------------------------------');
end;
end;
var
pool: NSAutoreleasePool;
stream : FSEventStreamRef;
callbackinfo : FSEventStreamContext;
pathsToWatch : CFArrayRef;
mypath : CFStringRef;
latency : CFAbsoluteTime;
begin
pool:=NSAutoreleasePool.alloc.init;
latency := 0.3; // more than enough .
mypath := CFSTR(PChar(NSHomeDirectory.UTF8String + '/Desktop'));
pathsToWatch := CFArrayCreate(nil,@mypath,1,nil);
stream:= FSEventStreamCreate(nil,
@EventsCallback,
@callbackinfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagFileEvents);
FSEventStreamScheduleWithRunLoop(stream,CFRunLoopGetCurrent,kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun;
pool.release;
end.