/* Find a path-named file in the ramdisk. There should not be a slash at the beginning, nor at the end. Assumes we hold rd_mutex. */ static rd_file_t * ramdisk_find_path(rd_dir_t * parent, const char * fn, int dir) { rd_file_t * f = NULL; char * cur; /* If the object is in a sub-tree, traverse the tree looking for the right directory */ while((cur = strchr(fn, '/'))) { /* We've got another part to look at */ if(cur != fn) { /* Look for it in the parent dir.. if it's not a dir itself, something is wrong. */ f = ramdisk_find(parent, fn, cur - fn); if(f == NULL || f->type != STAT_TYPE_DIR) return NULL; /* Pull out the rd_dir_t pointer */ parent = (rd_dir_t *)f->data; assert(parent != NULL); } /* Skip the last piece of the pathname */ fn = cur + 1; } /* Ok, no more directories */ /* If there was a remaining file part, then look for it in the dir. */ if(fn[0] != 0) { f = ramdisk_find(parent, fn, strlen(fn)); if((!dir && f->type == STAT_TYPE_DIR) || (dir && f->type != STAT_TYPE_DIR)) return NULL; } else { /* We must have been looking for the dir itself */ if(!dir) return NULL; } return f; }
void test_ramdisk(void) { uint8_t user_input = '2'; struct record_t test; struct record_t local_copy; struct record_t test_t; srand(cph_get_millis()); test.wan_msg.tagMac = 1; test.wan_msg.tagBattery = 2; test.wan_msg.tagRssi = 3; struct record_t test_2; local_copy = test; ramdisk_init(); while(true) { test_2.wan_msg.tagMac = rand(); test_2.wan_msg.tagRssi = rand() % 65535; test_2.wan_msg.tagBattery = rand() % 65535; test_2.next = NULL; if(user_input == '2') ramdisk_write(test_2); else if(user_input =='1') ramdisk_write(test); else if(user_input == 'f') local_copy = *ramdisk_next(ramdisk_find(test.wan_msg.tagMac)); else if(user_input == 'p') print_records(); else if(user_input == 'e') ramdisk_erase(local_copy); else if(user_input == 'r') local_copy = test; else user_input = 'q'; // scanf(" %c", &user_input); if(user_input == 'q') break; } }
struct record_t *ramdisk_next(struct record_t *target) { if(target == NULL) { return valid_head; } // ramdisk_find((*target).mac); ramdisk_find((*target).wan_msg.tagMac); if(current == NULL) return NULL; return(current ->next); }
int ramdisk_erase(struct record_t to_remove) { struct record_t *temp, *temp_2; // temp = ramdisk_find(to_remove.mac); temp = ramdisk_find(to_remove.wan_msg.tagMac); if(temp == NULL) return 0x00; else if(temp == valid_head) { temp ->next = deleted_head; deleted_head = temp; valid_head = NULL; return 0xff; } else { current = valid_head; while(current ->next != temp) { current = current ->next; } current ->next = temp ->next; temp_2 = deleted_head; deleted_head = temp; deleted_head ->next = temp_2; return 0xff; } }