mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-17 16:51:33 -04:00
Complete sysfs
This commit is contained in:
parent
0c236f1a36
commit
ace9a013ad
@ -35,6 +35,7 @@ public:
|
|||||||
|
|
||||||
void set_value(const std::string& mount_point, const std::string& path, const std::string& value);
|
void set_value(const std::string& mount_point, const std::string& path, const std::string& value);
|
||||||
void delete_value(const std::string& mount_point, const std::string& path);
|
void delete_value(const std::string& mount_point, const std::string& path);
|
||||||
|
void delete_folder(const std::string& mount_point, const std::string& path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,28 +53,44 @@ sys_folder& find_root_folder(const std::string& mount_point){
|
|||||||
return root_folders.back();
|
return root_folders.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_folder& find_folder(sys_folder& root, const std::vector<std::string>& path, size_t i = 0){
|
sys_folder& find_folder(sys_folder& root, const std::vector<std::string>& path, size_t i, size_t last){
|
||||||
auto& name = path[i];
|
auto& name = path[i];
|
||||||
|
|
||||||
for(auto& folder : root.folders){
|
for(auto& folder : root.folders){
|
||||||
if(folder.name == name){
|
if(folder.name == name){
|
||||||
if(i == path.size() - 1){
|
if(i == last - 1){
|
||||||
return folder;
|
return folder;
|
||||||
} else {
|
} else {
|
||||||
return find_folder(folder, path, i + 1);
|
return find_folder(folder, path, i + 1, last);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
root.folders.emplace_back(name);
|
root.folders.emplace_back(name);
|
||||||
|
|
||||||
if(i == path.size() - 1){
|
if(i == last - 1){
|
||||||
return root.folders.back();
|
return root.folders.back();
|
||||||
} else {
|
} else {
|
||||||
return find_folder(root.folders.back(), path, i + 1);
|
return find_folder(root.folders.back(), path, i + 1, last);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool exists_folder(sys_folder& root, const std::vector<std::string>& path, size_t i, size_t last){
|
||||||
|
auto& name = path[i];
|
||||||
|
|
||||||
|
for(auto& folder : root.folders){
|
||||||
|
if(folder.name == name){
|
||||||
|
if(i == last - 1){
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return exists_folder(folder, path, i + 1, last);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} //end of anonymous namespace
|
} //end of anonymous namespace
|
||||||
|
|
||||||
sysfs::sysfs_file_system::sysfs_file_system(std::string mp) : mount_point(mp) {
|
sysfs::sysfs_file_system::sysfs_file_system(std::string mp) : mount_point(mp) {
|
||||||
@ -85,19 +101,93 @@ sysfs::sysfs_file_system::~sysfs_file_system(){
|
|||||||
//Nothing to delete
|
//Nothing to delete
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t sysfs::sysfs_file_system::get_file(const std::vector<std::string>& file_path, vfs::file& file){
|
size_t sysfs::sysfs_file_system::get_file(const std::vector<std::string>& file_path, vfs::file& f){
|
||||||
//TODO
|
auto& root_folder = find_root_folder(mount_point);
|
||||||
|
|
||||||
|
if(exists_folder(root_folder, file_path, 0, file_path.size() - 1)){
|
||||||
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size() - 1);
|
||||||
|
|
||||||
|
for(auto& file : folder.folders){
|
||||||
|
if(file.name == file_path.back()){
|
||||||
|
f.file_name = file.name;
|
||||||
|
f.directory = true;
|
||||||
|
f.hidden = false;
|
||||||
|
f.system = false;
|
||||||
|
f.size = 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto& file : folder.values){
|
||||||
|
if(file.name == file_path.back()){
|
||||||
|
f.file_name = file.name;
|
||||||
|
f.directory = false;
|
||||||
|
f.hidden = false;
|
||||||
|
f.system = false;
|
||||||
|
f.size = 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return std::ERROR_NOT_EXISTS;
|
return std::ERROR_NOT_EXISTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t sysfs::sysfs_file_system::read(const std::vector<std::string>& file_path, std::string& content){
|
size_t sysfs::sysfs_file_system::read(const std::vector<std::string>& file_path, std::string& content){
|
||||||
//TODO
|
auto& root_folder = find_root_folder(mount_point);
|
||||||
return 0;
|
|
||||||
|
if(exists_folder(root_folder, file_path, 0, file_path.size() - 1)){
|
||||||
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size() - 1);
|
||||||
|
|
||||||
|
for(auto& file : folder.values){
|
||||||
|
if(file.name == file_path.back()){
|
||||||
|
content = file.value;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto& file : folder.folders){
|
||||||
|
if(file.name == file_path.back()){
|
||||||
|
return std::ERROR_DIRECTORY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::ERROR_NOT_EXISTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t sysfs::sysfs_file_system::ls(const std::vector<std::string>& file_path, std::vector<vfs::file>& contents){
|
size_t sysfs::sysfs_file_system::ls(const std::vector<std::string>& file_path, std::vector<vfs::file>& contents){
|
||||||
//TODO
|
auto& root_folder = find_root_folder(mount_point);
|
||||||
return 0;
|
|
||||||
|
if(exists_folder(root_folder, file_path, 0, file_path.size() - 1)){
|
||||||
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size() - 1);
|
||||||
|
|
||||||
|
for(auto& file : folder.folders){
|
||||||
|
vfs::file f;
|
||||||
|
f.file_name = file.name;
|
||||||
|
f.directory = true;
|
||||||
|
f.hidden = false;
|
||||||
|
f.system = false;
|
||||||
|
f.size = 0;
|
||||||
|
contents.push_back(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto& file : folder.values){
|
||||||
|
vfs::file f;
|
||||||
|
f.file_name = file.name;
|
||||||
|
f.directory = false;
|
||||||
|
f.hidden = false;
|
||||||
|
f.system = false;
|
||||||
|
f.size = 0;
|
||||||
|
contents.push_back(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return std::ERROR_NOT_EXISTS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t sysfs::sysfs_file_system::touch(const std::vector<std::string>& ){
|
size_t sysfs::sysfs_file_system::touch(const std::vector<std::string>& ){
|
||||||
@ -126,7 +216,7 @@ void set_value(const std::string& mount_point, const std::string& path, const st
|
|||||||
auto last = file_path.back();
|
auto last = file_path.back();
|
||||||
file_path.pop_back();
|
file_path.pop_back();
|
||||||
|
|
||||||
auto& folder = find_folder(root_folder, file_path);
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size());
|
||||||
|
|
||||||
for(auto& v : folder.values){
|
for(auto& v : folder.values){
|
||||||
if(v.name == last){
|
if(v.name == last){
|
||||||
@ -139,5 +229,39 @@ void set_value(const std::string& mount_point, const std::string& path, const st
|
|||||||
}
|
}
|
||||||
|
|
||||||
void delete_value(const std::string& mount_point, const std::string& path){
|
void delete_value(const std::string& mount_point, const std::string& path){
|
||||||
//TODO
|
auto& root_folder = find_root_folder(mount_point);
|
||||||
|
|
||||||
|
auto file_path = std::split(path, '/');
|
||||||
|
auto last = file_path.back();
|
||||||
|
file_path.pop_back();
|
||||||
|
|
||||||
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size());
|
||||||
|
|
||||||
|
for(size_t i = 0; i < folder.values.size(); ++i){
|
||||||
|
auto& v = folder.values[i];
|
||||||
|
|
||||||
|
if(v.name == last){
|
||||||
|
folder.values.erase(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_folder(const std::string& mount_point, const std::string& path){
|
||||||
|
auto& root_folder = find_root_folder(mount_point);
|
||||||
|
|
||||||
|
auto file_path = std::split(path, '/');
|
||||||
|
auto last = file_path.back();
|
||||||
|
file_path.pop_back();
|
||||||
|
|
||||||
|
auto& folder = find_folder(root_folder, file_path, 0, file_path.size());
|
||||||
|
|
||||||
|
for(size_t i = 0; i < folder.folders.size(); ++i){
|
||||||
|
auto& v = folder.folders[i];
|
||||||
|
|
||||||
|
if(v.name == last){
|
||||||
|
folder.folders.erase(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user