Path: blob/main/tools/test/stress2/testcases/rename/rename.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 <fcntl.h>31#include <stdio.h>32#include <stdlib.h>33#include <string.h>34#include <unistd.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;4748umask(0);4950if (nb == 0) {51getdf(&bl, &in);52size = in / op->incarnations;5354if (size > 1000)55size = 1000; /* arbitrary limit number of files pr. dir */5657/* Resource requirements: */58while (size > 0) {59reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;60reserve_bl = 30 * size * op->incarnations;61if (reserve_bl <= bl && reserve_in <= in)62break;63size = size / 2;64}65if (size == 0)66reserve_bl = reserve_in = 0;6768if (op->verbose > 1)69printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",70size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);71reservedf(reserve_bl, reserve_in);72putval(size);73} else {74size = getval();75}76if (size == 0)77exit(0);7879return (0);80}8182void83cleanup(void)84{85}8687static void88test_rename(void)89{90int i, j;91pid_t pid;92char file1[128];93char file2[128];94int tfd;9596pid = getpid();97for (i = 0; i < (int)size; i++) {98sprintf(file1,"p%05d.%05d", pid, i);99if ((tfd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)100err(1, "openat(%s), %s:%d", file1, __FILE__, __LINE__);101close(tfd);102}103for (j = 0; j < 100 && done_testing == 0; j++) {104for (i = 0; i < (int)size; i++) {105sprintf(file1,"p%05d.%05d", pid, i);106sprintf(file2,"p%05d.%05d.togo", pid, i);107if (rename(file1, file2) == -1)108err(1, "rename(%s, %s). %s:%d", file1, file2,109__FILE__, __LINE__);110}111for (i = 0; i < (int)size; i++) {112sprintf(file1,"p%05d.%05d", pid, i);113sprintf(file2,"p%05d.%05d.togo", pid, i);114if (rename(file2, file1) == -1)115err(1, "rename(%s, %s). %s:%d", file2, file1,116__FILE__, __LINE__);117}118}119120for (i = 0; i < (int)size; i++) {121sprintf(file1,"p%05d.%05d", pid, i);122if (unlink(file1) == -1)123err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);124}125}126127int128test(void)129{130test_rename();131132return (0);133}134135136