docs clarification
[hband-tools.git] / user-tools / chattr-nocow
blob684ec0779d8fe7cb1afb3dd66e5d71c23441e21e
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 chattr-cow - try hard to enable Copy-on-Write attribute on files
10 chattr-nocow - try hard to disable Copy-on-Write attribute on files
12 =cut
14 EOF
17 set -e
18 set -u
20 . /usr/lib/tool/bash-utils
23 is_cow()
25 lsattr -d "$1" | cut -f1 -d' ' | grep -qv 'C'
28 set_cow()
30 local onoff file tmpf expected_cow current_dir_cow
31 local sigs="ERR RETURN INT QUIT TERM"
33 onoff=$1
34 file=$2
35 tmpf=$file.set-no-cow
36 dir=`dirname "$file"`
38 [ $onoff = on ] && expected_cow=-C || expected_cow=+C
39 is_cow "$dir" && current_dir_cow=-C || current_dir_cow=+C
41 if [ $expected_cow != $current_dir_cow ]
42 then
43 chattr $expected_cow "$dir"
44 trap 'trap - $sigs; chattr $current_dir_cow "$dir"' $sigs
47 cp -f --preserve=mode,ownership,timestamps,xattr --no-preserve=links "$file" "$tmpf"
48 mv -f "$tmpf" "$file"
49 return
52 is_in_use()
54 lsof -Fp -- "$1" >/dev/null 2>&1
58 case "${0##*/}" in
59 chattr-nocow)
60 cow=off
61 expected_cow=+C
63 chattr-cow)
64 cow=on
65 expected_cow=-C
68 errx 38 "bad invocation"
70 esac
72 for file in "$@"
74 if [ -d "$file" ]
75 then
76 chattr $expected_cow "$file"
77 else
78 if is_in_use "$file"
79 then
80 errx 16 "$file: is in use"
81 else
82 set_cow $cow "$file"
85 done
87 exit 0