The Composite File Tree
Treating individual files and directory branches uniformly.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Composite
ARCANUM SCHOOL: Divination // Treemancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE BASH SCRIPT //
#!/usr/bin/env bash
# The Component Interface
scan_node() {
local node=$1
local indent=$2
if [[ -d "$node" ]]; then
scan_directory "$node" "$indent"
elif [[ -f "$node" ]]; then
scan_file "$node" "$indent"
fi
}
# Leaf
scan_file() {
local file=$1
local indent=$2
echo "${indent}File: $(basename "$file") - Size: $(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null) bytes"
}
# Composite
scan_directory() {
local dir=$1
local indent=$2
echo "${indent}Directory: $(basename "$dir")/"
# Recursively scan children
for child in "$dir"/*; do
[ -e "$child" ] || continue
scan_node "$child" " $indent"
done
}
# Usage (create mock structure)
mkdir -p /tmp/composite_test/sub
touch /tmp/composite_test/file1.txt
touch /tmp/composite_test/sub/file2.txt
scan_node "/tmp/composite_test" ""
# Cleanup
rm -rf /tmp/composite_test
The very filesystem of Unix is a Composite pattern. Files are leaves, directories are branches. By writing recursive scanning functions, a scriptmancer can traverse entire hierarchies, casting operations upon single bytes and sprawling directory structures with uniform ease.
Runic Tags:
#bash
#composite
#structural
#filesystem