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