OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [fs/] [path.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Path manipulation functions.
3
 *
4
 * Copyright (C) 2008 Bahadir Balban
5
 */
6
#include <l4/macros.h>
7
#include <l4/lib/list.h>
8
#include <l4/api/errno.h>
9
#include <lib/pathstr.h>
10
#include <malloc/malloc.h>
11
#include <path.h>
12
#include <stdio.h>
13
#include <fs.h>
14
#include <task.h>
15
#include <vfs.h>
16
 
17
const char *pathdata_next_component(struct pathdata *pdata)
18
{
19
        struct pathcomp *p, *n;
20
        const char *pathstr;
21
 
22
        list_foreach_removable_struct(p, n, &pdata->list, list) {
23
                list_remove(&p->list);
24
                pathstr = p->str;
25
                kfree(p);
26
                return pathstr;
27
        }
28
        return "";
29
}
30
 
31
/* Check there's at least one element, unlink and return the last element */
32
const char *pathdata_last_component(struct pathdata *pdata)
33
{
34
        struct pathcomp *p;
35
        const char *pathstr;
36
 
37
        if (!list_empty(&pdata->list)) {
38
                p = link_to_struct(pdata->list.prev, struct pathcomp, list);
39
                list_remove(&p->list);
40
                pathstr = p->str;
41
                kfree(p);
42
                return pathstr;
43
        }
44
 
45
        return "";
46
}
47
 
48
/* Unlink and free all path components in pathdata, and then free pathdata */
49
void pathdata_destroy(struct pathdata *p)
50
{
51
        struct pathcomp *c, *n;
52
 
53
        list_foreach_removable_struct(c, n, &p->list, list) {
54
                list_remove(&c->list);
55
                kfree(c);
56
        }
57
        kfree(p);
58
}
59
 
60
void pathdata_print(struct pathdata *p)
61
{
62
        struct pathcomp *comp;
63
 
64
        printf("Extracted path is:\n");
65
        list_foreach_struct(comp, &p->list, list)
66
                printf("%s\n", comp->str);
67
}
68
 
69
/* Extracts all path components from pathname into more presentable form */
70
struct pathdata *pathdata_parse(const char *pathname,
71
                                char *pathbuf, struct tcb *task)
72
{
73
        struct pathdata *pdata = kzalloc(sizeof(*pdata));
74
        struct pathcomp *comp;
75
        char *str;
76
 
77
        if (!pdata)
78
                return PTR_ERR(-ENOMEM);
79
 
80
        /* Initialise pathdata */
81
        link_init(&pdata->list);
82
        strcpy(pathbuf, pathname);
83
 
84
        /* First component is root if there's a root */
85
        if (pathname[0] == VFS_CHAR_SEP) {
86
                if (!(comp = kzalloc(sizeof(*comp)))) {
87
                        kfree(pdata);
88
                        return PTR_ERR(-ENOMEM);
89
                }
90
                link_init(&comp->list);
91
                comp->str = VFS_STR_ROOTDIR;
92
                list_insert_tail(&comp->list, &pdata->list);
93
 
94
                if (task)
95
                        /* Lookup start vnode is root vnode */
96
                        pdata->vstart = task->fs_data->rootdir;
97
                else /* If no task, we use the root mountpoint pivot vnode */
98
                        pdata->vstart = vfs_root.pivot;
99
 
100
        /* Otherwise start from current directory */
101
        } else {
102
                struct dentry *curdir;
103
 
104
                if (!(comp = kzalloc(sizeof(*comp)))) {
105
                        kfree(pdata);
106
                        return PTR_ERR(-ENOMEM);
107
                }
108
                link_init(&comp->list);
109
 
110
                /* Get current dentry for this task */
111
                curdir = link_to_struct(task->fs_data->curdir->dentries.next,
112
                                    struct dentry, vref);
113
 
114
                /* Use its name in path component */
115
                comp->str = curdir->name;
116
                list_insert_tail(&comp->list, &pdata->list);
117
 
118
                /* Lookup start vnode is current dir vnode */
119
                pdata->vstart = task->fs_data->curdir;
120
        }
121
 
122
        /* Add every other path component */
123
        str = splitpath(&pathbuf, VFS_CHAR_SEP);
124
        while(*str) {
125
                /* Any curdir components in path are ignored. */
126
                if (!strcmp(str, VFS_STR_CURDIR)) {
127
                        ;
128
                } else {
129
                        if (!(comp = kzalloc(sizeof(*comp)))) {
130
                                pathdata_destroy(pdata);
131
                                return PTR_ERR(-ENOMEM);
132
                        }
133
                        link_init(&comp->list);
134
                        comp->str = str;
135
                        list_insert_tail(&comp->list, &pdata->list);
136
                }
137
 
138
                /* Next component */
139
                str = splitpath(&pathbuf, VFS_CHAR_SEP);
140
        }
141
        // pathdata_print(pdata);
142
 
143
        return pdata;
144
}
145
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.