#include "inodelist.h" #include "stdio.h" #include "stdlib.h" #include "string.h" //Source : https://www.tutorialspoint.com/data_structures_algorithms/doubly_linked_list_program_in_c.htm static inodelist_t *head = NULL; static inodelist_t *last = NULL; static inodelist_t *current = NULL; inodelist_t *inodelist_get_head() { return head; } inodelist_t *inodelist_get_tail() { return last; } inodelist_t *inodelist_get_current() { return current; } bool inodelist_is_empty() { return (head == NULL); } uint32_t inodelist_get_length() { uint32_t length = 0; inodelist_t *current; for(current = head; current != NULL; current = current->next){ length++; } return length; } void inodelist_debug_display_forward() { inodelist_t *ptr = head; printf("[\n"); while(ptr != NULL) { inode_debug_print(&ptr->data); ptr = ptr->next; } printf("]\n"); } void inodelist_debug_display_backward() { inodelist_t *ptr = last; printf("[\n"); while(ptr != NULL) { inode_debug_print(&ptr->data); ptr = ptr ->prev; } printf("]\n"); } void inodelist_insert_head(inode_t inode) { inodelist_t *link = (inodelist_t*) malloc(sizeof(inodelist_t)); inode_copy(&link->data, &inode); if(inodelist_is_empty()) { last = link; } else { head->prev = link; } link->next = head; head = link; } void inodelist_insert_tail(inode_t inode) { inodelist_t *link = (inodelist_t*) malloc(sizeof(inodelist_t)); inode_copy(&link->data, &inode); if(inodelist_is_empty()) { last = link; } else { last->next = link; link->prev = last; } last = link; } inodelist_t* inodelist_remove_head() { inodelist_t *tempLink = head; if(head->next == NULL){ last = NULL; } else { head->next->prev = NULL; } head = head->next; return tempLink; } inodelist_t* inodelist_remove_tail() { inodelist_t *tempLink = head; if(head->next == NULL) { head = NULL; } else { last->prev->next = NULL; } last = last->prev; return tempLink; } inodelist_t* inodelist_remove_at(int index) { inodelist_t* current = head; uint32_t cpt = 0; if(head == NULL) { return NULL; } while(current->next != NULL) { if(current->next == NULL) { return NULL; } if( cpt == index ) { break; } cpt++; current = current->next; } if(current == head) { head = head->next; } else { current->prev->next = current->next; } if(current == last) { last = current->prev; } else { current->next->prev = current->prev; } return current; } int32_t inodelist_get_index_from_value(uint32_t inodeval) { inodelist_t* current = head; int32_t cpt = 0; while(current->next != NULL) { if( current->data.inode == inodeval ) { return cpt; } cpt++; current = current->next; } return -1; } inodelist_t *inodelist_get_inode_from_value(uint32_t inodeval) { inodelist_t* current = head; while(current->next != NULL) { if( current->data.inode == inodeval ) { return current; } current = current->next; } return NULL; } void inodelist_insert_if_not(uint32_t inodeval) { if(inodelist_is_empty()) { inode_t temp; temp.inode = inodeval; temp.pid = 0; strcpy(temp.cmdline,""); inodelist_insert_head(temp); return; } inodelist_t* current = head; while(current->next != NULL) { if( current->data.inode == inodeval ) { fprintf(stderr, "inode:%d already there\n", inodeval ); return; } current = current->next; } inode_t temp; temp.inode = inodeval; inodelist_insert_tail(temp); return; } void inodelist_insert_if_not_with_hashkey(uint32_t inodeval, const char *hashkey, const char *hashkey2, uint32_t hk_len) { if(inodelist_is_empty()) { inode_t temp; temp.inode = inodeval; bzero(temp.cmdline,MAX_CMD_LINE); bzero(temp.hashkey,HASHKEYSIZE); bzero(temp.hashkey2,HASHKEYSIZE); memcpy(temp.hashkey,hashkey,hk_len); memcpy(temp.hashkey2,hashkey2,hk_len); temp.mydumper = NULL; inodelist_insert_head(temp); return; } inodelist_t* current = head; while(current->next != NULL) { if( current->data.inode == inodeval ) { //~ fprintf(stderr, "inode:%d already there\n", inodeval ); return; } current = current->next; } inode_t temp; temp.inode = inodeval; bzero(temp.cmdline,MAX_CMD_LINE); bzero(temp.hashkey,HASHKEYSIZE); bzero(temp.hashkey2,HASHKEYSIZE); strncpy(temp.hashkey,hashkey,hk_len); strncpy(temp.hashkey2,hashkey2,hk_len); temp.mydumper = NULL; inodelist_insert_head(temp); return; } inodelist_t* inodelist_find_hashkey(const char *hashkey) { inodelist_t* current = head; while(current->next != NULL) { if( 0 == memcmp( current->data.hashkey, hashkey, strlen(hashkey) ) ) { return current; } else if ( 0 == memcmp( current->data.hashkey2, hashkey, strlen(hashkey) ) ) { return current; } current = current->next; } return NULL; } int32_t inodelist_get_index_from_pid(pid_t pid) { inodelist_t* current = head; int32_t cpt = 0; while(current->next != NULL) { if( current->data.pid == pid ) { return cpt; } cpt++; current = current->next; } return -1; }