**************------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------0013dirs.sh 0000#! /bin/bash # Grab core defs source core.sh # Block access routines source block.sh # String routines source string.sh # dir_find_file filename [dirblock] : Find the inode for a give file name function dir_find_file { local file=$1 dirblock=$2 block if [ -z "$dirblock" ] then dirblock=$BROOT fi block=`block_get $dirblock | grep -aE "^[[:digit:]]{4}$file\$" | cut -b1-4` # If we didnt see the filename in this dir, look for a continuation if [ -z "$block" ] then block=`block_get $dirblock | grep -aE '^0001[[:digit:]]{4}$' | cut -b 5-8` if [ -z "$block" ] then die "File $file not found" else dir_find_file $file $block check_last fi else echo -n "$block" fi } # dir_add_file filename block [dirblock] : Add the given file to the given dirblock (Note: This doesn't allocate space for the file, do that yourself) function dir_add_file { local file=$1 block=$2 dirblock=$3 byteoff if [ -z "$dirblock" ] then dirblock=$BROOT fi #echo "Adding to block $dirblock" # Look for an end of dir marker, if you dont see it, then recurse if [ -z `block_get $dirblock | grep -aE '^0000'` ] then #echo "Block $dirblock is not the end, recursing" dir_add_file $file $block `block_get $dirblock | grep -aE '^0001[[:digit:]]{4}' | cut -b 5-8` else # Get the byte offset for the end marker byteoff=`block_get $dirblock | grep -abE '^0000' | cut -d: -f1` # current number of bytes used + 4 byte block num + string + newline + 9 bytes reservered for link marker if [ $(( byteoff + 4 + `strlen $file` + 1 + 9 )) -gt $BSIZE ] then #echo "Block is full" # Request a new block local newblock=`bt_alloc` #echo "Got block $newblock" # Use this regex to extract all data before the end marker #[[ `block_get $dirblock` =~ '^(.*)0000$' ]] # Write the new continuation marker #block_set $dirblock "`echo "${BASH_REMATCH[1]}0001$newblock" | tr ' ' "\n"`" local blockdata=`block_get $dirblock` #echo "Current block: `./decodestr.pl "${blockdata}"`" #echo "Writting contin marker: `./decodestr.pl "${blockdata/0000/0001$newblock}"`" block_set $dirblock "${blockdata/0000/0001$newblock}" # Initialize the new block block_set $newblock "0000\n" # Now add the file to the new dirblock dirblock=$newblock fi #[[ `block_get $dirblock` =~ '^(.*)0000$' ]] #block_set $dirblock "`echo -e "${BASH_REMATCH[1]}$block$file\n0000" | tr ' ' "\n"`" #echo "Writting entry to block $dirblock" local blockdata=`block_get $dirblock` #echo "Data: `./decodestr.pl "${blockdata}"`" #echo "Writting data: `./decodestr.pl "${blockdata/0000/$block$file\n0000}"`" block_set $dirblock "${blockdata/0000/$block$file\n0000}" fi } # dir_remove_file file [dirblock] : Remove the given filename from the directory function dir_remove_file { local file=$1 dirblock=$2 block if [ -z "$dirblock" ] then dirblock=$BROOT fi block=`block_get $dirblock | grep -aE "^[[:digit:]]{4}$file\$" | cut -b1-4` # If we didnt see the filename in this dir, look for a continuation if [ -z "$block" ] then block=`block_get $dirblock | grep -aE '^0001[[:digit:]]{4}$' | cut -b 5-8` if [ -z "$block" ] then die "File $file not found" else dir_remove_file $file $block check_last fi else block_set $dirblock "$(block_get $dirblock | grep -aEv "^[[:digit:]]{4}$file\$")" fi } # dir_ls [dirblock] : Walk the directory and output the file name and size for each entry function dir_ls { local dirblock=$1 block name size oldifs if [ -z "$dirblock" ] then dirblock=$BROOT fi #oldifs=$IFS #IFS=`echo` # Set IFS to a single newline for line in `block_get $dirblock` do #IFS=$oldifs block=${line:0:4} name=${line:4} if [ "$block" = "0000" ] then break # End marker means I'm outta here else if [ "$block" = "0001" ] then dir_ls $name # Recurse else size=`file_size $block` echo "$name $size" fi fi #IFS=`echo` done #IFS=$oldifs } 0005000600070008000900100011000000120401