2 Unix SMB/CIFS implementation.
3 SMB client library implementation (Old interface compatibility)
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2008
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsmb_internal.h"
28 struct smbc_compat_fdlist
{
31 struct smbc_compat_fdlist
*next
, *prev
;
34 static SMBCCTX
* statcont
= NULL
;
35 static int smbc_compat_initialized
= 0;
36 static int smbc_compat_nextfd
= 0;
37 static struct smbc_compat_fdlist
* smbc_compat_fd_in_use
= NULL
;
38 static struct smbc_compat_fdlist
* smbc_compat_fd_avail
= NULL
;
40 /* Find an fd and return the SMBCFILE * or NULL on failure */
44 struct smbc_compat_fdlist
* f
= smbc_compat_fd_in_use
;
53 /* Add an fd, returns 0 on success, -1 on error with errno set */
55 add_fd(SMBCFILE
* file
)
57 struct smbc_compat_fdlist
* f
= smbc_compat_fd_avail
;
60 /* We found one that's available */
61 DLIST_REMOVE(smbc_compat_fd_avail
, f
);
64 * None were available, so allocate one. Keep the number of
65 * file descriptors determinate. This allows the application
66 * to allocate bitmaps or mapping of file descriptors based on
67 * a known maximum number of file descriptors that will ever
70 if (smbc_compat_nextfd
>= FD_SETSIZE
) {
75 f
= SMB_MALLOC_P(struct smbc_compat_fdlist
);
81 f
->fd
= SMBC_BASE_FD
+ smbc_compat_nextfd
++;
85 DLIST_ADD(smbc_compat_fd_in_use
, f
);
92 /* Delete an fd, returns 0 on success */
96 struct smbc_compat_fdlist
* f
= smbc_compat_fd_in_use
;
106 DLIST_REMOVE(smbc_compat_fd_in_use
, f
);
108 DLIST_ADD(smbc_compat_fd_avail
, f
);
117 smbc_init(smbc_get_auth_data_fn fn
,
120 if (!smbc_compat_initialized
) {
121 statcont
= smbc_new_context();
125 smbc_setDebug(statcont
, debug
);
126 smbc_setFunctionAuthData(statcont
, fn
);
128 if (!smbc_init_context(statcont
)) {
129 smbc_free_context(statcont
, False
);
133 smbc_compat_initialized
= 1;
142 smbc_set_context(SMBCCTX
* context
)
144 SMBCCTX
*old_context
= statcont
;
147 /* Save provided context. It must have been initialized! */
150 /* You'd better know what you're doing. We won't help you. */
151 smbc_compat_initialized
= 1;
159 smbc_open(const char *furl
,
166 file
= smbc_getFunctionOpen(statcont
)(statcont
, furl
, flags
, mode
);
172 smbc_getFunctionClose(statcont
)(statcont
, file
);
178 smbc_creat(const char *furl
,
184 file
= smbc_getFunctionCreat(statcont
)(statcont
, furl
, mode
);
190 /* Hmm... should we delete the file too ? I guess we could try */
191 smbc_getFunctionClose(statcont
)(statcont
, file
);
192 smbc_getFunctionUnlink(statcont
)(statcont
, furl
);
203 SMBCFILE
* file
= find_fd(fd
);
204 return smbc_getFunctionRead(statcont
)(statcont
, file
, buf
, bufsize
);
212 SMBCFILE
* file
= find_fd(fd
);
213 return smbc_getFunctionWrite(statcont
)(statcont
, file
, buf
, bufsize
);
221 SMBCFILE
* file
= find_fd(fd
);
222 return smbc_getFunctionLseek(statcont
)(statcont
, file
, offset
, whence
);
228 SMBCFILE
* file
= find_fd(fd
);
230 return smbc_getFunctionClose(statcont
)(statcont
, file
);
234 smbc_unlink(const char *fname
)
236 return smbc_getFunctionUnlink(statcont
)(statcont
, fname
);
240 smbc_rename(const char *ourl
,
243 return smbc_getFunctionRename(statcont
)(statcont
, ourl
,
248 smbc_opendir(const char *durl
)
253 file
= smbc_getFunctionOpendir(statcont
)(statcont
, durl
);
259 smbc_getFunctionClosedir(statcont
)(statcont
, file
);
265 smbc_closedir(int dh
)
267 SMBCFILE
* file
= find_fd(dh
);
269 return smbc_getFunctionClosedir(statcont
)(statcont
, file
);
273 smbc_getdents(unsigned int dh
,
274 struct smbc_dirent
*dirp
,
277 SMBCFILE
* file
= find_fd(dh
);
278 return smbc_getFunctionGetdents(statcont
)(statcont
, file
, dirp
, count
);
282 smbc_readdir(unsigned int dh
)
284 SMBCFILE
* file
= find_fd(dh
);
285 return smbc_getFunctionReaddir(statcont
)(statcont
, file
);
291 SMBCFILE
* file
= find_fd(dh
);
292 return smbc_getFunctionTelldir(statcont
)(statcont
, file
);
296 smbc_lseekdir(int fd
,
299 SMBCFILE
* file
= find_fd(fd
);
300 return smbc_getFunctionLseekdir(statcont
)(statcont
, file
, offset
);
304 smbc_mkdir(const char *durl
,
307 return smbc_getFunctionMkdir(statcont
)(statcont
, durl
, mode
);
311 smbc_rmdir(const char *durl
)
313 return smbc_getFunctionRmdir(statcont
)(statcont
, durl
);
317 smbc_notify(int dh
, smbc_bool recursive
, uint32_t completion_filter
,
318 unsigned callback_timeout_ms
,
319 smbc_notify_callback_fn cb
, void *private_data
)
321 SMBCFILE
*dir
= find_fd(dh
);
322 return smbc_getFunctionNotify(statcont
)(
323 statcont
, dir
, recursive
, completion_filter
,
324 callback_timeout_ms
, cb
, private_data
);
328 smbc_stat(const char *url
,
331 return smbc_getFunctionStat(statcont
)(statcont
, url
, st
);
338 SMBCFILE
* file
= find_fd(fd
);
339 return smbc_getFunctionFstat(statcont
)(statcont
, file
, st
);
343 smbc_statvfs(char *path
,
346 return smbc_getFunctionStatVFS(statcont
)(statcont
, path
, st
);
350 smbc_fstatvfs(int fd
,
353 SMBCFILE
* file
= find_fd(fd
);
354 return smbc_getFunctionFstatVFS(statcont
)(statcont
, file
, st
);
358 smbc_ftruncate(int fd
,
361 SMBCFILE
* file
= find_fd(fd
);
362 return smbc_getFunctionFtruncate(statcont
)(statcont
, file
, size
);
366 smbc_chmod(const char *url
,
369 return smbc_getFunctionChmod(statcont
)(statcont
, url
, mode
);
373 smbc_utimes(const char *fname
,
374 struct timeval
*tbuf
)
376 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, tbuf
);
381 smbc_utime(const char *fname
,
382 struct utimbuf
*utbuf
)
384 struct timeval tv
[2];
387 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, NULL
);
389 tv
[0].tv_sec
= utbuf
->actime
;
390 tv
[1].tv_sec
= utbuf
->modtime
;
391 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
393 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, tv
);
398 smbc_setxattr(const char *fname
,
404 return smbc_getFunctionSetxattr(statcont
)(statcont
,
410 smbc_lsetxattr(const char *fname
,
416 return smbc_getFunctionSetxattr(statcont
)(statcont
,
422 smbc_fsetxattr(int fd
,
428 SMBCFILE
* file
= find_fd(fd
);
433 return smbc_getFunctionSetxattr(statcont
)(statcont
,
439 smbc_getxattr(const char *fname
,
444 return smbc_getFunctionGetxattr(statcont
)(statcont
,
450 smbc_lgetxattr(const char *fname
,
455 return smbc_getFunctionGetxattr(statcont
)(statcont
,
461 smbc_fgetxattr(int fd
,
466 SMBCFILE
* file
= find_fd(fd
);
471 return smbc_getFunctionGetxattr(statcont
)(statcont
,
477 smbc_removexattr(const char *fname
,
480 return smbc_getFunctionRemovexattr(statcont
)(statcont
, fname
, name
);
484 smbc_lremovexattr(const char *fname
,
487 return smbc_getFunctionRemovexattr(statcont
)(statcont
, fname
, name
);
491 smbc_fremovexattr(int fd
,
494 SMBCFILE
* file
= find_fd(fd
);
499 return smbc_getFunctionRemovexattr(statcont
)(statcont
,
504 smbc_listxattr(const char *fname
,
508 return smbc_getFunctionListxattr(statcont
)(statcont
,
513 smbc_llistxattr(const char *fname
,
517 return smbc_getFunctionListxattr(statcont
)(statcont
,
522 smbc_flistxattr(int fd
,
526 SMBCFILE
* file
= find_fd(fd
);
531 return smbc_getFunctionListxattr(statcont
)(statcont
,
532 file
->fname
, list
, size
);
536 smbc_print_file(const char *fname
,
539 return smbc_getFunctionPrintFile(statcont
)(statcont
, fname
,
544 smbc_open_print_job(const char *fname
)
548 file
= smbc_getFunctionOpenPrintJob(statcont
)(statcont
, fname
);
549 if (!file
) return -1;
554 smbc_list_print_jobs(const char *purl
,
555 smbc_list_print_job_fn fn
)
557 return smbc_getFunctionListPrintJobs(statcont
)(statcont
, purl
, fn
);
561 smbc_unlink_print_job(const char *purl
,
564 return smbc_getFunctionUnlinkPrintJob(statcont
)(statcont
, purl
, id
);