Path: blob/main/tools/test/stress2/testcases/dirrename/dirrename.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <sys/param.h>28#include <sys/stat.h>29#include <err.h>30#include <errno.h>31#include <fcntl.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <unistd.h>3637#include "stress.h"3839static unsigned long size;4041int42setup(int nb)43{44int64_t in;45int64_t bl;46int64_t reserve_in;47int64_t reserve_bl;4849umask(0);5051if (nb == 0) {52getdf(&bl, &in);53size = in / op->incarnations;5455if (size > 1000)56size = 1000; /* arbitrary limit number of files pr. dir */5758/* Resource requirements: */59while (size > 0) {60reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;61reserve_bl = 30 * size * op->incarnations;62if (reserve_bl <= bl && reserve_in <= in)63break;64size = size / 2;65}66if (size == 0)67reserve_bl = reserve_in = 0;6869if (op->verbose > 1)70printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",71size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);72reservedf(reserve_bl, reserve_in);73putval(size);74} else {75size = getval();76}77if (size == 0)78exit(0);7980return (0);81}8283void84cleanup(void)85{86}8788static void89test_rename(void)90{91int i, j;92int errnotmp;93pid_t pid;94char file1[128];95char file2[128];9697pid = getpid();98for (i = 0; i < (int)size; i++) {99sprintf(file1,"p%05d.%05d", pid, i);100if (mkdir(file1, 0660) == -1) {101j = i;102errnotmp = errno;103while (j > 0) {104j--;105sprintf(file1,"p%05d.%05d", pid, j);106rmdir(file1);107}108errno = errnotmp;109sprintf(file1,"p%05d.%05d", pid, i);110err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);111}112}113for (j = 0; j < 100 && done_testing == 0; j++) {114for (i = 0; i < (int)size; i++) {115sprintf(file1,"p%05d.%05d", pid, i);116sprintf(file2,"p%05d.%05d.togo", pid, i);117if (rename(file1, file2) == -1)118err(1, "rename(%s, %s). %s:%d", file1, file2,119__FILE__, __LINE__);120}121for (i = 0; i < (int)size; i++) {122sprintf(file1,"p%05d.%05d", pid, i);123sprintf(file2,"p%05d.%05d.togo", pid, i);124if (rename(file2, file1) == -1)125err(1, "rename(%s, %s). %s:%d", file2, file1,126__FILE__, __LINE__);127}128}129130for (i = 0; i < (int)size; i++) {131sprintf(file1,"p%05d.%05d", pid, i);132if (rmdir(file1) == -1)133err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);134}135}136137int138test(void)139{140test_rename();141142return (0);143}144145146