Some fs/ cleanup.

This commit is contained in:
Ben Vanik 2014-08-16 21:36:01 -07:00
parent fdab788017
commit 383d3acbb0
11 changed files with 301 additions and 337 deletions

View File

@ -9,10 +9,14 @@
#include <xenia/kernel/fs/device.h>
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::fs;
namespace xe {
namespace kernel {
namespace fs {
Device::Device(const std::string& path) : path_(path) {}
Device::~Device() = default;
} // namespace fs
} // namespace kernel
} // namespace xe

View File

@ -12,9 +12,7 @@
#include <string>
#include <xenia/common.h>
#include <xenia/core.h>
#include <xenia/kernel/fs/entry.h>
namespace xe {

View File

@ -65,7 +65,7 @@ X_STATUS STFSContainerEntry::QueryDirectory(
auto end = (uint8_t*)out_info + length;
auto entry = *stfs_entry_iterator_;
auto entry = stfs_entry_iterator_->get();
auto entry_name = entry->name;
if (((uint8_t*)&out_info->file_name[0]) + entry_name.size() > end) {

View File

@ -46,7 +46,7 @@ public:
private:
xe_mmap_ref mmap_;
STFSEntry* stfs_entry_;
std::vector<STFSEntry*>::iterator stfs_entry_iterator_;
std::vector<std::unique_ptr<STFSEntry>>::iterator stfs_entry_iterator_;
};

View File

@ -10,9 +10,9 @@
#include <xenia/kernel/fs/entry.h>
#include <xenia/kernel/fs/device.h>
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::fs;
namespace xe {
namespace kernel {
namespace fs {
MemoryMapping::MemoryMapping(uint8_t* address, size_t length)
: address_(address), length_(length) {}
@ -28,3 +28,7 @@ Entry::Entry(Type type, Device* device, const std::string& path)
}
Entry::~Entry() = default;
} // namespace fs
} // namespace kernel
} // namespace xe

View File

@ -12,19 +12,19 @@
#include <string>
#include <xenia/common.h>
#include <xenia/core.h>
#include <xenia/xbox.h>
XEDECLARECLASS2(xe, kernel, KernelState);
XEDECLARECLASS2(xe, kernel, XFile);
XEDECLARECLASS2(xe, kernel, XFileInfo);
XEDECLARECLASS2(xe, kernel, XDirectoryInfo);
XEDECLARECLASS2(xe, kernel, XVolumeInfo);
XEDECLARECLASS2(xe, kernel, XFileSystemAttributeInfo);
namespace xe {
namespace kernel {
class KernelState;
class XFile;
class XFileInfo;
class XFileSystemAttributeInfo;
class XDirectoryInfo;
class XVolumeInfo;
} // namespace kernel
} // namespace xe
namespace xe {
namespace kernel {
@ -32,7 +32,6 @@ namespace fs {
class Device;
class MemoryMapping {
public:
MemoryMapping(uint8_t* address, size_t length);
@ -46,7 +45,6 @@ private:
size_t length_;
};
class Entry {
public:
enum Type {
@ -64,19 +62,18 @@ public:
const std::string& name() const { return name_; }
virtual X_STATUS QueryInfo(XFileInfo* out_info) = 0;
virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info,
size_t length, const char* file_name, bool restart) = 0;
virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, size_t length,
const char* file_name, bool restart) = 0;
virtual bool can_map() { return false; }
virtual MemoryMapping* CreateMemoryMapping(
xe_file_mode file_mode, const size_t offset, const size_t length) {
virtual MemoryMapping* CreateMemoryMapping(xe_file_mode file_mode,
const size_t offset,
const size_t length) {
return NULL;
}
virtual X_STATUS Open(
KernelState* kernel_state,
uint32_t desired_access, bool async,
XFile** out_file) = 0;
virtual X_STATUS Open(KernelState* kernel_state, uint32_t desired_access,
bool async, XFile** out_file) = 0;
private:
Type type_;
@ -86,10 +83,8 @@ private:
std::string name_;
};
} // namespace fs
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_FS_ENTRY_H_

View File

@ -14,14 +14,11 @@
#include <xenia/kernel/fs/devices/host_path_device.h>
#include <xenia/kernel/fs/devices/stfs_container_device.h>
namespace xe {
namespace kernel {
namespace fs {
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::fs;
FileSystem::FileSystem() {
}
FileSystem::FileSystem() {}
FileSystem::~FileSystem() {
// Delete all devices.
@ -106,14 +103,14 @@ int FileSystem::RegisterDevice(const std::string& path, Device* device) {
return 0;
}
int FileSystem::RegisterHostPathDevice(
const std::string& path, const std::wstring& local_path) {
int FileSystem::RegisterHostPathDevice(const std::string& path,
const std::wstring& local_path) {
Device* device = new HostPathDevice(path, local_path);
return RegisterDevice(path, device);
}
int FileSystem::RegisterDiscImageDevice(
const std::string& path, const std::wstring& local_path) {
int FileSystem::RegisterDiscImageDevice(const std::string& path,
const std::wstring& local_path) {
DiscImageDevice* device = new DiscImageDevice(path, local_path);
if (device->Init()) {
return 1;
@ -121,8 +118,8 @@ int FileSystem::RegisterDiscImageDevice(
return RegisterDevice(path, device);
}
int FileSystem::RegisterSTFSContainerDevice(
const std::string& path, const std::wstring& local_path) {
int FileSystem::RegisterSTFSContainerDevice(const std::string& path,
const std::wstring& local_path) {
STFSContainerDevice* device = new STFSContainerDevice(path, local_path);
if (device->Init()) {
return 1;
@ -178,3 +175,7 @@ Entry* FileSystem::ResolvePath(const std::string& path) {
XELOGE("ResolvePath(%s) failed - no root found", path.c_str());
return NULL;
}
} // namespace fs
} // namespace kernel
} // namespace xe

View File

@ -13,21 +13,14 @@
#include <poly/math.h>
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::fs;
namespace xe {
namespace kernel {
namespace fs {
const size_t kXESectorSize = 2048;
namespace {
#define kXESectorSize 2048
}
GDFXEntry::GDFXEntry() :
attributes(X_FILE_ATTRIBUTE_NONE), offset(0), size(0) {
}
GDFXEntry::GDFXEntry()
: attributes(X_FILE_ATTRIBUTE_NONE), offset(0), size(0) {}
GDFXEntry::~GDFXEntry() {
for (std::vector<GDFXEntry*>::iterator it = children.begin();
@ -57,7 +50,6 @@ void GDFXEntry::Dump(int indent) {
}
}
GDFX::GDFX(xe_mmap_ref mmap) {
mmap_ = xe_mmap_retain(mmap);
@ -70,30 +62,28 @@ GDFX::~GDFX() {
xe_mmap_release(mmap_);
}
GDFXEntry* GDFX::root_entry() {
return root_entry_;
}
GDFXEntry* GDFX::root_entry() { return root_entry_; }
GDFX::Error GDFX::Load() {
Error result = kErrorOutOfMemory;
ParseState state;
xe_zero_struct(&state, sizeof(state));
state.ptr = (uint8_t*)xe_mmap_get_addr(mmap_);
state.size = xe_mmap_get_length(mmap_);
result = Verify(state);
XEEXPECTZERO(result);
auto result = Verify(state);
if (result != kSuccess) {
return result;
}
result = ReadAllEntries(state, state.ptr + state.root_offset);
XEEXPECTZERO(result);
result = kSuccess;
XECLEANUP:
if (result != kSuccess) {
return result;
}
return kSuccess;
}
void GDFX::Dump() {
if (root_entry_) {
root_entry_->Dump(0);
@ -126,8 +116,7 @@ GDFX::Error GDFX::Verify(ParseState& state) {
state.root_sector = poly::load<uint32_t>(fs_ptr + 20);
state.root_size = poly::load<uint32_t>(fs_ptr + 24);
state.root_offset = state.game_offset + (state.root_sector * kXESectorSize);
if (state.root_size < 13 ||
state.root_size > 32 * 1024 * 1024) {
if (state.root_size < 13 || state.root_size > 32 * 1024 * 1024) {
return kErrorDamagedFile;
}
@ -208,3 +197,7 @@ bool GDFX::ReadEntry(ParseState& state, const uint8_t* buffer,
return true;
}
} // namespace fs
} // namespace kernel
} // namespace xe

View File

@ -10,23 +10,18 @@
#ifndef XENIA_KERNEL_FS_GDFX_H_
#define XENIA_KERNEL_FS_GDFX_H_
#include <xenia/common.h>
#include <xenia/core.h>
#include <vector>
#include <xenia/core.h>
#include <xenia/xbox.h>
#include <xenia/kernel/fs/entry.h>
namespace xe {
namespace kernel {
namespace fs {
class GDFX;
class GDFXEntry {
public:
GDFXEntry();
@ -44,7 +39,6 @@ public:
std::vector<GDFXEntry*> children;
};
class GDFX {
public:
enum Error {
@ -67,13 +61,18 @@ private:
typedef struct {
uint8_t* ptr;
size_t size; // Size (bytes) of total image
// Size (bytes) of total image.
size_t size;
size_t game_offset; // Offset (bytes) of game partition
// Offset (bytes) of game partition.
size_t game_offset;
size_t root_sector; // Offset (sector) of root
size_t root_offset; // Offset (bytes) of root
size_t root_size; // Size (bytes) of root
// Offset (sector) of root.
size_t root_sector;
// Offset (bytes) of root.
size_t root_offset;
// Size (bytes) of root.
size_t root_size;
} ParseState;
Error Verify(ParseState& state);
@ -87,10 +86,8 @@ private:
GDFXEntry* root_entry_;
};
} // namespace fs
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_FS_GDFX_H_

View File

@ -13,9 +13,9 @@
#include <algorithm>
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::fs;
namespace xe {
namespace kernel {
namespace fs {
#define XEGETUINT24BE(p) \
(((uint32_t)poly::load_and_swap<uint8_t>((p)+0) << 16) | \
@ -43,7 +43,6 @@ bool STFSVolumeDescriptor::Read(const uint8_t* p) {
return true;
};
bool STFSHeader::Read(const uint8_t* p) {
xe_copy_struct(license_entries, p + 0x22C, 0x100);
xe_copy_struct(header_hash, p + 0x32C, 0x14);
@ -94,70 +93,55 @@ bool STFSHeader::Read(const uint8_t* p) {
return true;
}
STFSEntry::STFSEntry() :
attributes(X_FILE_ATTRIBUTE_NONE), offset(0), size(0),
update_timestamp(0), access_timestamp(0) {
}
STFSEntry::~STFSEntry() {
for (auto it = children.begin(); it != children.end(); ++it) {
delete *it;
}
}
STFSEntry::STFSEntry()
: attributes(X_FILE_ATTRIBUTE_NONE),
offset(0),
size(0),
update_timestamp(0),
access_timestamp(0) {}
STFSEntry* STFSEntry::GetChild(const char* name) {
// TODO(benvanik): a faster search
for (auto it = children.begin(); it != children.end(); ++it) {
STFSEntry* entry = *it;
for (const auto& entry : children) {
if (strcasecmp(entry->name.c_str(), name) == 0) {
return entry;
return entry.get();
}
}
return NULL;
return nullptr;
}
void STFSEntry::Dump(int indent) {
printf("%s%s\n", std::string(indent, ' ').c_str(), name.c_str());
for (auto it = children.begin(); it != children.end(); ++it) {
STFSEntry* entry = *it;
for (const auto& entry : children) {
entry->Dump(indent + 2);
}
}
STFS::STFS(xe_mmap_ref mmap) {
mmap_ = xe_mmap_retain(mmap);
root_entry_ = NULL;
}
STFS::~STFS() {
delete root_entry_;
xe_mmap_release(mmap_);
}
STFSEntry* STFS::root_entry() {
return root_entry_;
}
STFS::Error STFS::Load() {
Error result = kErrorOutOfMemory;
uint8_t* map_ptr = (uint8_t*)xe_mmap_get_addr(mmap_);
size_t map_size = xe_mmap_get_length(mmap_);
result = ReadHeaderAndVerify(map_ptr);
XEEXPECTZERO(result);
auto result = ReadHeaderAndVerify(map_ptr);
if (result != kSuccess) {
return result;
}
result = ReadAllEntries(map_ptr);
XEEXPECTZERO(result);
result = kSuccess;
XECLEANUP:
if (result != kSuccess) {
return result;
}
return kSuccess;
}
void STFS::Dump() {
if (root_entry_) {
root_entry_->Dump(0);
@ -192,10 +176,7 @@ STFS::Error STFS::ReadHeaderAndVerify(const uint8_t* map_ptr) {
}
STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
root_entry_ = new STFSEntry();
root_entry_->offset = 0;
root_entry_->size = 0;
root_entry_->name = "";
root_entry_.reset(new STFSEntry());
root_entry_->attributes = X_FILE_ATTRIBUTE_DIRECTORY;
std::vector<STFSEntry*> entries;
@ -221,7 +202,7 @@ STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
uint32_t access_timestamp = poly::load_and_swap<uint32_t>(p + 0x3C);
p += 0x40;
STFSEntry* entry = new STFSEntry();
auto entry = std::make_unique<STFSEntry>();
entry->name = std::string((char*)filename, filename_length_flags & 0x3F);
entry->name.append(1, '\0');
// bit 0x40 = consecutive blocks (not fragmented?)
@ -234,18 +215,7 @@ STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
}
entry->update_timestamp = update_timestamp;
entry->access_timestamp = access_timestamp;
entries.push_back(entry);
const uint8_t* p = map_ptr + entry->offset;
if (path_indicator == 0xFFFF) {
// Root entry.
root_entry_->children.push_back(entry);
} else {
// Lookup and add.
auto parent = entries[path_indicator];
parent->children.push_back(entry);
}
entries.push_back(entry.get());
// Fill in all block records.
// It's easier to do this now and just look them up later, at the cost
@ -255,9 +225,7 @@ STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
uint32_t block_index = start_block_index;
size_t remaining_size = file_size;
uint32_t info = 0x80;
while (remaining_size &&
block_index &&
info >= 0x80) {
while (remaining_size && block_index && info >= 0x80) {
size_t block_size = std::min(0x1000ull, remaining_size);
size_t offset = BlockToOffset(ComputeBlockNumber(block_index));
entry->block_list.push_back({offset, block_size});
@ -270,6 +238,15 @@ STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
info = block_hash.info;
}
}
if (path_indicator == 0xFFFF) {
// Root entry.
root_entry_->children.push_back(std::move(entry));
} else {
// Lookup and add.
auto parent = entries[path_indicator];
parent->children.push_back(std::move(entry));
}
}
auto block_hash = GetBlockHash(map_ptr, table_block_index, 0);
@ -327,12 +304,14 @@ uint32_t STFS::ComputeBlockNumber(uint32_t block_index) {
return block;
}
STFS::BlockHash_t STFS::GetBlockHash(
const uint8_t* map_ptr,
uint32_t block_index, uint32_t table_offset) {
STFS::BlockHash_t STFS::GetBlockHash(const uint8_t* map_ptr,
uint32_t block_index,
uint32_t table_offset) {
static const uint32_t table_spacing[] = {
0xAB, 0x718F, 0xFE7DA, // The distance in blocks between tables
0xAC, 0x723A, 0xFD00B, // For when tables are 1 block and when they are 2 blocks
0xAB, 0x718F,
0xFE7DA, // The distance in blocks between tables
0xAC, 0x723A,
0xFD00B, // For when tables are 1 block and when they are 2 blocks
};
uint32_t record = block_index % 0xAA;
uint32_t table_index =
@ -350,3 +329,7 @@ STFS::BlockHash_t STFS::GetBlockHash(
uint32_t next_block_index = XEGETUINT24BE(record_data + 0x15);
return {next_block_index, info};
}
} // namespace fs
} // namespace kernel
} // namespace xe

View File

@ -10,23 +10,19 @@
#ifndef XENIA_KERNEL_FS_STFS_H_
#define XENIA_KERNEL_FS_STFS_H_
#include <xenia/common.h>
#include <xenia/core.h>
#include <memory>
#include <vector>
#include <xenia/core.h>
#include <xenia/xbox.h>
#include <xenia/kernel/fs/entry.h>
namespace xe {
namespace kernel {
namespace fs {
class STFS;
// http://www.free60.org/STFS
enum STFSPackageType {
@ -79,7 +75,6 @@ enum STFSDescriptorType : uint32_t {
STFS_DESCRIPTOR_SVOD = 1,
};
class STFSVolumeDescriptor {
public:
bool Read(const uint8_t* p);
@ -94,7 +89,6 @@ public:
uint32_t total_unallocated_block_count;
};
class STFSHeader {
public:
bool Read(const uint8_t* p);
@ -132,11 +126,9 @@ public:
uint8_t title_thumbnail_image[0x4000];
};
class STFSEntry {
public:
STFSEntry();
~STFSEntry();
STFSEntry* GetChild(const char* name);
@ -149,7 +141,7 @@ public:
uint32_t update_timestamp;
uint32_t access_timestamp;
std::vector<STFSEntry*> children;
std::vector<std::unique_ptr<STFSEntry>> children;
typedef struct {
size_t offset;
@ -158,7 +150,6 @@ public:
std::vector<BlockRecord_t> block_list;
};
class STFS {
public:
enum Error {
@ -173,7 +164,7 @@ public:
virtual ~STFS();
const STFSHeader* header() const { return &header_; }
STFSEntry* root_entry();
STFSEntry* root_entry() const { return root_entry_.get(); }
Error Load();
void Dump();
@ -188,21 +179,19 @@ private:
uint32_t next_block_index;
uint32_t info;
} BlockHash_t;
BlockHash_t GetBlockHash(const uint8_t* map_ptr,
uint32_t block_index, uint32_t table_offset);
BlockHash_t GetBlockHash(const uint8_t* map_ptr, uint32_t block_index,
uint32_t table_offset);
xe_mmap_ref mmap_;
STFSPackageType package_type_;
STFSHeader header_;
uint32_t table_size_shift_;
STFSEntry* root_entry_;
std::unique_ptr<STFSEntry> root_entry_;
};
} // namespace fs
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_FS_STFS_H_