File: hardlink_fails.sh

package info (click to toggle)
rdfind 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 648 kB
  • sloc: cpp: 1,670; sh: 1,622; makefile: 32
file content (59 lines) | stat: -rwxr-xr-x 1,614 bytes parent folder | download
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
#!/bin/sh
# Investigate what happen when hard linking fails.
#
# See https://github.com/pauldreik/rdfind/issues/5


set -e
. "$(dirname "$0")/common_funcs.sh"

reset_teststate

#make identical files.
files="a subdir/b c some/deeply/nested/subdir/d"
nfiles=4
for n in $files ; do
    mkdir -p $(dirname $datadir/$n)
    echo "hello hardlink" > $datadir/$n
done

#eliminate them.
$rdfind -makehardlinks true $datadir/

#make sure one is a hard link to the other.
for n in $files ; do
    nhardlinks=$(stat -c %h $datadir/$n)
    if [ $nhardlinks -ne $nfiles ] ; then
	dbgecho "expected $nfiles hardlinks, got $nhardlinks"
	exit 1
    fi
done
dbgecho passed the happy path

#now try to make a hardlink to somewhere that fails.
#ideally, we want to partitions so it is not possible to hardlink,
#but it is difficult to fix that unless the test environment
#is setup that way. therefore, make the hardlinking fail by
#trying to hardlink something we do not have access to.
#unless run as root which would be horrible.
if [ "$(id -u)" -eq 0 ]; then
    dbgecho "running as root or through sudo, dangerous! Will not proceed with this unit tests."
    exit 1
fi

reset_teststate
system_file=$(which ls)
cp $system_file .
$rdfind -makehardlinks true . $system_file 2>&1 |tee rdfind.out
if ! grep -iq "failed" rdfind.out ; then
   dbgecho "expected failure when trying to make hardlink on system partition"
   exit 1
fi

#make sure that our own copy is still there
if [ ! -e $(basename $system_file) ] ; then
    dbgecho file is missing, rdfind should not have removed it!
    exit 1
fi

dbgecho "all is good in this test!"