NCDUdevCache.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @file NCDUdevCache.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/offset.h>
  32. #include <misc/string_begins_with.h>
  33. #include <misc/concat_strings.h>
  34. #include <misc/compare.h>
  35. #include <base/BLog.h>
  36. #include <udevmonitor/NCDUdevCache.h>
  37. #include <generated/blog_channel_NCDUdevCache.h>
  38. static int string_comparator (void *unused, const char **str1, const char **str2)
  39. {
  40. int c = strcmp(*str1, *str2);
  41. return B_COMPARE(c, 0);
  42. }
  43. static void free_device (NCDUdevCache *o, struct NCDUdevCache_device *device)
  44. {
  45. if (device->is_cleaned) {
  46. // remove from cleaned devices list
  47. LinkedList1_Remove(&o->cleaned_devices_list, &device->cleaned_devices_list_node);
  48. } else {
  49. // remove from devices tree
  50. BAVL_Remove(&o->devices_tree, &device->devices_tree_node);
  51. }
  52. // free map
  53. BStringMap_Free(&device->map);
  54. // free structure
  55. free(device);
  56. }
  57. static struct NCDUdevCache_device * lookup_device (NCDUdevCache *o, const char *devpath)
  58. {
  59. BAVLNode *tree_node = BAVL_LookupExact(&o->devices_tree, &devpath);
  60. if (!tree_node) {
  61. return NULL;
  62. }
  63. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  64. ASSERT(!device->is_cleaned)
  65. return device;
  66. }
  67. static void rename_devices (NCDUdevCache *o, const char *prefix, const char *new_prefix)
  68. {
  69. ASSERT(strlen(prefix) > 0)
  70. size_t prefix_len = strlen(prefix);
  71. // lookup prefix
  72. BAVLNode *tree_node = BAVL_Lookup(&o->devices_tree, &prefix);
  73. if (!tree_node) {
  74. return;
  75. }
  76. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  77. ASSERT(!device->is_cleaned)
  78. // if the result does not begin with prefix, we might gave gotten the device before all
  79. // devices beginning with prefix, so skip it
  80. if (!string_begins_with(device->devpath, prefix)) {
  81. tree_node = BAVL_GetNext(&o->devices_tree, tree_node);
  82. }
  83. while (tree_node) {
  84. // get next node (must be here because we rename this device)
  85. BAVLNode *next_tree_node = BAVL_GetNext(&o->devices_tree, tree_node);
  86. // get device
  87. device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  88. ASSERT(!device->is_cleaned)
  89. // if it doesn't begin with prefix, we're done
  90. if (!string_begins_with(device->devpath, prefix)) {
  91. break;
  92. }
  93. // build new devpath
  94. char *new_devpath = concat_strings(2, new_prefix, device->devpath + prefix_len);
  95. if (!new_devpath) {
  96. BLog(BLOG_ERROR, "concat_strings failed");
  97. goto fail_loop0;
  98. }
  99. // make sure the new name does not exist
  100. if (BAVL_LookupExact(&o->devices_tree, &new_devpath)) {
  101. BLog(BLOG_ERROR, "rename destination already exists");
  102. goto fail_loop1;
  103. }
  104. BLog(BLOG_DEBUG, "rename %s -> %s", device->devpath, new_devpath);
  105. // remove from tree
  106. BAVL_Remove(&o->devices_tree, &device->devices_tree_node);
  107. // update devpath in map
  108. if (!BStringMap_Set(&device->map, "DEVPATH", new_devpath)) {
  109. BLog(BLOG_ERROR, "BStringMap_Set failed");
  110. ASSERT_EXECUTE(BAVL_Insert(&o->devices_tree, &device->devices_tree_node, NULL))
  111. goto fail_loop1;
  112. }
  113. // update devpath pointer
  114. device->devpath = BStringMap_Get(&device->map, "DEVPATH");
  115. ASSERT(device->devpath)
  116. // insert to tree
  117. ASSERT_EXECUTE(BAVL_Insert(&o->devices_tree, &device->devices_tree_node, NULL))
  118. fail_loop1:
  119. free(new_devpath);
  120. fail_loop0:
  121. tree_node = next_tree_node;
  122. }
  123. }
  124. static int add_device (NCDUdevCache *o, BStringMap map)
  125. {
  126. ASSERT(BStringMap_Get(&map, "DEVPATH"))
  127. // alloc structure
  128. struct NCDUdevCache_device *device = malloc(sizeof(*device));
  129. if (!device) {
  130. BLog(BLOG_ERROR, "malloc failed");
  131. goto fail0;
  132. }
  133. // init map
  134. device->map = map;
  135. // set device path
  136. device->devpath = BStringMap_Get(&device->map, "DEVPATH");
  137. // insert to devices tree
  138. BAVLNode *ex_node;
  139. if (!BAVL_Insert(&o->devices_tree, &device->devices_tree_node, &ex_node)) {
  140. BLog(BLOG_DEBUG, "update %s", device->devpath);
  141. // get existing device
  142. struct NCDUdevCache_device *ex_device = UPPER_OBJECT(ex_node, struct NCDUdevCache_device, devices_tree_node);
  143. ASSERT(!ex_device->is_cleaned)
  144. // remove exiting device
  145. free_device(o, ex_device);
  146. // insert
  147. ASSERT_EXECUTE(BAVL_Insert(&o->devices_tree, &device->devices_tree_node, NULL))
  148. } else {
  149. BLog(BLOG_DEBUG, "add %s", device->devpath);
  150. }
  151. // set not cleaned
  152. device->is_cleaned = 0;
  153. // set refreshed
  154. device->is_refreshed = 1;
  155. return 1;
  156. fail0:
  157. return 0;
  158. }
  159. void NCDUdevCache_Init (NCDUdevCache *o)
  160. {
  161. // init devices tree
  162. BAVL_Init(&o->devices_tree, OFFSET_DIFF(struct NCDUdevCache_device, devpath, devices_tree_node), (BAVL_comparator)string_comparator, NULL);
  163. // init cleaned devices list
  164. LinkedList1_Init(&o->cleaned_devices_list);
  165. DebugObject_Init(&o->d_obj);
  166. }
  167. void NCDUdevCache_Free (NCDUdevCache *o)
  168. {
  169. DebugObject_Free(&o->d_obj);
  170. // free cleaned devices
  171. LinkedList1Node *list_node;
  172. while (list_node = LinkedList1_GetFirst(&o->cleaned_devices_list)) {
  173. struct NCDUdevCache_device *device = UPPER_OBJECT(list_node, struct NCDUdevCache_device, cleaned_devices_list_node);
  174. ASSERT(device->is_cleaned)
  175. free_device(o, device);
  176. }
  177. // free devices
  178. BAVLNode *tree_node;
  179. while (tree_node = BAVL_GetFirst(&o->devices_tree)) {
  180. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  181. ASSERT(!device->is_cleaned)
  182. free_device(o, device);
  183. }
  184. }
  185. const BStringMap * NCDUdevCache_Query (NCDUdevCache *o, const char *devpath)
  186. {
  187. DebugObject_Access(&o->d_obj);
  188. // lookup device
  189. struct NCDUdevCache_device *device = lookup_device(o, devpath);
  190. if (!device) {
  191. return NULL;
  192. }
  193. // return map
  194. return &device->map;
  195. }
  196. int NCDUdevCache_Event (NCDUdevCache *o, BStringMap map)
  197. {
  198. DebugObject_Access(&o->d_obj);
  199. // get device path
  200. const char *devpath = BStringMap_Get(&map, "DEVPATH");
  201. if (!devpath) {
  202. BLog(BLOG_ERROR, "missing DEVPATH");
  203. goto fail;
  204. }
  205. // get action
  206. const char *action = BStringMap_Get(&map, "ACTION");
  207. // if this is a remove event, remove device if we have it
  208. if (action && !strcmp(action, "remove")) {
  209. // remove existing device
  210. struct NCDUdevCache_device *device = lookup_device(o, devpath);
  211. if (device) {
  212. BLog(BLOG_DEBUG, "remove %s", devpath);
  213. free_device(o, device);
  214. } else {
  215. BLog(BLOG_DEBUG, "remove unknown %s", devpath);
  216. }
  217. // eat map
  218. BStringMap_Free(&map);
  219. return 1;
  220. }
  221. // if this is a move event, remove old device and contaned devices
  222. if (action && !strcmp(action, "move")) {
  223. const char *devpath_old = BStringMap_Get(&map, "DEVPATH_OLD");
  224. if (!devpath_old) {
  225. goto fail_rename0;
  226. }
  227. // remove old device
  228. struct NCDUdevCache_device *old_device = lookup_device(o, devpath_old);
  229. if (old_device) {
  230. BLog(BLOG_DEBUG, "remove moved %s", old_device->devpath);
  231. free_device(o, old_device);
  232. }
  233. // construct prefix "<devpath_old>/" and new prefix "<devpath>/"
  234. char *prefix = concat_strings(2, devpath_old, "/");
  235. if (!prefix) {
  236. BLog(BLOG_ERROR, "concat_strings failed");
  237. goto fail_rename0;;
  238. }
  239. char *new_prefix = concat_strings(2, devpath, "/");
  240. if (!new_prefix) {
  241. BLog(BLOG_ERROR, "concat_strings failed");
  242. goto fail_rename1;
  243. }
  244. // rename devices with paths starting with prefix
  245. rename_devices(o, prefix, new_prefix);
  246. free(new_prefix);
  247. fail_rename1:
  248. free(prefix);
  249. fail_rename0:;
  250. }
  251. // add device
  252. if (!add_device(o, map)) {
  253. BLog(BLOG_DEBUG, "failed to add device %s", devpath);
  254. goto fail;
  255. }
  256. return 1;
  257. fail:
  258. return 0;
  259. }
  260. void NCDUdevCache_StartClean (NCDUdevCache *o)
  261. {
  262. DebugObject_Access(&o->d_obj);
  263. // mark all devices not refreshed
  264. BAVLNode *tree_node = BAVL_GetFirst(&o->devices_tree);
  265. while (tree_node) {
  266. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  267. ASSERT(!device->is_cleaned)
  268. // set device not refreshed
  269. device->is_refreshed = 0;
  270. tree_node = BAVL_GetNext(&o->devices_tree, tree_node);
  271. }
  272. }
  273. void NCDUdevCache_FinishClean (NCDUdevCache *o)
  274. {
  275. DebugObject_Access(&o->d_obj);
  276. // move all devices not marked refreshed to the cleaned devices list
  277. BAVLNode *tree_node = BAVL_GetFirst(&o->devices_tree);
  278. while (tree_node) {
  279. BAVLNode *next_tree_node = BAVL_GetNext(&o->devices_tree, tree_node);
  280. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  281. ASSERT(!device->is_cleaned)
  282. if (!device->is_refreshed) {
  283. BLog(BLOG_DEBUG, "clean %s", device->devpath);
  284. // remove from devices tree
  285. BAVL_Remove(&o->devices_tree, &device->devices_tree_node);
  286. // insert to cleaned devices list
  287. LinkedList1_Append(&o->cleaned_devices_list, &device->cleaned_devices_list_node);
  288. // set device cleaned
  289. device->is_cleaned = 1;
  290. }
  291. tree_node = next_tree_node;
  292. }
  293. }
  294. int NCDUdevCache_GetCleanedDevice (NCDUdevCache *o, BStringMap *out_map)
  295. {
  296. DebugObject_Access(&o->d_obj);
  297. // get cleaned device
  298. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->cleaned_devices_list);
  299. if (!list_node) {
  300. return 0;
  301. }
  302. struct NCDUdevCache_device *device = UPPER_OBJECT(list_node, struct NCDUdevCache_device, cleaned_devices_list_node);
  303. ASSERT(device->is_cleaned)
  304. // remove from cleaned devices list
  305. LinkedList1_Remove(&o->cleaned_devices_list, &device->cleaned_devices_list_node);
  306. // give away map
  307. *out_map = device->map;
  308. // free structure
  309. free(device);
  310. return 1;
  311. }
  312. const char * NCDUdevCache_First (NCDUdevCache *o)
  313. {
  314. DebugObject_Access(&o->d_obj);
  315. BAVLNode *tree_node = BAVL_GetFirst(&o->devices_tree);
  316. if (!tree_node) {
  317. return NULL;
  318. }
  319. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  320. ASSERT(!device->is_cleaned)
  321. return device->devpath;
  322. }
  323. const char * NCDUdevCache_Next (NCDUdevCache *o, const char *key)
  324. {
  325. ASSERT(BAVL_LookupExact(&o->devices_tree, &key))
  326. BAVLNode *tree_node = BAVL_GetNext(&o->devices_tree, BAVL_LookupExact(&o->devices_tree, &key));
  327. if (!tree_node) {
  328. return NULL;
  329. }
  330. struct NCDUdevCache_device *device = UPPER_OBJECT(tree_node, struct NCDUdevCache_device, devices_tree_node);
  331. ASSERT(!device->is_cleaned)
  332. return device->devpath;
  333. }