add path to fsentry metadata

This commit is contained in:
timurgordon
2025-03-06 15:48:37 +01:00
parent fe934bba36
commit 90c81a1234
7 changed files with 37 additions and 19 deletions

View File

@@ -15,7 +15,7 @@ fn (d &Directory) get_metadata() vfs.Metadata {
} }
fn (d &Directory) get_path() string { fn (d &Directory) get_path() string {
return '/${d.metadata.name.trim_string_left('/')}' return d.metadata.path
} }
// is_dir returns true if the entry is a directory // is_dir returns true if the entry is a directory

View File

@@ -32,7 +32,7 @@ fn (f &File) get_metadata() vfs.Metadata {
} }
fn (f &File) get_path() string { fn (f &File) get_path() string {
return '/${f.metadata.name.trim_string_left('/')}' return f.metadata.path
} }
// is_dir returns true if the entry is a directory // is_dir returns true if the entry is a directory

View File

@@ -4,6 +4,7 @@ import freeflowuniverse.herolib.vfs
import freeflowuniverse.herolib.data.ourdb import freeflowuniverse.herolib.data.ourdb
import freeflowuniverse.herolib.data.encoder import freeflowuniverse.herolib.data.encoder
import time import time
import log
// DatabaseVFS represents the virtual filesystem // DatabaseVFS represents the virtual filesystem
@[heap] @[heap]
@@ -31,7 +32,7 @@ pub fn (mut fs DatabaseVFS) get_next_id() u32 {
} }
// load_entry loads an entry from the database by ID and sets up parent references // load_entry loads an entry from the database by ID and sets up parent references
pub fn (mut fs DatabaseVFS) load_entry(vfs_id u32) !FSEntry { fn (mut fs DatabaseVFS) load_entry(vfs_id u32) !FSEntry {
if metadata := fs.db_metadata.get(fs.get_database_id(vfs_id)!) { if metadata := fs.db_metadata.get(fs.get_database_id(vfs_id)!) {
// First byte is version, second byte indicates the type // First byte is version, second byte indicates the type
// TODO: check we dont overflow filetype (u8 in boundaries of filetype) // TODO: check we dont overflow filetype (u8 in boundaries of filetype)
@@ -79,12 +80,12 @@ pub fn (mut fs DatabaseVFS) save_entry(entry FSEntry) !u32 {
} }
File { File {
// First encode file data and store in db_data // First encode file data and store in db_data
data_encoded := entry.data.bytes() metadata_bytes := if entry.data.len == 0 {
metadata_bytes := if data_encoded.len == 0 {
entry.encode(none) entry.encode(none)
} else { } else {
// file has data so that will be stored in data_db // file has data so that will be stored in data_db
// its corresponding id stored with file metadata // its corresponding id stored with file metadata
data_encoded := entry.data.bytes()
data_db_id := fs.db_data.set(id: entry.metadata.id, data: data_encoded) or { data_db_id := fs.db_data.set(id: entry.metadata.id, data: data_encoded) or {
return error('Failed to save file data on id:${entry.metadata.id}: ${err}') return error('Failed to save file data on id:${entry.metadata.id}: ${err}')
} }

View File

@@ -18,7 +18,7 @@ pub fn (mut fs DatabaseVFS) directory_mkdir(mut dir Directory, name_ string) !&D
path := if dir.metadata.path == '/' { path := if dir.metadata.path == '/' {
'/${name}' '/${name}'
} else { } else {
"${dir.metadata.path.trim('/')}/${name}" "/${dir.metadata.path.trim('/')}/${name}"
} }
new_dir := fs.new_directory( new_dir := fs.new_directory(
@@ -26,7 +26,6 @@ pub fn (mut fs DatabaseVFS) directory_mkdir(mut dir Directory, name_ string) !&D
path: path path: path
parent_id: dir.metadata.id parent_id: dir.metadata.id
)! )!
println('new_dit ${new_dir}')
dir.children << new_dir.metadata.id dir.children << new_dir.metadata.id
fs.save_entry(dir)! fs.save_entry(dir)!
return new_dir return new_dir
@@ -109,11 +108,17 @@ pub fn (mut fs DatabaseVFS) directory_touch(dir_ Directory, name_ string) !&File
} }
} }
path := if dir.metadata.path == '/' {
'/${name}'
} else {
"/${dir.metadata.path.trim('/')}/${name}"
}
// Create new file with correct parent_id // Create new file with correct parent_id
mut new_file := fs.new_file( mut new_file := fs.new_file(
parent_id: dir.metadata.id parent_id: dir.metadata.id
name: name name: name
path: "${dir.metadata.path.trim('/')}/${name}" path: path
)! )!
// Ensure parent_id is set correctly // Ensure parent_id is set correctly
@@ -222,11 +227,16 @@ pub fn (mut fs DatabaseVFS) directory_move(dir_ Directory, args_ MoveDirArgs) !&
found = true found = true
child_id_to_remove = child_id child_id_to_remove = child_id
new_path := if args.dst_parent_dir.metadata.path == '/' {
'/${args.dst_entry_name}'
} else {
"/${args.dst_parent_dir.metadata.path.trim('/')}/${args.dst_entry_name}"
}
// Handle both files and directories // Handle both files and directories
if entry is File { if entry is File {
mut file_entry := entry as File mut file_entry := entry as File
file_entry.metadata.name = args.dst_entry_name file_entry.metadata.name = args.dst_entry_name
file_entry.metadata.path = "/${args.dst_parent_dir.metadata.path.trim('/')}/${args.dst_entry_name}" file_entry.metadata.path = new_path
file_entry.metadata.modified_at = time.now().unix() file_entry.metadata.modified_at = time.now().unix()
file_entry.parent_id = args.dst_parent_dir.metadata.id file_entry.parent_id = args.dst_parent_dir.metadata.id
@@ -248,7 +258,7 @@ pub fn (mut fs DatabaseVFS) directory_move(dir_ Directory, args_ MoveDirArgs) !&
// Handle directory // Handle directory
mut dir_entry := entry as Directory mut dir_entry := entry as Directory
dir_entry.metadata.name = args.dst_entry_name dir_entry.metadata.name = args.dst_entry_name
dir_entry.metadata.path = "${args.dst_parent_dir.metadata.path.trim_string_right('/')}/${args.dst_entry_name}" dir_entry.metadata.path = new_path
dir_entry.metadata.modified_at = time.now().unix() dir_entry.metadata.modified_at = time.now().unix()
dir_entry.parent_id = args.dst_parent_dir.metadata.id dir_entry.parent_id = args.dst_parent_dir.metadata.id
@@ -285,6 +295,7 @@ fn (mut fs DatabaseVFS) move_children_recursive(mut dir Directory) ! {
for child in dir.children { for child in dir.children {
if mut child_entry := fs.load_entry(child) { if mut child_entry := fs.load_entry(child) {
child_entry.parent_id = dir.metadata.id child_entry.parent_id = dir.metadata.id
child_entry.metadata.path = '${dir.metadata.path}/${child_entry.metadata.name}'
if child_entry is Directory { if child_entry is Directory {
// Recursively move subdirectories // Recursively move subdirectories
@@ -393,6 +404,7 @@ fn (mut fs DatabaseVFS) copy_children_recursive(mut src_dir Directory, mut dst_d
metadata: Metadata{ metadata: Metadata{
...entry_.metadata ...entry_.metadata
id: fs.get_next_id() id: fs.get_next_id()
path: '${dst_dir.metadata.path}/${entry_.metadata.name}'
} }
children: []u32{} children: []u32{}
parent_id: dst_dir.metadata.id parent_id: dst_dir.metadata.id
@@ -408,6 +420,7 @@ fn (mut fs DatabaseVFS) copy_children_recursive(mut src_dir Directory, mut dst_d
metadata: Metadata{ metadata: Metadata{
...entry_.metadata ...entry_.metadata
id: fs.get_next_id() id: fs.get_next_id()
path: '${dst_dir.metadata.path}/${entry_.metadata.name}'
} }
data: entry_.data data: entry_.data
parent_id: dst_dir.metadata.id parent_id: dst_dir.metadata.id
@@ -420,6 +433,7 @@ fn (mut fs DatabaseVFS) copy_children_recursive(mut src_dir Directory, mut dst_d
metadata: Metadata{ metadata: Metadata{
...entry_.metadata ...entry_.metadata
id: fs.get_next_id() id: fs.get_next_id()
path: '${dst_dir.metadata.path}/${entry_.metadata.name}'
} }
target: entry_.target target: entry_.target
parent_id: dst_dir.metadata.id parent_id: dst_dir.metadata.id

View File

@@ -53,7 +53,6 @@ fn (mut self DatabaseVFS) directory_get_entry(mut dir Directory, path string) ?F
panic('this should never happen') panic('this should never happen')
} }
for mut child in children { for mut child in children {
println('debugzoni1 ${child.metadata.path} ${path}')
if child.metadata.path == path { if child.metadata.path == path {
return child return child
} else if child is Directory { } else if child is Directory {

View File

@@ -32,12 +32,12 @@ pub fn (mut self DatabaseVFS) file_read(path_ string) ![]u8 {
return error('Not a file: ${path}') return error('Not a file: ${path}')
} }
pub fn (mut self DatabaseVFS) file_write(path string, data []u8) ! { pub fn (mut self DatabaseVFS) file_write(path_ string, data []u8) ! {
self.print()! log.info('[DatabaseVFS] Writing file ${path_}')
path := '/${path_.trim_left('/').trim_right('/')}'
if mut entry := self.get_entry(path) { if mut entry := self.get_entry(path) {
println(entry)
if mut entry is File { if mut entry is File {
log.info('[DatabaseVFS] Writing file ${path}') log.info('[DatabaseVFS] Writing to file ${path}')
entry.write(data.bytestr()) entry.write(data.bytestr())
self.save_entry(entry)! self.save_entry(entry)!
} else { } else {
@@ -50,6 +50,7 @@ pub fn (mut self DatabaseVFS) file_write(path string, data []u8) ! {
} }
pub fn (mut self DatabaseVFS) file_delete(path string) ! { pub fn (mut self DatabaseVFS) file_delete(path string) ! {
log.info('[DatabaseVFS] Deleting file ${path}')
parent_path := os.dir(path) parent_path := os.dir(path)
file_name := os.base(path) file_name := os.base(path)
@@ -83,6 +84,7 @@ pub fn (mut self DatabaseVFS) dir_list(path string) ![]vfs.FSEntry {
} }
pub fn (mut self DatabaseVFS) dir_delete(path string) ! { pub fn (mut self DatabaseVFS) dir_delete(path string) ! {
log.info('[DatabaseVFS] Deleting Directory ${path}')
parent_path := os.dir(path) parent_path := os.dir(path)
dir_name := os.base(path) dir_name := os.base(path)
@@ -91,6 +93,7 @@ pub fn (mut self DatabaseVFS) dir_delete(path string) ! {
} }
pub fn (mut self DatabaseVFS) link_create(target_path string, link_path string) !vfs.FSEntry { pub fn (mut self DatabaseVFS) link_create(target_path string, link_path string) !vfs.FSEntry {
log.info('[DatabaseVFS] Creating link ${target_path}')
parent_path := os.dir(link_path) parent_path := os.dir(link_path)
link_name := os.base(link_path) link_name := os.base(link_path)
@@ -118,6 +121,7 @@ pub fn (mut self DatabaseVFS) link_create(target_path string, link_path string)
} }
pub fn (mut self DatabaseVFS) link_read(path string) !string { pub fn (mut self DatabaseVFS) link_read(path string) !string {
log.info('[DatabaseVFS] Reading link ${path}')
mut entry := self.get_entry(path)! mut entry := self.get_entry(path)!
if mut entry is Symlink { if mut entry is Symlink {
return entry.get_target()! return entry.get_target()!
@@ -126,6 +130,7 @@ pub fn (mut self DatabaseVFS) link_read(path string) !string {
} }
pub fn (mut self DatabaseVFS) link_delete(path string) ! { pub fn (mut self DatabaseVFS) link_delete(path string) ! {
log.info('[DatabaseVFS] Deleting link ${path}')
parent_path := os.dir(path) parent_path := os.dir(path)
file_name := os.base(path) file_name := os.base(path)
@@ -134,8 +139,6 @@ pub fn (mut self DatabaseVFS) link_delete(path string) ! {
} }
pub fn (mut self DatabaseVFS) exists(path_ string) bool { pub fn (mut self DatabaseVFS) exists(path_ string) bool {
println('debugznoiki')
self.print() or {panic(err.msg())}
path := if !path_.starts_with('/') { path := if !path_.starts_with('/') {
'/${path_}' '/${path_}'
} else { } else {
@@ -144,6 +147,8 @@ pub fn (mut self DatabaseVFS) exists(path_ string) bool {
if path == '/' { if path == '/' {
return true return true
} }
// self.print() or {panic(err)}
log.info('[DatabaseVFS] Checking path exists ${path}')
self.get_entry(path) or { return false } self.get_entry(path) or { return false }
return true return true
} }
@@ -164,6 +169,7 @@ pub fn (mut self DatabaseVFS) rename(old_path string, new_path string) !vfs.FSEn
} }
pub fn (mut self DatabaseVFS) copy(src_path string, dst_path string) !vfs.FSEntry { pub fn (mut self DatabaseVFS) copy(src_path string, dst_path string) !vfs.FSEntry {
log.info('[DatabaseVFS] Copying ${src_path} to ${dst_path}')
src_parent_path := os.dir(src_path) src_parent_path := os.dir(src_path)
dst_parent_path := os.dir(dst_path) dst_parent_path := os.dir(dst_path)
@@ -197,7 +203,6 @@ pub fn (mut self DatabaseVFS) move(src_path string, dst_path string) !vfs.FSEntr
src_parent_path := os.dir(src_path) src_parent_path := os.dir(src_path)
dst_parent_path := os.dir(dst_path) dst_parent_path := os.dir(dst_path)
log.info('${src_parent_path}')
if !self.exists(src_parent_path) { if !self.exists(src_parent_path) {
return error('${src_parent_path} does not exist') return error('${src_parent_path} does not exist')

View File

@@ -87,7 +87,6 @@ pub fn (mut self NestedVFS) file_create(path string) !vfs.FSEntry {
} }
pub fn (mut self NestedVFS) file_read(path string) ![]u8 { pub fn (mut self NestedVFS) file_read(path string) ![]u8 {
println('debuzone- File read ${path}')
// Special handling for macOS resource fork files (._*) // Special handling for macOS resource fork files (._*)
if path.starts_with('/._') || path.contains('/._') { if path.starts_with('/._') || path.contains('/._') {