xref: /freebsd/tools/test/stress2/misc/rename2.sh (revision 2a60dec0919c20a03c2830a9466a86f9f44fbaf0)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# With lookup_shared=1 rename() will fail from time to time with ENOENT and
308a272653SPeter Holm# the following stat() will succeed. (Variation of rename.sh)
318a272653SPeter Holm
328a272653SPeter Holm. ../default.cfg
338a272653SPeter Holm
348a272653SPeter Holmhere=`pwd`
358a272653SPeter Holmcd /tmp
368a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > rename2.c
378a272653SPeter Holmmycc -o rename2 -Wall rename2.c || exit 1
388a272653SPeter Holmrm -f rename2.c
398a272653SPeter Holmcd $here
408a272653SPeter Holm
418a272653SPeter Holmrm -rf /tmp/rename.dir.*
428a272653SPeter Holmstart=`date +%s`
43*2a60dec0SPeter Holmwhile [ $((`date +%s` - start)) -lt 300 ]; do
448a272653SPeter Holm	for j in `jot 10`; do
458a272653SPeter Holm		/tmp/rename2 &
468a272653SPeter Holm	done
478a272653SPeter Holm	wait
488a272653SPeter Holmdone
498a272653SPeter Holmrm -rf /tmp/rename.dir.* /tmp/rename2
508a272653SPeter Holmexit 0
518a272653SPeter HolmEOF
528a272653SPeter Holm#include <sys/types.h>
538a272653SPeter Holm#include <sys/stat.h>
548a272653SPeter Holm#include <err.h>
558a272653SPeter Holm#include <fcntl.h>
568a272653SPeter Holm#include <stdio.h>
578a272653SPeter Holm#include <stdlib.h>
588a272653SPeter Holm#include <string.h>
598a272653SPeter Holm#include <time.h>
608a272653SPeter Holm#include <unistd.h>
618a272653SPeter Holm
628a272653SPeter Holmstatic char dir1[128];
638a272653SPeter Holmstatic char dir2[128];
648a272653SPeter Holm
658a272653SPeter Holmint
668a272653SPeter Holmmain(int argc, char **argv)
678a272653SPeter Holm{
688a272653SPeter Holm	struct stat sb;
698a272653SPeter Holm	time_t start;
708a272653SPeter Holm
718a272653SPeter Holm	sprintf(dir1, "/tmp/rename.dir.%d", getpid());
728a272653SPeter Holm	sprintf(dir2, "/tmp/rename.dir.2.%d", getpid());
738a272653SPeter Holm	if (mkdir(dir1, 0700) == -1)
748a272653SPeter Holm		err(1, "mkdir(%s)", dir1);
758a272653SPeter Holm
768a272653SPeter Holm	if (chdir(dir1) == -1)
778a272653SPeter Holm		err(1, "chdir(%s)", dir1);
788a272653SPeter Holm	if (chdir("..") == -1)
798a272653SPeter Holm		err(1, "chdir(%s)", "..");
808a272653SPeter Holm
818a272653SPeter Holm	start = time(NULL);
828a272653SPeter Holm	while ((time(NULL) - start) < 120) {
838a272653SPeter Holm		if (rename(dir1, dir2) == -1) {
848a272653SPeter Holm			warn("rename(%s, %s)", dir1, dir2);
858a272653SPeter Holm			if (stat(dir1, &sb) == -1)
868a272653SPeter Holm				err(1, "stat(%s)", dir1);
878a272653SPeter Holm			else
888a272653SPeter Holm				errx(1, "stat(%s) succeeded!", dir1);
898a272653SPeter Holm		}
908a272653SPeter Holm		if (rename(dir2, dir1) == -1) {
918a272653SPeter Holm			warn("rename(%s, %s)", dir2, dir1);
928a272653SPeter Holm			if (stat(dir2, &sb) == -1)
938a272653SPeter Holm				err(1, "stat(%s)", dir2);
948a272653SPeter Holm			else
958a272653SPeter Holm				errx(1, "stat(%s) succeeded!", dir2);
968a272653SPeter Holm		}
978a272653SPeter Holm	}
988a272653SPeter Holm
998a272653SPeter Holm	if (rmdir(dir1) == -1)
1008a272653SPeter Holm		err(1, "rmdir(%s)", dir1);
1018a272653SPeter Holm
1028a272653SPeter Holm	return (0);
1038a272653SPeter Holm}
104