The Pedigree Project 0.1
RawFs.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "RawFs.h"
21#include "pedigree/kernel/Log.h"
22#include "pedigree/kernel/machine/Device.h"
23#include "pedigree/kernel/panic.h"
24#include "pedigree/kernel/processor/types.h"
25#include "pedigree/kernel/utilities/new"
26
27#include "RawFsDir.h"
28#include "RawFsFile.h"
29#include "modules/Module.h"
30#include "modules/system/vfs/VFS.h"
31
32class Disk;
33class File;
34
35static RawFs* g_pRawFs = 0;
36
37RawFsDir::~RawFsDir() = default;
38
39RawFs::RawFs() : m_pRoot(0) {
40 m_pRoot = new RawFsDir(String(""), this, 0);
41}
42
43RawFs::~RawFs() {
44 if (shutdown() != SyncStatus::Success)
45 panic("rawfs: filesystem shutdown failed");
46 if (m_pRoot)
47 delete m_pRoot;
48}
49
50Filesystem::SyncStatus RawFs::sync() {
51 return m_pRoot->syncFiles(false) ? SyncStatus::Success : SyncStatus::IoError;
52}
53
54Filesystem::SyncStatus RawFs::shutdown() {
55 return m_pRoot->syncFiles(true) ? SyncStatus::Success : SyncStatus::IoError;
56}
57
59 return m_pRoot;
60}
61
62static bool hasDiskChildren(Device* pDev) {
63 for (size_t i = 0; i < pDev->getNumChildren(); i++) {
64 Device* pChild = pDev->getChild(i);
65 if (pChild->getType() == Device::Disk)
66 return true;
67 if (hasDiskChildren(pChild))
68 return true;
69 }
70 return false;
71}
72
73static void searchNode(Device* pDev, RawFsDir* pDir) {
74 for (size_t i = 0; i < pDev->getNumChildren(); i++) {
75 Device* pChild = pDev->getChild(i);
76 if (pChild->getType() == Device::Disk) {
77 String str;
78 pChild->getName(str);
79 if (hasDiskChildren(pChild)) {
80 NOTICE("rawfs: adding dir '" << str << "'");
81 RawFsDir* pDir2 = new RawFsDir(str, g_pRawFs, static_cast<File*>(pDir));
82 pDir->addEntry(pDir2);
83 pDir->addEntry(new RawFsFile(String("entire-disk"), g_pRawFs, static_cast<File*>(pDir),
84 reinterpret_cast<Disk*>(pChild)));
85 searchNode(pChild, pDir2);
86 } else {
87 NOTICE("rawfs: adding file '" << str << "'");
88 pDir->addEntry(new RawFsFile(str, g_pRawFs, static_cast<File*>(pDir),
89 reinterpret_cast<Disk*>(pChild)));
90 }
91 } else
92 searchNode(pChild, pDir);
93 }
94}
95
96static void rescanTree() {
97 RawFsDir* pD = static_cast<RawFsDir*>(g_pRawFs->getRoot());
98 pD->removeRecursive();
99
100 // Find the disk topology in an intermediate form.
102 // searchNode(&Device::root(), pD);
103}
104
105static bool init() {
106 g_pRawFs = new RawFs();
107 VFS::instance().addMountCallback(&rescanTree);
108 rescanTree();
109 VFS::instance().registerFilesystem(g_pRawFs, String("raw"));
110
111 return true;
112}
113
114static void destroy() {
115 if (!VFS::instance().removeMountCallback(&rescanTree)) {
116 panic("rawfs mount callback was not registered during unload");
117 }
118 if (!VFS::instance().unregisterFilesystem(g_pRawFs, true, true))
119 panic("rawfs: filesystem retirement failed");
120 g_pRawFs = nullptr;
121}
122
123MODULE_INFO_RUNTIME_PINNED("rawfs", &init, &destroy, "vfs");
size_t getNumChildren()
Definition Device.cc:131
Device * getChild(size_t n)
Definition Device.cc:127
@ Disk
A disk device - a block device in UNIX terms.
Definition Device.h:70
virtual void getName(String &str)
Definition Device.cc:118
virtual Type getType()
Definition Device.h:173
Definition Disk.h:35
Definition File.h:74
void removeRecursive()
Definition RawFsDir.cc:42
void addEntry(File *pEntry)
Definition RawFsDir.cc:38
Definition RawFs.h:33
virtual SyncStatus shutdown()
Definition RawFs.cc:54
virtual SyncStatus sync()
Definition RawFs.cc:50
virtual File * getRoot() const
Definition RawFs.cc:58
String registerFilesystem(Filesystem *pFs, const String &preferredStableName)
Definition VFS.cc:479
static VFS & instance()
Definition VFS.cc:291
void addMountCallback(MountCallback callback)
Definition VFS.cc:1002
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition panic.cc:117