-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCatalog.c
245 lines (192 loc) · 6.72 KB
/
Catalog.c
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
//
// Catalog.c
// kernel
//
// Created by Dietmar Planitzer on 9/11/24.
// Copyright © 2024 Dietmar Planitzer. All rights reserved.
//
#include "Catalog.h"
#include <filemanager/FileHierarchy.h>
#include <filesystem/kernfs/KernFS.h>
typedef struct Catalog {
FilesystemRef _Nonnull fs;
FileHierarchyRef _Nonnull fh;
InodeRef _Nonnull rootDirectory;
uint8_t nameLength;
char name[kMaxCatalogNameLength];
} Catalog;
CatalogRef _Nonnull gDriverCatalog;
CatalogRef _Nonnull gFSCatalog;
errno_t Catalog_Create(const char* _Nonnull name, CatalogRef _Nullable * _Nonnull pOutSelf)
{
decl_try_err();
const size_t cnlen = String_Length(name);
CatalogRef self;
if (cnlen > kMaxCatalogNameLength) {
throw(ERANGE);
}
try(kalloc_cleared(sizeof(Catalog), (void**) &self));
try(KernFS_Create((KernFSRef*)&self->fs));
try(Filesystem_Start(self->fs, ""));
try(FileHierarchy_Create(self->fs, &self->fh));
try(Filesystem_AcquireRootDirectory(self->fs, &self->rootDirectory));
self->nameLength = cnlen;
memcpy(self->name, name, cnlen);
*pOutSelf = self;
return EOK;
catch:
*pOutSelf = NULL;
return err;
}
errno_t Catalog_GetName(CatalogRef _Nonnull self, char* _Nonnull buf, size_t bufSize)
{
if (bufSize < 1) {
return ERANGE;
}
if (bufSize < (self->nameLength + 1)) {
*buf = '\0';
return ERANGE;
}
memcpy(buf, self->name, self->nameLength);
buf[self->nameLength] = '\0';
return EOK;
}
FilesystemRef _Nonnull Catalog_CopyFilesystem(CatalogRef _Nonnull self)
{
return Object_RetainAs(self->fs, Filesystem);
}
bool Catalog_IsFsid(CatalogRef _Nonnull self, fsid_t fsid)
{
return (Filesystem_GetId(self->fs) == fsid) ? true : false;
}
errno_t Catalog_IsPublished(CatalogRef _Nonnull self, const char* _Nonnull path)
{
decl_try_err();
ResolvedPath rp;
err = FileHierarchy_AcquireNodeForPath(self->fh, kPathResolution_Target, path, self->rootDirectory, self->rootDirectory, kUserId_Root, kGroupId_Root, &rp);
ResolvedPath_Deinit(&rp);
return err;
}
errno_t Catalog_AcquireNodeForPath(CatalogRef _Nonnull self, const char* _Nonnull path, ResolvedPath* _Nonnull rp)
{
return FileHierarchy_AcquireNodeForPath(self->fh, kPathResolution_Target, path, self->rootDirectory, self->rootDirectory, kUserId_Root, kGroupId_Root, rp);
}
errno_t Catalog_Open(CatalogRef _Nonnull self, const char* _Nonnull path, unsigned int mode, IOChannelRef _Nullable * _Nonnull pOutChannel)
{
decl_try_err();
ResolvedPath rp;
err = FileHierarchy_AcquireNodeForPath(self->fh, kPathResolution_Target, path, self->rootDirectory, self->rootDirectory, kUserId_Root, kGroupId_Root, &rp);
if (err == EOK) {
Inode_Lock(rp.inode);
if (!Inode_IsDirectory(rp.inode)) {
err = Inode_CreateChannel(rp.inode, mode, pOutChannel);
}
else {
err = EISDIR;
}
Inode_Unlock(rp.inode);
}
ResolvedPath_Deinit(&rp);
return err;
}
static errno_t _Catalog_AcquireFolder(CatalogRef _Nonnull self, CatalogId folderId, InodeRef _Nullable * _Nonnull pOutDir)
{
if (folderId == kCatalogId_None) {
return Filesystem_AcquireRootDirectory(self->fs, pOutDir);
}
else {
return Filesystem_AcquireNodeWithId(self->fs, (ino_t)folderId, pOutDir);
}
}
// Publishes a folder with the name 'name' to the catalog. Pass kCatalog_None as
// the 'parentFolderId' to create the new folder inside the root folder.
errno_t Catalog_PublishFolder(CatalogRef _Nonnull self, CatalogId parentFolderId, const char* _Nonnull name, uid_t uid, gid_t gid, FilePermissions perms, CatalogId* _Nonnull pOutFolderId)
{
decl_try_err();
InodeRef pDir = NULL;
InodeRef pNode = NULL;
PathComponent pc;
*pOutFolderId = kCatalogId_None;
pc.name = name;
pc.count = String_Length(name);
err = _Catalog_AcquireFolder(self, parentFolderId, &pDir);
if (err == EOK) {
err = Filesystem_CreateNode(self->fs, kFileType_Directory, pDir, &pc, NULL, uid, gid, perms, &pNode);
if (err == EOK) {
*pOutFolderId = (CatalogId)Inode_GetId(pNode);
}
}
Inode_Relinquish(pNode);
Inode_Relinquish(pDir);
return err;
}
errno_t Catalog_Unpublish(CatalogRef _Nonnull self, CatalogId folderId, CatalogId entryId)
{
decl_try_err();
InodeRef pDir = NULL;
InodeRef pNode = NULL;
if (folderId == kCatalogId_None && entryId == kCatalogId_None) {
return EOK;
}
// Get the bus directory or devfs root
err = _Catalog_AcquireFolder(self, folderId, &pDir);
if (err == EOK) {
// Get the parent of the directory or the driver entry
if (entryId == kCatalogId_None) {
pNode = pDir;
pDir = NULL;
err = Filesystem_AcquireParentNode(self->fs, pDir, &pDir);
}
else {
err = Filesystem_AcquireNodeWithId(self->fs, (ino_t)entryId, &pNode);
}
// Delete the directory or the driver entry
if (err == EOK) {
err = Filesystem_Unlink(self->fs, pNode, pDir);
}
}
catch:
Inode_Relinquish(pNode);
Inode_Relinquish(pDir);
return err;
}
errno_t Catalog_PublishDriver(CatalogRef _Nonnull self, CatalogId folderId, const char* _Nonnull name, uid_t uid, gid_t gid, FilePermissions perms, DriverRef _Nonnull driver, intptr_t arg, CatalogId* _Nonnull pOutCatalogId)
{
decl_try_err();
InodeRef pDir = NULL;
InodeRef pNode = NULL;
PathComponent pc;
*pOutCatalogId = kCatalogId_None;
pc.name = name;
pc.count = String_Length(name);
err = _Catalog_AcquireFolder(self, folderId, &pDir);
if (err == EOK) {
err = KernFS_CreateDevice((KernFSRef)self->fs, pDir, &pc, driver, arg, uid, gid, perms, &pNode);
if (err == EOK) {
*pOutCatalogId = (CatalogId)Inode_GetId(pNode);
}
}
Inode_Relinquish(pNode);
Inode_Relinquish(pDir);
return err;
}
errno_t Catalog_PublishFilesystem(CatalogRef _Nonnull self, const char* _Nonnull name, uid_t uid, gid_t gid, FilePermissions perms, FilesystemRef _Nonnull fs, CatalogId* _Nonnull pOutCatalogId)
{
decl_try_err();
InodeRef pDir = NULL;
InodeRef pNode = NULL;
PathComponent pc;
*pOutCatalogId = kCatalogId_None;
pc.name = name;
pc.count = String_Length(name);
err = Filesystem_AcquireRootDirectory(self->fs, &pDir);
if (err == EOK) {
err = KernFS_CreateFilesystem((KernFSRef)self->fs, pDir, &pc, fs, uid, gid, perms, &pNode);
if (err == EOK) {
*pOutCatalogId = (CatalogId)Inode_GetId(pNode);
}
}
Inode_Relinquish(pNode);
Inode_Relinquish(pDir);
return err;
}