summaryrefslogtreecommitdiffhomepage
path: root/Private/Source/FileManager.cxx
blob: 4742769c690b3279a9bb9cfa8978e1c05fc3bc25 (plain)
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
/* -------------------------------------------

    Copyright Mahrouss Logic

------------------------------------------- */

#include <KernelKit/FileManager.hpp>
#include <NewKit/ErrorID.hpp>
#include <NewKit/Utils.hpp>

/// BUGS: 0
//! @brief File manager for NewOS.

namespace NewOS {
static FilesystemManagerInterface* kMounted = nullptr;

/// @brief FilesystemManager getter.
/// @return The mounted filesystem.
FilesystemManagerInterface* FilesystemManagerInterface::GetMounted() {
  return kMounted;
}

/// @brief Unmount filesystem.
/// @return The unmounted filesystem.
FilesystemManagerInterface* FilesystemManagerInterface::Unmount() {
  if (kMounted) {
    auto mount = kMounted;
    kMounted = nullptr;

    return mount;
  }

  return nullptr;
}

/// @brief Mount filesystem.
/// @param mountPtr The filesystem to mount.
/// @return if it succeeded true, otherwise false.
bool FilesystemManagerInterface::Mount(FilesystemManagerInterface* mountPtr) {
  if (kMounted == nullptr) {
    kMounted = mountPtr;
    return true;
  }

  return false;
}

#ifdef __FSKIT_NEWFS__
/// @brief Opens a new file.
/// @param path 
/// @param r 
/// @return 
NodePtr NewFilesystemManager::Open(const char* path, const char* r) {
  if (!path || *path == 0) return nullptr;

  if (!r || *r == 0) return nullptr;

  auto catalog = fImpl->GetCatalog(path);

  if (catalog->Kind != kNewFSCatalogKindFile) {
    fImpl->CloseCatalog(catalog);
    return nullptr;
  }

  return node_cast(catalog);
}

/// @brief Writes to a catalog
/// @param node 
/// @param data 
/// @param flags 
/// @return 
Void NewFilesystemManager::Write(NodePtr node, VoidPtr data,
                                       Int32 flags) {
  if ((reinterpret_cast<NewCatalog*>(node))->Kind == kNewFSCatalogKindFile)
    fImpl->WriteCatalog(reinterpret_cast<NewCatalog*>(node), data);
}

/**
 * NOTE: Write and Read are implemented using a custom NodePtr, retrieved
 * using OpenFork.
 */

/// @brief Reads from filesystem.
/// @param node 
/// @param flags 
/// @param sz 
/// @return 
VoidPtr NewFilesystemManager::Read(NodePtr node, Int32 flags, SizeT sz) {
  if ((reinterpret_cast<NewCatalog*>(node))->Kind == kNewFSCatalogKindFile)
    return fImpl->ReadCatalog(reinterpret_cast<NewCatalog*>(node), sz);

  return nullptr;
}

/// @brief Seek from Catalog.
/// @param node 
/// @param off 
/// @return 
bool NewFilesystemManager::Seek(NodePtr node, SizeT off) {
  if (!node || off == 0) return false;
  
  return fImpl->Seek(reinterpret_cast<NewCatalog*>(node), off);
}

/// @brief Tell where the catalog is/
/// @param node 
/// @return 
SizeT NewFilesystemManager::Tell(NodePtr node) {
  if (!node) return kNPos;

  return fImpl->Tell(reinterpret_cast<NewCatalog*>(node));
}

/// @brief Rewind the catalog.
/// @param node 
/// @return 
bool NewFilesystemManager::Rewind(NodePtr node) {
  if (!node) return false;

  return this->Seek(node, 0);
}

/// @brief The filesystem implementation.
/// @return 
NewFSParser* NewFilesystemManager::GetImpl() noexcept { return fImpl; }
#endif // __FSKIT_NEWFS__
}  // namespace NewOS