xref: /haiku/src/tests/kits/storage/testapps/NodeMonitorTest.cpp (revision 106388ddbfdd00f4409c86bd3fe8d581bae532ec)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include <Entry.h>
11 #include <File.h>
12 #include <Looper.h>
13 #include <NodeMonitor.h>
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 
19 class Looper : public BLooper {
20 	public:
21 		Looper(node_ref& nodeRef);
22 		virtual ~Looper();
23 
24 		virtual void MessageReceived(BMessage* message);
25 
26 	private:
27 		node_ref	fNodeRef;
28 };
29 
30 
31 Looper::Looper(node_ref& nodeRef)
32 	:
33 	fNodeRef(nodeRef)
34 {
35 	status_t status = watch_node(&fNodeRef, B_WATCH_DIRECTORY, this);
36 	if (status != B_OK) {
37 		fprintf(stderr, "Could not watch file.\n");
38 		PostMessage(B_QUIT_REQUESTED);
39 	}
40 }
41 
42 
43 Looper::~Looper()
44 {
45 	status_t status = watch_node(&fNodeRef, B_STOP_WATCHING, this);
46 	if (status != B_OK)
47 		fprintf(stderr, "Could not stop watching: %s\n", strerror(status));
48 }
49 
50 
51 void
52 Looper::MessageReceived(BMessage* message)
53 {
54 	if (message->what == B_NODE_MONITOR)
55 		message->PrintToStream();
56 }
57 
58 
59 int
60 main()
61 {
62 	BEntry entry("/tmp", true);
63 	node_ref nodeRef;
64 	if (entry.GetNodeRef(&nodeRef) != B_OK) {
65 		fprintf(stderr, "Could not open /tmp.\n");
66 		return -1;
67 	}
68 
69 	//printf("device: %ld, node: %lld\n", nodeRef.device, nodeRef.node);
70 
71 	// start looper
72 	Looper& looper = *new Looper(nodeRef);
73 	looper.Run();
74 
75 	// run tests
76 	entry.SetTo("/tmp/_watch_test_");
77 
78 	BFile file;
79 	status_t status = file.SetTo(&entry, B_CREATE_FILE | B_READ_WRITE);
80 	if (status != B_OK)
81 		fprintf(stderr, "could not create watch test.\n");
82 	file.Write("test", 4);
83 	file.Unset();
84 
85 	if (entry.Remove() != B_OK)
86 		fprintf(stderr, "could not remove watch test.\n");
87 
88 	snooze(500000LL);
89 
90 	// quit looper
91 	looper.PostMessage(B_QUIT_REQUESTED);
92 	wait_for_thread(looper.Thread(), &status);
93 
94 	return 0;
95 }
96