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 "RawFsDir.h"
22 #include "RawFsFile.h"
23 #include "modules/Module.h"
24 #include "modules/system/vfs/VFS.h"
25 #include "pedigree/kernel/Log.h"
26 #include "pedigree/kernel/machine/Device.h"
27 #include "pedigree/kernel/processor/types.h"
28 #include "pedigree/kernel/utilities/new"
29 
30 class Disk;
31 class File;
32 
33 static RawFs *g_pRawFs = 0;
34 
35 RawFsDir::~RawFsDir() = default;
36 
37 RawFs::RawFs() : m_pRoot(0)
38 {
39  m_pRoot = new RawFsDir(String(""), this, 0);
40 }
41 
42 RawFs::~RawFs()
43 {
44  if (m_pRoot)
45  delete m_pRoot;
46 }
47 
49 {
50  return m_pRoot;
51 }
52 
53 static bool hasDiskChildren(Device *pDev)
54 {
55  for (size_t i = 0; i < pDev->getNumChildren(); i++)
56  {
57  Device *pChild = pDev->getChild(i);
58  if (pChild->getType() == Device::Disk)
59  return true;
60  if (hasDiskChildren(pChild))
61  return true;
62  }
63  return false;
64 }
65 
66 static void searchNode(Device *pDev, RawFsDir *pDir)
67 {
68  for (size_t i = 0; i < pDev->getNumChildren(); i++)
69  {
70  Device *pChild = pDev->getChild(i);
71  if (pChild->getType() == Device::Disk)
72  {
73  String str;
74  pChild->getName(str);
75  if (hasDiskChildren(pChild))
76  {
77  NOTICE("rawfs: adding dir '" << str << "'");
78  RawFsDir *pDir2 =
79  new RawFsDir(str, g_pRawFs, static_cast<File *>(pDir));
80  pDir->addEntry(pDir2);
81  pDir->addEntry(new RawFsFile(
82  String("entire-disk"), g_pRawFs, static_cast<File *>(pDir),
83  reinterpret_cast<Disk *>(pChild)));
84  searchNode(pChild, pDir2);
85  }
86  else
87  {
88  NOTICE("rawfs: adding file '" << str << "'");
89  pDir->addEntry(new RawFsFile(
90  str, g_pRawFs, static_cast<File *>(pDir),
91  reinterpret_cast<Disk *>(pChild)));
92  }
93  }
94  else
95  searchNode(pChild, pDir);
96  }
97 }
98 
99 static void rescanTree()
100 {
101  RawFsDir *pD = static_cast<RawFsDir *>(g_pRawFs->getRoot());
102  pD->removeRecursive();
103 
104  // Find the disk topology in an intermediate form.
106  // searchNode(&Device::root(), pD);
107 }
108 
109 static bool init()
110 {
111  g_pRawFs = new RawFs();
112  VFS::instance().addMountCallback(&rescanTree);
113  rescanTree();
114  String alias("raw");
115  VFS::instance().addAlias(g_pRawFs, alias);
116 
117  return true;
118 }
119 
120 static void destroy()
121 {
122 }
123 
124 MODULE_INFO("rawfs", &init, &destroy, "vfs");
Device * getChild(size_t n)
Definition: Device.cc:132
size_t getNumChildren()
Definition: Device.cc:137
virtual void getName(String &str)
Definition: Device.cc:121
void addAlias(Filesystem *pFs, const String &alias)
Definition: VFS.cc:123
A disk device - a block device in UNIX terms.
Definition: Device.h:54
void addMountCallback(MountCallback callback)
Definition: VFS.cc:305
Definition: String.h:49
void removeRecursive()
Definition: RawFsDir.cc:45
static VFS & instance()
Definition: VFS.cc:56
Definition: Disk.h:32
Definition: Device.h:43
virtual File * getRoot() const
Definition: RawFs.cc:48
void addEntry(File *pEntry)
Definition: RawFsDir.cc:40
#define NOTICE(text)
Definition: Log.h:74
virtual Type getType()
Definition: Device.h:163
Definition: RawFs.h:32
Definition: File.h:66