Ticket #9834: modified_time_monitor.cpp

File modified_time_monitor.cpp, 1.4 KB (added by bonefish, 8 years ago)
Line 
1#include <errno.h>
2#include <stdio.h>
3#include <string.h>
4#include <sys/stat.h>
5
6#include <Looper.h>
7#include <Node.h>
8#include <NodeMonitor.h>
9
10
11struct Looper : BLooper {
12 Looper(const char* path)
13 :
14 BLooper(),
15 fPath(path)
16 {
17 }
18
19 virtual void MessageReceived(BMessage* message)
20 {
21 if (message->what != B_NODE_MONITOR)
22 return BLooper::MessageReceived(message);
23
24 if (message->GetInt32("opcode", 0) == B_STAT_CHANGED
25 && (message->GetInt32("fields", 0) & B_STAT_MODIFICATION_TIME)
26 != 0) {
27 struct stat st;
28 if (lstat(fPath, &st) == 0) {
29 printf("node %" B_PRIdDEV ":%" B_PRIdINO " modification time "
30 "changed: %ld.%ld\n", st.st_dev, st.st_ino,
31 (long)st.st_mtim.tv_sec, (long)st.st_mtim.tv_nsec);
32 }
33 }
34 }
35
36private:
37 const char* fPath;
38};
39
40
41int
42main(int argc, const char* const* argv)
43{
44 if (argc < 2) {
45 fprintf(stderr, "Usage: %s <path>\n", argv[0]);
46 return 1;
47 }
48
49 const char* path = argv[1];
50
51 struct stat st;
52 if (lstat(path, &st) != 0) {
53 fprintf(stderr, "Error: Failed to stat() \"%s\": %s\n", path,
54 strerror(errno));
55 return 1;
56 }
57
58 Looper* looper = new Looper(path);
59 thread_id thread = looper->Run();
60
61 node_ref nodeRef(st.st_dev, st.st_ino);
62 status_t error = watch_node(&nodeRef, B_WATCH_STAT, looper);
63 if (error != B_OK) {
64 fprintf(stderr, "Error: Failed to start watching node: %s\n",
65 strerror(error));
66 return 1;
67 }
68
69 wait_for_thread(thread, NULL);
70
71 return 0;
72}