xref: /freebsd/tools/regression/tmpfs/t_rename (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1f8c94cecSXin LI#!/bin/sh
2f8c94cecSXin LI#
3d73f7c17SXin LI# $NetBSD: t_rename,v 1.7 2007/07/23 15:05:43 jmmv Exp $
4f8c94cecSXin LI#
5f8c94cecSXin LI# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6f8c94cecSXin LI# All rights reserved.
7f8c94cecSXin LI#
8f8c94cecSXin LI# This code is derived from software contributed to The NetBSD Foundation
9f8c94cecSXin LI# by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10f8c94cecSXin LI# 2005 program.
11f8c94cecSXin LI#
12f8c94cecSXin LI# Redistribution and use in source and binary forms, with or without
13f8c94cecSXin LI# modification, are permitted provided that the following conditions
14f8c94cecSXin LI# are met:
15f8c94cecSXin LI# 1. Redistributions of source code must retain the above copyright
16f8c94cecSXin LI#    notice, this list of conditions and the following disclaimer.
17f8c94cecSXin LI# 2. Redistributions in binary form must reproduce the above copyright
18f8c94cecSXin LI#    notice, this list of conditions and the following disclaimer in the
19f8c94cecSXin LI#    documentation and/or other materials provided with the distribution.
20f8c94cecSXin LI#
21f8c94cecSXin LI# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22f8c94cecSXin LI# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23f8c94cecSXin LI# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24f8c94cecSXin LI# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25f8c94cecSXin LI# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26f8c94cecSXin LI# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27f8c94cecSXin LI# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28f8c94cecSXin LI# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29f8c94cecSXin LI# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30f8c94cecSXin LI# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31f8c94cecSXin LI# POSSIBILITY OF SUCH DAMAGE.
32f8c94cecSXin LI#
33f8c94cecSXin LI#
34f8c94cecSXin LI
35f8c94cecSXin LI#
36f8c94cecSXin LI# Verifies that the rename operation works (either by renaming entries or
37f8c94cecSXin LI# by moving them).
38f8c94cecSXin LI#
39f8c94cecSXin LI
40f8c94cecSXin LItest_run() {
41f8c94cecSXin LI	test_mount
42f8c94cecSXin LI
43f8c94cecSXin LI	test_name "'.' and '..' entries cannot be renamed"
44f8c94cecSXin LI	mkdir a || die
45f8c94cecSXin LI	mv a/. c 2>/dev/null && die
46f8c94cecSXin LI	mv a/.. c 2>/dev/null && die
47f8c94cecSXin LI	rmdir a || die
48f8c94cecSXin LI
49f8c94cecSXin LI	test_name "Cross device renames do not work"
50f8c94cecSXin LI	mkdir a || die
51d73f7c17SXin LI	${Src_Dir}/h_tools rename a /var/tmp/a >/dev/null 2>&1 && die
52f8c94cecSXin LI	rmdir a || die
53f8c94cecSXin LI
54f8c94cecSXin LI	test_name "Directories can be renamed"
55f8c94cecSXin LI	mkdir a || die
56f8c94cecSXin LI	mv a c || die
57f8c94cecSXin LI	test -d a && die
58f8c94cecSXin LI	test -d c || die
59f8c94cecSXin LI	rmdir c || die
60f8c94cecSXin LI
61f8c94cecSXin LI	test_name "The '..' entry is updated after moves"
62f8c94cecSXin LI	mkdir a || die
63f8c94cecSXin LI	mkdir b || die
64f8c94cecSXin LI	mv b a || die
65f8c94cecSXin LI	test -d a/b/../b || die
66f8c94cecSXin LI	test -d a/b/../../a || die
67f8c94cecSXin LI	eval $(stat -s a/b)
68f8c94cecSXin LI	[ ${st_nlink} = 2 ] || die
69f8c94cecSXin LI	eval $(stat -s a)
70f8c94cecSXin LI	[ ${st_nlink} = 3 ] || die
71f8c94cecSXin LI	rmdir a/b || die
72f8c94cecSXin LI	rmdir a || die
73f8c94cecSXin LI
74f8c94cecSXin LI	test_name "The '..' entry is correct after renames"
75f8c94cecSXin LI	mkdir a || die
76f8c94cecSXin LI	mkdir b || die
77f8c94cecSXin LI	mv b a || die
78f8c94cecSXin LI	mv a c || die
79f8c94cecSXin LI	test -d c/b/../b || die
80f8c94cecSXin LI	test -d c/b/../../c || die
81f8c94cecSXin LI	rmdir c/b || die
82f8c94cecSXin LI	rmdir c || die
83f8c94cecSXin LI
84f8c94cecSXin LI	test_name "The '..' entry is correct after multiple moves"
85f8c94cecSXin LI	mkdir a || die
86f8c94cecSXin LI	mkdir b || die
87f8c94cecSXin LI	mv b a || die
88f8c94cecSXin LI	mv a c || die
89f8c94cecSXin LI	mv c/b d || die
90f8c94cecSXin LI	test -d d/../c || die
91f8c94cecSXin LI	rmdir d || die
92f8c94cecSXin LI	rmdir c || die
93f8c94cecSXin LI
94f8c94cecSXin LI	test_name "Rename works if the target file exists"
95f8c94cecSXin LI	touch a || die
96f8c94cecSXin LI	touch b || die
97f8c94cecSXin LI	mv a b || die
98f8c94cecSXin LI	test -f a && die
99f8c94cecSXin LI	test -f b || die
100f8c94cecSXin LI	rm b
101f8c94cecSXin LI
102d73f7c17SXin LI	test_name "Rename a directory to a override an empty directory works"
103d73f7c17SXin LI	mkdir a || die
104d73f7c17SXin LI	touch a/c || die
105d73f7c17SXin LI	mkdir b || die
106d73f7c17SXin LI	${Src_Dir}/h_tools rename a b >/dev/null 2>&1 || die
107d73f7c17SXin LI	test -e a && die
108d73f7c17SXin LI	test -d b || die
109d73f7c17SXin LI	test -f b/c || die
110d73f7c17SXin LI	rm b/c
111d73f7c17SXin LI	rmdir b
112d73f7c17SXin LI
113d73f7c17SXin LI	test_name "Rename a directory to a override a non-empty directory fails"
114d73f7c17SXin LI	mkdir a || die
115d73f7c17SXin LI	touch a/c || die
116d73f7c17SXin LI	mkdir b || die
117d73f7c17SXin LI	touch b/d || die
118d73f7c17SXin LI	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
119d73f7c17SXin LI	echo ${err} | grep 'Directory not empty' >/dev/null || die
120d73f7c17SXin LI	test -d a || die
121d73f7c17SXin LI	test -f a/c || die
122d73f7c17SXin LI	test -d b || die
123d73f7c17SXin LI	test -f b/d || die
124d73f7c17SXin LI	rm a/c
125d73f7c17SXin LI	rm b/d
126d73f7c17SXin LI	rmdir a
127d73f7c17SXin LI	rmdir b
128d73f7c17SXin LI
129d73f7c17SXin LI	test_name "Rename a directory to a override a file fails"
130d73f7c17SXin LI	mkdir a || die
131d73f7c17SXin LI	touch b || die
132d73f7c17SXin LI	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
133d73f7c17SXin LI	echo ${err} | grep 'Not a directory' >/dev/null || die
134d73f7c17SXin LI	test -d a || die
135d73f7c17SXin LI	test -f b || die
136d73f7c17SXin LI	rmdir a
137d73f7c17SXin LI	rm b
138d73f7c17SXin LI
139d73f7c17SXin LI	test_name "Rename a file to a override a directory fails"
140d73f7c17SXin LI	touch a || die
141d73f7c17SXin LI	mkdir b || die
142d73f7c17SXin LI	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
143d73f7c17SXin LI	echo ${err} | grep 'Is a directory' >/dev/null || die
144d73f7c17SXin LI	test -f a || die
145d73f7c17SXin LI	test -d b || die
146d73f7c17SXin LI	rm a
147d73f7c17SXin LI	rmdir b
148d73f7c17SXin LI
149f8c94cecSXin LI	mkdir dir || die
150f8c94cecSXin LI	touch dir/a
151f8c94cecSXin LI	echo 'mv dir/a dir/b' | kqueue_monitor 2 dir dir/a || die
152f8c94cecSXin LI	test_name "Renaming a file raises NOTE_RENAME on the old file"
153f8c94cecSXin LI	kqueue_check dir/a NOTE_RENAME || die
154f8c94cecSXin LI	test_name "Renaming a file raises NOTE_WRITE on the parent directory"
155f8c94cecSXin LI	kqueue_check dir NOTE_WRITE || die
156f8c94cecSXin LI	rm dir/b || die
157f8c94cecSXin LI	rmdir dir || die
158f8c94cecSXin LI
159f8c94cecSXin LI	mkdir dir || die
160f8c94cecSXin LI	touch dir/a
161f8c94cecSXin LI	touch dir/b
162f8c94cecSXin LI	echo 'mv dir/a dir/b' | kqueue_monitor 3 dir dir/a dir/b || die
163f8c94cecSXin LI	test_name "Renaming a file raises NOTE_RENAME on the old file"
164f8c94cecSXin LI	kqueue_check dir/a NOTE_RENAME || die
165f8c94cecSXin LI	test_name "Renaming a file raises NOTE_WRITE on the parent directory"
166f8c94cecSXin LI	kqueue_check dir NOTE_WRITE || die
167f8c94cecSXin LI	test_name "Renaming a file raises NOTE_DELETE on the target file"
168f8c94cecSXin LI	kqueue_check dir/b NOTE_DELETE || die
169f8c94cecSXin LI	rm dir/b || die
170f8c94cecSXin LI	rmdir dir || die
171f8c94cecSXin LI
172f8c94cecSXin LI	mkdir dir1 || die
173f8c94cecSXin LI	mkdir dir2 || die
174f8c94cecSXin LI	touch dir1/a
175f8c94cecSXin LI	echo 'mv dir1/a dir2/a' | kqueue_monitor 3 dir1 dir1/a dir2 || die
176f8c94cecSXin LI	test_name "Moving a file raises NOTE_RENAME on the old file"
177f8c94cecSXin LI	kqueue_check dir1/a NOTE_RENAME || die
178f8c94cecSXin LI	test_name "Moving a file raises NOTE_WRITE on the source directory"
179f8c94cecSXin LI	kqueue_check dir1 NOTE_WRITE || die
180f8c94cecSXin LI	test_name "Moving a file raises NOTE_WRITE on the target directory"
181f8c94cecSXin LI	kqueue_check dir2 NOTE_WRITE || die
182f8c94cecSXin LI	rm dir2/a || die
183f8c94cecSXin LI	rmdir dir1 || die
184f8c94cecSXin LI	rmdir dir2 || die
185f8c94cecSXin LI
186f8c94cecSXin LI	test_unmount
187f8c94cecSXin LI}
188f8c94cecSXin LI
189f8c94cecSXin LI. ${SUBRDIR}/h_funcs.subr
190