2 Unix SMB/Netbios implementation.
4 VFS initialisation and support functions
5 Copyright (C) Tim Potter 1999
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.
27 extern int DEBUGLEVEL;
29 /* Some structures to help us initialise the vfs operations table */
36 /* Default vfs hooks. WARNING: The order of these initialisers is
37 very important. They must be in the same order as defined in
38 vfs.h. Change at your own peril. */
40 struct vfs_ops default_vfs_ops = {
44 vfswrap_dummy_connect,
45 vfswrap_dummy_disconnect,
48 /* Directory operations */
74 /****************************************************************************
75 initialise default vfs hooks
76 ****************************************************************************/
77 int vfs_init_default(connection_struct *conn)
79 DEBUG(3, ("Initialising default vfs hooks\n"));
81 memcpy(&conn->vfs_ops, &default_vfs_ops, sizeof(conn->vfs_ops));
85 /****************************************************************************
86 initialise custom vfs hooks
87 ****************************************************************************/
89 BOOL vfs_init_custom(connection_struct *conn)
92 struct vfs_ops *ops, *(*fptr)(struct vfs_options *options);
94 DEBUG(3, ("Initialising custom vfs hooks from %s\n",
95 lp_vfsobj(SNUM(conn))));
97 /* Open object file */
99 handle = dlopen(lp_vfsobj(SNUM(conn)), RTLD_NOW);
101 DEBUG(0, ("Error opening %s: %s\n", lp_vfsobj(SNUM(conn)),
106 /* Get handle on vfs_init() symbol */
108 fptr = dlsym(handle, "vfs_init");
110 DEBUG(0, ("No vfs_init() symbol found in %s\n",
111 lp_vfsobj(SNUM(conn))));
117 /* Initialise vfs_ops structure */
119 if ((ops = fptr(lp_vfsoptions(SNUM(conn)))) == NULL) {
123 /* Fill in unused operations with default (disk based) ones.
124 There's probably a neater way to do this then a whole bunch of
127 memcpy(&conn->vfs_ops, ops, sizeof(conn->vfs_ops));
129 if (conn->vfs_ops.connect == NULL) {
130 conn->vfs_ops.connect = default_vfs_ops.connect;
133 if (conn->vfs_ops.disconnect == NULL) {
134 conn->vfs_ops.disconnect = default_vfs_ops.disconnect;
137 if (conn->vfs_ops.disk_free == NULL) {
138 conn->vfs_ops.disk_free = default_vfs_ops.disk_free;
141 if (conn->vfs_ops.opendir == NULL) {
142 conn->vfs_ops.opendir = default_vfs_ops.opendir;
145 if (conn->vfs_ops.readdir == NULL) {
146 conn->vfs_ops.readdir = default_vfs_ops.readdir;
149 if (conn->vfs_ops.mkdir == NULL) {
150 conn->vfs_ops.mkdir = default_vfs_ops.mkdir;
153 if (conn->vfs_ops.rmdir == NULL) {
154 conn->vfs_ops.rmdir = default_vfs_ops.rmdir;
157 if (conn->vfs_ops.closedir == NULL) {
158 conn->vfs_ops.closedir = default_vfs_ops.closedir;
161 if (conn->vfs_ops.open == NULL) {
162 conn->vfs_ops.open = default_vfs_ops.open;
165 if (conn->vfs_ops.close == NULL) {
166 conn->vfs_ops.close = default_vfs_ops.close;
169 if (conn->vfs_ops.read == NULL) {
170 conn->vfs_ops.read = default_vfs_ops.read;
173 if (conn->vfs_ops.write == NULL) {
174 conn->vfs_ops.write = default_vfs_ops.write;
177 if (conn->vfs_ops.lseek == NULL) {
178 conn->vfs_ops.lseek = default_vfs_ops.lseek;
181 if (conn->vfs_ops.rename == NULL) {
182 conn->vfs_ops.rename = default_vfs_ops.rename;
185 if (conn->vfs_ops.sync == NULL) {
186 conn->vfs_ops.sync = default_vfs_ops.sync;
189 if (conn->vfs_ops.stat == NULL) {
190 conn->vfs_ops.stat = default_vfs_ops.stat;
193 if (conn->vfs_ops.fstat == NULL) {
194 conn->vfs_ops.fstat = default_vfs_ops.fstat;
197 if (conn->vfs_ops.lstat == NULL) {
198 conn->vfs_ops.lstat = default_vfs_ops.lstat;
201 if (conn->vfs_ops.lock == NULL) {
202 conn->vfs_ops.lock = default_vfs_ops.lock;
205 if (conn->vfs_ops.unlink == NULL) {
206 conn->vfs_ops.unlink = default_vfs_ops.unlink;
209 if (conn->vfs_ops.chmod == NULL) {
210 conn->vfs_ops.chmod = default_vfs_ops.chmod;
213 if (conn->vfs_ops.utime == NULL) {
214 conn->vfs_ops.utime = default_vfs_ops.utime;
221 /*******************************************************************
222 check if a vfs file exists
223 ********************************************************************/
224 BOOL vfs_file_exist(connection_struct *conn,char *fname,SMB_STRUCT_STAT *sbuf)
227 if (!sbuf) sbuf = &st;
229 if (conn->vfs_ops.stat(fname,sbuf) != 0)
232 return(S_ISREG(sbuf->st_mode));
235 /****************************************************************************
236 write data to a fd on the vfs
237 ****************************************************************************/
238 ssize_t vfs_write_data(files_struct *fsp,char *buffer,size_t N)
242 int fd = fsp->fd_ptr->fd;
246 ret = fsp->conn->vfs_ops.write(fd,buffer + total,N - total);
248 if (ret == -1) return -1;
249 if (ret == 0) return total;
253 return (ssize_t)total;
256 /****************************************************************************
257 transfer some data between two file_struct's
258 ****************************************************************************/
259 SMB_OFF_T vfs_transfer_file(int in_fd, files_struct *in_fsp,
260 int out_fd, files_struct *out_fsp,
261 SMB_OFF_T n, char *header, int headlen, int align)
263 static char *buf=NULL;
268 DEBUG(4,("vfs_transfer_file n=%.0f (head=%d) called\n",(double)n,headlen));
270 /* Check we have at least somewhere to read from */
272 SMB_ASSERT((in_fd != -1) || (in_fsp != NULL));
275 size = lp_readsize();
276 size = MAX(size,1024);
279 while (!buf && size>0) {
280 buf = (char *)Realloc(buf,size+8);
285 DEBUG(0,("Can't allocate transfer buffer!\n"));
289 abuf = buf + (align%8);
296 int s = (int)MIN(n,(SMB_OFF_T)size);
301 if (header && (headlen >= MIN(s,1024))) {
311 if (header && headlen > 0)
313 ret = MIN(headlen,size);
314 memcpy(buf1,header,ret);
317 if (headlen <= 0) header = NULL;
322 in_fsp->conn->vfs_ops.read(in_fsp->fd_ptr->fd,buf1+ret,s-ret) : read(in_fd,buf1+ret,s-ret);
328 ret2 = out_fsp->conn->vfs_ops.write(out_fsp->fd_ptr->fd,buf1,ret);
330 ret2= (out_fd != -1) ? write_data(out_fd,buf1,ret) : ret;
334 if (ret2 > 0) total += ret2;
335 /* if we can't write then dump excess data */
337 vfs_transfer_file(in_fd, in_fsp, -1,NULL,n-(ret+headlen),NULL,0,0);
339 if (ret <= 0 || ret2 != ret)
346 /*******************************************************************
347 a vfs_readdir wrapper which just returns the file name
348 ********************************************************************/
349 char *vfs_readdirname(connection_struct *conn, void *p)
354 if (!p) return(NULL);
356 ptr = (struct dirent *)conn->vfs_ops.readdir(p);
357 if (!ptr) return(NULL);
362 if (telldir(p) < 0) return(NULL);
365 #ifdef HAVE_BROKEN_READDIR
366 /* using /usr/ucb/cc is BAD */
372 memcpy(buf, dname, NAMLEN(ptr)+1);
373 unix_to_dos(buf, True);
377 unix_to_dos(dname, True);