2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
39 static char path[128];
40 static unsigned long size;
54 size = in / op->incarnations;
57 size = 100; /* arbitrary limit number of files pr. dir */
59 /* Resource requirements: */
61 reserve_in = 1 * size * op->incarnations + 2 * op->incarnations;
62 reserve_bl = 100 * size * op->incarnations;
63 // printf("size = %lu, reserve(%jd, %jd)\n", size, reserve_bl/1024, reserve_in);
64 if (reserve_bl <= bl && reserve_in <= in)
69 reserve_bl = reserve_in = 0;
72 printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
73 size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
74 reservedf(reserve_bl, reserve_in);
80 sprintf(path,"%s.%05d", getprogname(), getpid());
81 if (mkdir(path, 0770) < 0)
82 err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
83 if (chdir(path) == -1)
84 err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
92 (void)system("rm -f p*");
94 if (rmdir(path) == -1)
95 warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
108 for (i = 0; i < size; i++) {
109 sprintf(file1,"p%05d.%05d", pid, i);
110 if ((tfd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)
111 err(1, "openat(%s), %s:%d", file1, __FILE__, __LINE__);
114 for (j = 0; j < 100; j++) {
115 for (i = 0; i < size; i++) {
116 sprintf(file1,"p%05d.%05d", pid, i);
117 sprintf(file2,"p%05d.%05d.togo", pid, i);
118 if (rename(file1, file2) == -1)
119 err(1, "rename(%s, %s). %s:%d", file1, file2,
122 for (i = 0; i < size; i++) {
123 sprintf(file1,"p%05d.%05d", pid, i);
124 sprintf(file2,"p%05d.%05d.togo", pid, i);
125 if (rename(file2, file1) == -1)
126 err(1, "rename(%s, %s). %s:%d", file2, file1,
131 for (i = 0; i < size; i++) {
132 sprintf(file1,"p%05d.%05d", pid, i);
133 if (unlink(file1) == -1)
134 err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);