mirror of
https://onedev.site.tesses.net/crosslang
synced 2026-04-16 08:27:03 +00:00
605 lines
20 KiB
C++
605 lines
20 KiB
C++
#include "CrossLang.hpp"
|
|
|
|
namespace Tesses::CrossLang {
|
|
|
|
TObjectVFS::TObjectVFS(GC* gc, TObject obj)
|
|
{
|
|
this->ls = new GCList(gc);
|
|
this->ls->Add(obj);
|
|
this->obj = obj;
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(obj,dict))
|
|
{
|
|
gc->BarrierBegin();
|
|
if(!dict->HasValue("OpenFile"))
|
|
{
|
|
dict->DeclareFunction(gc,"OpenFile","Open a file",{"path","mode"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
return nullptr;
|
|
});
|
|
}
|
|
if(!dict->HasValue("EnumeratePaths"))
|
|
{
|
|
dict->DeclareFunction(gc,"EnumeratePaths","Enumerate paths",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
return TVFSPathEnumerator::Create(ls,Tesses::Framework::Filesystem::VFSPathEnumerator());
|
|
});
|
|
}
|
|
if(!dict->HasValue("ReadLink"))
|
|
{
|
|
dict->DeclareFunction(gc,"ReadLink","Read a symlink",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
return Tesses::Framework::Filesystem::VFSPath();
|
|
});
|
|
}
|
|
|
|
if(!dict->HasValue("VFSPathToSystem"))
|
|
{
|
|
dict->DeclareFunction(gc,"VFSPathToSystem","Convert path to system",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
Tesses::Framework::Filesystem::VFSPath path;
|
|
if(GetArgumentAsPath(args,0,path))
|
|
{
|
|
return path.ToString();
|
|
}
|
|
return "/";
|
|
});
|
|
}
|
|
if(!dict->HasValue("SystemToVFSPath"))
|
|
{
|
|
dict->DeclareFunction(gc,"SystemToVFSPath","Convert system to path",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
std::string p;
|
|
if(GetArgument(args,0,p))
|
|
{
|
|
return Tesses::Framework::Filesystem::VFSPath(p);
|
|
}
|
|
return Tesses::Framework::Filesystem::VFSPath();
|
|
});
|
|
}
|
|
if(!dict->HasValue("GetDate"))
|
|
{
|
|
dict->DeclareFunction(gc,"GetDate","Get date from file",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
auto dict = TDictionary::Create(ls);
|
|
ls.GetGC()->BarrierBegin();
|
|
dict->SetValue("LastWrite", (int64_t)time(NULL));
|
|
dict->SetValue("LastAccess", (int64_t)time(NULL));
|
|
ls.GetGC()->BarrierEnd();
|
|
return dict;
|
|
});
|
|
}
|
|
if(!dict->HasValue("RegularFileExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"RegularFileExists","Regular file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("SymlinkExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"SymlinkExists","Symlink exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("CharacterDeviceExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"CharacterDeviceExists","Character file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("BlockDeviceExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"BlockDeviceExists","Block file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("SocketFileExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"SocketFileExists","Socket file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if(!dict->HasValue("FIFOFileExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"FIFOFileExists","FIFO file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("FileExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"FileExists","File exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("SpecialFileExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"SpecialFileExists","Special file exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
if(!dict->HasValue("DirectoryExists"))
|
|
{
|
|
dict->DeclareFunction(gc,"DirectoryExists","Directory exists",{"path"}, [](GCList& ls, std::vector<TObject> args)->TObject {
|
|
|
|
return false;
|
|
});
|
|
}
|
|
gc->BarrierEnd();
|
|
}
|
|
|
|
}
|
|
std::shared_ptr<Tesses::Framework::Streams::Stream> TObjectVFS::OpenFile(Tesses::Framework::Filesystem::VFSPath path, std::string mode)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "OpenFile",{path,mode});
|
|
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
|
|
if(GetObject(res,strm))
|
|
{
|
|
return strm;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
void TObjectVFS::CreateDirectory(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "CreateDirectory",{path});
|
|
|
|
}
|
|
|
|
}
|
|
void TObjectVFS::DeleteDirectory(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "DeleteDirectory",{path});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::Lock(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "Lock",{path});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::Unlock(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "Unlock",{path});
|
|
|
|
}
|
|
}
|
|
bool TObjectVFS::RegularFileExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "RegularFileExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool TObjectVFS::SymlinkExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "SymlinkExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::CharacterDeviceExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "CharacterDeviceExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::BlockDeviceExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "BlockDeviceExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::SocketFileExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "SocketFileExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::FIFOFileExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "FIFOFileExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::FileExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "FileExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool TObjectVFS::SpecialFileExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "SpecialFileExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
void TObjectVFS::CreateSymlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath symlinkFile)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "CreateSymlink",{existingFile,symlinkFile});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::CreateHardlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath newName)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "CreateHardlink",{existingFile,newName});
|
|
|
|
}
|
|
}
|
|
bool TObjectVFS::DirectoryExists(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "DirectoryExists",{path});
|
|
bool out;
|
|
if(GetObject(res,out))
|
|
{
|
|
return out;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
void TObjectVFS::DeleteFile(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "DeleteFile",{path});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::DeleteDirectoryRecurse(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "DeleteDirectoryRecurse",{path});
|
|
|
|
}
|
|
}
|
|
Tesses::Framework::Filesystem::VFSPathEnumerator TObjectVFS::EnumeratePaths(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList* ls=new GCList(this->ls->GetGC());
|
|
auto res = dict->CallMethod(*ls, "EnumeratePaths",{path});
|
|
auto enumerator = TEnumerator::CreateFromObject(*ls,res);
|
|
return Tesses::Framework::Filesystem::VFSPathEnumerator([path,enumerator,ls](Tesses::Framework::Filesystem::VFSPath& path0)->bool{
|
|
if(enumerator == nullptr) return false;
|
|
while(enumerator->MoveNext(ls->GetGC()))
|
|
{
|
|
auto res = enumerator->GetCurrent(*ls);
|
|
std::string name;
|
|
Tesses::Framework::Filesystem::VFSPath path1;
|
|
if(GetObject(res,path1))
|
|
{
|
|
path0 = path1;
|
|
return true;
|
|
}
|
|
else if(GetObject(res,name))
|
|
{
|
|
path0 = path / name;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
},[ls]()->void{
|
|
delete ls;
|
|
});
|
|
}
|
|
return Tesses::Framework::Filesystem::VFSPathEnumerator();
|
|
}
|
|
void TObjectVFS::MoveFile(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "MoveFile",{src,dest});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::MoveDirectory(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "MoveDirectory",{src,dest});
|
|
|
|
}
|
|
}
|
|
Tesses::Framework::Filesystem::VFSPath TObjectVFS::ReadLink(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "ReadLink",{path});
|
|
Tesses::Framework::Filesystem::VFSPath myPath;
|
|
if(GetObject(res,myPath))
|
|
{
|
|
return myPath;
|
|
}
|
|
}
|
|
return Tesses::Framework::Filesystem::VFSPath();
|
|
}
|
|
std::string TObjectVFS::VFSPathToSystem(Tesses::Framework::Filesystem::VFSPath path)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "VFSPathToSystem",{path});
|
|
std::string myPath;
|
|
if(GetObject(res,myPath))
|
|
{
|
|
return myPath;
|
|
}
|
|
}
|
|
return "/";
|
|
}
|
|
Tesses::Framework::Filesystem::VFSPath TObjectVFS::SystemToVFSPath(std::string path)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "SystemToVFSPath",{path});
|
|
Tesses::Framework::Filesystem::VFSPath myPath;
|
|
if(GetObject(res,myPath))
|
|
{
|
|
return myPath;
|
|
}
|
|
}
|
|
return Tesses::Framework::Filesystem::VFSPath();
|
|
}
|
|
void TObjectVFS::GetDate(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Date::DateTime& lastWrite, Tesses::Framework::Date::DateTime& lastAccess)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "GetDate",{path});
|
|
if(GetObjectHeap(res,dict))
|
|
{
|
|
this->ls->GetGC()->BarrierBegin();
|
|
res = dict->GetValue("LastWrite");
|
|
std::shared_ptr<Tesses::Framework::Date::DateTime> d;
|
|
if(GetObject(res,d))
|
|
lastWrite =*d;
|
|
|
|
res = dict->GetValue("LastAccess");
|
|
|
|
if(GetObject(res,d))
|
|
lastWrite = *d;
|
|
|
|
this->ls->GetGC()->BarrierEnd();
|
|
}
|
|
}
|
|
}
|
|
void TObjectVFS::SetDate(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Date::DateTime lastWrite, Tesses::Framework::Date::DateTime lastAccess)
|
|
{
|
|
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "SetDate",{path,std::make_shared<Tesses::Framework::Date::DateTime>(lastWrite),std::make_shared<Tesses::Framework::Date::DateTime>(lastAccess)});
|
|
|
|
}
|
|
}
|
|
void TObjectVFS::Chmod(Tesses::Framework::Filesystem::VFSPath path, uint32_t mode)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls, "Chmod",{path,(int64_t)mode});
|
|
|
|
}
|
|
}
|
|
bool TObjectVFS::StatVFS(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Filesystem::StatVFSData& data)
|
|
{
|
|
TDictionary* dict;
|
|
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
auto res = dict->CallMethod(ls, "StatVFS",{path});
|
|
int64_t _num;
|
|
TDictionary* _dict;
|
|
TObject _o;
|
|
if(GetObjectHeap(res,_dict))
|
|
{
|
|
this->ls->GetGC()->BarrierBegin();
|
|
_o = dict->GetValue("BlockSize");
|
|
if(GetObject(_o,_num)) data.BlockSize = (uint64_t)_num;
|
|
|
|
_o = dict->GetValue("FragmentSize");
|
|
if(GetObject(_o,_num)) data.FragmentSize = (uint64_t)_num;
|
|
_o = dict->GetValue("Blocks");
|
|
if(GetObject(_o,_num)) data.Blocks = (uint64_t)_num;
|
|
_o = dict->GetValue("BlocksFree");
|
|
if(GetObject(_o,_num)) data.BlocksFree = (uint64_t)_num;
|
|
_o = dict->GetValue("BlocksAvailable");
|
|
if(GetObject(_o,_num)) data.BlocksAvailable = (uint64_t)_num;
|
|
_o = dict->GetValue("TotalInodes");
|
|
if(GetObject(_o,_num)) data.TotalInodes = (uint64_t)_num;
|
|
_o = dict->GetValue("FreeInodes");
|
|
if(GetObject(_o,_num)) data.FreeInodes = (uint64_t)_num;
|
|
_o = dict->GetValue("AvailableInodes");
|
|
if(GetObject(_o,_num)) data.AvailableInodes = (uint64_t)_num;
|
|
_o = dict->GetValue("Id");
|
|
if(GetObject(_o,_num)) data.Id = (uint64_t)_num;
|
|
_o = dict->GetValue("Flags");
|
|
if(GetObject(_o,_num)) data.Flags = (uint64_t)_num;
|
|
_o = dict->GetValue("MaxNameLength");
|
|
if(GetObject(_o,_num)) data.MaxNameLength = (uint64_t)_num;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this->ls->GetGC()->BarrierEnd();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
void TObjectVFS::Close()
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls,"Close",{});
|
|
}
|
|
}
|
|
TObjectVFS::~TObjectVFS()
|
|
{
|
|
TDictionary* dict;
|
|
if(GetObjectHeap(this->obj, dict))
|
|
{
|
|
GCList ls(this->ls->GetGC());
|
|
dict->CallMethod(ls,"Close",{});
|
|
}
|
|
delete this->ls;
|
|
}
|
|
|
|
} |