btrfs-progs: check for negative return value from ioctl
[platform/upstream/btrfs-progs.git] / btrfs-completion
1 # original by Alfredo Esteban <aedelatorre at xxxxxxxxx>
2 # (http://www.spinics.net/lists/linux-btrfs/msg15899.html)
3 # edited by Joseph Wang <joequant at gmail.com>
4 # (http://lists.alioth.debian.org/pipermail/bash-completion-devel/2013-June/004868.html)
5 # edited by John C F <john.ch.fr at gmail.com> on 2015-02-02
6
7 _btrfs_devs()
8 {
9     local DEVS
10     DEVS=''; while read dev; do DEVS+="$dev "; done < <(lsblk -pnro name)
11         COMPREPLY+=( $( compgen -W "$DEVS" -- "$cur" ) )
12 }
13
14 _btrfs_mnts()
15 {
16         local MNTS
17         MNTS=''
18         while read mnt; do MNTS+="$mnt "
19         done < <(mount | awk '{print $3}')
20         COMPREPLY+=( $( compgen -W "$MNTS" -- "$cur" ) )
21 }
22
23 _btrfs() 
24 {
25         local cur prev words cword
26     _init_completion || return
27
28     COMPREPLY=()
29     
30         local cmd=${words[1]}
31
32     commands='subvolume filesystem balance device scrub check rescue restore inspect-internal property send receive quota qgroup replace help version'
33     commands_subvolume='create delete list snapshot find-new get-default set-default show sync'
34     commands_filesystem='defragment sync resize show df label usage'
35     commands_balance='start pause cancel resume status'
36     commands_device='scan add delete remove ready stats usage'
37     commands_scrub='start cancel resume status'
38     commands_rescue='chunk-recover super-recover'
39     commands_inspect_internal='inode-resolve logical-resolve subvolid-resolve rootid min-dev-size'
40     commands_property='get set list'
41     commands_quota='enable disable rescan'
42     commands_qgroup='assign remove create destroy show limit'
43     commands_replace='start status cancel'
44
45         if [[ "$cur" == -* && $cword -le 3 && "$cmd" != "help" ]]; then
46                 COMPREPLY=( $( compgen -W '--help' -- "$cur" ) )
47                 return 0
48         fi
49
50     if [[ $cword -eq 1 ]]; then
51         COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
52         return 0
53     elif [[ $cword -eq 2 ]]; then
54         case $cmd in
55             subvolume)
56                 opts="$commands_subvolume"
57                 ;;
58             filesystem)
59                 opts="$commands_filesystem"
60                 ;;
61             balance)
62                 opts="$commands_balance"
63                 ;;
64             device)
65                 opts="$commands_device"
66                 ;;
67             scrub)
68                 opts="$commands_scrub"
69                 ;;
70             check)
71                 _btrfs_devs
72                                 return 0
73                 ;;
74             rescue)
75                 opts="$commands_rescue"
76                 ;;
77             restore)
78                 _btrfs_devs
79                                 return 0
80                 ;;
81             inspect-internal)
82                 opts="$commands_inspect_internal"
83                 ;;
84             property)
85                 opts="$commands_property"
86                 ;;
87             send|receive)
88                 _filedir -d
89                 return 0
90                 ;;
91             quota)
92                 opts="$commands_quota"
93                 ;;
94             qgroup)
95                 opts="$commands_qgroup"
96                 ;;
97             replace)
98                 opts="$commands_replace"
99                 ;;
100             help)
101                 opts="--full"
102                 ;;
103             version)
104                 return 0
105                 ;;
106         esac
107         COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
108         return 0
109     elif [[ $cword -eq 3 ]]; then
110         case $cmd in
111             filesystem)
112                                 case $prev in
113                                         defragment)
114                                                 _filedir
115                                                 return 0
116                                                 ;;
117                                         label)
118                                                 _btrfs_mnts
119                                                 _btrfs_devs
120                                                 return 0
121                                                 ;;
122                                 esac
123                 ;;
124             device|rescue)
125                                 _btrfs_devs
126                 return 0
127                 ;;
128             replace)
129                 case $prev in
130                     status|cancel)
131                                                 _btrfs_mnts
132                         return 0
133                         ;;
134                     start)
135                                                 _btrfs_devs
136                         return 0
137                         ;;
138                 esac
139                 ;;
140         esac
141     fi
142
143     if [[ "$cmd" == "receive" && "$prev" == "-f" ]]; then
144         _filedir
145         return 0
146     fi
147
148     _filedir -d
149     return 0  
150 }
151
152 complete -F _btrfs btrfs