From 7d50f208b7ed3dfa3166d6399acbf965dd262183 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 20:34:26 +0330 Subject: [PATCH] Refactor usb-storage-action To have both add and remove modes --- src/scripts/utils/usb/usb-storage-action | 41 ++++++++++++++++++------ 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/scripts/utils/usb/usb-storage-action b/src/scripts/utils/usb/usb-storage-action index 732403e..c7a03c1 100644 --- a/src/scripts/utils/usb/usb-storage-action +++ b/src/scripts/utils/usb/usb-storage-action @@ -1,16 +1,37 @@ #!/bin/bash +# Get the action (add or remove) and the device name from the udev rule +ACTION=$1 + # Get the device name from the udev rule -DEVICE_NAME=$1 +DEVICE_NAME=$2 + +SOCKET_PATH="/tmp/usb-Sono-Socket.socket" -# Mount the device -udisksctl mount -b "$DEVICE_NAME" +if [ "$ACTION" == "add" ]; then + # Mount the device + udisksctl mount -b "$DEVICE_NAME" -# Check if the mount was successful -if [ $? -eq 0 ]; then - # Define the socket path - SOCKET_PATH="/tmp/usb-Sono-Socket.socket" + # Check if the mount was successful + if [ $? -eq 0 ]; then + # Write a message to the socket + echo "success connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "success connect $DEVICE_NAME" >> /tmp/tmp.log + else + echo "failure connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "failure connect $DEVICE_NAME" >> /tmp/tmp.log + fi +elif [ "$ACTION" == "remove" ]; then + # Unmount the device + udisksctl unmount -b "$DEVICE_NAME" - # Write a message to the socket - echo "USB storage device connected" | socat - UNIX-CONNECT:$SOCKET_PATH - echo "USB storage device connected" >> /tmp/tmp.log + # Check if the unmount was successful + if [ $? -eq 0 ]; then + # Write a message to the socket + echo "success disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "success disconnect $DEVICE_NAME" >> /tmp/tmp.log + else + echo "failure disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "failure disconnect $DEVICE_NAME" >> /tmp/tmp.log + fi +fi