[dbench @ cvs-1:tridge-20031015155848-mo532i7yg19srujm]
[amitay/dbench.git] / io.c
1 /* 
2    dbench version 2
3    
4    Copyright (C) by Andrew Tridgell <tridge@samba.org> 1999, 2001
5    Copyright (C) 2001 by Martin Pool <mbp@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* Wrappers for system calls that catch errors. */
23
24 #include "dbench.h"
25
26 #define MAX_FILES 1000
27
28 static char buf[70000];
29 extern int line_count;
30
31 static struct {
32         int fd;
33         int handle;
34 } ftable[MAX_FILES];
35
36 void do_unlink(char *fname)
37 {
38         strupper(fname);
39
40         if (unlink(fname) != 0) {
41                 printf("(%d) unlink %s failed (%s)\n", 
42                        line_count, fname, strerror(errno));
43         }
44 }
45
46 void expand_file(int fd, int size)
47 {
48         int s;
49         while (size) {
50                 s = MIN(sizeof(buf), size);
51                 write(fd, buf, s);
52                 size -= s;
53         }
54 }
55
56 void do_open(char *fname, int handle, int size)
57 {
58         int fd, i;
59         int flags = O_RDWR|O_CREAT;
60         struct stat st;
61         static int count;
62
63         strupper(fname);
64
65         if (size == 0) flags |= O_TRUNC;
66
67         fd = open(fname, flags, 0600);
68         if (fd == -1) {
69                 printf("(%d) open %s failed for handle %d (%s)\n", 
70                        line_count, fname, handle, strerror(errno));
71                 return;
72         }
73         fstat(fd, &st);
74         if (size > st.st_size) {
75 #if DEBUG
76                 printf("(%d) expanding %s to %d from %d\n", 
77                        line_count, fname, size, (int)st.st_size);
78 #endif
79                 expand_file(fd, size - st.st_size);
80         } else if (size < st.st_size) {
81                 printf("truncating %s to %d from %d\n", 
82                        fname, size, (int)st.st_size);
83                 ftruncate(fd, size);
84         }
85         for (i=0;i<MAX_FILES;i++) {
86                 if (ftable[i].handle == 0) break;
87         }
88         if (i == MAX_FILES) {
89                 printf("file table full for %s\n", fname);
90                 exit(1);
91         }
92         ftable[i].handle = handle;
93         ftable[i].fd = fd;
94         if (count++ % 100 == 0) {
95                 printf(".");
96         }
97 }
98
99 void do_write(int handle, int size, int offset)
100 {
101         int i;
102
103         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
104
105         for (i=0;i<MAX_FILES;i++) {
106                 if (ftable[i].handle == handle) break;
107         }
108         if (i == MAX_FILES) {
109 #if 1
110                 printf("(%d) do_write: handle %d was not open size=%d ofs=%d\n", 
111                        line_count, handle, size, offset);
112 #endif
113                 return;
114         }
115         lseek(ftable[i].fd, offset, SEEK_SET);
116         if (write(ftable[i].fd, buf, size) != size) {
117                 printf("write failed on handle %d\n", handle);
118         }
119 }
120
121 void do_read(int handle, int size, int offset)
122 {
123         int i;
124         for (i=0;i<MAX_FILES;i++) {
125                 if (ftable[i].handle == handle) break;
126         }
127         if (i == MAX_FILES) {
128                 printf("(%d) do_read: handle %d was not open size=%d ofs=%d\n", 
129                        line_count, handle, size, offset);
130                 return;
131         }
132         lseek(ftable[i].fd, offset, SEEK_SET);
133         read(ftable[i].fd, buf, size);
134 }
135
136 void do_close(int handle)
137 {
138         int i;
139         for (i=0;i<MAX_FILES;i++) {
140                 if (ftable[i].handle == handle) break;
141         }
142         if (i == MAX_FILES) {
143                 printf("(%d) do_close: handle %d was not open\n", 
144                        line_count, handle);
145                 return;
146         }
147         close(ftable[i].fd);
148         ftable[i].handle = 0;
149 }
150
151 void do_mkdir(char *fname)
152 {
153         strupper(fname);
154
155         if (mkdir(fname, 0700) != 0) {
156 #if DEBUG
157                 printf("mkdir %s failed (%s)\n", 
158                        fname, strerror(errno));
159 #endif
160         }
161 }
162
163 void do_rmdir(char *fname)
164 {
165         strupper(fname);
166
167         if (rmdir(fname) != 0) {
168                 printf("rmdir %s failed (%s)\n", 
169                        fname, strerror(errno));
170         }
171 }
172
173 void do_rename(char *old, char *new)
174 {
175         strupper(old);
176         strupper(new);
177
178         if (rename(old, new) != 0) {
179                 printf("rename %s %s failed (%s)\n", 
180                        old, new, strerror(errno));
181         }
182 }
183
184
185 void do_stat(char *fname, int size)
186 {
187         struct stat st;
188
189         strupper(fname);
190
191         if (stat(fname, &st) != 0) {
192                 printf("(%d) do_stat: %s size=%d %s\n", 
193                        line_count, fname, size, strerror(errno));
194                 return;
195         }
196         if (S_ISDIR(st.st_mode)) return;
197
198         if (st.st_size != size) {
199                 printf("(%d) do_stat: %s wrong size %d %d\n", 
200                        line_count, fname, (int)st.st_size, size);
201         }
202 }
203
204 void do_create(char *fname, int size)
205 {
206         do_open(fname, 5000, size);
207         do_close(5000);
208 }