1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18
19#
20# DESCRIPTION:
21# 'zfs diff -t' should display inode change time correctly.
22#
23# STRATEGY:
24# 1. Create a snapshot
25# 2. Create some files with a random delay and snapshot the filesystem again
26# 3. Verify 'zfs diff -t' correctly display timestamps
27#
28
29verify_runnable "both"
30
31function cleanup
32{
33	for snap in $TESTSNAP1 $TESTSNAP2; do
34		if snapexists "$snap"; then
35			log_must zfs destroy "$snap"
36		fi
37	done
38	find "$MNTPOINT" -type f -delete
39	rm -f "$FILEDIFF"
40}
41
42#
43# Creates $count files in $fspath. Waits a random delay between each file.
44#
45function create_random # <fspath> <count>
46{
47	fspath="$1"
48	typeset -i count="$2"
49	typeset -i i=0
50
51	while (( i < count )); do
52		log_must touch "$fspath/file$i"
53		sleep $(random 3)
54		(( i = i + 1 ))
55	done
56}
57
58log_assert "'zfs diff -t' should display inode change time correctly."
59log_onexit cleanup
60
61DATASET="$TESTPOOL/$TESTFS"
62TESTSNAP1="$DATASET@snap1"
63TESTSNAP2="$DATASET@snap2"
64MNTPOINT="$(get_prop mountpoint $DATASET)"
65FILEDIFF="$TESTDIR/zfs-diff.txt"
66FILENUM=5
67
68# 1. Create a snapshot
69log_must zfs snapshot "$TESTSNAP1"
70
71# 2. Create some files with a random delay and snapshot the filesystem again
72create_random "$MNTPOINT" $FILENUM
73log_must zfs snapshot "$TESTSNAP2"
74
75# 3. Verify 'zfs diff -t' correctly display timestamps
76typeset -i count=0
77log_must eval "zfs diff -t $TESTSNAP1 $TESTSNAP2 > $FILEDIFF"
78nawk '{print substr($1,1,index($1,".")-1)" "$NF}' < "$FILEDIFF" | while read line
79do
80	read ctime file <<< "$line"
81
82	# If path from 'zfs diff' is not a file (could be xattr object) skip it
83	if [[ ! -f "$file" ]]; then
84		continue;
85	fi
86
87	filetime="$(stat -c '%Z' $file)"
88	if [[ "$filetime" != "$ctime" ]]; then
89		log_fail "Unexpected ctime for file $file ($filetime != $ctime)"
90	else
91		log_note "Correct ctime read on $file: $ctime"
92	fi
93
94	(( i = i + 1 ))
95done
96if [[ $i != $FILENUM ]]; then
97	log_fail "Wrong number of files verified ($i != $FILENUM)"
98fi
99
100log_pass "'zfs diff -t' displays inode change time correctly."
101