1 #include "NodeMonitorHandler.h" 2 3 /* 4 * static util functions for the super lazy 5 */ 6 7 8 /* static */ status_t 9 NodeMonitorHandler::make_entry_ref(dev_t device, ino_t directory, 10 const char * name, 11 entry_ref * ref) 12 { 13 ref->device = device; 14 ref->directory = directory; 15 return ref->set_name(name); 16 } 17 18 19 /* static */ void 20 NodeMonitorHandler::make_node_ref(dev_t device, ino_t node, node_ref * ref) 21 { 22 ref->device = device; 23 ref->node = node; 24 } 25 26 27 /* 28 * public functions: constructor, destructor, MessageReceived 29 */ 30 31 NodeMonitorHandler::NodeMonitorHandler(const char * name) 32 : BHandler(name) 33 { 34 // nothing to do 35 } 36 37 38 NodeMonitorHandler::~NodeMonitorHandler() 39 { 40 // nothing to do 41 } 42 43 44 /* virtual */ void 45 NodeMonitorHandler::MessageReceived(BMessage * msg) 46 { 47 status_t status = B_MESSAGE_NOT_UNDERSTOOD; 48 if (msg->what == B_NODE_MONITOR) { 49 int32 opcode; 50 if (msg->FindInt32("opcode", &opcode) == B_OK) { 51 switch (opcode) { 52 case B_ENTRY_CREATED: 53 status = HandleEntryCreated(msg); 54 break; 55 case B_ENTRY_REMOVED: 56 status = HandleEntryRemoved(msg); 57 break; 58 case B_ENTRY_MOVED: 59 status = HandleEntryMoved(msg); 60 break; 61 case B_STAT_CHANGED: 62 status = HandleStatChanged(msg); 63 break; 64 case B_ATTR_CHANGED: 65 status = HandleAttrChanged(msg); 66 break; 67 case B_DEVICE_MOUNTED: 68 status = HandleDeviceMounted(msg); 69 break; 70 case B_DEVICE_UNMOUNTED: 71 status = HandleDeviceUnmounted(msg); 72 break; 73 default: 74 break; 75 } 76 } 77 } 78 if (status == B_MESSAGE_NOT_UNDERSTOOD) { 79 inherited::MessageReceived(msg); 80 } 81 } 82 83 /* 84 * default virtual functions do nothing 85 */ 86 87 /* virtual */ void 88 NodeMonitorHandler::EntryCreated(const char *name, ino_t directory, 89 dev_t device, ino_t node) 90 { 91 // ignore 92 } 93 94 95 /* virtual */ void 96 NodeMonitorHandler::EntryRemoved(const char *name, ino_t directory, 97 dev_t device, ino_t node) 98 { 99 // ignore 100 } 101 102 103 /* virtual */ void 104 NodeMonitorHandler::EntryMoved(const char *name, const char *fromName, 105 ino_t fromDirectory, ino_t toDirectory, dev_t device,ino_t node, 106 dev_t nodeDevice) 107 { 108 // ignore 109 } 110 111 112 /* virtual */ void 113 NodeMonitorHandler::StatChanged(ino_t node, dev_t device, int32 statFields) 114 { 115 // ignore 116 } 117 118 119 /* virtual */ void 120 NodeMonitorHandler::AttrChanged(ino_t node, dev_t device) 121 { 122 // ignore 123 } 124 125 126 /* virtual */ void 127 NodeMonitorHandler::DeviceMounted(dev_t new_device, dev_t device, 128 ino_t directory) 129 { 130 // ignore 131 } 132 133 134 /* virtual */ void 135 NodeMonitorHandler::DeviceUnmounted(dev_t new_device) 136 { 137 // ignore 138 } 139 140 141 /* 142 * private functions to rip apart the corresponding BMessage 143 */ 144 145 status_t 146 NodeMonitorHandler::HandleEntryCreated(BMessage * msg) 147 { 148 const char *name; 149 ino_t directory; 150 dev_t device; 151 ino_t node; 152 if ((msg->FindString("name", &name) != B_OK) || 153 (msg->FindInt64("directory", &directory) != B_OK) || 154 (msg->FindInt32("device", &device) != B_OK) || 155 (msg->FindInt64("node", &node) != B_OK)) { 156 return B_MESSAGE_NOT_UNDERSTOOD; 157 } 158 EntryCreated(name, directory, device, node); 159 return B_OK; 160 } 161 162 163 status_t 164 NodeMonitorHandler::HandleEntryRemoved(BMessage * msg) 165 { 166 const char *name; 167 ino_t directory; 168 dev_t device; 169 ino_t node; 170 if ((msg->FindString("name", &name) != B_OK) || 171 (msg->FindInt64("directory", &directory) != B_OK) || 172 (msg->FindInt32("device", &device) != B_OK) || 173 (msg->FindInt64("node", &node) != B_OK)) { 174 return B_MESSAGE_NOT_UNDERSTOOD; 175 } 176 EntryRemoved(name, directory, device, node); 177 return B_OK; 178 } 179 180 181 status_t 182 NodeMonitorHandler::HandleEntryMoved(BMessage * msg) 183 { 184 const char *name; 185 const char *fromName; 186 ino_t fromDirectory; 187 ino_t toDirectory; 188 dev_t device; 189 ino_t node; 190 dev_t deviceNode; 191 if ((msg->FindString("name", &name) != B_OK) || 192 (msg->FindString("from name", &fromName) != B_OK) || 193 (msg->FindInt64("from directory", &fromDirectory) != B_OK) || 194 (msg->FindInt64("to directory", &toDirectory) != B_OK) || 195 (msg->FindInt32("device", &device) != B_OK) || 196 (msg->FindInt32("node device", &deviceNode) != B_OK) || 197 (msg->FindInt64("node", &node) != B_OK)) { 198 return B_MESSAGE_NOT_UNDERSTOOD; 199 } 200 EntryMoved(name, fromName, fromDirectory, toDirectory, device, node, 201 deviceNode); 202 return B_OK; 203 } 204 205 206 status_t 207 NodeMonitorHandler::HandleStatChanged(BMessage * msg) 208 { 209 ino_t node; 210 dev_t device; 211 int32 statFields; 212 if ((msg->FindInt64("node", &node) != B_OK) || 213 (msg->FindInt32("device", &device) != B_OK) || 214 (msg->FindInt32("fields", &statFields) != B_OK)) { 215 return B_MESSAGE_NOT_UNDERSTOOD; 216 } 217 StatChanged(node, device, statFields); 218 return B_OK; 219 } 220 221 222 status_t 223 NodeMonitorHandler::HandleAttrChanged(BMessage * msg) 224 { 225 ino_t node; 226 dev_t device; 227 if ((msg->FindInt64("node", &node) != B_OK) || 228 (msg->FindInt32("device", &device) != B_OK)) { 229 return B_MESSAGE_NOT_UNDERSTOOD; 230 } 231 AttrChanged(node, device); 232 return B_OK; 233 } 234 235 236 status_t 237 NodeMonitorHandler::HandleDeviceMounted(BMessage * msg) 238 { 239 dev_t new_device; 240 dev_t device; 241 ino_t directory; 242 if ((msg->FindInt32("new device", &new_device) != B_OK) || 243 (msg->FindInt32("device", &device) != B_OK) || 244 (msg->FindInt64("directory", &directory) != B_OK)) { 245 return B_MESSAGE_NOT_UNDERSTOOD; 246 } 247 DeviceMounted(new_device, device, directory); 248 return B_OK; 249 } 250 251 252 status_t 253 NodeMonitorHandler::HandleDeviceUnmounted(BMessage * msg) 254 { 255 dev_t new_device; 256 if (msg->FindInt32("new device", &new_device) != B_OK) { 257 return B_MESSAGE_NOT_UNDERSTOOD; 258 } 259 DeviceUnmounted(new_device); 260 return B_OK; 261 } 262