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