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/tests/functional/truncate/truncate.cfg
18. $STF_SUITE/include/libtest.shlib
19. $STF_SUITE/include/math.shlib
20
21#
22# DESCRIPTION:
23# Ensure both truncate(2)/ftruncate(2) update target file mtime/ctime attributes
24#
25# STRATEGY:
26# 1. Create a file
27# 2. Truncate the file
28# 3. Verify both mtime/ctime are updated
29# 4. Rinse and repeat for both truncate(2) and ftruncate(2) with various sizes
30#
31
32verify_runnable "both"
33
34function verify_truncate # <filename> <filesize> <option>
35{
36	typeset filename="$1"
37	typeset -i size="$2"
38	typeset option="$3"
39
40	log_must mkfile $sizeavg $filename # always start with $sizeavg
41	if is_freebsd; then
42		typeset -i timestm="$(stat -f "%m" $filename)"
43		typeset -i timestc="$(stat -f "%c" $filename)"
44		log_must sleep 1
45		log_must truncate_test -s $size $filename $option
46		verify_eq $size "$(stat_size $filename)" "size"
47		verify_ne $timestm "$(stat -f "%m" $filename)" "mtime"
48		verify_ne $timestc "$(stat -f "%c" $filename)" "ctime"
49	else
50		typeset -i timestm="$(stat -c %Y $filename)"
51		typeset -i timestc="$(stat -c %Z $filename)"
52		log_must sleep 1
53		log_must truncate_test -s $size $filename $option
54		verify_eq $size "$(stat_size $filename)" "size"
55		verify_ne $timestm "$(stat -c %Y $filename)" "mtime"
56		verify_ne $timestc "$(stat -c %Z $filename)" "ctime"
57	fi
58	log_must rm -f $filename
59}
60
61function cleanup
62{
63	[[ -f $truncfile ]] && rm -f $truncfile
64}
65
66log_assert "Ensure both truncate(2)/ftruncate(2) update target file timestamps"
67log_onexit cleanup
68
69truncfile="$TESTDIR/truncate.$$"
70sizemin="123"
71sizeavg="$((256*1024))"
72sizemax="$((1024*1024))"
73
74# truncate(2)
75verify_truncate $truncfile $sizemin ""
76verify_truncate $truncfile $sizeavg ""
77verify_truncate $truncfile $sizemax ""
78
79# ftruncate(2)
80verify_truncate $truncfile $sizemin "-f"
81verify_truncate $truncfile $sizeavg "-f"
82verify_truncate $truncfile $sizemax "-f"
83
84log_pass "Successful truncation correctly update timestamps"
85