preparing for release of alpha.0.7
[samba.git] / source / utils / nbio.c
1 #define NBDEBUG 0
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    SMB torture tester
7    Copyright (C) Andrew Tridgell 1997-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #define NO_SYSLOG
25
26 #include "includes.h"
27
28 #define MAX_FILES 1000
29
30 static char buf[70000];
31 extern int line_count;
32
33 static struct {
34         int fd;
35         int handle;
36 } ftable[MAX_FILES];
37
38 static struct cli_state *c;
39
40 static void sigsegv(int sig)
41 {
42         char line[200];
43         printf("segv at line %d\n", line_count);
44         slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
45                 (int)getpid(), (int)getpid());
46         system(line);
47         exit(1);
48 }
49
50 void nb_setup(struct cli_state *cli)
51 {
52         signal(SIGSEGV, sigsegv);
53         /* to be like a true Windows client we need to negotiate oplocks */
54         cli->use_oplocks = True;
55         c = cli;
56 }
57
58
59 void nb_unlink(char *fname)
60 {
61         strupper(fname);
62
63         if (!cli_unlink(c, fname)) {
64 #if NBDEBUG
65                 printf("(%d) unlink %s failed (%s)\n", 
66                        line_count, fname, cli_errstr(c));
67 #endif
68         }
69 }
70
71 void nb_open(char *fname, int handle, int size)
72 {
73         int fd, i;
74         int flags = O_RDWR|O_CREAT;
75         size_t st_size;
76         static int count;
77
78         strupper(fname);
79
80         if (size == 0) flags |= O_TRUNC;
81
82         fd = cli_open(c, fname, flags, DENY_NONE);
83         if (fd == -1) {
84 #if NBDEBUG
85                 printf("(%d) open %s failed for handle %d (%s)\n", 
86                        line_count, fname, handle, cli_errstr(c));
87 #endif
88                 return;
89         }
90         cli_getattrE(c, fd, NULL, &st_size, NULL, NULL, NULL);
91         if (size > st_size) {
92 #if NBDEBUG
93                 printf("(%d) needs expanding %s to %d from %d\n", 
94                        line_count, fname, size, (int)st_size);
95 #endif
96         } else if (size < st_size) {
97 #if NBDEBUG
98                 printf("(%d) needs truncating %s to %d from %d\n", 
99                        line_count, fname, size, (int)st_size);
100 #endif
101         }
102         for (i=0;i<MAX_FILES;i++) {
103                 if (ftable[i].handle == 0) break;
104         }
105         if (i == MAX_FILES) {
106                 printf("file table full for %s\n", fname);
107                 exit(1);
108         }
109         ftable[i].handle = handle;
110         ftable[i].fd = fd;
111         if (count++ % 100 == 0) {
112                 printf(".");
113         }
114 }
115
116 void nb_write(int handle, int size, int offset)
117 {
118         int i;
119
120         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
121
122         for (i=0;i<MAX_FILES;i++) {
123                 if (ftable[i].handle == handle) break;
124         }
125         if (i == MAX_FILES) {
126 #if NBDEBUG
127                 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n", 
128                        line_count, handle, size, offset);
129 #endif
130                 return;
131         }
132         if (cli_smbwrite(c, ftable[i].fd, buf, offset, size) != size) {
133                 printf("(%d) write failed on handle %d\n", 
134                        line_count, handle);
135         }
136 }
137
138 void nb_read(int handle, int size, int offset)
139 {
140         int i, ret;
141
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_read: handle %d was not open size=%d ofs=%d\n", 
147                        line_count, handle, size, offset);
148                 return;
149         }
150         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != size) {
151 #if NBDEBUG
152                 printf("(%d) read failed on handle %d ofs=%d size=%d res=%d\n", 
153                        line_count, handle, offset, size, ret);
154 #endif
155         }
156 }
157
158 void nb_close(int handle)
159 {
160         int i;
161         for (i=0;i<MAX_FILES;i++) {
162                 if (ftable[i].handle == handle) break;
163         }
164         if (i == MAX_FILES) {
165                 printf("(%d) nb_close: handle %d was not open\n", 
166                        line_count, handle);
167                 return;
168         }
169         cli_close(c, ftable[i].fd);
170         ftable[i].handle = 0;
171 }
172
173 void nb_mkdir(char *fname)
174 {
175         strupper(fname);
176
177         if (!cli_mkdir(c, fname)) {
178 #if NBDEBUG
179                 printf("mkdir %s failed (%s)\n", 
180                        fname, cli_errstr(c));
181 #endif
182         }
183 }
184
185 void nb_rmdir(char *fname)
186 {
187         strupper(fname);
188
189         if (!cli_rmdir(c, fname)) {
190 #if NBDEBUG
191                 printf("rmdir %s failed (%s)\n", 
192                        fname, cli_errstr(c));
193 #endif
194         }
195 }
196
197 void nb_rename(char *old, char *new)
198 {
199         strupper(old);
200         strupper(new);
201
202         if (!cli_rename(c, old, new)) {
203 #if NBDEBUG
204                 printf("rename %s %s failed (%s)\n", 
205                        old, new, cli_errstr(c));
206 #endif
207         }
208 }
209
210
211 void nb_stat(char *fname, int size)
212 {
213         size_t st_size;
214
215         strupper(fname);
216
217         if (!cli_getatr(c, fname, NULL, &st_size, NULL)) {
218 #if NBDEBUG
219                 printf("(%d) nb_stat: %s size=%d %s\n", 
220                        line_count, fname, size, cli_errstr(c));
221 #endif
222                 return;
223         }
224         if (st_size != size) {
225 #if NBDEBUG
226                 printf("(%d) nb_stat: %s wrong size %d %d\n", 
227                        line_count, fname, (int)st_size, size);
228 #endif
229         }
230 }
231
232 void nb_create(char *fname, int size)
233 {
234         nb_open(fname, 5000, size);
235         nb_close(5000);
236 }