Added OLD_NTDOMAIN to remove warnings about undefined functions.
[jra/samba/.git] / source3 / smbd / vfs-wrap.c
1 #define OLD_NTDOMAIN 1
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    Wrap disk only vfs functions to sidestep dodgy compilers.
6    Copyright (C) Tim Potter 1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /* Check for NULL pointer parameters in vfswrap_* functions */
26
27 #define VFS_CHECK_NULL
28
29 /* We don't want to have NULL function pointers lying around.  Someone
30    is sure to try and execute them.  These stubs are used to prevent
31    this possibility. */
32
33 int vfswrap_dummy_connect(connection_struct *conn, char *service, char *user)
34 {
35     return 0;    /* Return >= 0 for success */
36 }
37
38 void vfswrap_dummy_disconnect(connection_struct *conn)
39 {
40 }
41
42 /* Disk operations */
43
44 SMB_BIG_UINT vfswrap_disk_free(connection_struct *conn, char *path, BOOL small_query, SMB_BIG_UINT *bsize, 
45                                SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
46 {
47     SMB_BIG_UINT result;
48
49 #ifdef VFS_CHECK_NULL
50     if ((path == NULL) || (bsize == NULL) || (dfree == NULL) ||
51         (dsize == NULL)) {
52         
53         smb_panic("NULL pointer passed to vfswrap_disk_free() function\n");
54     }
55 #endif
56
57     result = sys_disk_free(path, small_query, bsize, dfree, dsize);
58     return result;
59 }
60     
61 /* Directory operations */
62
63 DIR *vfswrap_opendir(connection_struct *conn, char *fname)
64 {
65     DIR *result;
66
67     START_PROFILE(syscall_opendir);
68
69 #ifdef VFS_CHECK_NULL
70     if (fname == NULL) {
71         smb_panic("NULL pointer passed to vfswrap_opendir()\n");
72     }
73 #endif
74
75     result = opendir(fname);
76     END_PROFILE(syscall_opendir);
77     return result;
78 }
79
80 struct dirent *vfswrap_readdir(connection_struct *conn, DIR *dirp)
81 {
82     struct dirent *result;
83
84     START_PROFILE(syscall_readdir);
85
86 #ifdef VFS_CHECK_NULL
87     if (dirp == NULL) {
88         smb_panic("NULL pointer passed to vfswrap_readdir()\n");
89     }
90 #endif
91
92     result = readdir(dirp);
93     END_PROFILE(syscall_readdir);
94     return result;
95 }
96
97 int vfswrap_mkdir(connection_struct *conn, char *path, mode_t mode)
98 {
99     int result;
100
101     START_PROFILE(syscall_mkdir);
102
103 #ifdef VFS_CHECK_NULL
104     if (path == NULL) {
105         smb_panic("NULL pointer passed to vfswrap_mkdir()\n");
106     }
107 #endif
108
109     result = mkdir(path, mode);
110     END_PROFILE(syscall_mkdir);
111     return result;
112 }
113
114 int vfswrap_rmdir(connection_struct *conn, char *path)
115 {
116     int result;
117
118     START_PROFILE(syscall_rmdir);
119
120 #ifdef VFS_CHECK_NULL
121     if (path == NULL) {
122         smb_panic("NULL pointer passed to vfswrap_rmdir()\n");
123     }
124 #endif
125
126     result = rmdir(path);
127     END_PROFILE(syscall_rmdir);
128     return result;
129 }
130
131 int vfswrap_closedir(connection_struct *conn, DIR *dirp)
132 {
133     int result;
134
135     START_PROFILE(syscall_closedir);
136
137 #ifdef VFS_CHECK_NULL
138     if (dirp == NULL) {
139         smb_panic("NULL pointer passed to vfswrap_closedir()\n");
140     }
141 #endif
142
143     result = closedir(dirp);
144     END_PROFILE(syscall_closedir);
145     return result;
146 }
147
148 /* File operations */
149     
150 int vfswrap_open(connection_struct *conn, char *fname, int flags, mode_t mode)
151 {
152     int result;
153
154     START_PROFILE(syscall_open);
155
156 #ifdef VFS_CHECK_NULL
157     if (fname == NULL) {
158         smb_panic("NULL pointer passed to vfswrap_open()\n");
159     }
160 #endif
161
162     result = sys_open(fname, flags, mode);
163     END_PROFILE(syscall_open);
164     return result;
165 }
166
167 int vfswrap_close(files_struct *fsp, int fd)
168 {
169     int result;
170
171     START_PROFILE(syscall_close);
172
173     result = close(fd);
174     END_PROFILE(syscall_close);
175     return result;
176 }
177
178 ssize_t vfswrap_read(files_struct *fsp, int fd, char *data, size_t n)
179 {
180     ssize_t result;
181
182     START_PROFILE_BYTES(syscall_read, n);
183
184 #ifdef VFS_CHECK_NULL
185     if (data == NULL) {
186         smb_panic("NULL pointer passed to vfswrap_read()\n");
187     }
188 #endif
189
190     result = read(fd, data, n);
191     END_PROFILE(syscall_read);
192     return result;
193 }
194
195 ssize_t vfswrap_write(files_struct *fsp, int fd, char *data, size_t n)
196 {
197     ssize_t result;
198
199     START_PROFILE_BYTES(syscall_write, n);
200
201 #ifdef VFS_CHECK_NULL
202     if (data == NULL) {
203         smb_panic("NULL pointer passed to vfswrap_write()\n");
204     }
205 #endif
206
207     result = write(fd, data, n);
208     END_PROFILE(syscall_write);
209     return result;
210 }
211
212 SMB_OFF_T vfswrap_lseek(files_struct *fsp, int filedes, SMB_OFF_T offset, int whence)
213 {
214     SMB_OFF_T result;
215
216     START_PROFILE(syscall_lseek);
217
218     result = sys_lseek(filedes, offset, whence);
219     END_PROFILE(syscall_lseek);
220     return result;
221 }
222
223 int vfswrap_rename(connection_struct *conn, char *old, char *new)
224 {
225     int result;
226
227     START_PROFILE(syscall_rename);
228
229 #ifdef VFS_CHECK_NULL
230     if ((old == NULL) || (new == NULL)) {
231         smb_panic("NULL pointer passed to vfswrap_rename()\n");
232     }
233 #endif
234
235     result = rename(old, new);
236     END_PROFILE(syscall_rename);
237     return result;
238 }
239
240 int vfswrap_fsync(files_struct *fsp, int fd)
241 {
242 #ifdef HAVE_FSYNC
243     int result;
244
245     START_PROFILE(syscall_fsync);
246
247     result = fsync(fd);
248     END_PROFILE(syscall_fsync);
249     return result;
250 #else
251         return 0;
252 #endif
253 }
254
255 int vfswrap_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *sbuf)
256 {
257     int result;
258
259     START_PROFILE(syscall_stat);
260
261 #ifdef VFS_CHECK_NULL
262     if ((fname == NULL) || (sbuf == NULL)) {
263         smb_panic("NULL pointer passed to vfswrap_stat()\n");
264     }
265 #endif
266
267     result = sys_stat(fname, sbuf);
268     END_PROFILE(syscall_stat);
269     return result;
270 }
271
272 int vfswrap_fstat(files_struct *fsp, int fd, SMB_STRUCT_STAT *sbuf)
273 {
274     int result;
275
276     START_PROFILE(syscall_fstat);
277
278 #ifdef VFS_CHECK_NULL
279     if (sbuf == NULL) {
280         smb_panic("NULL pointer passed to vfswrap_fstat()\n");
281     }
282 #endif
283
284     result = sys_fstat(fd, sbuf);
285     END_PROFILE(syscall_fstat);
286     return result;
287 }
288
289 int vfswrap_lstat(connection_struct *conn, char *path, SMB_STRUCT_STAT *sbuf)
290 {
291     int result;
292
293     START_PROFILE(syscall_lstat);
294
295 #ifdef VFS_CHECK_NULL
296     if ((path == NULL) || (sbuf == NULL)) {
297         smb_panic("NULL pointer passed to vfswrap_lstat()\n");
298     }
299 #endif
300
301     result = sys_lstat(path, sbuf);
302     END_PROFILE(syscall_lstat);
303     return result;
304 }
305
306 int vfswrap_unlink(connection_struct *conn, char *path)
307 {
308     int result;
309
310     START_PROFILE(syscall_unlink);
311
312 #ifdef VFS_CHECK_NULL
313     if (path == NULL) {
314         smb_panic("NULL pointer passed to vfswrap_unlink()\n");
315     }
316 #endif
317
318     result = unlink(path);
319     END_PROFILE(syscall_unlink);
320     return result;
321 }
322
323 int vfswrap_chmod(connection_struct *conn, char *path, mode_t mode)
324 {
325     int result;
326
327     START_PROFILE(syscall_chmod);
328
329 #ifdef VFS_CHECK_NULL
330     if (path == NULL) {
331         smb_panic("NULL pointer passed to vfswrap_chmod()\n");
332     }
333 #endif
334
335     result = chmod(path, mode);
336     END_PROFILE(syscall_chmod);
337     return result;
338 }
339
340 int vfswrap_chown(connection_struct *conn, char *path, uid_t uid, gid_t gid)
341 {
342     int result;
343
344     START_PROFILE(syscall_chown);
345
346 #ifdef VFS_CHECK_NULL
347     if (path == NULL) {
348         smb_panic("NULL pointer passed to vfswrap_chown()\n");
349     }
350 #endif
351
352     result = sys_chown(path, uid, gid);
353     END_PROFILE(syscall_chown);
354     return result;
355 }
356
357 int vfswrap_chdir(connection_struct *conn, char *path)
358 {
359     int result;
360
361     START_PROFILE(syscall_chdir);
362
363 #ifdef VFS_CHECK_NULL
364     if (path == NULL) {
365         smb_panic("NULL pointer passed to vfswrap_chdir()\n");
366     }
367 #endif
368
369     result = chdir(path);
370     END_PROFILE(syscall_chdir);
371     return result;
372 }
373
374 char *vfswrap_getwd(connection_struct *conn, char *path)
375 {
376     char *result;
377
378     START_PROFILE(syscall_getwd);
379
380 #ifdef VFS_CHECK_NULL
381     if (path == NULL) {
382         smb_panic("NULL pointer passed to vfswrap_getwd()\n");
383     }
384 #endif
385
386     result = sys_getwd(path);
387     END_PROFILE(syscall_getwd);
388     return result;
389 }
390
391 int vfswrap_utime(connection_struct *conn, char *path, struct utimbuf *times)
392 {
393     int result;
394
395     START_PROFILE(syscall_utime);
396
397 #ifdef VFS_CHECK_NULL
398     if ((path == NULL) || (times == NULL)) {
399         smb_panic("NULL pointer passed to vfswrap_utime()\n");
400     }
401 #endif
402
403     result = utime(path, times);
404     END_PROFILE(syscall_utime);
405     return result;
406 }
407
408 int vfswrap_ftruncate(files_struct *fsp, int fd, SMB_OFF_T len)
409 {
410         int result = -1;
411     START_PROFILE(syscall_ftruncate);
412
413 #ifdef HAVE_FTRUNCATE_EXTEND
414         result = sys_ftruncate(fd, len);
415     END_PROFILE(syscall_ftruncate);
416     return result;
417 #else
418
419         /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
420                 extend a file with ftruncate. Provide alternate implementation
421                 for this */
422
423         struct vfs_ops *vfs_ops = fsp->conn->vfs_ops;
424         SMB_STRUCT_STAT st;
425         char c = 0;
426         SMB_OFF_T currpos;
427
428         currpos = vfs_ops->lseek(fsp, (SMB_OFF_T)0, SEEK_CUR);
429         if(currpos == -1) {
430                 goto done;
431         }
432
433         /* Do an fstat to see if the file is longer than
434                 the requested size (call ftruncate),
435                 or shorter, in which case seek to len - 1 and write 1
436                 byte of zero */
437         if(vfs_ops->fstat(fsp, &st)<0) {
438                 goto done;
439         }
440
441 #ifdef S_ISFIFO
442         if (S_ISFIFO(st.st_mode)) {
443                 result = 0;
444                 goto done;
445         }
446 #endif
447
448         if(st.st_size == len) {
449                 result = 0;
450                 goto done;
451         }
452
453         if(st.st_size > len) {
454                 /* Yes this is *deliberately* sys_ftruncate ! JRA */
455                 result = sys_ftruncate(fd, len);
456                 goto done;
457         }
458
459         if(vfs_ops->lseek(fsp, len-1, SEEK_SET) != len -1) {
460                 goto done;
461         }
462
463         if(vfs_ops->write(fsp, &c, 1)!=1) {
464                 goto done;
465         }
466
467         /* Seek to where we were */
468         if(vfs_ops->lseek(fsp, currpos, SEEK_SET) != currpos) {
469                 goto done;
470         }
471   done:
472
473 #endif
474
475     END_PROFILE(syscall_ftruncate);
476     return result;
477 }
478
479 BOOL vfswrap_lock(files_struct *fsp, int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
480 {
481     BOOL result;
482
483     START_PROFILE(syscall_fcntl_lock);
484
485     result =  fcntl_lock(fd, op, offset, count,type);
486     END_PROFILE(syscall_fcntl_lock);
487     return result;
488 }
489
490 size_t vfswrap_fget_nt_acl(files_struct *fsp, int fd, SEC_DESC **ppdesc)
491 {
492         return get_nt_acl(fsp, ppdesc);
493 }
494
495 size_t vfswrap_get_nt_acl(files_struct *fsp, char *name, SEC_DESC **ppdesc)
496 {
497         return get_nt_acl(fsp, ppdesc);
498 }
499
500 BOOL vfswrap_fset_nt_acl(files_struct *fsp, int fd, uint32 security_info_sent, SEC_DESC *psd)
501 {
502         return set_nt_acl(fsp, security_info_sent, psd);
503 }
504
505 BOOL vfswrap_set_nt_acl(files_struct *fsp, char *name, uint32 security_info_sent, SEC_DESC *psd)
506 {
507         return set_nt_acl(fsp, security_info_sent, psd);
508 }
509 #undef OLD_NTDOMAIN