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# 3. All advertising materials mentioning features or use of this software 21f8c94cecSXin LI# must display the following acknowledgement: 22f8c94cecSXin LI# This product includes software developed by the NetBSD 23f8c94cecSXin LI# Foundation, Inc. and its contributors. 24f8c94cecSXin LI# 4. Neither the name of The NetBSD Foundation nor the names of its 25f8c94cecSXin LI# contributors may be used to endorse or promote products derived 26f8c94cecSXin LI# from this software without specific prior written permission. 27f8c94cecSXin LI# 28f8c94cecSXin LI# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29f8c94cecSXin LI# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30f8c94cecSXin LI# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31f8c94cecSXin LI# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32f8c94cecSXin LI# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33f8c94cecSXin LI# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34f8c94cecSXin LI# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35f8c94cecSXin LI# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36f8c94cecSXin LI# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37f8c94cecSXin LI# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38f8c94cecSXin LI# POSSIBILITY OF SUCH DAMAGE. 39f8c94cecSXin LI# 40f8c94cecSXin LI# $FreeBSD$ 41f8c94cecSXin LI# 42f8c94cecSXin LI 43f8c94cecSXin LI# 44f8c94cecSXin LI# Verifies that the rename operation works (either by renaming entries or 45f8c94cecSXin LI# by moving them). 46f8c94cecSXin LI# 47f8c94cecSXin LI 48f8c94cecSXin LItest_run() { 49f8c94cecSXin LI test_mount 50f8c94cecSXin LI 51f8c94cecSXin LI test_name "'.' and '..' entries cannot be renamed" 52f8c94cecSXin LI mkdir a || die 53f8c94cecSXin LI mv a/. c 2>/dev/null && die 54f8c94cecSXin LI mv a/.. c 2>/dev/null && die 55f8c94cecSXin LI rmdir a || die 56f8c94cecSXin LI 57f8c94cecSXin LI test_name "Cross device renames do not work" 58f8c94cecSXin LI mkdir a || die 59d73f7c17SXin LI ${Src_Dir}/h_tools rename a /var/tmp/a >/dev/null 2>&1 && die 60f8c94cecSXin LI rmdir a || die 61f8c94cecSXin LI 62f8c94cecSXin LI test_name "Directories can be renamed" 63f8c94cecSXin LI mkdir a || die 64f8c94cecSXin LI mv a c || die 65f8c94cecSXin LI test -d a && die 66f8c94cecSXin LI test -d c || die 67f8c94cecSXin LI rmdir c || die 68f8c94cecSXin LI 69f8c94cecSXin LI test_name "The '..' entry is updated after moves" 70f8c94cecSXin LI mkdir a || die 71f8c94cecSXin LI mkdir b || die 72f8c94cecSXin LI mv b a || die 73f8c94cecSXin LI test -d a/b/../b || die 74f8c94cecSXin LI test -d a/b/../../a || die 75f8c94cecSXin LI eval $(stat -s a/b) 76f8c94cecSXin LI [ ${st_nlink} = 2 ] || die 77f8c94cecSXin LI eval $(stat -s a) 78f8c94cecSXin LI [ ${st_nlink} = 3 ] || die 79f8c94cecSXin LI rmdir a/b || die 80f8c94cecSXin LI rmdir a || die 81f8c94cecSXin LI 82f8c94cecSXin LI test_name "The '..' entry is correct after renames" 83f8c94cecSXin LI mkdir a || die 84f8c94cecSXin LI mkdir b || die 85f8c94cecSXin LI mv b a || die 86f8c94cecSXin LI mv a c || die 87f8c94cecSXin LI test -d c/b/../b || die 88f8c94cecSXin LI test -d c/b/../../c || die 89f8c94cecSXin LI rmdir c/b || die 90f8c94cecSXin LI rmdir c || die 91f8c94cecSXin LI 92f8c94cecSXin LI test_name "The '..' entry is correct after multiple moves" 93f8c94cecSXin LI mkdir a || die 94f8c94cecSXin LI mkdir b || die 95f8c94cecSXin LI mv b a || die 96f8c94cecSXin LI mv a c || die 97f8c94cecSXin LI mv c/b d || die 98f8c94cecSXin LI test -d d/../c || die 99f8c94cecSXin LI rmdir d || die 100f8c94cecSXin LI rmdir c || die 101f8c94cecSXin LI 102f8c94cecSXin LI test_name "Rename works if the target file exists" 103f8c94cecSXin LI touch a || die 104f8c94cecSXin LI touch b || die 105f8c94cecSXin LI mv a b || die 106f8c94cecSXin LI test -f a && die 107f8c94cecSXin LI test -f b || die 108f8c94cecSXin LI rm b 109f8c94cecSXin LI 110d73f7c17SXin LI test_name "Rename a directory to a override an empty directory works" 111d73f7c17SXin LI mkdir a || die 112d73f7c17SXin LI touch a/c || die 113d73f7c17SXin LI mkdir b || die 114d73f7c17SXin LI ${Src_Dir}/h_tools rename a b >/dev/null 2>&1 || die 115d73f7c17SXin LI test -e a && die 116d73f7c17SXin LI test -d b || die 117d73f7c17SXin LI test -f b/c || die 118d73f7c17SXin LI rm b/c 119d73f7c17SXin LI rmdir b 120d73f7c17SXin LI 121d73f7c17SXin LI test_name "Rename a directory to a override a non-empty directory fails" 122d73f7c17SXin LI mkdir a || die 123d73f7c17SXin LI touch a/c || die 124d73f7c17SXin LI mkdir b || die 125d73f7c17SXin LI touch b/d || die 126d73f7c17SXin LI err=$(${Src_Dir}/h_tools rename a b 2>&1) && die 127d73f7c17SXin LI echo ${err} | grep 'Directory not empty' >/dev/null || die 128d73f7c17SXin LI test -d a || die 129d73f7c17SXin LI test -f a/c || die 130d73f7c17SXin LI test -d b || die 131d73f7c17SXin LI test -f b/d || die 132d73f7c17SXin LI rm a/c 133d73f7c17SXin LI rm b/d 134d73f7c17SXin LI rmdir a 135d73f7c17SXin LI rmdir b 136d73f7c17SXin LI 137d73f7c17SXin LI test_name "Rename a directory to a override a file fails" 138d73f7c17SXin LI mkdir a || die 139d73f7c17SXin LI touch b || die 140d73f7c17SXin LI err=$(${Src_Dir}/h_tools rename a b 2>&1) && die 141d73f7c17SXin LI echo ${err} | grep 'Not a directory' >/dev/null || die 142d73f7c17SXin LI test -d a || die 143d73f7c17SXin LI test -f b || die 144d73f7c17SXin LI rmdir a 145d73f7c17SXin LI rm b 146d73f7c17SXin LI 147d73f7c17SXin LI test_name "Rename a file to a override a directory fails" 148d73f7c17SXin LI touch a || die 149d73f7c17SXin LI mkdir b || die 150d73f7c17SXin LI err=$(${Src_Dir}/h_tools rename a b 2>&1) && die 151d73f7c17SXin LI echo ${err} | grep 'Is a directory' >/dev/null || die 152d73f7c17SXin LI test -f a || die 153d73f7c17SXin LI test -d b || die 154d73f7c17SXin LI rm a 155d73f7c17SXin LI rmdir b 156d73f7c17SXin LI 157f8c94cecSXin LI mkdir dir || die 158f8c94cecSXin LI touch dir/a 159f8c94cecSXin LI echo 'mv dir/a dir/b' | kqueue_monitor 2 dir dir/a || die 160f8c94cecSXin LI test_name "Renaming a file raises NOTE_RENAME on the old file" 161f8c94cecSXin LI kqueue_check dir/a NOTE_RENAME || die 162f8c94cecSXin LI test_name "Renaming a file raises NOTE_WRITE on the parent directory" 163f8c94cecSXin LI kqueue_check dir NOTE_WRITE || die 164f8c94cecSXin LI rm dir/b || die 165f8c94cecSXin LI rmdir dir || die 166f8c94cecSXin LI 167f8c94cecSXin LI mkdir dir || die 168f8c94cecSXin LI touch dir/a 169f8c94cecSXin LI touch dir/b 170f8c94cecSXin LI echo 'mv dir/a dir/b' | kqueue_monitor 3 dir dir/a dir/b || die 171f8c94cecSXin LI test_name "Renaming a file raises NOTE_RENAME on the old file" 172f8c94cecSXin LI kqueue_check dir/a NOTE_RENAME || die 173f8c94cecSXin LI test_name "Renaming a file raises NOTE_WRITE on the parent directory" 174f8c94cecSXin LI kqueue_check dir NOTE_WRITE || die 175f8c94cecSXin LI test_name "Renaming a file raises NOTE_DELETE on the target file" 176f8c94cecSXin LI kqueue_check dir/b NOTE_DELETE || die 177f8c94cecSXin LI rm dir/b || die 178f8c94cecSXin LI rmdir dir || die 179f8c94cecSXin LI 180f8c94cecSXin LI mkdir dir1 || die 181f8c94cecSXin LI mkdir dir2 || die 182f8c94cecSXin LI touch dir1/a 183f8c94cecSXin LI echo 'mv dir1/a dir2/a' | kqueue_monitor 3 dir1 dir1/a dir2 || die 184f8c94cecSXin LI test_name "Moving a file raises NOTE_RENAME on the old file" 185f8c94cecSXin LI kqueue_check dir1/a NOTE_RENAME || die 186f8c94cecSXin LI test_name "Moving a file raises NOTE_WRITE on the source directory" 187f8c94cecSXin LI kqueue_check dir1 NOTE_WRITE || die 188f8c94cecSXin LI test_name "Moving a file raises NOTE_WRITE on the target directory" 189f8c94cecSXin LI kqueue_check dir2 NOTE_WRITE || die 190f8c94cecSXin LI rm dir2/a || die 191f8c94cecSXin LI rmdir dir1 || die 192f8c94cecSXin LI rmdir dir2 || die 193f8c94cecSXin LI 194f8c94cecSXin LI test_unmount 195f8c94cecSXin LI} 196f8c94cecSXin LI 197f8c94cecSXin LI. ${SUBRDIR}/h_funcs.subr 198