4 Copyright (C) by Andrew Tridgell <tridge@samba.org> 1999, 2001
5 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* Wrappers for system calls that catch errors. */
26 #define MAX_FILES 1000
28 static char buf[70000];
29 extern int line_count;
36 void do_unlink(char *fname)
40 if (unlink(fname) != 0) {
41 printf("(%d) unlink %s failed (%s)\n",
42 line_count, fname, strerror(errno));
46 void expand_file(int fd, int size)
50 s = MIN(sizeof(buf), size);
56 void do_open(char *fname, int handle, int size)
59 int flags = O_RDWR|O_CREAT;
65 if (size == 0) flags |= O_TRUNC;
67 fd = open(fname, flags, 0600);
69 printf("(%d) open %s failed for handle %d (%s)\n",
70 line_count, fname, handle, strerror(errno));
74 if (size > st.st_size) {
76 printf("(%d) expanding %s to %d from %d\n",
77 line_count, fname, size, (int)st.st_size);
79 expand_file(fd, size - st.st_size);
80 } else if (size < st.st_size) {
81 printf("truncating %s to %d from %d\n",
82 fname, size, (int)st.st_size);
85 for (i=0;i<MAX_FILES;i++) {
86 if (ftable[i].handle == 0) break;
89 printf("file table full for %s\n", fname);
92 ftable[i].handle = handle;
94 if (count++ % 100 == 0) {
99 void do_write(int handle, int size, int offset)
103 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
105 for (i=0;i<MAX_FILES;i++) {
106 if (ftable[i].handle == handle) break;
108 if (i == MAX_FILES) {
110 printf("(%d) do_write: handle %d was not open size=%d ofs=%d\n",
111 line_count, handle, size, offset);
115 lseek(ftable[i].fd, offset, SEEK_SET);
116 if (write(ftable[i].fd, buf, size) != size) {
117 printf("write failed on handle %d\n", handle);
121 void do_read(int handle, int size, int offset)
124 for (i=0;i<MAX_FILES;i++) {
125 if (ftable[i].handle == handle) break;
127 if (i == MAX_FILES) {
128 printf("(%d) do_read: handle %d was not open size=%d ofs=%d\n",
129 line_count, handle, size, offset);
132 lseek(ftable[i].fd, offset, SEEK_SET);
133 read(ftable[i].fd, buf, size);
136 void do_close(int handle)
139 for (i=0;i<MAX_FILES;i++) {
140 if (ftable[i].handle == handle) break;
142 if (i == MAX_FILES) {
143 printf("(%d) do_close: handle %d was not open\n",
148 ftable[i].handle = 0;
151 void do_mkdir(char *fname)
155 if (mkdir(fname, 0700) != 0) {
157 printf("mkdir %s failed (%s)\n",
158 fname, strerror(errno));
163 void do_rmdir(char *fname)
167 if (rmdir(fname) != 0) {
168 printf("rmdir %s failed (%s)\n",
169 fname, strerror(errno));
173 void do_rename(char *old, char *new)
178 if (rename(old, new) != 0) {
179 printf("rename %s %s failed (%s)\n",
180 old, new, strerror(errno));
185 void do_stat(char *fname, int size)
191 if (stat(fname, &st) != 0) {
192 printf("(%d) do_stat: %s size=%d %s\n",
193 line_count, fname, size, strerror(errno));
196 if (S_ISDIR(st.st_mode)) return;
198 if (st.st_size != size) {
199 printf("(%d) do_stat: %s wrong size %d %d\n",
200 line_count, fname, (int)st.st_size, size);
204 void do_create(char *fname, int size)
206 do_open(fname, 5000, size);