-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathramnode.c
101 lines (89 loc) · 1.89 KB
/
ramnode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* ramnode.c
*
* Created on: Nov 15, 2016
* Author: dinesh
*/
#include <string.h>
#include <libgen.h>
#include "ramnode.h"
void addNode(ramNode *head, ramNode *node) {
node->next = NULL;
if (head == NULL) {
head = node;
} else {
ramNode *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
}
void printLog(int fd, const char *msg) {
write(fd, msg, strlen(msg));
}
ramNode* searchNode(ramNode *head, const char *name) {
ramNode *temp = head;
while (temp != NULL && strcmp(name, temp->name) != 0)
temp = temp->next;
if (temp != NULL)
return temp;
else
return NULL;
}
void printNodes(ramNode *head) {
printf("MKD: Nodes are: ");
ramNode *temp = head;
while (temp != NULL) {
printf(" %s \n", temp->name);
temp = temp->next;
}
}
int deleteNode(ramNode *head, const char *path) {
ramNode *temp = head;
ramNode *prev = temp;
ramNode *foundTemp = NULL;
ramNode *foundPrev = NULL;
char *tempName;
while (temp != NULL) {
tempName = (char *) malloc(temp->size);
strcpy(tempName, temp->name);
char *dirName = dirname(tempName);
// Check whether it has any children, If so, throw an error
if (strcmp(dirName, path) == 0)
return -ENOTEMPTY;
else if(strcmp(temp->name, path) == 0 ) {
foundTemp = temp;
foundPrev = prev;
}
prev = temp;
temp = temp->next;
}
if(foundTemp == NULL)
return -ENOENT;
else {
foundPrev->next = foundTemp->next;
return 0;
}
}
int deleteFile(ramNode *head, const char *path) {
ramNode *temp = head;
ramNode *prev = temp;
ramNode *foundTemp = NULL;
ramNode *foundPrev = NULL;
while (temp != NULL) {
// Check whether it has any children, If so, throw an error
if(strcmp(temp->name, path) == 0 ) {
foundTemp = temp;
foundPrev = prev;
}
prev = temp;
temp = temp->next;
}
if(foundTemp == NULL)
return -ENOENT;
else {
foundPrev->next = foundTemp->next;
return 0;
}
}