mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +00:00
Ext2FS: Stop using FS::DirectoryEntry
We were only using this as a temporary helper object while constructing directories. Create a simpler Ext2FSDirectoryEntry instead for this.
This commit is contained in:
parent
108263314a
commit
6ad2d31952
2 changed files with 19 additions and 12 deletions
|
@ -45,6 +45,12 @@ static const size_t max_link_count = 65535;
|
||||||
static const size_t max_block_size = 4096;
|
static const size_t max_block_size = 4096;
|
||||||
static const ssize_t max_inline_symlink_length = 60;
|
static const ssize_t max_inline_symlink_length = 60;
|
||||||
|
|
||||||
|
struct Ext2FSDirectoryEntry {
|
||||||
|
String name;
|
||||||
|
InodeIdentifier inode;
|
||||||
|
u8 file_type { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
static u8 to_ext2_file_type(mode_t mode)
|
static u8 to_ext2_file_type(mode_t mode)
|
||||||
{
|
{
|
||||||
if (is_regular_file(mode))
|
if (is_regular_file(mode))
|
||||||
|
@ -880,13 +886,13 @@ KResult Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntr
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Ext2FSInode::write_directory(const Vector<FS::DirectoryEntry>& entries)
|
bool Ext2FSInode::write_directory(const Vector<Ext2FSDirectoryEntry>& entries)
|
||||||
{
|
{
|
||||||
LOCKER(m_lock);
|
LOCKER(m_lock);
|
||||||
|
|
||||||
int directory_size = 0;
|
int directory_size = 0;
|
||||||
for (auto& entry : entries)
|
for (auto& entry : entries)
|
||||||
directory_size += EXT2_DIR_REC_LEN(entry.name_length);
|
directory_size += EXT2_DIR_REC_LEN(entry.name.length());
|
||||||
|
|
||||||
auto block_size = fs().block_size();
|
auto block_size = fs().block_size();
|
||||||
|
|
||||||
|
@ -903,7 +909,7 @@ bool Ext2FSInode::write_directory(const Vector<FS::DirectoryEntry>& entries)
|
||||||
for (size_t i = 0; i < entries.size(); ++i) {
|
for (size_t i = 0; i < entries.size(); ++i) {
|
||||||
auto& entry = entries[i];
|
auto& entry = entries[i];
|
||||||
|
|
||||||
int record_length = EXT2_DIR_REC_LEN(entry.name_length);
|
int record_length = EXT2_DIR_REC_LEN(entry.name.length());
|
||||||
if (i == entries.size() - 1)
|
if (i == entries.size() - 1)
|
||||||
record_length += occupied_size - directory_size;
|
record_length += occupied_size - directory_size;
|
||||||
|
|
||||||
|
@ -917,11 +923,11 @@ bool Ext2FSInode::write_directory(const Vector<FS::DirectoryEntry>& entries)
|
||||||
|
|
||||||
stream << u32(entry.inode.index());
|
stream << u32(entry.inode.index());
|
||||||
stream << u16(record_length);
|
stream << u16(record_length);
|
||||||
stream << u8(entry.name_length);
|
stream << u8(entry.name.length());
|
||||||
stream << u8(entry.file_type);
|
stream << u8(entry.file_type);
|
||||||
stream << entry.name;
|
stream << entry.name;
|
||||||
|
|
||||||
int padding = record_length - entry.name_length - 8;
|
int padding = record_length - entry.name.length() - 8;
|
||||||
for (int j = 0; j < padding; ++j)
|
for (int j = 0; j < padding; ++j)
|
||||||
stream << u8(0);
|
stream << u8(0);
|
||||||
}
|
}
|
||||||
|
@ -954,14 +960,14 @@ KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode
|
||||||
dbg() << "Ext2FSInode::add_child(): Adding inode " << child.index() << " with name '" << name << "' and mode " << mode << " to directory " << index();
|
dbg() << "Ext2FSInode::add_child(): Adding inode " << child.index() << " with name '" << name << "' and mode " << mode << " to directory " << index();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Vector<FS::DirectoryEntry> entries;
|
Vector<Ext2FSDirectoryEntry> entries;
|
||||||
bool name_already_exists = false;
|
bool name_already_exists = false;
|
||||||
KResult result = traverse_as_directory([&](auto& entry) {
|
KResult result = traverse_as_directory([&](auto& entry) {
|
||||||
if (name == entry.name) {
|
if (name == entry.name) {
|
||||||
name_already_exists = true;
|
name_already_exists = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
entries.append({ entry.name.characters_without_null_termination(), entry.name.length(), entry.inode, entry.file_type });
|
entries.append({ entry.name, entry.inode, entry.file_type });
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -977,7 +983,7 @@ KResult Ext2FSInode::add_child(Inode& child, const StringView& name, mode_t mode
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
entries.empend(name.characters_without_null_termination(), name.length(), child.identifier(), to_ext2_file_type(mode));
|
entries.empend(name, child.identifier(), to_ext2_file_type(mode));
|
||||||
bool success = write_directory(entries);
|
bool success = write_directory(entries);
|
||||||
if (success)
|
if (success)
|
||||||
m_lookup_cache.set(name, child.index());
|
m_lookup_cache.set(name, child.index());
|
||||||
|
@ -1005,10 +1011,10 @@ KResult Ext2FSInode::remove_child(const StringView& name)
|
||||||
dbg() << "Ext2FSInode::remove_child(): Removing '" << name << "' in directory " << index();
|
dbg() << "Ext2FSInode::remove_child(): Removing '" << name << "' in directory " << index();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Vector<FS::DirectoryEntry> entries;
|
Vector<Ext2FSDirectoryEntry> entries;
|
||||||
KResult result = traverse_as_directory([&](auto& entry) {
|
KResult result = traverse_as_directory([&](auto& entry) {
|
||||||
if (name != entry.name)
|
if (name != entry.name)
|
||||||
entries.append({ entry.name.characters_without_null_termination(), entry.name.length(), entry.inode, entry.file_type });
|
entries.append({ entry.name, entry.inode, entry.file_type });
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
|
@ -1364,7 +1370,7 @@ KResult Ext2FS::create_directory(InodeIdentifier parent_id, const String& name,
|
||||||
dbg() << "Ext2FS: create_directory: created new directory named '" << name << "' with inode " << inode->identifier();
|
dbg() << "Ext2FS: create_directory: created new directory named '" << name << "' with inode " << inode->identifier();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Vector<DirectoryEntry> entries;
|
Vector<Ext2FSDirectoryEntry> entries;
|
||||||
entries.empend(".", inode->identifier(), EXT2_FT_DIR);
|
entries.empend(".", inode->identifier(), EXT2_FT_DIR);
|
||||||
entries.empend("..", parent_id, EXT2_FT_DIR);
|
entries.empend("..", parent_id, EXT2_FT_DIR);
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ struct ext2_super_block;
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
class Ext2FS;
|
class Ext2FS;
|
||||||
|
struct Ext2FSDirectoryEntry;
|
||||||
|
|
||||||
class Ext2FSInode final : public Inode {
|
class Ext2FSInode final : public Inode {
|
||||||
friend class Ext2FS;
|
friend class Ext2FS;
|
||||||
|
@ -76,7 +77,7 @@ private:
|
||||||
virtual KResult chown(uid_t, gid_t) override;
|
virtual KResult chown(uid_t, gid_t) override;
|
||||||
virtual KResult truncate(u64) override;
|
virtual KResult truncate(u64) override;
|
||||||
|
|
||||||
bool write_directory(const Vector<FS::DirectoryEntry>&);
|
bool write_directory(const Vector<Ext2FSDirectoryEntry>&);
|
||||||
void populate_lookup_cache() const;
|
void populate_lookup_cache() const;
|
||||||
KResult resize(u64);
|
KResult resize(u64);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue