forked from agroce/testfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposixtfs.c
165 lines (154 loc) · 3.96 KB
/
posixtfs.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <libgen.h>
#include "super.h"
#include "testfs.h"
#include "inode.h"
#include "dir.h"
#include "posixtfs.h"
int put_context_at_dir(struct super_block *sb, const char *path, struct context *c) {
char *cpath = (char*)path;
char **components = malloc(strlen(path)*sizeof(char *));
//printf("CALLING PUT CONTEXT\n");
int lpos = 0;
char *dir = dirname(cpath);
components[lpos] = malloc((strlen(path)+1)*sizeof(char));
strcpy(components[lpos], basename(cpath));
//printf("PATH %d: %s\n", lpos, components[lpos]);
lpos++;
while (!(strcmp(dir,"/") == 0) && !(strcmp(dir,".") == 0)) {
components[lpos] = malloc((strlen(path)+1)*sizeof(char));
strcpy(components[lpos], basename(cpath));
//printf("PATH %d: %s\n", lpos, components[lpos]);
lpos++;
cpath = dirname(cpath);
dir = basename(cpath);
}
//printf("SETTING CURRENT DIRECTORY\n");
c->cur_dir = testfs_get_inode(sb, 0);
for (int pos = lpos-1; pos > 1; pos--) {
//printf("%d: %s\n", lpos, components[pos]);
int nr = testfs_dir_name_to_inode_nr(c->cur_dir, components[pos]);
free(components[pos]);
if (nr < 0) {
return -1;
}
if (testfs_inode_get_type(testfs_get_inode(sb, nr)) != I_DIR) {
return -1;
}
c->cur_dir = testfs_get_inode(sb, nr);
}
//printf("DONE SETTING CURRENT DIRECTORY");
char *p = malloc(strlen(components[0])+1*sizeof(char));
strcpy(p, components[0]);
free(components);
c->cmd[1] = p;
return 0;
}
int tfs_mkdir(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_mkdir(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_rmdir(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_rm(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_unlink(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_rm(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_create(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_create(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_write(struct super_block *sb, const char *path, char *data) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.cmd[2] = data;
c.nargs = 3;
r = cmd_write(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_stat(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_stat(sb, &c);
free(c.cmd[1]);
return r;
}
int tfs_checkfs(struct super_block * sb) {
struct context c;
c.nargs = 1;
return cmd_checkfs(sb, &c);
}
int tfs_ls(struct super_block * sb) {
struct context c;
c.nargs = 1;
c.cur_dir = testfs_get_inode(sb, 0);
return cmd_ls(sb, &c);
}
int tfs_lsr(struct super_block * sb) {
struct context c;
c.nargs = 1;
c.cur_dir = testfs_get_inode(sb, 0);
return cmd_lsr(sb, &c);
}
int tfs_cat(struct super_block *sb, const char *path) {
struct context c;
int r;
char *pcopy = malloc((strlen(path)+1)*sizeof(char));
strcpy(pcopy, path);
if (put_context_at_dir(sb, pcopy, &c) < 0) {
return -1;
}
c.nargs = 2;
r = cmd_cat(sb, &c);
free(c.cmd[1]);
return r;
}