src/util/vfs/vfs-devlist.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/vfs.h"
7
8#include <sys/iosupport.h>
9
10static bool _vdlClose(struct VDir* vd);
11static void _vdlRewind(struct VDir* vd);
12static struct VDirEntry* _vdlListNext(struct VDir* vd);
13static struct VFile* _vdlOpenFile(struct VDir* vd, const char* path, int mode);
14static struct VDir* _vdlOpenDir(struct VDir* vd, const char* path);
15
16static const char* _vdleName(struct VDirEntry* vde);
17static enum VFSType _vdleType(struct VDirEntry* vde);
18
19struct VDirEntryDevList {
20 struct VDirEntry d;
21 size_t index;
22 char* name;
23};
24
25struct VDirDevList {
26 struct VDir d;
27 struct VDirEntryDevList vde;
28};
29
30struct VDir* VDeviceList() {
31 struct VDirDevList* vd = malloc(sizeof(struct VDirDevList));
32 if (!vd) {
33 return 0;
34 }
35
36 vd->d.close = _vdlClose;
37 vd->d.rewind = _vdlRewind;
38 vd->d.listNext = _vdlListNext;
39 vd->d.openFile = _vdlOpenFile;
40 vd->d.openDir = _vdlOpenDir;
41
42 vd->vde.d.name = _vdleName;
43 vd->vde.d.type = _vdleType;
44 vd->vde.index = 0;
45 vd->vde.name = 0;
46
47 return &vd->d;
48}
49
50static bool _vdlClose(struct VDir* vd) {
51 struct VDirDevList* vdl = (struct VDirDevList*) vd;
52 free(vdl->vde.name);
53 free(vdl);
54 return true;
55}
56
57static void _vdlRewind(struct VDir* vd) {
58 struct VDirDevList* vdl = (struct VDirDevList*) vd;
59 free(vdl->vde.name);
60 vdl->vde.name = 0;
61 vdl->vde.index = 3;
62}
63
64static struct VDirEntry* _vdlListNext(struct VDir* vd) {
65 struct VDirDevList* vdl = (struct VDirDevList*) vd;
66 if (vdl->vde.name) {
67 ++vdl->vde.index;
68 free(vdl->vde.name);
69 vdl->vde.name = 0;
70 }
71 while (vdl->vde.index < STD_MAX) {
72 const devoptab_t *devops = devoptab_list[vdl->vde.index];
73 if (devops->dirStateSize > 0) {
74 vdl->vde.name = malloc(strlen(devops->name) + 3);
75 sprintf(vdl->vde.name, "%s:", devops->name);
76 return &vdl->vde.d;
77 }
78
79 ++vdl->vde.index;
80 }
81 return 0;
82}
83
84static struct VFile* _vdlOpenFile(struct VDir* vd, const char* path, int mode) {
85 UNUSED(vd);
86 UNUSED(path);
87 UNUSED(mode);
88 return 0;
89}
90
91static struct VDir* _vdlOpenDir(struct VDir* vd, const char* path) {
92 UNUSED(vd);
93 return VDirOpen(path);
94}
95
96static const char* _vdleName(struct VDirEntry* vde) {
97 struct VDirEntryDevList* vdle = (struct VDirEntryDevList*) vde;
98 return vdle->name;
99}
100
101static enum VFSType _vdleType(struct VDirEntry* vde) {
102 UNUSED(vde);
103 return VFS_DIRECTORY;
104}