feat: Implement rename functionality for directories and files

- Added `rename` method to `Directory` struct to rename files and
  directories, updating metadata and timestamps.  This improves
  file management capabilities.
- Added `rename` method to `OurDBVFS` to provide a unified
  interface for renaming files and directories across the VFS. This
  allows for consistent file system operations.
- Added tests for the new rename functionality in `vfsourdb_test.v`
  to ensure correctness and robustness. This enhances confidence in
  the implementation.
This commit is contained in:
Mahmoud Emad
2025-02-23 22:35:37 +02:00
parent c0b57e2a01
commit 306de32de8
3 changed files with 55 additions and 16 deletions

View File

@@ -248,7 +248,30 @@ pub fn (mut dir Directory) move(src_name string, dst_name string) !FSEntry {
if mut entry := dir.myvfs.load_entry(child_id) {
if entry.metadata.name == src_name {
found = true
// Create a new directory entry with the new name
entry.metadata.name = dst_name
entry.metadata.modified_at = time.now().unix()
dir.myvfs.save_entry(entry)!
new_entry = entry
break
}
}
}
if !found {
return error('${src_name} not found')
}
return new_entry
}
pub fn (mut dir Directory) rename(src_name string, dst_name string) !FSEntry {
mut found := false
mut new_entry := FSEntry(dir)
for child_id in dir.children {
if mut entry := dir.myvfs.load_entry(child_id) {
if entry.metadata.name == src_name {
found = true
entry.metadata.name = dst_name
entry.metadata.modified_at = time.now().unix()
dir.myvfs.save_entry(entry)!

View File

@@ -127,8 +127,14 @@ pub fn (mut self OurDBVFS) get(path string) !vfscore.FSEntry {
return convert_to_vfscore_entry(entry)
}
pub fn (mut self OurDBVFS) rename(old_path string, new_path string) ! {
return error('Not implemented')
pub fn (mut self OurDBVFS) rename(old_path string, new_path string) !vfscore.FSEntry {
src_parent_path := os.dir(old_path)
src_name := os.base(old_path)
dst_name := os.base(new_path)
mut src_parent_dir := self.get_directory(src_parent_path)!
renamed_dir := src_parent_dir.rename(src_name, dst_name)!
return convert_to_vfscore_entry(renamed_dir)
}
pub fn (mut self OurDBVFS) copy(src_path string, dst_path string) ! {

View File

@@ -56,31 +56,41 @@ fn test_vfsourdb() ! {
assert entries.len == 1
assert entries[0].get_metadata().name == 'test.txt'
// Test directory rename
renamed_dir := vfs.rename('/test_dir2', '/test_dir')!
assert moved_dir.get_metadata().name == 'test_dir2'
assert moved_dir.get_metadata().file_type == .directory
// Test directory listing
entries = vfs.dir_list('/test_dir')!
assert entries.len == 1
assert entries[0].get_metadata().name == 'test.txt'
// Test exists
assert vfs.exists('/test_dir2') == true
assert vfs.exists('/test_dir2/test.txt') == true
assert vfs.exists('/test_dir') == true
assert vfs.exists('/test_dir/test.txt') == true
assert vfs.exists('/nonexistent') == false
// Test symlink creation and reading
vfs.link_create('/test_dir2/test.txt', '/test_dir2/test_link')!
link_target := vfs.link_read('/test_dir2/test_link')!
assert link_target == '/test_dir2/test.txt'
vfs.link_create('/test_dir/test.txt', '/test_dir/test_link')!
link_target := vfs.link_read('/test_dir/test_link')!
assert link_target == '/test_dir/test.txt'
// Test symlink deletion
vfs.link_delete('/test_dir2/test_link')!
assert vfs.exists('/test_dir2/test_link') == false
vfs.link_delete('/test_dir/test_link')!
assert vfs.exists('/test_dir/test_link') == false
// Test file deletion
vfs.file_delete('/test_dir2/test.txt')!
assert vfs.exists('/test_dir2/test.txt') == false
assert vfs.exists('/test_dir2') == true
vfs.file_delete('/test_dir/test.txt')!
assert vfs.exists('/test_dir/test.txt') == false
assert vfs.exists('/test_dir') == true
entries = vfs.dir_list('/test_dir2')!
entries = vfs.dir_list('/test_dir')!
assert entries.len == 0
// Test directory deletion
vfs.dir_delete('/test_dir2')!
assert vfs.exists('/test_dir2') == false
vfs.dir_delete('/test_dir')!
assert vfs.exists('/test_dir') == false
println('Test completed successfully!')
}