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(ino_t directory, dev_t device, ino_t node) 97 { 98 // ignore 99 } 100 101 102 /* virtual */ void 103 NodeMonitorHandler::EntryMoved(const char *name, ino_t from_directory, 104 ino_t to_directory, dev_t device, ino_t node) 105 { 106 // ignore 107 } 108 109 110 /* virtual */ void 111 NodeMonitorHandler::StatChanged(ino_t node, dev_t device) 112 { 113 // ignore 114 } 115 116 117 /* virtual */ void 118 NodeMonitorHandler::AttrChanged(ino_t node, dev_t device) 119 { 120 // ignore 121 } 122 123 124 /* virtual */ void 125 NodeMonitorHandler::DeviceMounted(dev_t new_device, dev_t device, 126 ino_t directory) 127 { 128 // ignore 129 } 130 131 132 /* virtual */ void 133 NodeMonitorHandler::DeviceUnmounted(dev_t new_device) 134 { 135 // ignore 136 } 137 138 139 /* 140 * private functions to rip apart the corresponding BMessage 141 */ 142 143 status_t 144 NodeMonitorHandler::HandleEntryCreated(BMessage * msg) 145 { 146 const char *name; 147 ino_t directory; 148 dev_t device; 149 ino_t node; 150 if ((msg->FindString("name", &name) != B_OK) || 151 (msg->FindInt64("directory", &directory) != B_OK) || 152 (msg->FindInt32("device", &device) != B_OK) || 153 (msg->FindInt64("node", &node) != B_OK)) { 154 return B_MESSAGE_NOT_UNDERSTOOD; 155 } 156 EntryCreated(name, directory, device, node); 157 return B_OK; 158 } 159 160 161 status_t 162 NodeMonitorHandler::HandleEntryRemoved(BMessage * msg) 163 { 164 ino_t directory; 165 dev_t device; 166 ino_t node; 167 if ((msg->FindInt64("directory", &directory) != B_OK) || 168 (msg->FindInt32("device", &device) != B_OK) || 169 (msg->FindInt64("node", &node) != B_OK)) { 170 return B_MESSAGE_NOT_UNDERSTOOD; 171 } 172 EntryRemoved(directory, device, node); 173 return B_OK; 174 } 175 176 177 status_t 178 NodeMonitorHandler::HandleEntryMoved(BMessage * msg) 179 { 180 const char *name; 181 ino_t from_directory; 182 ino_t to_directory; 183 dev_t device; 184 ino_t node; 185 if ((msg->FindString("name", &name) != B_OK) || 186 (msg->FindInt64("from directory", &from_directory) != B_OK) || 187 (msg->FindInt64("to directory", &to_directory) != B_OK) || 188 (msg->FindInt32("device", &device) != B_OK) || 189 (msg->FindInt64("node", &node) != B_OK)) { 190 return B_MESSAGE_NOT_UNDERSTOOD; 191 } 192 EntryMoved(name, from_directory, to_directory, device, node); 193 return B_OK; 194 } 195 196 197 status_t 198 NodeMonitorHandler::HandleStatChanged(BMessage * msg) 199 { 200 ino_t node; 201 dev_t device; 202 if ((msg->FindInt64("node", &node) != B_OK) || 203 (msg->FindInt32("device", &device) != B_OK)) { 204 return B_MESSAGE_NOT_UNDERSTOOD; 205 } 206 StatChanged(node, device); 207 return B_OK; 208 } 209 210 211 status_t 212 NodeMonitorHandler::HandleAttrChanged(BMessage * msg) 213 { 214 ino_t node; 215 dev_t device; 216 if ((msg->FindInt64("node", &node) != B_OK) || 217 (msg->FindInt32("device", &device) != B_OK)) { 218 return B_MESSAGE_NOT_UNDERSTOOD; 219 } 220 AttrChanged(node, device); 221 return B_OK; 222 } 223 224 225 status_t 226 NodeMonitorHandler::HandleDeviceMounted(BMessage * msg) 227 { 228 dev_t new_device; 229 dev_t device; 230 ino_t directory; 231 if ((msg->FindInt32("new device", &new_device) != B_OK) || 232 (msg->FindInt32("device", &device) != B_OK) || 233 (msg->FindInt64("directory", &directory) != B_OK)) { 234 return B_MESSAGE_NOT_UNDERSTOOD; 235 } 236 DeviceMounted(new_device, device, directory); 237 return B_OK; 238 } 239 240 241 status_t 242 NodeMonitorHandler::HandleDeviceUnmounted(BMessage * msg) 243 { 244 dev_t new_device; 245 if (msg->FindInt32("new device", &new_device) != B_OK) { 246 return B_MESSAGE_NOT_UNDERSTOOD; 247 } 248 DeviceUnmounted(new_device); 249 return B_OK; 250 } 251