Path: blob/main/tools/test/stress2/testcases/mkdir/mkdir.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/types.h>28#include <sys/sysctl.h>29#include <unistd.h>30#include <stdio.h>31#include <stdlib.h>32#include <sys/stat.h>33#include <sys/param.h>34#include <err.h>3536#include "stress.h"3738static unsigned long size;3940int41setup(int nb)42{43int64_t in;44int64_t bl;45int64_t reserve_in;46int64_t reserve_bl;47int pct;4849if (nb == 0) {50getdf(&bl, &in);51size = in / op->incarnations;5253pct = 90;54if (op->hog == 0)55pct = random_int(1, 90);56size = size / 100 * pct + 1;5758size = size % 200; /* arbitrary limit depth */5960/* Resource requirements: */61while (size > 0) {62reserve_in = 1 * size * op->incarnations;63reserve_bl = 4096 * size * op->incarnations;64if (reserve_bl <= bl && reserve_in <= in)65break;66size = size / 2;67}68if (size == 0)69reserve_bl = reserve_in = 0;7071if (op->verbose > 1)72printf("mkdir(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",73size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);74reservedf(reserve_bl, reserve_in);75putval(size);76} else77size = getval();7879if (size == 0)80exit(0);8182return (0);83}8485void86cleanup(void)87{88}8990static void91mkDir(char *path, int level) {92char newPath[MAXPATHLEN + 1];9394if (mkdir(path, 0770) == -1) {95warn("mkdir(%s), level %d. %s:%d", path, level, __FILE__, __LINE__);96size = level;97} else98chdir(path);99100if (done_testing == 1)101size = level;102103if (level < (int)size) {104sprintf(newPath,"d%d", level+1);105mkDir(newPath, level+1);106}107}108109static void110rmDir(char *path, int level) {111char newPath[MAXPATHLEN + 1];112113if (level < (int)size) {114sprintf(newPath,"d%d", level+1);115rmDir(newPath, level+1);116}117chdir ("..");118if (rmdir(path) == -1) {119err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);120}121}122123int124test(void)125{126char path[MAXPATHLEN + 1];127128umask(0);129sprintf(path,"p%05d.d%d", getpid(), 1);130mkDir(path, 1);131rmDir(path, 1);132133return (0);134}135136137