1 /* 2 * Copyright 2007, 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 <Directory.h> 11 #include <Entry.h> 12 #include <File.h> 13 #include <Looper.h> 14 #include <Path.h> 15 #include <PathMonitor.h> 16 17 #include <stdio.h> 18 #include <string.h> 19 20 using BPrivate::BPathMonitor; 21 22 23 class Looper : public BLooper { 24 public: 25 Looper(const char* path); 26 virtual ~Looper(); 27 28 virtual void MessageReceived(BMessage* message); 29 30 private: 31 BPath fPath; 32 }; 33 34 35 Looper::Looper(const char* path) 36 : BLooper("path monitor test"), 37 fPath(path) 38 { 39 status_t status = BPathMonitor::StartWatching(path, B_WATCH_ALL, this); 40 if (status != B_OK) { 41 fprintf(stderr, "Could not watch path \"%s\": %s.\n", path, strerror(status)); 42 PostMessage(B_QUIT_REQUESTED); 43 } 44 } 45 46 47 Looper::~Looper() 48 { 49 status_t status = BPathMonitor::StopWatching(fPath.Path(), this); 50 if (status != B_OK) 51 fprintf(stderr, "Could not stop watching: %s\n", strerror(status)); 52 } 53 54 55 void 56 Looper::MessageReceived(BMessage* message) 57 { 58 if (message->what == B_PATH_MONITOR) 59 message->PrintToStream(); 60 } 61 62 63 // #pragma mark - 64 65 66 void 67 create_file(const char* path) 68 { 69 printf("******* create file %s *******\n", path); 70 BFile file; 71 status_t status = file.SetTo(path, B_CREATE_FILE | B_READ_WRITE); 72 if (status != B_OK) 73 fprintf(stderr, "could not create watch test.\n"); 74 file.Write("test", 4); 75 } 76 77 78 void 79 remove_file(const char* path) 80 { 81 printf("******* remove file %s *******\n", path); 82 remove(path); 83 } 84 85 86 void 87 create_directory(const char* path) 88 { 89 printf("******* create directory %s *******\n", path); 90 create_directory(path, 0755); 91 } 92 93 94 void 95 remove_directory(const char* path) 96 { 97 printf("******* remove directory %s *******\n", path); 98 rmdir(path); 99 } 100 101 102 // #pragma mark - 103 104 105 void 106 test_a() 107 { 108 puts("******************* test A ********************"); 109 110 create_directory("/tmp/a"); 111 create_directory("/tmp/ab"); 112 create_directory("/tmp/a/b"); 113 create_directory("/tmp/a/bc"); 114 create_directory("/tmp/a/b/c"); 115 create_directory("/tmp/a/b/cd"); 116 117 create_file("/tmp/a/b/c/d"); 118 snooze(100000); 119 remove_file("/tmp/a/b/c/d"); 120 121 remove_directory("/tmp/a/b/cd"); 122 remove_directory("/tmp/a/bc"); 123 remove_directory("/tmp/ab"); 124 125 snooze(100000); 126 } 127 128 129 void 130 test_b() 131 { 132 puts("******************* test B ********************"); 133 remove_directory("/tmp/a/b/c"); 134 snooze(100000); 135 create_file("/tmp/a/b/c"); 136 snooze(100000); 137 remove_file("/tmp/a/b/c"); 138 } 139 140 141 void 142 test_c() 143 { 144 puts("******************* test C ********************"); 145 create_directory("/tmp/a/b/c"); 146 create_directory("/tmp/a/b/c/d"); 147 snooze(100000); 148 remove_directory("/tmp/a/b/c/d"); 149 remove_directory("/tmp/a/b/c"); 150 } 151 152 153 void 154 test_d() 155 { 156 puts("******************* test D ********************"); 157 remove_directory("/tmp/a/b"); 158 remove_directory("/tmp/a"); 159 } 160 161 162 int 163 main() 164 { 165 // start looper 166 Looper& looper = *new Looper("/tmp/a/b/c"); 167 looper.Run(); 168 169 // run tests 170 test_a(); 171 test_b(); 172 test_c(); 173 test_d(); 174 175 #if 0 176 char b[2100]; 177 gets(b); 178 #endif 179 180 snooze(500000LL); 181 182 // quit looper 183 looper.PostMessage(B_QUIT_REQUESTED); 184 status_t status; 185 wait_for_thread(looper.Thread(), &status); 186 187 return 0; 188 } 189