3 Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define MAX_FILES 1000
25 static char buf[70000];
26 extern int line_count;
29 extern int sync_open, sync_dirs;
37 /* Find the directory holding a file, and flush it to disk. We do
38 this in -S mode after a directory-modifying mode, to simulate the
39 way knfsd tries to flush directories. MKDIR and similar operations
40 are meant to be synchronous on NFSv2. */
41 void sync_parent(char *fname)
47 if (strchr(fname, '/')) {
48 copy_name = strdup(fname);
49 slash = strrchr(copy_name, '/');
52 copy_name = strdup(".");
55 dir_fd = open(copy_name, O_RDONLY);
57 printf("open directory \"%s\" for sync failed: %s\n",
61 if (fdatasync(dir_fd) == -1) {
62 printf("datasync directory \"%s\" failed: %s\n",
66 if (close(dir_fd) == -1) {
67 printf("close directory failed: %s\n",
75 void nb_setup(int client)
80 void nb_unlink(char *fname)
84 if (unlink(fname) != 0) {
85 printf("(%d) unlink %s failed (%s)\n",
86 line_count, fname, strerror(errno));
92 void expand_file(int fd, int size)
96 s = MIN(sizeof(buf), size);
102 void nb_open(char *fname, int handle, int size)
105 int flags = O_RDWR|O_CREAT;
111 if (size == 0) flags |= O_TRUNC;
116 fd = open(fname, flags, 0600);
118 printf("(%d) open %s failed for handle %d (%s)\n",
119 line_count, fname, handle, strerror(errno));
123 if (size > st.st_size) {
125 printf("(%d) expanding %s to %d from %d\n",
126 line_count, fname, size, (int)st.st_size);
128 expand_file(fd, size - st.st_size);
129 } else if (size < st.st_size) {
130 printf("truncating %s to %d from %d\n",
131 fname, size, (int)st.st_size);
134 for (i=0;i<MAX_FILES;i++) {
135 if (ftable[i].handle == 0) break;
137 if (i == MAX_FILES) {
138 printf("file table full for %s\n", fname);
141 ftable[i].handle = handle;
143 if (count++ % 100 == 0) {
148 void nb_write(int handle, int size, int offset)
152 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
154 for (i=0;i<MAX_FILES;i++) {
155 if (ftable[i].handle == handle) break;
157 if (i == MAX_FILES) {
159 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n",
160 line_count, handle, size, offset);
164 lseek(ftable[i].fd, offset, SEEK_SET);
165 if (write(ftable[i].fd, buf, size) != size) {
166 printf("write failed on handle %d\n", handle);
170 void nb_read(int handle, int size, int offset)
173 for (i=0;i<MAX_FILES;i++) {
174 if (ftable[i].handle == handle) break;
176 if (i == MAX_FILES) {
177 printf("(%d) nb_read: handle %d was not open size=%d ofs=%d\n",
178 line_count, handle, size, offset);
181 lseek(ftable[i].fd, offset, SEEK_SET);
182 read(ftable[i].fd, buf, size);
185 void nb_close(int handle)
188 for (i=0;i<MAX_FILES;i++) {
189 if (ftable[i].handle == handle) break;
191 if (i == MAX_FILES) {
192 printf("(%d) nb_close: handle %d was not open\n",
197 ftable[i].handle = 0;
200 void nb_mkdir(char *fname)
204 if (mkdir(fname, 0700) != 0) {
206 printf("mkdir %s failed (%s)\n",
207 fname, strerror(errno));
214 void nb_rmdir(char *fname)
218 if (rmdir(fname) != 0) {
219 printf("rmdir %s failed (%s)\n",
220 fname, strerror(errno));
226 void nb_rename(char *old, char *new)
231 if (rename(old, new) != 0) {
232 printf("rename %s %s failed (%s)\n",
233 old, new, strerror(errno));
240 void nb_stat(char *fname, int size)
246 if (stat(fname, &st) != 0) {
247 printf("(%d) nb_stat: %s size=%d %s\n",
248 line_count, fname, size, strerror(errno));
251 if (S_ISDIR(st.st_mode)) return;
253 if (st.st_size != size) {
254 printf("(%d) nb_stat: %s wrong size %d %d\n",
255 line_count, fname, (int)st.st_size, size);
259 void nb_create(char *fname, int size)
261 nb_open(fname, 5000, size);