3 Copyright (C) Andrew Tridgell 1999
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.
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.
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.
22 #define MAX_FILES 1000
24 static char buf[70000];
25 extern int line_count;
34 void nb_setup(int client)
39 void nb_unlink(char *fname)
43 if (unlink(fname) != 0) {
44 printf("(%d) unlink %s failed (%s)\n",
45 line_count, fname, strerror(errno));
49 void expand_file(int fd, int size)
53 s = MIN(sizeof(buf), size);
59 void nb_open(char *fname, int handle, int size)
62 int flags = O_RDWR|O_CREAT;
68 if (size == 0) flags |= O_TRUNC;
70 fd = open(fname, flags, 0600);
72 printf("(%d) open %s failed for handle %d (%s)\n",
73 line_count, fname, handle, strerror(errno));
77 if (size > st.st_size) {
79 printf("(%d) expanding %s to %d from %d\n",
80 line_count, fname, size, (int)st.st_size);
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);
88 for (i=0;i<MAX_FILES;i++) {
89 if (ftable[i].handle == 0) break;
92 printf("file table full for %s\n", fname);
95 ftable[i].handle = handle;
97 if (count++ % 100 == 0) {
102 void nb_write(int handle, int size, int offset)
106 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
108 for (i=0;i<MAX_FILES;i++) {
109 if (ftable[i].handle == handle) break;
111 if (i == MAX_FILES) {
113 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n",
114 line_count, handle, size, offset);
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);
124 void nb_read(int handle, int size, int offset)
127 for (i=0;i<MAX_FILES;i++) {
128 if (ftable[i].handle == handle) break;
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);
135 lseek(ftable[i].fd, offset, SEEK_SET);
136 read(ftable[i].fd, buf, size);
139 void nb_close(int handle)
142 for (i=0;i<MAX_FILES;i++) {
143 if (ftable[i].handle == handle) break;
145 if (i == MAX_FILES) {
146 printf("(%d) nb_close: handle %d was not open\n",
151 ftable[i].handle = 0;
154 void nb_mkdir(char *fname)
158 if (mkdir(fname, 0700) != 0) {
160 printf("mkdir %s failed (%s)\n",
161 fname, strerror(errno));
166 void nb_rmdir(char *fname)
170 if (rmdir(fname) != 0) {
171 printf("rmdir %s failed (%s)\n",
172 fname, strerror(errno));
176 void nb_rename(char *old, char *new)
181 if (rename(old, new) != 0) {
182 printf("rename %s %s failed (%s)\n",
183 old, new, strerror(errno));
188 void nb_stat(char *fname, int size)
194 if (stat(fname, &st) != 0) {
195 printf("(%d) nb_stat: %s size=%d %s\n",
196 line_count, fname, size, strerror(errno));
199 if (S_ISDIR(st.st_mode)) return;
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);
207 void nb_create(char *fname, int size)
209 nb_open(fname, 5000, size);