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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include "inode.h"
#include <assert.h>
#include <sys/mman.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <libgen.h>
#include "defs.h"
#include "block_walker.h"
/* Auxiliares */
uint16_t get_path_length(const char *path);
char *get_next_dir(const char *path, uint16_t position);
int16_t name_check(char *path, direntry *entry_block);
int16_t inode_search(const char *disk_mem, inode *current, char *path);
/*Parser*/
int16_t name_check(char *path, direntry *entry_block)
{
int16_t result=-1;
int i = 0;
assert(path != NULL);
assert(entry_block != NULL);
for(i=0; i<16 && entry_block[i].inum != 0; i++){
if(strcmp(entry_block[i].name, path)){
result=entry_block[i].inum;
break;
}
}
return result;
}
int16_t inode_search(const char *disk_mem, inode *current, char *path)
{
bwalker *bw = NULL;
direntry *dentry = NULL;
int16_t result = -1;
uint16_t block;
assert (disk_mem != NULL);
assert (current != NULL);
assert (path != NULL);
bw=bwalker_create(disk_mem, current->size, &(current->indirect));
while(result==-1 && (block=bwalker_next(bw))!=0){
dentry=(direntry *)disk_mem+BLOCKPOOL_OFFSET+(block*BLOCK_SIZE);
result=name_check(path, dentry);
}
bwalker_destroy (bw);
return result;
}
/* Publico. */
/*Parse*/
int16_t get_inode_from_path(const char *disk_mem, char *path,
int16_t current_inum)
{
int16_t i=1, path_len=strlen(path), result=0;
char *path_mod = NULL;
inode *current=(inode *)disk_mem+INODEPOOL_OFFSET+current_inum*INODE_SIZE;
assert (disk_mem != NULL);
assert (path != NULL);
/* Chequeamos si es root al principio :) */
if (strcmp(path, "/") == 0)
return 1;
/* Vamos a ponerle 2 mas por las moscas :) */
path_mod = calloc(path_len + 2, sizeof(char));
strcpy(path_mod, path);
while(i<path_len){
/*el while empieza desde i=1, se come el primer slash, necesario
* para usar basename*/
if(path_mod[i] == '/'){
/*NULL-terminamos el string para conseguir solo ese dir/file*/
path_mod[i]='\0';
break;
}
i++;
}
result=inode_search(disk_mem, current, path_mod);
free(path_mod);
if(i < path_len){
/* *si no se llego al final, se busca el dir actual, y se continua
* la busqueda*/
/*RECURSION FTW!*/
return get_inode_from_path(disk_mem, path + i, result);
}
/*si se llego al final, devolvemos el inodo correspondiente a la
* iteracion*/
return result;
}
int inode_getattr(inode *f, ino_t inum, struct stat *stbuf)
{
assert (f != NULL);
stbuf->st_ino = inum;
stbuf->st_mode = f->mode;
stbuf->st_nlink = f->nlinks;
stbuf->st_uid = f->uid;
stbuf->st_gid = f->gid;
stbuf->st_size = f->size;
stbuf->st_blksize = BLOCK_SIZE;
stbuf->st_blocks = (f->size / BLOCK_SIZE);
stbuf->st_atime = f->atime;
stbuf->st_ctime = f->ctime;
stbuf->st_mtime = f->mtime;
return (0);
}
uint16_t inode_allocate_block(const char *disk_mem)
{
uint16_t result = 0;
uint16_t i = 0;
assert (disk_mem != NULL);
/* Solo queremos desde el segundo inodo para
* que tengamos como error el 0 */
for(i = 1; i < FIB_SIZE; i++)
if(GET_BIT(disk_mem + FIB_OFFSET, i) == 0)
{
/* cambiamos el bitmap */
SET_BIT_1((char*)disk_mem + FIB_OFFSET, i);
result = i;
break;
}
assert(GET_BIT(disk_mem + FIB_OFFSET, i) == 1);
return (result);
}
void inode_free_block(const char *disk_mem, uint16_t pos)
{
assert (disk_mem != NULL);
assert (pos <= FIB_SIZE);
SET_BIT_0((char *)disk_mem + FIB_OFFSET, pos);
}
int inode_chmod (inode *f, mode_t mode)
{
assert (f != NULL);
f->mode = mode;
f->ctime = NEW_TIME;
return (0);
}
int inode_chown(inode *f, uid_t uid, gid_t gid)
{
assert (f != NULL);
if (gid != -1)
f->gid = gid;
if (uid != -1)
f->uid = uid;
f->ctime = NEW_TIME;
return 0;
}
int inode_utime(inode *f, const struct timespec tv[2])
{
assert (f != NULL);
f->atime = (uint32_t) tv[1].tv_nsec;
f->ctime = (uint32_t) tv[1].tv_nsec;
f->mtime = (uint32_t) tv[1].tv_nsec;
return (0);
}
mode_t inode_get_mode(inode *i)
{
assert (i != NULL);
return i->mode;
}
/* Direntry */
void direntry_clean(direntry *dentry)
{
assert (dentry != NULL);
dentry->inum = 0;
dentry->name[0] = '\0';
dentry->symlink = 0;
}
|