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
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/>.
27 #include "include/libsmb_internal.h"
29 struct smbc_compat_fdlist {
32 struct smbc_compat_fdlist *next, *prev;
35 static SMBCCTX * statcont = NULL;
36 static int smbc_compat_initialized = 0;
37 static int smbc_compat_nextfd = 0;
38 static struct smbc_compat_fdlist * smbc_compat_fd_in_use = NULL;
39 static struct smbc_compat_fdlist * smbc_compat_fd_avail = NULL;
41 /* Find an fd and return the SMBCFILE * or NULL on failure */
42 static SMBCFILE * find_fd(int fd)
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 */
54 static int add_fd(SMBCFILE * file)
56 struct smbc_compat_fdlist * f = smbc_compat_fd_avail;
59 /* We found one that's available */
60 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 */
93 static int del_fd(int fd)
95 struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
105 DLIST_REMOVE(smbc_compat_fd_in_use, f);
107 DLIST_ADD(smbc_compat_fd_avail, f);
115 int smbc_init(smbc_get_auth_data_fn fn, int debug)
117 if (!smbc_compat_initialized) {
118 statcont = smbc_new_context();
122 statcont->debug = debug;
123 statcont->callbacks.auth_fn = fn;
125 if (!smbc_init_context(statcont)) {
126 smbc_free_context(statcont, False);
130 smbc_compat_initialized = 1;
138 SMBCCTX *smbc_set_context(SMBCCTX * context)
140 SMBCCTX *old_context = statcont;
143 /* Save provided context. It must have been initialized! */
146 /* You'd better know what you're doing. We won't help you. */
147 smbc_compat_initialized = 1;
154 int smbc_open(const char *furl, int flags, mode_t mode)
159 file = statcont->open(statcont, furl, flags, mode);
165 statcont->close_fn(statcont, file);
170 int smbc_creat(const char *furl, mode_t mode)
175 file = statcont->creat(statcont, furl, mode);
181 /* Hmm... should we delete the file too ? I guess we could try */
182 statcont->close_fn(statcont, file);
183 statcont->unlink(statcont, furl);
189 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
191 SMBCFILE * file = find_fd(fd);
192 return statcont->read(statcont, file, buf, bufsize);
195 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
197 SMBCFILE * file = find_fd(fd);
198 return statcont->write(statcont, file, buf, bufsize);
201 off_t smbc_lseek(int fd, off_t offset, int whence)
203 SMBCFILE * file = find_fd(fd);
204 return statcont->lseek(statcont, file, offset, whence);
207 int smbc_close(int fd)
209 SMBCFILE * file = find_fd(fd);
211 return statcont->close_fn(statcont, file);
214 int smbc_unlink(const char *fname)
216 return statcont->unlink(statcont, fname);
219 int smbc_rename(const char *ourl, const char *nurl)
221 return statcont->rename(statcont, ourl, statcont, nurl);
224 int smbc_opendir(const char *durl)
229 file = statcont->opendir(statcont, durl);
235 statcont->closedir(statcont, file);
240 int smbc_closedir(int dh)
242 SMBCFILE * file = find_fd(dh);
244 return statcont->closedir(statcont, file);
247 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
249 SMBCFILE * file = find_fd(dh);
250 return statcont->getdents(statcont, file,dirp, count);
253 struct smbc_dirent* smbc_readdir(unsigned int dh)
255 SMBCFILE * file = find_fd(dh);
256 return statcont->readdir(statcont, file);
259 off_t smbc_telldir(int dh)
261 SMBCFILE * file = find_fd(dh);
262 return statcont->telldir(statcont, file);
265 int smbc_lseekdir(int fd, off_t offset)
267 SMBCFILE * file = find_fd(fd);
268 return statcont->lseekdir(statcont, file, offset);
271 int smbc_mkdir(const char *durl, mode_t mode)
273 return statcont->mkdir(statcont, durl, mode);
276 int smbc_rmdir(const char *durl)
278 return statcont->rmdir(statcont, durl);
281 int smbc_stat(const char *url, struct stat *st)
283 return statcont->stat(statcont, url, st);
286 int smbc_fstat(int fd, struct stat *st)
288 SMBCFILE * file = find_fd(fd);
289 return statcont->fstat(statcont, file, st);
292 int smbc_chmod(const char *url, mode_t mode)
294 return statcont->chmod(statcont, url, mode);
297 int smbc_utimes(const char *fname, struct timeval *tbuf)
299 return statcont->utimes(statcont, fname, tbuf);
303 int smbc_utime(const char *fname, struct utimbuf *utbuf)
305 struct timeval tv[2];
308 return statcont->utimes(statcont, fname, NULL);
310 tv[0].tv_sec = utbuf->actime;
311 tv[1].tv_sec = utbuf->modtime;
312 tv[0].tv_usec = tv[1].tv_usec = 0;
314 return statcont->utimes(statcont, fname, tv);
318 int smbc_setxattr(const char *fname,
324 return statcont->setxattr(statcont, fname, name, value, size, flags);
327 int smbc_lsetxattr(const char *fname,
333 return statcont->setxattr(statcont, fname, name, value, size, flags);
336 int smbc_fsetxattr(int fd,
342 SMBCFILE * file = find_fd(fd);
347 return statcont->setxattr(statcont, file->fname,
348 name, value, size, flags);
351 int smbc_getxattr(const char *fname,
356 return statcont->getxattr(statcont, fname, name, value, size);
359 int smbc_lgetxattr(const char *fname,
364 return statcont->getxattr(statcont, fname, name, value, size);
367 int smbc_fgetxattr(int fd,
372 SMBCFILE * file = find_fd(fd);
377 return statcont->getxattr(statcont, file->fname, name, value, size);
380 int smbc_removexattr(const char *fname,
383 return statcont->removexattr(statcont, fname, name);
386 int smbc_lremovexattr(const char *fname,
389 return statcont->removexattr(statcont, fname, name);
392 int smbc_fremovexattr(int fd,
395 SMBCFILE * file = find_fd(fd);
400 return statcont->removexattr(statcont, file->fname, name);
403 int smbc_listxattr(const char *fname,
407 return statcont->listxattr(statcont, fname, list, size);
410 int smbc_llistxattr(const char *fname,
414 return statcont->listxattr(statcont, fname, list, size);
417 int smbc_flistxattr(int fd,
421 SMBCFILE * file = find_fd(fd);
426 return statcont->listxattr(statcont, file->fname, list, size);
429 int smbc_print_file(const char *fname, const char *printq)
431 return statcont->print_file(statcont, fname, statcont, printq);
434 int smbc_open_print_job(const char *fname)
436 SMBCFILE * file = statcont->open_print_job(statcont, fname);
437 if (!file) return -1;
441 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
443 return statcont->list_print_jobs(statcont, purl, fn);
446 int smbc_unlink_print_job(const char *purl, int id)
448 return statcont->unlink_print_job(statcont, purl, id);