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);
15static bool _vdlDeleteFile(struct VDir* vd, const char* path);
16
17static const char* _vdleName(struct VDirEntry* vde);
18static enum VFSType _vdleType(struct VDirEntry* vde);
19
20struct VDirEntryDevList {
21 struct VDirEntry d;
22 size_t index;
23 char* name;
24};
25
26struct VDirDevList {
27 struct VDir d;
28 struct VDirEntryDevList vde;
29};
30
31struct VDir* VDeviceList() {
32 struct VDirDevList* vd = malloc(sizeof(struct VDirDevList));
33 if (!vd) {
34 return 0;
35 }
36
37 vd->d.close = _vdlClose;
38 vd->d.rewind = _vdlRewind;
39 vd->d.listNext = _vdlListNext;
40 vd->d.openFile = _vdlOpenFile;
41 vd->d.openDir = _vdlOpenDir;
42 vd->d.deleteFile = _vdlDeleteFile;
43
44 vd->vde.d.name = _vdleName;
45 vd->vde.d.type = _vdleType;
46 vd->vde.index = 0;
47 vd->vde.name = 0;
48
49 return &vd->d;
50}
51
52static bool _vdlClose(struct VDir* vd) {
53 struct VDirDevList* vdl = (struct VDirDevList*) vd;
54 free(vdl->vde.name);
55 free(vdl);
56 return true;
57}
58
59static void _vdlRewind(struct VDir* vd) {
60 struct VDirDevList* vdl = (struct VDirDevList*) vd;
61 free(vdl->vde.name);
62 vdl->vde.name = 0;
63 vdl->vde.index = 3;
64}
65
66static struct VDirEntry* _vdlListNext(struct VDir* vd) {
67 struct VDirDevList* vdl = (struct VDirDevList*) vd;
68 if (vdl->vde.name) {
69 ++vdl->vde.index;
70 free(vdl->vde.name);
71 vdl->vde.name = 0;
72 }
73 while (vdl->vde.index < STD_MAX) {
74 const devoptab_t *devops = devoptab_list[vdl->vde.index];
75 if (devops->dirStateSize > 0) {
76 vdl->vde.name = malloc(strlen(devops->name) + 3);
77 sprintf(vdl->vde.name, "%s:", devops->name);
78 return &vdl->vde.d;
79 }
80
81 ++vdl->vde.index;
82 }
83 return 0;
84}
85
86static struct VFile* _vdlOpenFile(struct VDir* vd, const char* path, int mode) {
87 UNUSED(vd);
88 UNUSED(path);
89 UNUSED(mode);
90 return 0;
91}
92
93static struct VDir* _vdlOpenDir(struct VDir* vd, const char* path) {
94 UNUSED(vd);
95 return VDirOpen(path);
96}
97
98static bool _vdlDeleteFile(struct VDir* vd, const char* path) {
99 UNUSED(vd);
100 UNUSED(path);
101 return false;
102}
103
104static const char* _vdleName(struct VDirEntry* vde) {
105 struct VDirEntryDevList* vdle = (struct VDirEntryDevList*) vde;
106 return vdle->name;
107}
108
109static enum VFSType _vdleType(struct VDirEntry* vde) {
110 UNUSED(vde);
111 return VFS_DIRECTORY;
112}