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 3 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, see <http://www.gnu.org/licenses/>.
21 /* Wrappers for system calls that catch errors. */
25 #define MAX_FILES 1000
27 static char buf[70000];
28 extern int line_count;
35 void do_unlink(char *fname)
39 if (unlink(fname) != 0) {
40 printf("(%d) unlink %s failed (%s)\n",
41 line_count, fname, strerror(errno));
45 void expand_file(int fd, int size)
49 s = MIN(sizeof(buf), size);
55 void do_open(char *fname, int handle, int size)
58 int flags = O_RDWR|O_CREAT;
64 if (size == 0) flags |= O_TRUNC;
66 fd = open(fname, flags, 0600);
68 printf("(%d) open %s failed for handle %d (%s)\n",
69 line_count, fname, handle, strerror(errno));
73 if (size > st.st_size) {
75 printf("(%d) expanding %s to %d from %d\n",
76 line_count, fname, size, (int)st.st_size);
78 expand_file(fd, size - st.st_size);
79 } else if (size < st.st_size) {
80 printf("truncating %s to %d from %d\n",
81 fname, size, (int)st.st_size);
84 for (i=0;i<MAX_FILES;i++) {
85 if (ftable[i].handle == 0) break;
88 printf("file table full for %s\n", fname);
91 ftable[i].handle = handle;
93 if (count++ % 100 == 0) {
98 void do_write(int handle, int size, int offset)
102 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
104 for (i=0;i<MAX_FILES;i++) {
105 if (ftable[i].handle == handle) break;
107 if (i == MAX_FILES) {
109 printf("(%d) do_write: handle %d was not open size=%d ofs=%d\n",
110 line_count, handle, size, offset);
114 lseek(ftable[i].fd, offset, SEEK_SET);
115 if (write(ftable[i].fd, buf, size) != size) {
116 printf("write failed on handle %d (%s)\n", handle, strerror(errno));
120 void do_read(int handle, int size, int offset)
123 for (i=0;i<MAX_FILES;i++) {
124 if (ftable[i].handle == handle) break;
126 if (i == MAX_FILES) {
127 printf("(%d) do_read: handle %d was not open size=%d ofs=%d\n",
128 line_count, handle, size, offset);
131 lseek(ftable[i].fd, offset, SEEK_SET);
132 read(ftable[i].fd, buf, size);
135 void do_close(int handle)
138 for (i=0;i<MAX_FILES;i++) {
139 if (ftable[i].handle == handle) break;
141 if (i == MAX_FILES) {
142 printf("(%d) do_close: handle %d was not open\n",
147 ftable[i].handle = 0;
150 void do_mkdir(char *fname)
154 if (mkdir(fname, 0700) != 0) {
156 printf("mkdir %s failed (%s)\n",
157 fname, strerror(errno));
162 void do_rmdir(char *fname)
166 if (rmdir(fname) != 0) {
167 printf("rmdir %s failed (%s)\n",
168 fname, strerror(errno));
172 void do_rename(char *old, char *new)
177 if (rename(old, new) != 0) {
178 printf("rename %s %s failed (%s)\n",
179 old, new, strerror(errno));
184 void do_stat(char *fname, int size)
190 if (stat(fname, &st) != 0) {
191 printf("(%d) do_stat: %s size=%d %s\n",
192 line_count, fname, size, strerror(errno));
195 if (S_ISDIR(st.st_mode)) return;
197 if (st.st_size != size) {
198 printf("(%d) do_stat: %s wrong size %d %d\n",
199 line_count, fname, (int)st.st_size, size);
203 void do_create(char *fname, int size)
205 do_open(fname, 5000, size);