Files
markbase/vfs_tool.sh
T

247 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
# MarkBaseFS VFS Operations Tool (Shell Implementation)
# Date: 2026-09-21
# Purpose: 文件系统操作演示
set -e
# 参数解析
DATABASE="${1:-/tmp/markbase_test_suite/test.sqlite}"
ROOT_PATH="/tmp/markbase_test_suite/volume"
COMMAND="${2:-info}"
shift 2 2>/dev/null || true
# 功能函数定义
list_dir() {
local path="${1:-/}"
echo "Listing directory: $path"
echo "----------------------------------------"
local full_path="$ROOT_PATH"
if [ "$path" != "/" ]; then
full_path="$ROOT_PATH${path}"
fi
if [ -d "$full_path" ]; then
ls -lh "$full_path" | tail -n +2 | awk '{
type = ($1 ~ /^d/) ? "DIR " : "FILE"
size = $5
name = $9
printf " %s %10s %s\n", type, size, name
}'
echo ""
echo "✅ Found $(ls -1 "$full_path" | wc -l) items"
else
echo "❌ Directory not found: $full_path"
fi
echo ""
}
read_file() {
local path="$1"
echo "Reading file: $path"
echo "----------------------------------------"
local full_path="$ROOT_PATH${path}"
if [ -f "$full_path" ]; then
cat "$full_path"
echo ""
echo ""
echo "✅ Read $(wc -c < "$full_path") bytes"
else
echo "❌ File not found: $full_path"
fi
echo ""
}
write_file() {
local path="$1"
local content="$2"
echo "Writing to file: $path"
local full_path="$ROOT_PATH${path}"
echo "$content" > "$full_path"
echo " ✓ File written: $full_path"
echo " ✓ Bytes written: ${#content}"
echo ""
echo "✅ Write successful"
echo ""
}
create_item() {
local path="$1"
local type="$2"
echo "Creating $type: $path"
local full_path="$ROOT_PATH${path}"
if [ "$type" = "file" ]; then
touch "$full_path"
echo " ✓ File created: $full_path"
echo ""
echo "✅ File created successfully"
elif [ "$type" = "folder" ] || [ "$type" = "directory" ]; then
mkdir -p "$full_path"
echo " ✓ Directory created: $full_path"
echo ""
echo "✅ Directory created successfully"
else
echo "❌ Invalid type: $type (use 'file' or 'folder')"
fi
echo ""
}
delete_item() {
local path="$1"
echo "Deleting: $path"
local full_path="$ROOT_PATH${path}"
if [ -e "$full_path" ]; then
if [ -d "$full_path" ]; then
rm -rf "$full_path"
echo " ✓ Directory deleted: $full_path"
else
rm -f "$full_path"
echo " ✓ File deleted: $full_path"
fi
echo ""
echo "✅ Delete successful"
else
echo "❌ Path not found: $full_path"
fi
echo ""
}
show_info() {
echo "Filesystem Information"
echo "----------------------------------------"
echo " Database: $DATABASE"
echo ""
if [ -f "$DATABASE" ]; then
# 数据库信息
local node_count=$(sqlite3 "$DATABASE" "SELECT COUNT(*) FROM file_nodes" 2>/dev/null || echo 0)
local file_count=$(sqlite3 "$DATABASE" "SELECT COUNT(*) FROM file_nodes WHERE node_type='file'" 2>/dev/null || echo 0)
local folder_count=$(sqlite3 "$DATABASE" "SELECT COUNT(*) FROM file_nodes WHERE node_type='folder'" 2>/dev/null || echo 0)
echo " Total nodes: $node_count"
echo " Files: $file_count"
echo " Folders: $folder_count"
echo ""
# 文件系统信息
if [ -d "$ROOT_PATH" ]; then
local total_size=$(du -sh "$ROOT_PATH" | cut -f1)
local file_count=$(find "$ROOT_PATH" -type f | wc -l)
local dir_count=$(find "$ROOT_PATH" -type d | wc -l)
echo " Root path: $ROOT_PATH"
echo " Total size: $total_size"
echo " Files on disk: $file_count"
echo " Directories on disk: $dir_count"
fi
echo ""
echo "✅ Database healthy"
else
echo " ❌ Database not found"
fi
echo ""
}
quick_test() {
echo "Running Quick Test"
echo "----------------------------------------"
echo "Test 1: Check database"
if [ -f "$DATABASE" ]; then
echo " ✓ Database exists"
else
echo " ✗ Database not found"
return 1
fi
echo "Test 2: Check root path"
if [ -d "$ROOT_PATH" ]; then
echo " ✓ Root path exists"
else
echo " ✗ Root path not found"
return 1
fi
echo "Test 3: List root directory"
local count=$(ls -1 "$ROOT_PATH" | wc -l)
echo " ✓ Found $count items in root"
echo "Test 4: Read test file"
if [ -f "$ROOT_PATH/README.md" ]; then
local content=$(cat "$ROOT_PATH/README.md")
echo " ✓ README.md exists (${#content} bytes)"
else
echo " ✗ README.md not found"
fi
echo ""
echo "✅ All tests passed"
echo ""
}
show_menu() {
echo "Available Commands:"
echo " list [path] - List directory contents"
echo " read <path> - Read file contents"
echo " write <path> <text> - Write to file"
echo " create <path> <type> - Create file/folder"
echo " delete <path> - Delete file/folder"
echo " info - Show filesystem info"
echo " test - Run quick test"
echo " help - Show this menu"
echo ""
}
# 主程序开始
echo "========================================"
echo "MarkBaseFS VFS Operations Tool v1.1"
echo "========================================"
echo ""
echo "Database: $DATABASE"
echo "Root Path: $ROOT_PATH"
echo ""
# 命令分发
case "$COMMAND" in
list)
list_dir "$@"
;;
read)
read_file "$@"
;;
write)
write_file "$1" "$2"
;;
create)
create_item "$1" "$2"
;;
delete)
delete_item "$1"
;;
info)
show_info
;;
test)
quick_test
;;
help|--help|-h)
show_menu
;;
*)
echo "❌ Unknown command: $COMMAND"
show_menu
;;
esac