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