root/Whitix/trunk/lib/keobject.c

Revision 2028, 3.4 KB (checked in by mwhitworth, 3 years ago)

Don't use attributes in keobject code, remove KeObjectPopulate.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18
19#include <error.h>
20#include <fs/kfs.h>
21#include <string.h>
22#include <keobject.h>
23#include <malloc.h>
24#include <module.h>
25#include <panic.h>
26#include <print.h>
27#include <fs/devfs.h>
28#include <fs/icfs.h>
29
30void KeObjectInit(struct KeObject* object, struct KeSet* parent)
31{
32        object->refs = 1;
33        object->dir = NULL;
34        object->parent = parent;
35}
36
37SYMBOL_EXPORT(KeObjectInit);
38
39void KeObjectDetach(struct KeObject* object)
40{
41        if (object->dir)
42                IcFsRemoveDir(object->dir);
43       
44        if (object->parent)
45                ListRemove(&object->next);
46               
47        MemFree(object->name);
48}
49
50SYMBOL_EXPORT(KeObjectDetach);
51
52void KeObjectFree(struct KeObject* object)
53{
54        struct KeSet* parent = object->parent;
55       
56        KeObjectDetach(object);
57
58        if (parent && parent->type && parent->type->release)
59                parent->type->release(object);
60        else
61                KernelPanic("No type to free from\n");
62}
63
64SYMBOL_EXPORT(KeObjectFree);
65
66int KeObjectAttach(struct KeObject* object, const char* name, ...)
67{
68        VaList vaList;
69        int err;
70       
71        VaStart(vaList, name);
72       
73        err = KeObjectVaAttach(object, name, vaList);
74       
75        VaEnd(vaList);
76       
77        return err;
78}
79
80int KeObjectVaAttach(struct KeObject* object, const char* name, VaList vaList)
81{
82        if (!object)
83                return -EFAULT;
84               
85        /* If the pointer to the IcFs entry is already set, we've already bound
86         * this object to the filesystem.
87         */
88        if (object->dir)
89                return -EEXIST;
90
91        object->name = vasprintf(name, vaList);
92       
93        if (!object->name)
94                return -EFAULT;
95       
96        /* Is there already an object in the set with the same name? */
97        if (object->parent && KeSetFind(object->parent, object->name))
98                return -EEXIST;
99               
100        /* Create a directory in IcFs, using the object's parent directory as the
101         * parent directory.
102         */
103         
104         object->dir=IcFsCreateDir(KeObjGetParentDir(object), object->name);
105         
106         if (!object->dir)
107                return -EIO;
108         
109         /* Add the new object to the parent's list of KeObjects. */
110         KeSetAdd(object->parent, object);
111         
112         return 0;
113}
114
115int KeObjUnbindFs(struct KeObject* object)
116{
117        /* TODO */
118        return 0;
119}
120
121struct KeObject* KeSetFind(struct KeSet* set, const char* name)
122{
123        struct KeObject* curr;
124        static int i = 0;
125       
126        SpinLock(&set->lock);
127       
128        ListForEachEntry(curr, &set->head, next)
129        {
130                const char* objName = KeObjGetName(curr);
131               
132                if (!objName)
133                        continue;
134                       
135                if (!strcmp(name, objName))
136                        goto out;
137        }
138       
139        curr = NULL;
140
141out:
142        SpinUnlock(&set->lock);
143        return curr;
144}
145
146void KeSetInit(struct KeSet* set, struct KeSet* parent, struct KeObjType* type)
147{
148        INIT_LIST_HEAD(&set->head);
149        KeObjectInit(&set->object, parent);
150       
151        set->devDir = NULL;
152        set->type = type;
153}
154
155SYMBOL_EXPORT(KeSetInit);
156
157int KeSetAttach(struct KeSet* set, const char* name, ...)
158{
159        if (!set)
160                return -EFAULT;
161               
162        return KeObjectAttach(&set->object, name);
163}
164
165SYMBOL_EXPORT(KeSetAttach);
Note: See TracBrowser for help on using the browser.