aboutsummaryrefslogtreecommitdiff
path: root/dir.c
blob: 891072fee37741e447f711b746790dc169ee651d (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "dir.h"

#include <assert.h>
#include <unistd.h>
#include <libgen.h>
#include <string.h>
#include <stdbool.h>
#include <sys/mman.h>

#include "defs.h"
#include "block_walker.h"

uint16_t dir_create(const char *disk_mem, mode_t mode)
{
    uint16_t ipos = 0;
    dir *d = NULL;
    time_t new_time = NEW_TIME;

    assert (disk_mem != NULL);

    ipos = inode_allocate_block(disk_mem);
    if(ipos == 0)
        return 0;

    d = (dir *) disk_mem + INODEPOOL_OFFSET + (ipos * INODE_SIZE);
    d->uid = getuid();
    d->gid = getgid();
    d->mode = mode;
    d->size = 0;
    d->type = I_DIR;
    d->nlinks = 1;
    d->ctime = new_time;
    d->mtime = new_time;
    d->atime = new_time;

    return (ipos);
}

uint16_t dir_add_direntry(const char *disk_mem, dir *d, const char *name,
                          uint16_t inumber, uint16_t sym)
{
    bwalker *walker = NULL;
    uint16_t block = 0;
    direntry *dentry = NULL;
    uint16_t i = 0;
    int result = 0;

    assert (disk_mem != NULL);
    assert (d != NULL);
    assert (name != NULL);
    assert (strlen(name) < NAME_LENGTH);

    walker = bwalker_create(disk_mem, d->size, &(d->indirect));

    if(bwalker_direct_length(walker)*BLOCK_SIZE<d->size+32){
        block = bwalker_allocate_block(walker);
    }else{
        block = bwalker_last_block(walker);

        if(!block)
            goto CLEAN;
    }

    dentry = (direntry *) disk_mem + BLOCKPOOL_OFFSET + (block * BLOCK_SIZE);

    for(i = 0; i < 16; i++){
        if(strcmp(dentry[i].name, "\0") == 0){
            d->size += sizeof(direntry); /* Deberia ser 32*/
            dentry = &(dentry[i]);
            strcpy(dentry->name, name);
            dentry->inum = inumber;
            dentry->symlink = sym;
            result = 1;
            goto CLEAN;
        }
    }

CLEAN:
    bwalker_destroy(walker);

    return result;
}

uint16_t dir_remove_direntry(const char *disk_mem, dir *d, const char *name)
{
    bwalker *walker = NULL;
    uint16_t result = 0;
    uint16_t block = 0;
    direntry *rm_dentry = NULL;
    direntry *last_dentry = NULL;
    uint16_t i;

    assert(disk_mem != NULL);
    assert(d != NULL);
    assert(name != NULL);
    assert(strlen(name) < NAME_LENGTH);

    walker = bwalker_create(disk_mem, d->size, &(d->indirect));

    /* Conseguimos el ultimo direntry */
    block = bwalker_last_block(walker);
    assert(block != 0);
    last_dentry = (direntry *) disk_mem + BLOCKPOOL_OFFSET +
        (block * BLOCK_SIZE);
    assert(last_dentry[0].inum != 0);
    for(i = 0; i < 16; i++){
        if(last_dentry[i].inum == 0)
            break;
    }
    assert(i > 0);
    last_dentry = &(last_dentry[i-1]);

    while((block = bwalker_next(walker)))
    {
        rm_dentry = (direntry *) disk_mem + BLOCKPOOL_OFFSET +
            (block * BLOCK_SIZE);
        for(i = 0; i < 16; i++)
            if(strcmp(name, rm_dentry[i].name) == 0)
            {
                /* Retornamos esto para eliminarlo fuera. */
                result = rm_dentry[i].inum;
                /* Copiamos a la nueva posicion para ahorrarnos la 
                 * fragmentacion externa. */
                strcpy(rm_dentry[i].name, last_dentry->name);
                rm_dentry[i].inum = last_dentry->inum;
                rm_dentry[i].symlink = last_dentry->symlink;

                /* Borramos los contenidos del ultimo. */
                last_dentry->name[0] = '\0';
                last_dentry->inum = 0;
                last_dentry->symlink = 0;

                goto END;
            }

        if(bwalker_block_is_last(walker))
            break;
    }
END:
    bwalker_destroy(walker);
    return result;
}


dir *dir_get_from_path(const char *disk_mem, const char *path)
{
    uint16_t inum = 0;
    char *dir_name = NULL;

    assert (disk_mem != NULL);
    assert (path != NULL);
    dir_name = calloc(strlen(path) + 1, sizeof(char));
    strcpy(dir_name, path);
    inum = get_inode_from_path(disk_mem, dirname(dir_name), 1);
    free(dir_name);
    if(inum==0){
        return NULL;
    }
    return ((dir *)disk_mem+INODEPOOL_OFFSET+INODE_SIZE*inum);

}

uint16_t dir_search(const char *disk_mem, dir *idir,const char *name)
{
    bwalker *bw = NULL;
    direntry *dentry;
    uint16_t bnum, i;


    assert(disk_mem != NULL);
    assert(idir != NULL);
    assert(name != NULL);
    assert(strcmp(name, ""));


    /* Salimos si esto esta vacio obviamente. */
    if(idir->size == 0){
        return 0;
    }

    bw = bwalker_create(disk_mem, idir->size, &(idir->indirect));
    while ((bnum = bwalker_next(bw)))
    {
        dentry=(direntry *) disk_mem + BLOCKPOOL_OFFSET + (bnum*BLOCK_SIZE);
        for(i=0; i<16; i++){
            if(strcmp(name, dentry[i].name) == 0){
                bwalker_destroy(bw);
                return dentry[i].inum;
            }
        }

        if(bwalker_block_is_last(bw))
            break;
    }
    bwalker_destroy(bw);
    return 0;
}

int dir_rename_inode(const char *disk_mem, dir *d,
                     const char *old_name, const char *new_name)
{
    bwalker *bw = NULL;
    direntry *dentry;
    uint16_t i;
    uint16_t bnum;

    assert(disk_mem != NULL);
    assert(d != NULL);
    assert(strcmp(old_name, ""));
    assert(new_name != NULL);
    assert(old_name != NULL);
    assert(strlen(new_name) < NAME_LENGTH);

    /* Salimos si esto esta vacio obviamente. */
    if(d->size == 0){
        return 0;
    }

    bw = bwalker_create(disk_mem, d->size, &(d->indirect));
    while ((bnum = bwalker_next(bw)))
    {
        dentry=(direntry *) disk_mem + BLOCKPOOL_OFFSET + (bnum*BLOCK_SIZE);
        for(i=0; i<16; i++){
            if(strcmp(old_name, dentry[i].name) == 0){
                bwalker_destroy(bw);
                strcpy(dentry[i].name,new_name);
                return 1;
            }
        }

        if(bwalker_block_is_last(bw))
            break;
    }
    bwalker_destroy(bw);
    return 0;
}

uint16_t dir_readdir(const char *disk_mem, dir *d, void *buf,
                     fuse_fill_dir_t filler)
{

    bwalker *bw = NULL;
    direntry *dentry;
    uint16_t bnum, i;


    assert(disk_mem != NULL);
    assert(d != NULL);

    /* Salimos si esto esta vacio obviamente. */
    if(d->size == 0)
        return 0;

    bw = bwalker_create(disk_mem, d->size, &(d->indirect));
    while ((bnum = bwalker_next(bw))){
        dentry=(direntry *) disk_mem + BLOCKPOOL_OFFSET + (bnum*BLOCK_SIZE);
        for(i=0; i<16 && dentry[i].name[0] != '\0' ; i++){
            filler(buf, (const char *)&(dentry[i].name), NULL, 0);
        }

        if(bwalker_block_is_last(bw))
            break;
    }
    bwalker_destroy(bw);
    return 0;
}