Path: blob/main/tools/test/stress2/testcases/dirnprename/dirnprename.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 <unistd.h>30#include <err.h>31#include <errno.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>3637#include "stress.h"3839static char path[128];40static unsigned long size;4142int43setup(int nb)44{45char file1[512];46int64_t in;47int64_t bl;48int64_t reserve_in;49int64_t reserve_bl;50int i;5152umask(0);5354if (nb == 0) {55getdf(&bl, &in);56size = in / op->incarnations;5758if (size > 1000)59size = 1000; /* arbitrary limit number of files pr. dir */6061/* Resource requirements: */62while (size > 0) {63reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;64reserve_bl = 30 * size * op->incarnations;65if (reserve_bl <= bl && reserve_in <= in)66break;67size = size / 2;68}69if (size == 0)70reserve_bl = reserve_in = 0;7172if (op->verbose > 1)73printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",74size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);75reservedf(reserve_bl, reserve_in);76putval(size);77} else {78size = getval();79}80if (size == 0)81exit(0);8283sprintf(path, "%s.%05d", getprogname(), getpid());84if (mkdir(path, 0770) == -1)85err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);8687/*88* The value 97 was determined by figuring out how many 255 length89* names would cause us to overflow into indirect blocks w/ the90* default UFS parameters.91*/92for (i = 0; i < 97; i++) {93sprintf(file1, "%s/%0255d", path, i);94if (mkdir(file1, 0770) == -1)95err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);96}97return (0);98}99100void101cleanup(void)102{103char file1[512];104int i;105106/* see comment above */107for (i = 0; i < 97; i++) {108sprintf(file1, "%s/%0255d", path, i);109if (rmdir(file1) == -1)110warn("rmdir(%s), %s:%d", file1, __FILE__, __LINE__);111}112if (rmdir(path) == -1)113warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);114}115116static void117test_rename(void)118{119int i, j;120int errnotmp;121pid_t pid;122char file1[128];123char file2[128];124125pid = getpid();126for (i = 0; i < (int)size; i++) {127sprintf(file1,"p%05d.%05d", pid, i);128if (mkdir(file1, 0660) == -1) {129j = i;130errnotmp = errno;131while (j > 0) {132j--;133sprintf(file1,"p%05d.%05d", pid, j);134rmdir(file1);135}136errno = errnotmp;137sprintf(file1,"p%05d.%05d", pid, i);138err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);139}140}141for (j = 0; j < 100 && done_testing == 0; j++) {142for (i = 0; i < (int)size; i++) {143sprintf(file1,"p%05d.%05d", pid, i);144sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);145if (rename(file1, file2) == -1)146err(1, "rename(%s, %s). %s:%d", file1, file2,147__FILE__, __LINE__);148}149for (i = 0; i < (int)size; i++) {150sprintf(file1,"p%05d.%05d", pid, i);151sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);152if (rename(file2, file1) == -1)153err(1, "rename(%s, %s). %s:%d", file2, file1,154__FILE__, __LINE__);155}156}157158for (i = 0; i < (int)size; i++) {159sprintf(file1,"p%05d.%05d", pid, i);160if (rmdir(file1) == -1)161err(1, "rmdir(%s), %s:%d", file1, __FILE__, __LINE__);162}163}164165int166test(void)167{168test_rename();169170return (0);171}172173174