|
| 1 | + |
| 2 | +#include <errno.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "dir.h" |
| 7 | + |
| 8 | +int dir_add(struct node *dirnode, struct direntry *entry, int replace, int *added) { |
| 9 | + struct direntry **dir = (struct direntry **) &dirnode->data; |
| 10 | + struct direntry *existing_entry; |
| 11 | + |
| 12 | + if(dir_find(dirnode, entry->name, strlen(entry->name), &existing_entry)) { |
| 13 | + if(replace) { |
| 14 | + *added = 0; |
| 15 | + existing_entry->node = entry->node; |
| 16 | + return 1; |
| 17 | + } else { |
| 18 | + errno = EEXIST; |
| 19 | + return 0; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + *added = 1; |
| 24 | + |
| 25 | + if(*dir == NULL) { |
| 26 | + *dir = entry; |
| 27 | + entry->next = NULL; |
| 28 | + } else { |
| 29 | + entry->next = *dir; |
| 30 | + *dir = entry; |
| 31 | + } |
| 32 | + |
| 33 | + // The entry is now linked in the directory |
| 34 | + entry->node->vstat.st_nlink++; |
| 35 | + |
| 36 | + // If the entry is a directory, .. is an implicit hardlink to the parent |
| 37 | + // directory. |
| 38 | + if(S_ISDIR(entry->node->vstat.st_mode)) { |
| 39 | + dirnode->vstat.st_nlink++; |
| 40 | + } |
| 41 | + |
| 42 | + return 1; |
| 43 | +} |
| 44 | + |
| 45 | +int dir_add_alloc(struct node *dirnode, const char *name, struct node *node, int replace) { |
| 46 | + struct direntry *entry = malloc(sizeof(struct direntry)); |
| 47 | + int added; |
| 48 | + |
| 49 | + if(!entry) { |
| 50 | + errno = ENOMEM; |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + strcpy(entry->name, name); |
| 55 | + entry->node = node; |
| 56 | + |
| 57 | + if(!dir_add(dirnode, entry, replace, &added)) { |
| 58 | + free(entry); |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + if(!added) free(entry); |
| 63 | + |
| 64 | + return 1; |
| 65 | +} |
| 66 | + |
| 67 | +int dir_remove(struct node *dirnode, const char *name) { |
| 68 | + struct direntry **dir = (struct direntry **) &dirnode->data; |
| 69 | + |
| 70 | + struct direntry *ent = *dir; |
| 71 | + struct direntry **ptr = dir; |
| 72 | + |
| 73 | + while(ent != NULL) { |
| 74 | + if(strcmp(ent->name, name) == 0) { |
| 75 | + *ptr = ent->next; |
| 76 | + |
| 77 | + // See dir_add for details |
| 78 | + if(S_ISDIR(ent->node->vstat.st_mode)) { |
| 79 | + dirnode->vstat.st_nlink--; |
| 80 | + } |
| 81 | + |
| 82 | + free(ent); |
| 83 | + |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + ptr = &ent->next; |
| 88 | + ent = ent->next; |
| 89 | + } |
| 90 | + |
| 91 | + errno = ENOENT; |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +int dir_find(struct node *dirnode, const char *name, int namelen, struct direntry **entry) { |
| 97 | + struct direntry *ent = (struct direntry *) dirnode->data; |
| 98 | + |
| 99 | + while(ent != NULL) { |
| 100 | + if(strlen(ent->name) == namelen) { |
| 101 | + if(strncmp(ent->name, name, namelen) == 0) { |
| 102 | + if(entry != NULL) *entry = ent; |
| 103 | + return 1; |
| 104 | + } |
| 105 | + } |
| 106 | + ent = ent->next; |
| 107 | + } |
| 108 | + |
| 109 | + errno = ENOENT; |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
0 commit comments