summaryrefslogtreecommitdiffstats
path: root/data/vifm-media
blob: 81e503a5f64323b94f5efc5beeb2690596dd1229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash

# This script is meant to support common media managing utilities.
#
# Parameters:
#  - list           -- list media
#  - mount <device> -- mount a device
#  - unmount <path> -- unmount given mount point
#
# Supported utilities (only one is used, listed higher to lower priority):
#  - udevil: udisks-like command-line tool with no D-Bus or daemon
#    requirements: `udevil` and `devmon` commands (both are part of udevil)
#  - udisks: first vesion of disk management D-Bus daemon
#    requirements: `udisks` and `umount` commands
#  - udisks2: second vesion of disk management D-Bus daemon
#    requirements: `udisksctl` and either `python2` or `python3` with "dbus"
#                  module
#
# Support for additional protocols (used in addition to standard drives, but
# support depends on installed dependencies):
#  - MTP: /dev/libmtp-* devices
#    requirements: requires `simple-mtpfs` command to obtain device's name or
#                  mount/unmount it
#    limitations: can list and unmount only if this script was used for mounting
#                 because source field of a FUSE mount is mounter's name rather
#                 than the mounted device

function usage_error() {
    echo "Usage: vifm-media list | mount <device> | unmount <path>"
    exit 1
}

function list() {
    while read -r dev; do
        if [ "${dev:0:10}" = /dev/disk/ ]; then
            continue
        fi

        local out=$(info "$dev")

        if grep -qe '^\s*partition table:\s*$' <<< "$out"; then
            # can't mount the whole drive
            continue
        fi

        if grep -qe '^\s*removable:\s*0\s*$' <<< "$out" &&
           grep -qe '^\s*system internal:\s*1\s*$' <<< "$out"; then
            # skip drives which aren't detachable
            continue
        fi

        if grep -qe '^\s*has media:\s*0\s*$' <<< "$out"; then
            continue
        fi

        echo device="$dev"

        local label=$(grep -m1 '^\s*label:.*$' <<< "$out" | sed 's/^[^:]*:\s*//')
        echo "label=$label"

        local paths=$(grep -m1 '^\s*mount paths:.*$' <<< "$out" | sed 's/^[^:]*:\s*//')
        IFS=',' paths=( $paths )
        for path in "${paths[@]}"; do
            echo "mount-point=$path"
        done

        echo
    done <<< "$("$monitor" --enumerate-device-files)"
}

if type -P devmon udevil &>/dev/null; then
    monitor=devmon

    function info() {
        udevil info "$@"
    }
    function mount() {
        devmon --mount "$1"
    }
    function unmount() {
        devmon --unmount "$1"
    }
elif type -P udisks umount &>/dev/null; then
    monitor=udisks

    function info() {
        udisks --show-info "$@"
    }
    function mount() {
        udisks --mount "$1"
    }
    function unmount() {
        umount "$1"
    }
elif python2 -c 'import dbus' &>/dev/null ||
     ${python3:=python3} -c 'import dbus' &>/dev/null &&
     type -P udisksctl &>/dev/null; then
    python=${python3:-python2}
    function udisks2() {
        $python - "$@" <<"EOF"
import dbus
import sys

def decode(array):
    return bytearray(array).replace(b'\x00', b'').decode('utf-8')

def devs():
    bus = dbus.SystemBus()
    ud_manager_obj = bus.get_object('org.freedesktop.UDisks2',
                                    '/org/freedesktop/UDisks2')
    om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
    for v in om.GetManagedObjects().values():
        drive_info = v.get('org.freedesktop.UDisks2.Block', {})
        part_table = v.get('org.freedesktop.UDisks2.PartitionTable')
        usage = drive_info.get('IdUsage')
        system = drive_info.get('HintSystem')
        ro = drive_info.get('ReadOnly')
        if usage == "filesystem" and \
           not system and \
           not ro and \
           part_table is None:
            device = drive_info.get('Device')
            if device is not None:
                device = decode(device)
                fs = v.get('org.freedesktop.UDisks2.Filesystem', {})
                yield (device, drive_info.get('IdLabel'), fs)

def list_usb():
    for (device, label, fs) in devs():
        print('device=%s' % device)
        print('label=%s' % label)
        for point in fs.get('MountPoints', {}):
            point = decode(point)
            print("mount-point=%s" % point)

def path_to_dev(path):
    import os
    path = os.path.normpath(path)
    for (device, label, fs) in devs():
        for point in fs.get('MountPoints', {}):
            point = os.path.normpath(decode(point))
            if point == path:
                return device
    return None

def mount(device):
    bus = dbus.SystemBus()
    obj = bus.get_object('org.freedesktop.UDisks2',
                         '/org/freedesktop/UDisks2/block_devices%s'%device[4:])
    print(obj.Mount({}, dbus_interface='org.freedesktop.UDisks2.Filesystem'))

def unmount(path):
    device = path_to_dev(path)
    if device is None:
        sys.exit(1)

    bus = dbus.SystemBus()
    obj = bus.get_object('org.freedesktop.UDisks2',
                         '/org/freedesktop/UDisks2/block_devices%s'%device[4:])
    obj.Unmount({}, dbus_interface='org.freedesktop.UDisks2.Filesystem')

if len(sys.argv) < 2 or sys.argv[1] == 'list':
    list_usb()
elif len(sys.argv) == 3 and sys.argv[1] == 'mount':
    mount(sys.argv[2])
elif len(sys.argv) == 3 and sys.argv[1] == 'unmount':
    unmount(sys.argv[2])
EOF
    }
    function list() {
        udisks2 list
    }
    function mount() {
        udisks2 mount "$1"
    }
    function unmount() {
        udisks2 unmount "$1"
    }
else
    echo "Neither of the supported backends were found" \
         "(udevil, udisks, udisks2)" >&2
    exit 1
fi

function have_simple_mtpfs() {
    type simple-mtpfs > /dev/null 2>&1
}

function make_mtp_mount_path() {
    local dev=$1

    echo "/tmp/vifm-media$(echo -n "$dev" | tr -c 'a-zA-Z0-9-' _)"
}

function is_mtp_mount_path() {
    local path=$1

    case "$path" in
        /tmp/vifm-media*) return 0 ;;
        *)                return 1 ;;
    esac
}

function list_mtp() {
    for dev in /dev/libmtp-*; do
        echo device="$dev"

        if have_simple_mtpfs; then
            local label=$(simple-mtpfs --list-devices "$dev" | cut -f2- -d' ')
            echo "label=$label"
        fi

        echo -n /dev/libmtp-1-9 | tr -c 'a-zA-Z0-9-' _

        local mount_path=$(make_mtp_mount_path "$dev")
        if findmnt "$mount_path"; then
            echo "mount-point=$mount_path"
        fi
    done
}

function is_mtp() {
    local dev=$1

    case "$dev" in
        /dev/libmtp-*) return 0 ;;
        *)             return 1 ;;
    esac
}

function mount_mtp() {
    local dev=$1

    if ! have_simple_mtpfs; then
        echo "vifm-media: need simple-mtpfs command to mount over MTP" 1>&2
        exit 1
    fi

    local mount_path=$(make_mtp_mount_path "$dev")
    if [ -e "$mount_path" ]; then
        if ! rmdir "$mount_path"; then
            exit 1
        fi
    fi

    if ! ( umask 077 && mkdir "$mount_path" ); then
        exit 1
    fi

    if ! simple-mtpfs "$dev" "$mount_path"; then
        rmdir "$mount_path"
        exit 1
    fi
}

function unmount_mtp() {
    local path=$1

    umount "$path" && rmdir "$path"
}

case "$1" in
    list)
        if [ $# -ne 1 ]; then
            usage_error
        fi
        list
        list_mtp
        ;;
    mount)
        if [ $# -ne 2 ]; then
            usage_error
        fi

        if is_mtp "$2"; then
            mount_mtp "$2"
        else
            mount "$2"
        fi
        ;;
    unmount)
        if [ $# -ne 2 ]; then
            usage_error
        fi

        if is_mtp_mount_path "$2"; then
            unmount_mtp "$2"
        else
            unmount "$2"
        fi
        ;;

    *)
        usage_error
        ;;
esac