Index: scripts/factory_reset.sh =================================================================== diff -u -r5610b1404271cf1be791b99903fa73ad4f7b7dc9 -r75134a10d974e471981d6b77d7e040cde08f0d98 --- scripts/factory_reset.sh (.../factory_reset.sh) (revision 5610b1404271cf1be791b99903fa73ad4f7b7dc9) +++ scripts/factory_reset.sh (.../factory_reset.sh) (revision 75134a10d974e471981d6b77d7e040cde08f0d98) @@ -19,35 +19,71 @@ # @details # This file is called by UI Software to remove and reset the device +ERR_FAILED_RM_WIFI_CONF=150 +ERR_FAILED_CLEAR_BT_CACHE=151 +ERR_FAILED_RESET_SETTINGS_CONF=152 +ERR_FAILED_CLEAR_LOGS=153 +ERR_FAILED_RM_SSH=154 + LOC_SETTINGS_CONF="/home/root/.config/Settings/System.conf" LOC_DEFAULT_SETTINGS_CONF="/home/root/scripts/default_settings.conf" #TODO update to reflect real location LOC_WIFI_CONF="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" -LOC_BLUETOOTH_CONF="/etc/bluetooth/main.conf" -LOC_SSH_FILE="$HOME/.ssh/" +LOC_BLUETOOTH_CACHE_FOLDER="/var/lib/bluetooth/" +LOC_SSH_FOLDER="$HOME/.ssh/" +LOC_LOG_PARENT_FOLDER="/media/sd-card/" -# ------------------------------ Remove Wifi Conf -if [ -f $LOC_WIFI_CONF ]; then - rm "$LOC_WIFI_CONF" - echo "Deleted Wifi Conf" -fi +# removes all the files in the directory and subdirectory in a recursive manner +# exits with passed error code if either rm command fails or folder is not empty of files +# %1 - Directory path +# %2 - Error message string/msg +# %3 - Error code to return on exit in case of error +function clearFolderContent() { + if [ -f $1 ]; then + rm -rf "$1/*/*" + fi -# ------------------------------ Remove Bluetooth Conf + rmCommandReturn=$? + fileCount=$(find $1 -type f | wc -l) + + if [ "$fileCount" -ne 0 ] || [ "$rmCommandReturn" -ne 0 ]; then + echo "Error : $2 ($fileCount)" + exit $3 + fi +echo "$2 success" +} -if [ -f $LOC_BLUETOOTH_CONF ]; then - rm "$LOC_BLUETOOTH_CONF" - echo "Deleted Bluetooth Conf" -fi +# Removes a single file +# exits with passed error code if either rm command fails or if file was not removed +# %1 - path to file +# %2 - Error message string/msg +# %3 - Error code to return on exit in case of error +function removeSingleFile() { + if [ -f $1 ]; then + rm "$1" + fi + if [ -f $1 ]; then + echo "Error: $2" + # the file still exists, exit with error message + exit $3 + fi +} +# ------------------------------ Remove Wifi Conf +removeSingleFile $LOC_WIFI_CONF "Failed to remove WiFi conf file" $ERR_FAILED_RM_WIFI_CONF + +# ------------------------------ Remove Bluetooth cache +clearFolderContent $LOC_BLUETOOTH_CACHE_FOLDER "Failed to remove Bluetooth cache" $ERR_FAILED_CLEAR_BT_CACHE + # ------------------------------ Reset Settings Conf +storedPassword=$(grep Password $LOC_SETTINGS_CONF) cp $LOC_DEFAULT_SETTINGS_CONF $LOC_SETTINGS_CONF +sed -i "s/^.*Password.*$/$storedPassword/" $LOC_SETTINGS_CONF # ------------------------------ Remove Logs -rm -rf /media/sd-card/*/* +#TODO this will always fail due to the fact UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be >0 +clearFolderContent $LOC_LOG_PARENT_FOLDER "Failed to remove logs" $ERR_FAILED_CLEAR_LOGS # ------------------------------ Remove ssh -if [ -d $LOC_SSH_FILE ]; then - rm -rf "$LOC_SSH_FILE" - echo "Deleted .ssh" -fi +clearFolderContent $LOC_SSH_FOLDER "Failed to remove ssh" $ERR_FAILED_RM_SSH -exit 0 +exit