commit a3f0c7c86b538e729cf849edab36046daffa45e3
parent 70e4a5d19faccb9ddaeb3eb530d79d1157c74914
Author: Jaromil <jaromil@dyne.org>
Date:   Mon, 26 Sep 2011 11:33:07 +0200
clean up mount code and list output ( fix #32 )
Diffstat:
| M | src/tomb |  |  | 127 | ++++++++++++++++++++++++++----------------------------------------------------- | 
1 file changed, 42 insertions(+), 85 deletions(-)
diff --git a/src/tomb b/src/tomb
@@ -201,53 +201,6 @@ EOF
     return 0
 }
 
-get_arg_tomb() {
-# set up variables to be used by caller:
-# tombfile - filename without path
-# tombdir  - directory where the tomb is
-# tombname - name of the tomb (filename without extension)
-# the full path is made with $tombdir/$tombfile
-    if [ -z $1 ]; then
-	error "internal: get_arg_tomb called without argument"
-	return 1
-    fi
-
-    arg=${1}
-    if ! [ -r ${arg} ]; then
-	error "file not found: $arg"
-	return 1
-    fi
-
-    tombfile=`basename $arg`
-    tombdir=`dirname $arg`
-
-    file ${tombdir}/${tombfile} | grep -i 'luks encrypted file' 2>&1 >/dev/null
-    if [ $? != 0 ]; then
-	error "$arg is not a valid tomb file, operation aborted"
-	return 1
-    fi
-
-    tombname=${tombfile%%\.*}
-    func "tomb found: ${tombdir}/${tombfile}"
-    # now check if the key is kept beside or in args
-    # we use the extension .key
-
-    # the problem with .tomb.gpg is that decoding by hand using gpg it
-    # can override the tomb contents if the key is in the same
-    # directory than the tomb
-    if [ $KEY ]; then
-	tombkey=$KEY # commandline -k flag
-	act "tomb key specified manually: $tombkey"
-    elif [ -r ${tombdir}/${tombname}.tomb.key ]; then
-	tombkey=${tombdir}/${tombname}.tomb.key
-	act "key found for tomb '${tombname}': ${tombkey}"
-    else
-	error "key not found for tomb '${tombname}'"
-	return 1
-    fi
-    return 0
-}
-
 usage() {
 	cat <<EOF
 Tomb $VERSION - a strong and gentle undertaker for your secrets
@@ -269,7 +222,7 @@ EOF
 if [ "$STEGHIDE" = 1 ]; then
     cat <<EOF
  bury       hide a tomb key FILE inside a jpeg PLACE
- exhume     extract a tomb key FILE from a jpeg PL
+ exhume     extract a tomb key FILE from a jpeg PLACE
 EOF
 fi
 cat <<EOF
@@ -527,10 +480,23 @@ mount_tomb() {
         return 1
     fi
 
-    # TODO: eliminate this function
-    get_arg_tomb $CMD2
-
+    # set up variables to be used
+    # the full path is made with $tombdir/$tombfile
     local tombkey
+    local tombfile
+    local tombdir
+    local tombname
+    tombfile=`basename ${CMD2}`
+    tombdir=`dirname ${CMD2}`
+    # check file type (if its a Luks fs)
+    file ${tombdir}/${tombfile} | grep -i 'luks encrypted file' 2>&1 >/dev/null
+    if [ $? != 0 ]; then
+	error "$CMD2 is not a valid tomb file, operation aborted"
+	return 1
+    fi
+    tombname=${tombfile%%\.*}
+    func "tomb found: ${tombdir}/${tombfile}"
+
     if option_is_set -k ; then
 	if [[ "`option_value -k`" == "-" ]]; then
 	    # take key from stdin
@@ -544,12 +510,12 @@ mount_tomb() {
 	fi
     else
 	# guess key as lying besides the tomb
-        tombkey="${PARAM[1]}.key"
+        tombkey=${tombdir}/${tombfile}.key
     fi
-    echo the key used is $tombkey
-    if [ $? != 0 ]; then
-        error "operation aborted."
-        return 1
+    if ! [ -r ${tombkey} ]; then
+	error "key file not found: ${tombkey}"
+	error "operation aborted."
+	return 1
     fi
 
     if ! [ $CMD3 ]; then
@@ -557,33 +523,22 @@ mount_tomb() {
         act "mountpoint not specified, using default: $tombmount"
     elif ! [ -x $CMD3 ]; then
         error "mountpoint $CMD3 doesn't exist, operation aborted."
-        if [ -n "$usbkey_mount" ]; then
-            umount $usbkey_mount
-            rmdir  $usbkey_mount
-            unset usbkey_mount
-        fi
         return 1
     else
-        tombmount=$CMD3
+	tombmount=${CMD3}
     fi
-
+    
     # check if its already open
-    mount -l | grep "${tombname}.tomb.*\[$tombname\]$" 2>&1 > /dev/null
+    mount -l | grep "${tombfile}.*\[$tombname\]$" 2>&1 > /dev/null
     if [ $? = 0 ]; then
-        error "$tombname is already mounted on $tombmount"
-        act "tomb list - show all tombs currently open"
-        if [ -n "$usbkey_mount" ]; then
-            umount $usbkey_mount
-            rmdir  $usbkey_mount
-            unset usbkey_mount
-        fi
-        error "operation aborted."
-        return 1
+        error "$tombname is already open on $tombmount"
+	act "here below its status is reported:"
+	list_tombs ${tombname}
+	return 1
     fi
-
+    
     notice "mounting $tombfile on mountpoint $tombmount"
 
-
     # we need root from here on
     mkdir -p $tombmount
 
@@ -641,7 +596,7 @@ mount_tomb() {
 
     act "encrypted storage filesystem check"
     fsck -p -C0 /dev/mapper/${mapper}
-    act "tomb engraved as $tombname"
+    func "tomb engraved as $tombname"
     tune2fs -L ${tombname} /dev/mapper/${mapper} > /dev/null
 
     mount -o $MOUNTOPTS /dev/mapper/${mapper} ${tombmount}
@@ -1067,7 +1022,7 @@ list_tombs() {
 	tombused=${ts[(ws:;:)2]}
 	tombavail=${ts[(ws:;:)3]}
 	tombpercent=${ts[(ws:;:)4]}
-    tombp=${tombpercent%%%}
+	tombp=${tombpercent%%%}
 	tombsince=`date --date=@${mapper[(ws:.:)3]} +%c`
 
 	# breaking up such strings is good for translation
@@ -1078,29 +1033,31 @@ list_tombs() {
 	print "$fg_bold[white]$tombfs $tombfsopts"
 
 	print -n "$fg_no_bold[green]$tombname"
+	print -n "$fg_no_bold[white] open since "
+	print "$fg_bold[white]$tombsince$fg_no_bold[white]"
+
+	print -n "$fg_no_bold[green]$tombname"
 	print -n "$fg[white] size "
 	print -n "$fg_bold[white]$tombtot"
 	print -n "$fg_no_bold[white] of which "
 	print -n "$fg_bold[white]$tombused"
-	print -n "$fg_no_bold[white] used "
+	print -n "$fg_no_bold[white] used: "
 	print -n "$fg_bold[white]$tombavail"
 	print -n "$fg_no_bold[white] free ("
 	print -n "$fg_bold[white]$tombpercent"
 	print "$fg_no_bold[white] full)"
     
-    if [[ ${tombp} -ge 90 ]]; then
-        error "Your tomb is almost full!"
-    fi
+	if [[ ${tombp} -ge 90 ]]; then
+	    print -n "$fg_no_bold[green]$tombname"
+            print "$fg_bold[red] Your tomb is almost full!"
+	fi
 
-	print -n "$fg_no_bold[green]$tombname"
-	print -n "$fg_no_bold[white] open since "
-	print "$fg_bold[white]$tombsince$fg_no_bold[white]"
 	
 	# now check hooks
 	mtomb=`sed 's:\/:\\\/:g' <<< $tombmount`
 	mounted_hooks=`mount | awk "/^$mtomb/"' {print $1 ";" $3}'`
 	for h in ${(f)mounted_hooks}; do
-	    print -n "$fg[green]$tombname"
+	    print -n "$fg_no_bold[green]$tombname"
 	    print -n "$fg_no_bold[white] hooks "
 	    print -n "$fg_bold[white]`basename ${h[(ws:;:)1]}`"
 	    print -n "$fg_no_bold[white] on "