Skip to content

Commit cd5768e

Browse files
committed
fs: Pass filesystem along when initializing
1 parent e709fd1 commit cd5768e

12 files changed

Lines changed: 48 additions & 50 deletions

File tree

api/fs/common.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828

2929
namespace fs {
3030

31-
// Generic structure for directory entries
3231
struct Dirent;
33-
32+
struct File_system;
3433

3534
/**
3635
* @brief Type used as a building block to represent buffers
@@ -185,24 +184,23 @@ namespace fs {
185184
/** @var no_error: Always returns boolean false when used in expressions */
186185
extern error_t no_error;
187186

188-
/** Async function types **/
189-
using on_init_func = delegate<void(error_t)>;
187+
/** Async function types **/
188+
using on_init_func = delegate<void(error_t, File_system&)>;
190189
using on_ls_func = delegate<void(error_t, dirvec_t)>;
191190
using on_read_func = delegate<void(error_t, buffer_t, uint64_t)>;
192191
using on_stat_func = delegate<void(error_t, const Dirent&)>;
193192

194193

195-
struct List {
194+
struct List
195+
{
196196
error_t error;
197197
dirvec_t entries;
198198
auto begin() { return entries->begin(); }
199199
auto end() { return entries->end(); }
200200
auto cbegin() { return entries->cbegin(); }
201201
auto cend() { return entries->cend(); }
202-
203202
};
204203

204+
} //< fs
205205

206-
} //< namespace fs
207-
208-
#endif //< FS_ERROR_HPP
206+
#endif

api/fs/disk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace fs {
4242

4343
struct Partition;
4444
using on_parts_func = delegate<void(fs::error_t, std::vector<Partition>&)>;
45-
using on_init_func = delegate<void(fs::error_t)>;
45+
using on_init_func = delegate<void(fs::error_t, File_system&)>;
4646
using lba_t = uint32_t;
4747

4848
enum partition_t {

api/fs/filesystem.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ namespace fs {
2727
struct Dirent;
2828

2929
struct File_system {
30-
31-
/** Initialize this filesystem with LBA at @base_sector */
32-
virtual void init(uint64_t lba, uint64_t size, on_init_func on_init) = 0;
33-
3430
/** Get unique (per device type) device id for underlying device.*/
3531
virtual Device_id device_id() = 0;
3632

@@ -76,6 +72,9 @@ namespace fs {
7672
/** Returns the name of this filesystem */
7773
virtual std::string name() const = 0;
7874

75+
/** Initialize this filesystem with LBA at @base_sector */
76+
virtual void init(uint64_t lba, uint64_t size, on_init_func on_init) = 0;
77+
7978
/** Default destructor */
8079
virtual ~File_system() noexcept = default;
8180
}; //< class File_system

src/fs/disk.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ namespace fs {
6060

6161
void Disk::init_fs(on_init_func func)
6262
{
63-
INFO("Disk","init_fs reading block 0");
6463
device.read(
6564
0,
6665
hw::Block_device::on_read_func::make_packed(
6766
[this, func] (hw::Block_device::buffer_t data)
6867
{
6968
if (!data) {
7069
// TODO: error-case for unable to read MBR
71-
func({ error_t::E_IO, "Unable to read MBR"});
70+
func({ error_t::E_IO, "Unable to read MBR"}, fs());
7271
return;
7372
}
7473

@@ -106,7 +105,7 @@ namespace fs {
106105
}
107106

108107
// no partition was found (TODO: extended partitions)
109-
func({ error_t::E_MNT, "No FAT partition auto-detected"});
108+
func({ error_t::E_MNT, "No FAT partition auto-detected"}, fs());
110109
})
111110
);
112111
}
@@ -137,7 +136,7 @@ namespace fs {
137136
{
138137
if (!data) {
139138
// TODO: error-case for unable to read MBR
140-
func({ error_t::E_IO, "Unable to read MBR" });
139+
func({ error_t::E_IO, "Could not read MBR" }, fs());
141140
return;
142141
}
143142

src/fs/fat.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ namespace fs
137137
[this, on_init] (buffer_t data)
138138
{
139139
auto* mbr = (MBR::mbr*) data.get();
140-
assert(mbr != nullptr);
140+
if (mbr == nullptr) {
141+
on_init({ error_t::E_IO, "Could not read MBR" }, *this);
142+
return;
143+
}
141144

142145
// verify image signature
143146
debug("OEM name: \t%s\n", mbr->oem_name);
144147
debug("MBR signature: \t0x%x\n", mbr->magic);
145148
if (UNLIKELY(mbr->magic != 0xAA55)) {
146-
on_init({ error_t::E_MNT, "Missing or invalid MBR signature" });
149+
on_init({ error_t::E_MNT, "Missing or invalid MBR signature" }, *this);
147150
return;
148151
}
149152

@@ -166,7 +169,7 @@ namespace fs
166169
this->lba_base, this->lba_size, this->lba_size * 512);
167170

168171
// on_init callback
169-
on_init(no_error);
172+
on_init(no_error, *this);
170173
})
171174
);
172175
}

test/fs/integration/fat16/fat16.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ void Service::start(const std::string&)
3838

3939
// auto-init filesystem
4040
disk->init_fs(
41-
[disk] (fs::error_t err)
41+
[] (fs::error_t err, auto& fs)
4242
{
4343
CHECKSERT(!err, "Filesystem auto-initialized");
4444

45-
auto& fs = disk->fs();
4645
printf("\t\t%s filesystem\n", fs.name().c_str());
4746

4847
auto list = fs.ls("/");
@@ -57,12 +56,11 @@ void Service::start(const std::string&)
5756
});
5857
// re-init on MBR (sigh)
5958
disk->init_fs(disk->MBR,
60-
[disk] (fs::error_t err)
59+
[] (fs::error_t err, auto& fs)
6160
{
6261
CHECKSERT(!err, "Filesystem initialized on VBR1");
6362

6463
// verify that we can read file
65-
auto& fs = disk->fs();
6664
auto ent = fs.stat("/banana.txt");
6765
CHECKSERT(ent.is_valid(), "Stat file in root dir");
6866
CHECKSERT(ent.is_file(), "Entity is file");

test/fs/integration/fat32/fat32.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
// Includes std::string internal_banana
2525
#include "banana.ascii"
26+
static std::shared_ptr<fs::Disk> disk;
2627

27-
std::shared_ptr<fs::Disk> disk;
28-
29-
const uint64_t SIZE = 4000000000;
28+
const uint64_t SIZE = 4294967296;
3029
const std::string shallow_banana{"/banana.txt"};
3130
const std::string deep_banana{"/dir1/dir2/dir3/dir4/dir5/dir6/banana.txt"};
3231

@@ -35,16 +34,17 @@ void is_done() {
3534
if (++counter == 3) INFO("FAT32","SUCCESS\n");
3635
}
3736

38-
void test2() {
37+
void test2()
38+
{
3939
INFO("FAT32", "Remounting disk.");
40+
41+
CHECKSERT(not disk->empty(), "Disk not empty");
42+
CHECKSERT(disk->dev().size() == SIZE / 512, "Disk size is %llu bytes", SIZE);
43+
4044
disk->init_fs(disk->MBR,
41-
[] (fs::error_t err)
45+
[] (fs::error_t err, auto& fs)
4246
{
4347
CHECKSERT(not err, "Filesystem mounted on VBR1");
44-
CHECKSERT(not disk->empty(), "Disk not empty");
45-
CHECKSERT(disk->dev().size() == SIZE / 512, "Disk size is %llu bytes", SIZE);
46-
47-
auto& fs = disk->fs();
4848

4949
fs.stat(shallow_banana,
5050
[] (auto err, const auto& ent) {
@@ -97,7 +97,7 @@ void test2() {
9797
});
9898
}
9999

100-
void Service::start(const std::string&)
100+
void Service::start()
101101
{
102102
auto& device = hw::Devices::drive(0);
103103
disk = std::make_shared<fs::Disk> (device);
@@ -112,11 +112,10 @@ void Service::start(const std::string&)
112112

113113
// auto-mount filesystem
114114
disk->init_fs(
115-
[] (fs::error_t err)
115+
[] (fs::error_t err, auto& fs)
116116
{
117117
CHECKSERT(!err, "Filesystem auto-initializedd");
118118

119-
auto& fs = disk->fs();
120119
std::string fat32_str{"FAT32"};
121120
CHECKSERT(fs.name() == fat32_str, "Filesystem recognized as FAT32");
122121

@@ -128,9 +127,8 @@ void Service::start(const std::string&)
128127
auto& e = ents->at(0);
129128
CHECKSERT(e.is_file(), "Ent is a file");
130129
CHECKSERT(e.name() == "banana.txt", "Ents name is 'banana.txt'");
130+
131131
test2();
132132
});
133133
});
134-
135-
/**/
136134
}

test/fs/integration/fat32/fat32_disk.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ then
1111

1212
# Remove disk if exists
1313
rm -f $DISK
14-
# Preallocate space to a 2 GB file
15-
fallocate -l 4000000000 $DISK
14+
# Preallocate space to a 4GB file
15+
truncate -s 4G $DISK
1616
# Create FAT32 filesystem on "my.disk"
1717
mkfs.fat $DISK
1818

test/fs/integration/vfs/create_disk.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DISK=$LOCALDIR.disk
1212

1313
rm -f $DISK
1414
echo ">> Creating disk image from $LOCALDIR to $LOCALDIR.disk"
15-
fallocate -l 1048576 $DISK # 256000 sectors
15+
truncate -s 1048576 $DISK # 256000 sectors
1616
mkfs.fat $DISK
1717
mkdir -p $MOUNTDIR
1818
sudo mount $DISK $MOUNTDIR

test/fs/integration/vfs/service.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ fs::File_system& memdisk() {
6060
static auto disk = fs::new_shared_memdisk();
6161

6262
if (not disk->fs_ready())
63-
disk->init_fs([](fs::error_t err) {
63+
{
64+
disk->init_fs([](fs::error_t err, auto&) {
6465
if (err) panic("ERROR MOUNTING DISK\n");
6566
});
66-
67+
}
6768
return disk->fs();
6869
}
6970

@@ -321,14 +322,15 @@ void Service::start(const std::string&)
321322
auto my_disk = disk1;
322323

323324
// initializing a file system
324-
my_disk->init_fs([my_disk](auto err){
325-
325+
my_disk->init_fs(
326+
[my_disk](auto err, auto& fs)
327+
{
326328
if (err) {
327329
INFO("VFS_test", "Error mounting disk: %s \n", err.to_string().c_str());
328330
return;
329331
}
330332

331-
my_disk->fs().ls("/", [my_disk](auto err, auto dirvec){
333+
fs.ls("/", [my_disk](auto err, auto dirvec){
332334

333335
if (err) {
334336
INFO("VFS_test", "ls on disk %s failed", my_disk->name().c_str());

0 commit comments

Comments
 (0)