diff --git a/examples/hero/herofs/.gitignore b/examples/hero/herofs/.gitignore new file mode 100644 index 00000000..6fc1d38a --- /dev/null +++ b/examples/hero/herofs/.gitignore @@ -0,0 +1 @@ +herofs_basic diff --git a/examples/hero/herofs/herofs_basic.vsh b/examples/hero/herofs/herofs_basic.vsh index 43989b6c..7a6fa414 100755 --- a/examples/hero/herofs/herofs_basic.vsh +++ b/examples/hero/herofs/herofs_basic.vsh @@ -5,110 +5,143 @@ import freeflowuniverse.herolib.hero.herofs // Basic example of using HeroFS - the Hero Filesystem // Demonstrates creating a filesystem, directories, and files -fn main() { - // Initialize the HeroFS factory - mut fs_factory := herofs.new()! - println('HeroFS factory initialized') +fn test_cleanup() ! { + herofs.delete_fs_test()! +} - // Create a new filesystem - mut my_fs := fs_factory.fs.new( - name: 'my_documents' - description: 'Personal documents filesystem' - quota_bytes: 1024 * 1024 * 1024 // 1GB quota - )! - - // Save the filesystem - fs_factory.fs.set(mut my_fs)! - println('Created filesystem: ${my_fs.name} with ID: ${my_fs.id}') - - // Create root directory - mut root_dir := fs_factory.fs_dir.new( - name: 'root' - fs_id: my_fs.id - parent_id: 0 // Root has no parent - description: 'Root directory' - )! - - // Save the root directory - fs_factory.fs_dir.set(mut root_dir)! - println('Created root directory with ID: ${root_dir.id}') - - // Update the filesystem with the root directory ID - my_fs.root_dir_id = root_dir.id - fs_factory.fs.set(mut my_fs)! - - // Create some subdirectories - mut docs_dir := fs_factory.fs_dir.new( - name: 'documents' - fs_id: my_fs.id - parent_id: root_dir.id - description: 'Documents directory' - )! - - mut pics_dir := fs_factory.fs_dir.new( - name: 'pictures' - fs_id: my_fs.id - parent_id: root_dir.id - description: 'Pictures directory' - )! - - // Save the subdirectories - fs_factory.fs_dir.set(mut docs_dir)! - fs_factory.fs_dir.set(mut pics_dir)! - - // Add subdirectories to root directory - root_dir.directories << docs_dir.id - root_dir.directories << pics_dir.id - fs_factory.fs_dir.set(mut root_dir)! - - println('Created documents directory with ID: ${docs_dir.id}') - println('Created pictures directory with ID: ${pics_dir.id}') - - // Create a text file blob - text_content := 'Hello, world! This is a test file in HeroFS.'.bytes() - mut text_blob := fs_factory.fs_blob.new(data: text_content)! - - // Save the blob - fs_factory.fs_blob.set(mut text_blob)! - println('Created text blob with ID: ${text_blob.id}') - - // Create a file referencing the blob - mut text_file := fs_factory.fs_file.new( - name: 'hello.txt' - fs_id: my_fs.id - blobs: [text_blob.id] - mime_type: .txt - )! - - // Save the file - fs_factory.fs_file.set(mut text_file)! - // Associate file with documents directory - fs_factory.fs_file.add_to_directory(text_file.id, docs_dir.id)! - println('Created text file with ID: ${text_file.id}') - - // Demonstrate filesystem navigation using find - mut fs := fs_factory.fs.get(my_fs.id)! - - println('\nAll items in filesystem:') - results := fs.find('/', recursive: true)! - for result in results { - type_str := match result.result_type { - .file { 'FILE' } - .directory { 'DIR ' } - .symlink { 'LINK' } - } - println('- ${type_str}: ${result.path} (ID: ${result.id})') - - // If it's a file, show its content - if result.result_type == .file { - file := fs_factory.fs_file.get(result.id)! - if file.blobs.len > 0 { - blob := fs_factory.fs_blob.get(file.blobs[0])! - content := blob.data.bytestr() - println(' Content: "${content}"') - } - } +fn test_basic() ! { + defer { + test_cleanup() or { panic('cleanup failed: ${err.msg()}') } } - println('\nHeroFS basic example completed successfully!') + test_cleanup()! + + // Initialize the HeroFS factory for test purposes + mut fs_factory := herofs.new()! + + // Create a new filesystem (required for FsBlobMembership validation) + mut test_fs := fs_factory.fs.new_get_set( + name: 'test_filesystem' + description: 'Filesystem for testing FsBlobMembership functionality' + quota_bytes: 1024 * 1024 * 1024 // 1GB quota + )! + println('Created test filesystem with ID: ${test_fs.id}') + + + assert test_fs.id > 0 + assert test_fs.root_dir_id > 0 + + mut root_dir := test_fs.root_dir()! + + // this means root_dir is automatically there, no need to create + + println(root_dir) } + +test_basic()! + +// // Initialize the HeroFS factory +// mut fs_factory := herofs.new()! +// println('HeroFS factory initialized') + +// // Create a new filesystem +// mut my_fs := fs_factory.fs.new( +// name: 'my_documents' +// description: 'Personal documents filesystem' +// quota_bytes: 1024 * 1024 * 1024 // 1GB quota +// )! + +// // Save the filesystem +// fs_factory.fs.set(mut my_fs)! +// println('Created filesystem: ${my_fs.name} with ID: ${my_fs.id}') + +// // Create root directory +// mut root_dir := fs_factory.fs_dir.new( +// name: 'root' +// fs_id: my_fs.id +// parent_id: 0 // Root has no parent +// description: 'Root directory' +// )! + +// // Save the root directory +// fs_factory.fs_dir.set(mut root_dir)! +// println('Created root directory with ID: ${root_dir.id}') + +// // Update the filesystem with the root directory ID +// my_fs.root_dir_id = root_dir.id +// fs_factory.fs.set(mut my_fs)! + +// // Create some subdirectories +// mut docs_dir := fs_factory.fs_dir.new( +// name: 'documents' +// fs_id: my_fs.id +// parent_id: root_dir.id +// description: 'Documents directory' +// )! + +// mut pics_dir := fs_factory.fs_dir.new( +// name: 'pictures' +// fs_id: my_fs.id +// parent_id: root_dir.id +// description: 'Pictures directory' +// )! + +// // Save the subdirectories +// fs_factory.fs_dir.set(mut docs_dir)! +// fs_factory.fs_dir.set(mut pics_dir)! + +// // Add subdirectories to root directory +// root_dir.directories << docs_dir.id +// root_dir.directories << pics_dir.id +// fs_factory.fs_dir.set(mut root_dir)! + +// println('Created documents directory with ID: ${docs_dir.id}') +// println('Created pictures directory with ID: ${pics_dir.id}') + +// // Create a text file blob +// text_content := 'Hello, world! This is a test file in HeroFS.'.bytes() +// mut text_blob := fs_factory.fs_blob.new(data: text_content)! + +// // Save the blob +// fs_factory.fs_blob.set(mut text_blob)! +// println('Created text blob with ID: ${text_blob.id}') + +// // Create a file referencing the blob +// mut text_file := fs_factory.fs_file.new( +// name: 'hello.txt' +// fs_id: my_fs.id +// blobs: [text_blob.id] +// mime_type: .txt +// )! + +// // Save the file +// fs_factory.fs_file.set(mut text_file)! +// // Associate file with documents directory +// fs_factory.fs_file.add_to_directory(text_file.id, docs_dir.id)! +// println('Created text file with ID: ${text_file.id}') + +// // Demonstrate filesystem navigation using find +// mut fs := fs_factory.fs.get(my_fs.id)! + +// println('\nAll items in filesystem:') +// results := fs.find('/', recursive: true)! +// for result in results { +// type_str := match result.result_type { +// .file { 'FILE' } +// .directory { 'DIR ' } +// .symlink { 'LINK' } +// } +// println('- ${type_str}: ${result.path} (ID: ${result.id})') + +// // If it's a file, show its content +// if result.result_type == .file { +// file := fs_factory.fs_file.get(result.id)! +// if file.blobs.len > 0 { +// blob := fs_factory.fs_blob.get(file.blobs[0])! +// content := blob.data.bytestr() +// println(' Content: "${content}"') +// } +// } +// } + +// println('\nHeroFS basic example completed successfully!') diff --git a/lib/hero/db/core_methods.v b/lib/hero/db/core_methods.v index b8b4cec1..e58ec78f 100644 --- a/lib/hero/db/core_methods.v +++ b/lib/hero/db/core_methods.v @@ -91,5 +91,7 @@ fn (mut self DB) db_name[T]() string { } pub fn (mut self DB) new_id() !u32 { - return u32(self.redis.incr('db:id')!) + r:=u32(self.redis.incr('db:id')!) + assert r>0 + return r } diff --git a/lib/hero/herofs/fs.v b/lib/hero/herofs/fs.v index af4a4a4f..ec16d683 100644 --- a/lib/hero/herofs/fs.v +++ b/lib/hero/herofs/fs.v @@ -130,7 +130,7 @@ pub fn (mut self DBFs) new_get_set(args_ FsArg) !Fs { } if changes { - self.set(o)! + o=self.set(o)! } return o @@ -138,6 +138,9 @@ pub fn (mut self DBFs) new_get_set(args_ FsArg) !Fs { pub fn (mut self DBFs) set(o Fs) !Fs { mut o_mut := o + if o_mut.id==0{ + o_mut.id=self.db.new_id()! + } if o_mut.root_dir_id == 0 { // If no root directory is set, create one mut root_dir := self.factory.fs_dir.new( @@ -155,6 +158,13 @@ pub fn (mut self DBFs) set(o Fs) !Fs { return o_result } +pub fn (mut self Fs) root_dir() !FsDir { + mut o := self.factory.fs_dir.get(self.root_dir_id) or { + return error("Can't find root_dir\n${err}") + } + return o +} + pub fn (mut self DBFs) delete(id u32) ! { // Get the filesystem to retrieve its name fs := self.get(id)! diff --git a/lib/hero/herofs/fs_blob_membership_test.v b/lib/hero/herofs/fs_blob_membership_test.v index b1d62743..78c49bff 100644 --- a/lib/hero/herofs/fs_blob_membership_test.v +++ b/lib/hero/herofs/fs_blob_membership_test.v @@ -10,6 +10,9 @@ fn test_basic() ! { defer { test_cleanup() or { panic('cleanup failed: ${err.msg()}') } } + + test_cleanup()! + // Initialize the HeroFS factory for test purposes mut fs_factory := new()! @@ -19,23 +22,18 @@ fn test_basic() ! { description: 'Filesystem for testing FsBlobMembership functionality' quota_bytes: 1024 * 1024 * 1024 // 1GB quota )! - test_fs = fs_factory.fs.set(test_fs)! println('Created test filesystem with ID: ${test_fs.id}') - // Create root directory for the filesystem - mut root_dir := fs_factory.fs_dir.new( - name: 'root' - fs_id: test_fs.id - parent_id: 0 // Root has no parent - description: 'Root directory for testing' - )! - root_dir = fs_factory.fs_dir.set(root_dir)! - root_dir_id := root_dir.id + assert test_fs.id > 0 + assert test_fs.root_dir_id > 0 - // Update the filesystem with the root directory ID - test_fs.root_dir_id = root_dir_id - test_fs = fs_factory.fs.set(test_fs)! + mut root_dir := test_fs.root_dir()! + // this means root_dir is automatically there, no need to create + + println(root_dir) + + panic('sd') // Create test blob for membership test_data := 'This is test content for blob membership'.bytes() mut test_blob := fs_factory.fs_blob.new(data: test_data)! @@ -56,7 +54,7 @@ fn test_basic() ! { println('Created test file with ID: ${file_id}') // Add file to directory - mut dir := fs_factory.fs_dir.get(root_dir_id)! + mut dir := fs_factory.fs_dir.get(test_fs.root_dir_id)! dir.files << file_id dir = fs_factory.fs_dir.set(dir)! @@ -243,7 +241,7 @@ fn test_validation() ! { )! // Try to save it, which should fail - fs_factory.fs_blob_membership.set(test_membership) or { + test_membership=fs_factory.fs_blob_membership.set(test_membership) or { println('✓ Membership set correctly failed with non-existent blob') return } @@ -266,7 +264,7 @@ fn test_validation() ! { )! // Try to save it, which should fail - fs_factory.fs_blob_membership.set(test_membership2) or { + test_membership2=fs_factory.fs_blob_membership.set(test_membership2) or { println('✓ Membership set correctly failed with non-existent filesystem') return }