From 5a176bbe128a265a0be81390f5109e5ed28d2a75 Mon Sep 17 00:00:00 2001 From: David Runge Date: Thu, 10 Apr 2014 19:07:38 +0200 Subject: Fixed udisksctl functions to work with external drives, mmc devices and optical drives. --- .zsh.after/udisks.zsh | 447 ++++++++++++++++++++++++-------------------------- 1 file changed, 211 insertions(+), 236 deletions(-) diff --git a/.zsh.after/udisks.zsh b/.zsh.after/udisks.zsh index 784c616..3478aea 100644 --- a/.zsh.after/udisks.zsh +++ b/.zsh.after/udisks.zsh @@ -1,270 +1,245 @@ -#!/usr/bin/env bash - -# udisks_functions: https://bbs.archlinux.org/viewtopic.php?id=109307 -# last modified: 2012/08/05 -# functions: sdm, sdu, mcm, mcu, srm, sru, fdm, and fdu. -# aliases: sd*m, sd*u, mc*m, and mc*u. - -# options: -enable_device="true" -enable_mmc="true" -enable_optical="true" -enable_floppy="true" -enable_device_aliases="true" -enable_mmc_aliases="true" +# EXAMPLES: +# - mounting devices +# sdm sdb1 // mount first partition on sdb +# sdm sdb 1 // mount first partition on sdb +# sdm sdb // mount all available partitions on sdb +# sdm // mount all available devices +# mcm mmcblk0p1 // mount first partition on mmcblk0 +# mcm mmcblk0 1 // mount first partition on mmcblk0 +# mcm mmcblk0 // mount all partitions on mmcblk0 +# mcm // mount all partitions on all mmcblk devices +# srm 0 // mount first optical drive +# srm // mount all optical drives +# +# - unmounting devices +# sdu sdb1 // unmount first partition on sdb +# sdu sdb 1 // unmount first partition on sdb +# sdu sdb // unmount all available partitions on sdb +# sdu // unmount all available devices +# mcu mmcblk0p1 // unmount first partition on mmcblk0 +# mcu mmcblk0 1 // unmount first partition on mmcblk0 +# mcu mmcblk0 // unmount all partitions on mmcblk0 +# mcu // unmount all partitions on all mmcblk devices +# sru 0 // unmount first optical drive +# sru // unmount all optical drives +# # mount commands: mount="udisksctl mount -b" unmount="udisksctl unmount -b" -# partitions: -device_partition="[1-9]*" -mmc_partition="p[0-9]*" - -# device drives: -device="sd[b-j]" - -# mmc drives: -mmc="mmcblk[0-9]" - -# optical drives: -optical="sr[0-2]" - -# floppy drives: -floppy="fd[0-2]" - -# device aliases: -if [[ $enable_device_aliases = true && $enable_device = true ]]; then - for a in {b..j}; do - alias sd"$a"m="sdm sd$a" - alias sd"$a"u="sdu sd$a" - done -fi - -# mmc aliases: -if [[ $enable_mmc_aliases = true && $enable_mmc = true ]]; then - for a in {0..9}; do - alias mc"$a"m="mcm mmcblk$a" - alias mc"$a"u="mcu mmcblk$a" - done -fi +# Path to all devices +folder="/dev/" + +# Devices of the form /dev/sd{b,c,d,e,f,g,h,i,j,k,l,m,n} +# path +dev="/dev/sd" +# Device numbers/names +devices=( b c d e f g h i j k l m n ) +# Partition numbers +device_partitions=( 1 2 3 4 5 6 7 8 9 ) + +# Devices of the form /dev/mmcblk{0,1,2,3,4,5,6,7,8,9}p{1,2,3,4,5,6,7,8,9} +# path +mmc="/dev/mmcblk" +# Device numbers/names +mmc_devices=( 0 ) +# Partition numbers +mmc_partitions=( p1 p2 p3 p4 p5 p6 p7 p8 p9 ) + +# Devices of the form /dev/sr{0,1,2} +# path +optical="/dev/sr" +# Device numbers/names +optical_devices=( 0 1 2 ) # device functions: -if [[ $enable_device = true ]]; then sdm() { - if [[ -n "$2" ]]; then - if [[ "$1" = ?d* ]]; then - if [[ -b /dev/"$1$2" ]]; then - $=mount /dev/"$1$2" + if [[ -n "$2" ]]; then # if 2nd argument is set + if [[ "$1" = ?d* ]]; then # check if 1st argument features the letter d + if [[ -b $folder$1$2 ]]; then # check if block device $1$2 is available + $=mount $folder$1$2 + fi + fi + elif [[ -n "$1" ]]; then # if 1st argument is set + if [[ "$1" = ?d* ]]; then # check if 1st argument featurs a letter d + if [[ -b $folder$1"1" ]]; then #check if first device is available + for a in $device_partitions; do # loop through device partitions of first argument + echo "$folder$1$a" + if [[ -b $folder$1$a ]]; then # checking for each device if available and block device + $=mount "$folder$1$a" + fi + done + else + echo "Trying to mount: $folder$1" + if [[ -b $folder$1 ]]; then # check if 1st argument already is the block device to mount + $=mount $folder$1 fi fi - elif [[ -n "$1" ]]; then - if [[ "$1" = ?d* ]]; then - if [[ -b /dev/"$1"1 ]]; then - if [[ "$1" = ????* ]]; then - $=mount /dev/"$1" - else - for a in /dev/"$1"$device_partition; do - $=mount "$a" - done - fi - else - if [[ -b /dev/"$1" ]]; then - $=mount /dev/"$1" + fi + else # here we loop all available block devices and mount them + # TODO: check with df if the device is already mounted + for a in $devices; do # loop through the list of all devices + if [[ -b $dev$a ]]; then # check if first partition of block device is available + for b in $device_partitions; do # loop through the list of all partitions + if [[ -b $dev$a$b ]]; then # if it's a block device + echo "Trying to mount: $dev$a$b" + $=mount "$dev$a$b" fi - fi + done + fi + done + fi +} + +sdu() { + if [[ -n "$2" ]]; then + if [[ "$1" = ?d* ]]; then + if [[ -b $folder$1$2 ]]; then + echo "Trying to unmount: $folder$1$2" + $=unmount $folder$1$2 fi - else - for a in /dev/$device; do - if [[ -b "$a"1 ]]; then - for b in "$a"$device_partition; do - $=mount "$b" - done - else - $=mount "$a" - fi - done fi - } - - sdu() { - if [[ -n "$2" ]]; then - if [[ "$1" = ?d* ]]; then - if [[ -b /dev/"$1$2" ]]; then - $=unmount /dev/"$1$2" + elif [[ -n "$1" ]]; then + if [[ "$1" = ?d* ]]; then + if [[ -b $folder$1"1" ]]; then + for a in $device_partitions; do + if [[ -b $folder$1$a ]]; then # checking for each device if available and block device + echo "Trying to unmount: $folder$1$a" + $=unmount $folder$1$a + fi + done + else + if [[ -b $folder$1 ]]; then + echo "Trying to unmount: $folder$1" + $=unmount $folder$1 fi fi - elif [[ -n "$1" ]]; then - if [[ "$1" = ?d* ]]; then - if [[ -b /dev/"$1"1 ]]; then - if [[ "$1" = ????* ]]; then - $=unmount /dev/"$1" - else - for a in /dev/"$1"$device_partition; do - $=unmount "$a" - done - fi - else - if [[ -b /dev/"$1" ]]; then - $=unmount /dev/"$1" + fi + else # unmount all devices mounted + for a in $devices; do + if [[ -b $dev$a ]]; then + for b in $device_partitions; do + if [[ -b $dev$a$b ]]; then # if it's a block device + echo "Trying to unmount: $dev$a$b" + $=unmount $dev$a$b fi - fi + done fi - else - for a in /dev/$device; do - if [[ -b "$a"1 ]]; then - for b in "$a"$device_partition; do - $=unmount "$b" - done - else - $=unmount "$a" - fi - done - fi - } -fi + done + fi +} # mmc functions: -if [[ $enable_mmc = true ]]; then - mcm() { - if [[ -n "$2" ]]; then - if [[ "$1" = mmcblk* ]]; then - if [[ -b /dev/"$1$2" ]]; then - $=mount /dev/"$1$2" - fi +mcm() { + if [[ -n "$2" ]]; then + if [[ "$1" = mmcblk* ]]; then + if [[ -b $folder$1"p"$2 ]]; then + echo "Trying to mount: $folder$1"p"$2" + $=mount $folder$1"p"$2 fi - elif [[ -n "$1" ]]; then - if [[ "$1" = mmcblk* ]]; then - if [[ -b /dev/"$1"p0 ]]; then - if [[ "$1" = ???????* ]]; then - $=mount /dev/"$1" - else - for a in /dev/"$1"$mmc_partition; do - $=mount "$a" - done - fi - else - if [[ -b /dev/"$1" ]]; then - $=mount /dev/"$1" - fi - fi - fi - else - for a in /dev/$mmc; do - if [[ -b "$a"p0 ]]; then - for b in "$a"$mmc_partition; do - $=mount "$b" - done - else - $=mount "$a" - fi - done fi - } - - mcu() { - if [[ -n "$2" ]]; then - if [[ "$1" = mmcblk* ]]; then - if [[ -b /dev/"$1$2" ]]; then - $=unmount /dev/"$1$2" + elif [[ -n "$1" ]]; then + if [[ "$1" = mmcblk* ]]; then + if [[ -b $folder$1"p1" ]]; then + for a in $mmc_partitions; do + if [[ -b $folder$1$a ]]; then + echo "Trying to mount: $folder$1$a" + $=mount $folder$1$a + fi + done + else + if [[ -b $folder$1 ]]; then + echo "Trying to mount: $folder$1" + $=mount $folder$1 fi fi - elif [[ -n "$1" ]]; then - if [[ "$1" = mmcblk* ]]; then - if [[ -b /dev/"$1"p0 ]]; then - if [[ "$1" = ???????* ]]; then - $=unmount /dev/"$1" - else - for a in /dev/"$1"$mmc_partition; do - $=unmount "$a" - done - fi - else - if [[ -b /dev/"$1" ]]; then - $=unmount /dev/"$1" + fi + else + for a in $mmc_devices; do + if [[ -b $mmc$a"p1" ]]; then + for b in $mmc_partitions; do + if [[ -b $mmc$a$b ]]; then # if it's a block device + echo "Trying to mount: $mmc$a$b" + $=mount $mmc$a$b fi - fi + done fi - else - for a in /dev/$mmc; do - if [[ -b "$a"p0 ]]; then - for b in "$a"$mmc_partition; do - $=unmount "$b" - done - else - $=unmount "$a" - fi - done - fi - } -fi - -# optical functions: -if [[ $enable_optical = true ]]; then - srm() { - if [[ -n "$1" ]]; then - if [[ "$1" = ? ]]; then - if [[ -b /dev/sr"$1" ]]; then - $=mount /dev/sr"$1" - fi + done + fi +} + +mcu() { + if [[ -n "$2" ]]; then + if [[ "$1" = mmcblk* ]]; then + if [[ -b $folder$1"p"$2 ]]; then + echo "Trying to unmount: $folder$1"p"$2" + $=unmount $folder$1"p"$2 fi - else - for a in /dev/$optical; do - if [[ -b "$a" ]]; then - $=mount "$a" - fi - done fi - } - - sru() { - if [[ -n "$1" ]]; then - if [[ "$1" = ? ]]; then - if [[ -b /dev/sr"$1" ]]; then - $=unmount /dev/sr"$1" + elif [[ -n "$1" ]]; then + if [[ "$1" = mmcblk* ]]; then + if [[ -b $folder$1"p1" ]]; then + for a in $mmc_partitions; do + echo "Trying to unmount: $folder$1$a" + $=unmount $folder$1$a + done + else + if [[ -b $folder$1 ]]; then + echo "Trying to unmount: $folder$1" + $=unmount $folder$1 fi fi - else - for a in /dev/$optical; do - if [[ -b "$a" ]]; then - $=unmount "$a" - fi - done fi - } -fi - -# floppy functions: -if [[ $enable_floppy = true ]]; then - fdm() { - if [[ -n "$1" ]]; then - if [[ "$1" = ? ]]; then - if [[ -b /dev/fd"$1" ]]; then - $=mount /dev/fd"$1" - fi + else + for a in $mmc_devices; do + if [[ -b "$mmc$a${mmc_partitions[0]}" ]]; then + echo $mmc$a${mmc_partitions[0]} + for b in $mmc_partitions; do + if [[ -b $mmc$a$b ]]; then # if it's a block device + echo "Trying to unmount: $mmc$a$b" + $=unmount $mmc$a$b + fi + done + fi + done + fi +} + +# sr functions: +srm() { + if [[ -n "$1" ]]; then + if [[ "$1" = ? ]]; then + if [[ -b $optical$1 ]]; then + echo "Trying to mount: $optical$1" + $=mount $optical$1 fi - else - for a in /dev/$floppy; do - if [[ -b "$a" ]]; then - $=mount "$a" - fi - done fi - } - - fdu() { - if [[ -n "$1" ]]; then - if [[ "$1" = ? ]]; then - if [[ -b /dev/fd"$1" ]]; then - $=unmount /dev/fd"$1" - fi + else + for a in $optical_devices; do + if [[ -b $optical$a ]]; then + echo "Trying to mount: $optical$a" + $=mount $optical$a + fi + done + fi +} + +sru() { + if [[ -n "$1" ]]; then + if [[ "$1" = ? ]]; then + if [[ -b $optical$1 ]]; then + echo "Trying to unmount: $optical$1" + $=unmount $optical$1 fi - else - for a in /dev/$floppy; do - if [[ -b "$a" ]]; then - $=unmount "$a" - fi - done fi - } -fi + else + for a in $optical_devices; do + if [[ -b $optical$a ]]; then + echo "Trying to unmount: $optical$a" + $=unmount $optical$a + fi + done + fi +} + -- cgit v1.2.3-54-g00ecf