update the manpage with --iscsi-port
[tridge/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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* Wrappers for system calls that catch errors. */
22
23 #include "dbench.h"
24
25 #define MAX_FILES 1000
26
27 static char buf[70000];
28 extern int line_count;
29
30 static struct {
31         int fd;
32         int handle;
33 } ftable[MAX_FILES];
34
35 void do_unlink(char *fname)
36 {
37         strupper(fname);
38
39         if (unlink(fname) != 0) {
40                 printf("(%d) unlink %s failed (%s)\n", 
41                        line_count, fname, strerror(errno));
42         }
43 }
44
45 void expand_file(int fd, int size)
46 {
47         int s;
48         while (size) {
49                 s = MIN(sizeof(buf), size);
50                 write(fd, buf, s);
51                 size -= s;
52         }
53 }
54
55 void do_open(char *fname, int handle, int size)
56 {
57         int fd, i;
58         int flags = O_RDWR|O_CREAT;
59         struct stat st;
60         static int count;
61
62         strupper(fname);
63
64         if (size == 0) flags |= O_TRUNC;
65
66         fd = open(fname, flags, 0600);
67         if (fd == -1) {
68                 printf("(%d) open %s failed for handle %d (%s)\n", 
69                        line_count, fname, handle, strerror(errno));
70                 return;
71         }
72         fstat(fd, &st);
73         if (size > st.st_size) {
74 #if DEBUG
75                 printf("(%d) expanding %s to %d from %d\n", 
76                        line_count, fname, size, (int)st.st_size);
77 #endif
78                 expand_file(fd, size - st.st_size);
79         } else if (size < st.st_size) {
80                 printf("truncating %s to %d from %d\n", 
81                        fname, size, (int)st.st_size);
82                 ftruncate(fd, size);
83         }
84         for (i=0;i<MAX_FILES;i++) {
85                 if (ftable[i].handle == 0) break;
86         }
87         if (i == MAX_FILES) {
88                 printf("file table full for %s\n", fname);
89                 exit(1);
90         }
91         ftable[i].handle = handle;
92         ftable[i].fd = fd;
93         if (count++ % 100 == 0) {
94                 printf(".");
95         }
96 }
97
98 void do_write(int handle, int size, int offset)
99 {
100         int i;
101
102         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
103
104         for (i=0;i<MAX_FILES;i++) {
105                 if (ftable[i].handle == handle) break;
106         }
107         if (i == MAX_FILES) {
108 #if 1
109                 printf("(%d) do_write: handle %d was not open size=%d ofs=%d\n", 
110                        line_count, handle, size, offset);
111 #endif
112                 return;
113         }
114         lseek(ftable[i].fd, offset, SEEK_SET);
115         if (write(ftable[i].fd, buf, size) != size) {
116                 printf("write failed on handle %d (%s)\n", handle, strerror(errno));
117         }
118 }
119
120 void do_read(int handle, int size, int offset)
121 {
122         int i;
123         for (i=0;i<MAX_FILES;i++) {
124                 if (ftable[i].handle == handle) break;
125         }
126         if (i == MAX_FILES) {
127                 printf("(%d) do_read: handle %d was not open size=%d ofs=%d\n", 
128                        line_count, handle, size, offset);
129                 return;
130         }
131         lseek(ftable[i].fd, offset, SEEK_SET);
132         read(ftable[i].fd, buf, size);
133 }
134
135 void do_close(int handle)
136 {
137         int i;
138         for (i=0;i<MAX_FILES;i++) {
139                 if (ftable[i].handle == handle) break;
140         }
141         if (i == MAX_FILES) {
142                 printf("(%d) do_close: handle %d was not open\n", 
143                        line_count, handle);
144                 return;
145         }
146         close(ftable[i].fd);
147         ftable[i].handle = 0;
148 }
149
150 void do_mkdir(char *fname)
151 {
152         strupper(fname);
153
154         if (mkdir(fname, 0700) != 0) {
155 #if DEBUG
156                 printf("mkdir %s failed (%s)\n", 
157                        fname, strerror(errno));
158 #endif
159         }
160 }
161
162 void do_rmdir(char *fname)
163 {
164         strupper(fname);
165
166         if (rmdir(fname) != 0) {
167                 printf("rmdir %s failed (%s)\n", 
168                        fname, strerror(errno));
169         }
170 }
171
172 void do_rename(char *old, char *new)
173 {
174         strupper(old);
175         strupper(new);
176
177         if (rename(old, new) != 0) {
178                 printf("rename %s %s failed (%s)\n", 
179                        old, new, strerror(errno));
180         }
181 }
182
183
184 void do_stat(char *fname, int size)
185 {
186         struct stat st;
187
188         strupper(fname);
189
190         if (stat(fname, &st) != 0) {
191                 printf("(%d) do_stat: %s size=%d %s\n", 
192                        line_count, fname, size, strerror(errno));
193                 return;
194         }
195         if (S_ISDIR(st.st_mode)) return;
196
197         if (st.st_size != size) {
198                 printf("(%d) do_stat: %s wrong size %d %d\n", 
199                        line_count, fname, (int)st.st_size, size);
200         }
201 }
202
203 void do_create(char *fname, int size)
204 {
205         do_open(fname, 5000, size);
206         do_close(5000);
207 }
208
209