aboutsummaryrefslogtreecommitdiff
path: root/lib/fs/ext2/dir.c
blob: 27ee89054d6d34ec6a9edfe4131318934f7025a3 (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
/*
 * Copyright (c) 2007 Travis Geiselbrecht
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <string.h>
#include <stdlib.h>
#include <debug.h>
#include <trace.h>
#include <err.h>
#include "ext2_priv.h"

#define LOCAL_TRACE 0

/* read in the dir, look for the entry */
static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const char *name, inodenum_t *inum)
{
    uint file_blocknum;
    int err;
    uint8_t *buf;
    size_t namelen = strlen(name);

    if (!S_ISDIR(dir_inode->i_mode))
        return ERR_NOT_DIR;

    buf = malloc(EXT2_BLOCK_SIZE(ext2->sb));

    file_blocknum = 0;
    for (;;) {
        /* read in the offset */
        err = ext2_read_inode(ext2, dir_inode, buf, file_blocknum * EXT2_BLOCK_SIZE(ext2->sb), EXT2_BLOCK_SIZE(ext2->sb));
        if (err <= 0) {
            free(buf);
            return -1;
        }

        /* walk through the directory entries, looking for the one that matches */
        struct ext2_dir_entry_2 *ent;
        uint pos = 0;
        while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
            ent = (struct ext2_dir_entry_2 *)&buf[pos];

            LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
                    file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);

            /* sanity check the record length */
            if (LE16(ent->rec_len) == 0)
                break;

            if (ent->name_len == namelen && memcmp(name, ent->name, ent->name_len) == 0) {
                // match
                *inum = LE32(ent->inode);
                LTRACEF("match: inode %d\n", *inum);
                free(buf);
                return 1;
            }

            pos += ROUNDUP(LE16(ent->rec_len), 4);
        }

        file_blocknum++;

        /* sanity check the directory. 4MB should be enough */
        if (file_blocknum > 1024) {
            free(buf);
            return -1;
        }
    }
}

/* note, trashes path */
static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, inodenum_t *inum, int recurse)
{
    char *ptr;
    struct ext2_inode inode;
    struct ext2_inode dir_inode;
    int err;
    bool done;

    LTRACEF("path '%s', start_inode %p, inum %p, recurse %d\n", path, start_inode, inum, recurse);

    if (recurse > 4)
        return ERR_RECURSE_TOO_DEEP;

    /* chew up leading slashes */
    ptr = &path[0];
    while (*ptr == '/')
        ptr++;

    done = false;
    memcpy(&dir_inode, start_inode, sizeof(struct ext2_inode));
    while (!done) {
        /* process the first component */
        char *next_sep = strchr(ptr, '/');
        if (next_sep) {
            /* terminate the next component, giving us a substring */
            *next_sep = 0;
        } else {
            /* this is the last component */
            done = true;
        }

        LTRACEF("component '%s', done %d\n", ptr, done);

        /* do the lookup on this component */
        err = ext2_dir_lookup(ext2, &dir_inode, ptr, inum);
        if (err < 0)
            return err;

nextcomponent:
        LTRACEF("inum %u\n", *inum);

        /* load the next inode */
        err = ext2_load_inode(ext2, *inum, &inode);
        if (err < 0)
            return err;

        /* is it a symlink? */
        if (S_ISLNK(inode.i_mode)) {
            char link[512];

            LTRACEF("hit symlink\n");

            err = ext2_read_link(ext2, &inode, link, sizeof(link));
            if (err < 0)
                return err;

            LTRACEF("symlink read returns %d '%s'\n", err, link);

            /* recurse, parsing the link */
            if (link[0] == '/') {
                /* link starts with '/', so start over again at the rootfs */
                err = ext2_walk(ext2, link, &ext2->root_inode, inum, recurse + 1);
            } else {
                err = ext2_walk(ext2, link, &dir_inode, inum, recurse + 1);
            }

            LTRACEF("recursive walk returns %d\n", err);

            if (err < 0)
                return err;

            /* if we weren't done with our path parsing, start again with the result of this recurse */
            if (!done) {
                goto nextcomponent;
            }
        } else if (S_ISDIR(inode.i_mode)) {
            /* for the next cycle, point the dir inode at our new directory */
            memcpy(&dir_inode, &inode, sizeof(struct ext2_inode));
        } else {
            if (!done) {
                /* we aren't done and this walked over a nondir, abort */
                LTRACEF("not finished and component is nondir\n");
                return ERR_NOT_FOUND;
            }
        }

        if (!done) {
            /* move to the next separator */
            ptr = next_sep + 1;

            /* consume multiple separators */
            while (*ptr == '/')
                ptr++;
        }
    }

    return 0;
}

/* do a path parse, looking up each component */
int ext2_lookup(ext2_t *ext2, const char *_path, inodenum_t *inum)
{
    LTRACEF("path '%s', inum %p\n", _path, inum);

    char path[512];
    strlcpy(path, _path, sizeof(path));

    return ext2_walk(ext2, path, &ext2->root_inode, inum, 1);
}