[or-cvs] r14642: Made the progress bar actually work in the USB copying scrip (incognito/trunk/root_overlay/usr/sbin)

anonym at seul.org anonym at seul.org
Fri May 16 20:21:58 UTC 2008


Author: anonym
Date: 2008-05-16 16:21:58 -0400 (Fri, 16 May 2008)
New Revision: 14642

Modified:
   incognito/trunk/root_overlay/usr/sbin/create-usb
Log:
Made the progress bar actually work in the USB copying script.


Modified: incognito/trunk/root_overlay/usr/sbin/create-usb
===================================================================
--- incognito/trunk/root_overlay/usr/sbin/create-usb	2008-05-16 20:13:10 UTC (rev 14641)
+++ incognito/trunk/root_overlay/usr/sbin/create-usb	2008-05-16 20:21:58 UTC (rev 14642)
@@ -5,10 +5,10 @@
 #
 
 DEFAULT_WIDTH=80
-# declare OTHER_BLOCK to allow block devices besides USB
 
-# show an xdialog of available USB drives. the one which the user choses is partitioned into
-# a single big partition. USBPART will stored the new partition and USBDEV the device.
+# Show an Xdialog of available USB drives. The one which the user choses is
+# partitioned into a single big partition. USBPART will stored the path to
+# the new partition and USBDEV the path to the device.
 format_usb() {
     # Find the mkfs.vfat command
     MKFS_VFAT="$(which mkfs.vfat 2>/dev/null)"
@@ -75,8 +75,9 @@
     fi
 }
 
-# show an xdialog of available vfat partitions on attached USB drives. The chosen partition
-# will be stored in USBPART and the device it resides on is stored in USBDEV.
+# Show an Xdialog of available vfat partitions on attached USB drives. The
+# path to chosen partition will be stored in USBPART and the path to the 
+# device it resides on is stored in USBDEV.
 get_vfat() {
     I=0
 
@@ -122,7 +123,50 @@
     USBDEV="${USBPART/%[0-9]}"
 }
 
+# Copies the file with path equal to the first parameter to the file with path
+# equal to the second paramter. When a 1MB block has been copied (or block of
+# less than 1MB if it's the last one) a dot (.) is written, which can be used
+# by Xdialog. Upon error, ERR will be set to 1, otherwise 0.
+file_copy() {
+    SRC=$1
+    DST=$2
+    ERR=0
 
+    # Size of source file and the block size, in bytes
+    SIZE=$( stat --format=%s $SRC )
+    BLOCK_SIZE=$(( 1024*1024 ))
+
+    # The number of blocks, accounting for the last block even if smaller
+    # than BLOCK_SIZE.
+    BLOCKS=$(( (${SIZE}+${BLOCK_SIZE}-1)/${BLOCK_SIZE} ))
+
+    # Make sure permissions are OK
+    if [[ -r ${SRC} ]]; then
+        if [[ -e ${DST} ]]; then
+            if [[ -w ${DST} ]]; then
+                rm $DST
+            else
+                ERR=1
+                return
+            fi
+        fi
+    else
+        ERR=1
+        return
+    fi
+
+    # Copy the file, one block at a time
+    for i in $(seq 0 1 $(( ${BLOCKS} - 1 ))); do
+        dd if=${SRC} of=${DST} oflag=append conv=notrunc \
+           seek=${i} skip=${i} bs=${BLOCK_SIZE}c count=1 &> /dev/null
+	sync
+        echo -n "."
+    done
+
+    echo
+}
+
+
 # Main script:
 
 # Root of the Live CD
@@ -130,7 +174,7 @@
     MEDIAROOT="/mnt/cdrom"
 fi
 
-# determine isolinux/syslinux config directory
+# Determine isolinux/syslinux config directory
 if [[ -d "${MEDIAROOT}/syslinux" ]]; then
 	SYSLINUX="syslinux"
 elif [[ -d "${MEDIAROOT}/isolinux" ]]; then
@@ -181,7 +225,7 @@
 fi
 
 # Mount the drive
-MTPT="/mnt/createusb-mp"
+MTPT="$(mktemp -t -d incognitoXXXXXXXX)"
 mkdir -p ${MTPT}
 mount -v -t vfat "${USBPART}" "${MTPT}"
 if [[ $? -ne 0 ]]; then
@@ -189,23 +233,26 @@
     exit 1
 fi
 
-# Verify free space
+# Maximum capacity in KB
+TOTALSIZE="$(df -k -P "${MTPT}" | tail -n 1 | awk '{ print $2 }')"
+
+# Verify free space in KB
 MEDIAFREE="$(df -k -P "${MTPT}" | tail -n 1 | awk '{ print $4 }')"
 # Account for files that will be overwritten
+# note that "+1023" is for making the division round up
 for FILE in ${COPY_FILES}; do
     for FP in $(ls ${MTPT}/${FILE} 2>/dev/null); do
-	MEDIAFREE=$(( ${MEDIAFREE}+$(stat --format=%s ${FP})/1024 ))
+        MEDIAFREE=$(( ${MEDIAFREE}+($(stat --format=%s ${FP})+1023)/1024))
     done
 done
 # Decrement by space of files to copy
 for FILE in ${COPY_FILES}; do
     for FP in $(ls ${MEDIAROOT}/${FILE} 2>/dev/null); do
-	MEDIAFREE=$(( ${MEDIAFREE}-$(stat --format=%s ${FP})/1024 ))
+        MEDIAFREE=$(( ${MEDIAFREE}-($(stat --format=%s ${FP})+1023)/1024))
     done
 done
 
-echo "Free space of ${MEDIAFREE}k"
-
+# Make sure that at least 128 KB is available after copy
 if [[ ${MEDIAFREE} -lt 128 ]]; then
     ${DIALOG} --msgbox "Not enough free space on USB partition ${USBPART}, need $(( 128-${MEDIAFREE} ))k" 0 ${DEFAULT_WIDTH}
     umount -v "${MTPT}"
@@ -217,35 +264,26 @@
     mkdir -p "${MTPT}/${DIR}" 2>/dev/null
 done
 
-# Copy files
-TOTAL=$( echo ${COPY_FILES} | wc -w )
-CURRENT=0
+# Size of files to copy in MB
+COPY_SIZE=$(( (${TOTALSIZE}-${MEDIAFREE})/1024 ))
+
+# Copy files and show progress (note that the progress is approximate, and the
+# the number of dots file_copy probably are greater than COPY_SIZE)
+ERR=0
 for FILE in ${COPY_FILES}; do
     FP="${MEDIAROOT}/${FILE}"
-    if [[ -r "${FP}" ]]; then
-	cp -v "${FP}" "${MTPT}/${FILE//isolinux/syslinux}"
-    else
-	cp -v ${FP} "${MTPT}/${FILE//isolinux/syslinux}"
-    fi
-    ERR=$?
-    if [[ $? -ne 0 ]]; then
-	echo "cp gave error ${ERR}" >&2
+    file_copy "${FP}" "${MTPT}/${FILE//isolinux/syslinux}"
+    if [[ ${ERR} -ne 0 ]]; then
+	echo "Error copying file ${FP} to ${MTPT}/${FILE//isolinux/syslinux}" >&2
 	${DIALOG} --msgbox "Could not copy file ${FILE} to USB drive" 0 ${DEFAULT_WIDTH}
 	umount -v "${MTPT}"
 	rmdir "${MTPT}"
-	export FAILED=1
 	exit 1
     fi
-    [[ "${TOTAL}" -gt 0 ]] && echo $(( (${CURRENT}*100)/${TOTAL} ))
-    CURRENT=$(( ${CURRENT}+1 ))
-done | ${DIALOG} --progress "Copying files" 0 ${DEFAULT_WIDTH}
+done | ${DIALOG} --progress "Copying files" 0 ${DEFAULT_WIDTH} ${COPY_SIZE}
 
-if [[ "${FAILED}" -eq 1 ]]; then
-    exit 1
-fi
-
 # Unmount the drive
-( echo 90 ; echo 90 ; umount -v "${MTPT}" ) | ${DIALOG} --progress "Completing USB file copy" 0 ${DEFAULT_WIDTH}
+umount -v "${MTPT}"
 rmdir "${MTPT}"
 
 # Make bootable
@@ -255,12 +293,14 @@
     exit 1
 fi
 
+# Copy syslinux MBR
 dd if=/usr/lib/syslinux/mbr.bin "of=${USBDEV}"
 if [[ $? -ne 0 ]]; then
     ${DIALOG} --msgbox "Could not copy Master Boot Record to USB drive ${USBDEV} (dd)" 0 ${DEFAULT_WIDTH}
     exit 1
 fi
 
+# Install syslinux
 syslinux "${USBPART}"
 if [[ $? -ne 0 ]]; then
     ${DIALOG} --msgbox "Could not setup boot loader on USB partition ${USBPART} (syslinux)" 0 ${DEFAULT_WIDTH}
@@ -268,7 +308,6 @@
 fi
 
 # Success!
-
 ${DIALOG} --msgbox "Bootable USB drive successfully created" 0 ${DEFAULT_WIDTH}
 
 exit 0



More information about the tor-commits mailing list