summaryrefslogtreecommitdiffstats
path: root/extras/defrag.sh
blob: 465b097948810d98e325bb66dd6574cc938d71b3 (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
#!/bin/sh

# This script gets called from 'scale-n-defrag.sh' script.
# Don't run this stand alone.
# 
#

set -e

CP="cp"
MV="mv"

scan_dir()
{
    path=$1;
    find "$path" -type f -perm +01000 -exec $0 '{}' \; 
}

rsync_filename()
{
    path=$1
    dir=$(dirname "$path");
    file=$(basename "$path");

    echo "$dir/.$file.zr$$";
}

relocate_file()
{
    path=$1;
    tmp_path=$(rsync_filename "$path");

    pre_mtime=$(stat -c '%Y' "$path");
    $CP -a "$path" "$tmp_path";
    post_mtime=$(stat -c '%Y' "$path");

    if [ $pre_mtime = $post_mtime ]; then
	chmod -t "$tmp_path";
	$MV "$tmp_path" "$path";
	echo "file '$path' relocated" 
    else
	echo "file '$path' modified during defrag. skipping"
	rm -f "$tmp_path";
    fi
}

main()
{
    path="$1";

    if [ -d "$path" ]; then
	scan_dir "$path";
    else
	relocate_file "$@";
    fi

    usleep 500000 # 500ms
}

main "$1"