Make streams and vfs and http shared_ptr

This commit is contained in:
2025-09-29 02:22:27 -05:00
parent 71d0e36a5c
commit d785508571
61 changed files with 541 additions and 951 deletions

View File

@@ -8,6 +8,7 @@
#undef min
#else
#include <utime.h>
#include <sys/statvfs.h>
#endif
namespace Tesses::Framework::Filesystem
{
@@ -71,9 +72,9 @@ namespace Tesses::Framework::Filesystem
auto res = std::filesystem::read_symlink(this->VFSPathToSystem(path)).string();
return this->SystemToVFSPath(res.c_str());
}
Tesses::Framework::Streams::Stream* LocalFilesystem::OpenFile(VFSPath path, std::string mode)
std::shared_ptr<Tesses::Framework::Streams::Stream> LocalFilesystem::OpenFile(VFSPath path, std::string mode)
{
return new Tesses::Framework::Streams::FileStream(VFSPathToSystem(path), mode);
return std::make_shared<Tesses::Framework::Streams::FileStream>(VFSPathToSystem(path), mode);
}
void LocalFilesystem::DeleteDirectory(VFSPath path)
@@ -191,7 +192,45 @@ namespace Tesses::Framework::Filesystem
delete dir;
});
}
LocalFilesystem LocalFS;
bool LocalFilesystem::StatVFS(VFSPath path, StatVFSData& data)
{
auto pathStr = this->VFSPathToSystem(path);
#if defined(_WIN32)
//not supporting windows yet
VFS::StatVFS(path, data);
return true;
#else
struct statvfs vfs;
if(statvfs(pathStr.c_str(), &vfs) == 0)
{
data.BlockSize = vfs.f_bsize;
data.FragmentSize = vfs.f_frsize;
data.Blocks = vfs.f_blocks;
data.BlocksFree = vfs.f_bfree;
data.BlocksAvailable = vfs.f_bavail;
data.TotalInodes = vfs.f_files;
data.FreeInodes = vfs.f_ffree;
data.AvailableInodes = vfs.f_favail;
data.Id = vfs.f_fsid;
data.Flags = vfs.f_flag;
data.MaxNameLength = vfs.f_namemax;
return true;
}
return false;
#endif
}
void LocalFilesystem::Chmod(VFSPath path, uint32_t mode)
{
auto pathStr = this->VFSPathToSystem(path);
#if defined(_WIN32)
#else
chmod(pathStr.c_str(), (mode_t)mode);
#endif
}
std::shared_ptr<LocalFilesystem> LocalFS;
}
// C:/Users/Jim/Joel

View File

@@ -116,7 +116,7 @@ namespace Tesses::Framework::Filesystem
return nullptr;
}
Streams::Stream* MemoryFilesystem::OpenFile(VFSPath path, std::string mode)
std::shared_ptr<Streams::Stream> MemoryFilesystem::OpenFile(VFSPath path, std::string mode)
{
bool canRead=false;
bool canWrite=false;
@@ -181,7 +181,7 @@ namespace Tesses::Framework::Filesystem
if(canWrite) f->data->canAccess=false;
mtx->Unlock();
return new MemoryFilesystemStream(mtx,f->data,canRead,canWrite,canSeek);
return std::make_shared<MemoryFilesystemStream>(mtx,f->data,canRead,canWrite,canSeek);
}
else
{
@@ -204,7 +204,7 @@ namespace Tesses::Framework::Filesystem
file->data->readers++;
if(truncate) {file->data->file.clear(); file->data->lastWrite=Date::DateTime::NowUTC();}
mtx->Unlock();
return new MemoryFilesystemStream(mtx,file->data,canRead,canWrite,canSeek);
return std::make_shared<MemoryFilesystemStream>(mtx,file->data,canRead,canWrite,canSeek);
}
@@ -287,7 +287,7 @@ namespace Tesses::Framework::Filesystem
myDir->entries.push_back(f);
mtx->Unlock();
return new MemoryFilesystemStream(mtx,f->data,canRead,canWrite,canSeek);
return std::make_shared<MemoryFilesystemStream>(mtx,f->data,canRead,canWrite,canSeek);
}
if(thefile != nullptr)
{
@@ -305,7 +305,7 @@ namespace Tesses::Framework::Filesystem
thefile->data->readers++;
if(truncate) {thefile->data->file.clear(); thefile->data->lastWrite=Date::DateTime::NowUTC();}
mtx->Unlock();
return new MemoryFilesystemStream(mtx,thefile->data,canRead,canWrite,canSeek);
return std::make_shared<MemoryFilesystemStream>(mtx,thefile->data,canRead,canWrite,canSeek);
}
}

View File

@@ -3,29 +3,26 @@
#include <iostream>
namespace Tesses::Framework::Filesystem
{
MountableFilesystem::MountableFilesystem() : MountableFilesystem(new NullFilesystem(),true)
MountableFilesystem::MountableFilesystem() : MountableFilesystem(std::make_shared<NullFilesystem>())
{
}
MountableFilesystem::MountableFilesystem(VFS* root, bool owns)
MountableFilesystem::MountableFilesystem(std::shared_ptr<VFS> root)
{
this->root = root;
this->owns = owns;
}
MountableDirectory::~MountableDirectory()
{
if(this->owns && this->vfs) delete this->vfs;
for(auto dir : this->dirs) delete dir;
}
MountableFilesystem::~MountableFilesystem()
{
if(this->owns && this->root) delete root;
for(auto item : this->directories) delete item;
}
void MountableFilesystem::GetFS(VFSPath srcPath, VFSPath& destRoot, VFSPath& destPath, VFS*& vfs)
void MountableFilesystem::GetFS(VFSPath srcPath, VFSPath& destRoot, VFSPath& destPath, std::shared_ptr<VFS>& vfs)
{
if(srcPath.path.empty()) return;
for(auto item : this->directories)
@@ -52,7 +49,7 @@ namespace Tesses::Framework::Filesystem
void MountableDirectory::GetFS(VFSPath srcPath, VFSPath curDir, VFSPath& destRoot, VFSPath& destPath, VFS*& vfs)
void MountableDirectory::GetFS(VFSPath srcPath, VFSPath curDir, VFSPath& destRoot, VFSPath& destPath, std::shared_ptr<VFS>& vfs)
{
if(srcPath.path.empty()) return;
for(auto item : this->dirs)
@@ -83,7 +80,7 @@ namespace Tesses::Framework::Filesystem
path = path.CollapseRelativeParents();
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
if(vfs != nullptr)
@@ -91,12 +88,37 @@ namespace Tesses::Framework::Filesystem
return VFSPath();
}
Tesses::Framework::Streams::Stream* MountableFilesystem::OpenFile(VFSPath path, std::string mode)
bool MountableFilesystem::StatVFS(VFSPath path, StatVFSData& data)
{
path = path.CollapseRelativeParents();
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
if(vfs != nullptr)
return vfs->StatVFS(destPath,data);
return false;
}
void MountableFilesystem::Chmod(VFSPath path, uint32_t mode)
{
path = path.CollapseRelativeParents();
VFSPath destRoot;
VFSPath destPath = path;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
if(vfs != nullptr)
vfs->Chmod(destPath,mode);
}
std::shared_ptr<Tesses::Framework::Streams::Stream> MountableFilesystem::OpenFile(VFSPath path, std::string mode)
{
path = path.CollapseRelativeParents();
VFSPath destRoot;
VFSPath destPath = path;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -110,7 +132,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -128,7 +150,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -145,7 +167,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -160,7 +182,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -174,7 +196,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -189,7 +211,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -203,7 +225,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -217,7 +239,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -232,7 +254,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -248,7 +270,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -265,7 +287,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
if(destPath.path.empty()) return true;
@@ -280,7 +302,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -295,7 +317,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -309,7 +331,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -323,10 +345,10 @@ namespace Tesses::Framework::Filesystem
VFSPath existingDestRoot;
VFSPath existingDestPath = existingFile;
VFS* existingVFS = root;
std::shared_ptr<VFS> existingVFS = root;
VFSPath symlinkDestRoot;
VFSPath symlinkDestPath = symlinkFile;
VFS* symlinkVFS = root;
std::shared_ptr<VFS> symlinkVFS = root;
GetFS(existingFile, existingDestRoot, existingDestPath, existingVFS);
GetFS(symlinkFile, symlinkDestRoot, symlinkDestPath, symlinkVFS);
@@ -342,10 +364,10 @@ namespace Tesses::Framework::Filesystem
VFSPath srcDestRoot;
VFSPath srcDestPath = src;
VFS* srcVFS = root;
std::shared_ptr<VFS> srcVFS = root;
VFSPath destDestRoot;
VFSPath destDestPath = dest;
VFS* destVFS = root;
std::shared_ptr<VFS> destVFS = root;
GetFS(src, srcDestRoot, srcDestPath, srcVFS);
GetFS(dest, destDestRoot, destDestPath, destVFS);
@@ -360,10 +382,10 @@ namespace Tesses::Framework::Filesystem
VFSPath srcDestRoot;
VFSPath srcDestPath = src;
VFS* srcVFS = root;
std::shared_ptr<VFS> srcVFS = root;
VFSPath destDestRoot;
VFSPath destDestPath = dest;
VFS* destVFS = root;
std::shared_ptr<VFS> destVFS = root;
GetFS(src, srcDestRoot, srcDestPath, srcVFS);
GetFS(dest, destDestRoot, destDestPath, destVFS);
@@ -378,10 +400,10 @@ namespace Tesses::Framework::Filesystem
VFSPath existingDestRoot;
VFSPath existingDestPath = existingFile;
VFS* existingVFS = root;
std::shared_ptr<VFS> existingVFS = root;
VFSPath newNameRoot;
VFSPath newNamePath = newName;
VFS* newNameVFS = root;
std::shared_ptr<VFS> newNameVFS = root;
GetFS(existingFile, existingDestRoot, existingDestPath, existingVFS);
GetFS(newName, newNameRoot, newNamePath, newNameVFS);
@@ -427,7 +449,7 @@ namespace Tesses::Framework::Filesystem
VFSPath destRoot;
VFSPath destPath = path;
VFS* vfs = root;
std::shared_ptr<VFS> vfs = root;
GetFS(path, destRoot, destPath, vfs);
@@ -476,13 +498,12 @@ namespace Tesses::Framework::Filesystem
});
}
void MountableFilesystem::Mount(VFSPath path, VFS* fs, bool owns)
void MountableFilesystem::Mount(VFSPath path, std::shared_ptr<VFS> fs)
{
path = path.CollapseRelativeParents();
if(path.path.empty())
{
if(owns) delete fs;
return;
}
@@ -520,7 +541,7 @@ namespace Tesses::Framework::Filesystem
if(item->name == lastDir)
{
needToCreate=false;
if(item->owns && item->vfs) delete item->vfs;
item->vfs = fs;
break;
}
@@ -530,7 +551,7 @@ namespace Tesses::Framework::Filesystem
{
MountableDirectory* dir = new MountableDirectory();
dir->name = lastDir;
dir->owns=owns;
dir->vfs=fs;
fsLs->push_back(dir);
}
@@ -541,7 +562,6 @@ namespace Tesses::Framework::Filesystem
{
if(path.path.empty())
{
if(dir->owns && dir->vfs) delete dir->vfs;
dir->vfs = nullptr;
}

View File

@@ -2,7 +2,7 @@
namespace Tesses::Framework::Filesystem
{
Tesses::Framework::Streams::Stream* NullFilesystem::OpenFile(VFSPath path, std::string mode)
std::shared_ptr<Tesses::Framework::Streams::Stream> NullFilesystem::OpenFile(VFSPath path, std::string mode)
{
return nullptr;
}

View File

@@ -29,10 +29,10 @@ namespace Tesses::Framework::Filesystem
{
return this->path / path.CollapseRelativeParents();
}
SubdirFilesystem::SubdirFilesystem(VFS* parent, VFSPath path, bool owns)
SubdirFilesystem::SubdirFilesystem(std::shared_ptr<VFS> parent, VFSPath path)
{
this->parent = parent;
if(path.relative && dynamic_cast<LocalFilesystem*>(parent) != nullptr)
if(path.relative && std::dynamic_pointer_cast<LocalFilesystem>(parent) != nullptr)
{
Tesses::Framework::Filesystem::LocalFilesystem lfs;
auto curDir = std::filesystem::current_path();
@@ -41,9 +41,9 @@ namespace Tesses::Framework::Filesystem
}
else
this->path = path;
this->owns=owns;
}
Tesses::Framework::Streams::Stream* SubdirFilesystem::OpenFile(VFSPath path, std::string mode)
std::shared_ptr<Tesses::Framework::Streams::Stream> SubdirFilesystem::OpenFile(VFSPath path, std::string mode)
{
return this->parent->OpenFile(ToParent(path),mode);
}
@@ -151,7 +151,16 @@ namespace Tesses::Framework::Filesystem
SubdirFilesystem::~SubdirFilesystem()
{
if(this->owns)
delete this->parent;
}
bool SubdirFilesystem::StatVFS(VFSPath path, StatVFSData& vfsData)
{
return this->parent->StatVFS(path, vfsData);
}
void SubdirFilesystem::Chmod(VFSPath path, uint32_t mode)
{
return this->parent->Chmod(path, mode);
}
}

View File

@@ -194,11 +194,11 @@ namespace Tesses::Framework::Filesystem
VFSPath VFSPath::GetAbsoluteCurrentDirectory()
{
auto p = std::filesystem::current_path();
return LocalFS.SystemToVFSPath(p.string());
return LocalFS->SystemToVFSPath(p.string());
}
void VFSPath::SetAbsoluteCurrentDirectory(VFSPath path)
{
auto res = LocalFS.VFSPathToSystem(path);
auto res = LocalFS->VFSPathToSystem(path);
std::filesystem::path mpath=res;
std::filesystem::current_path(mpath);
}
@@ -499,5 +499,23 @@ namespace Tesses::Framework::Filesystem
{
}
bool VFS::StatVFS(VFSPath path, StatVFSData& data)
{
data.BlockSize = 512;
data.Blocks=10000000000000;
data.BlocksFree=10000000000000;
data.BlocksAvailable=10000000000000;
data.AvailableInodes=10000000000000;
data.TotalInodes=10000000000000;
data.FreeInodes=10000000000000;
data.FragmentSize=512;
data.Id = 85138;
data.Flags = 0;
data.MaxNameLength=255;
return true;
}
void VFS::Chmod(VFSPath path, uint32_t mode) {
}
}