Add define guards around FSTRING_LEN.
[Samba/vl.git] / source3 / libsmb / libsmb_compat.c
blob573d087d6e30c4de94e9ebe2272f3f67f725683d
1 /*
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/>.
25 #include "includes.h"
27 #include "include/libsmb_internal.h"
29 struct smbc_compat_fdlist {
30 SMBCFILE * file;
31 int fd;
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;
45 while (f) {
46 if (f->fd == fd)
47 return f->file;
48 f = f->next;
50 return NULL;
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;
58 if (f) {
59 /* We found one that's available */
60 DLIST_REMOVE(smbc_compat_fd_avail, f);
62 } else {
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
68 * be returned.
70 if (smbc_compat_nextfd >= FD_SETSIZE) {
71 errno = EMFILE;
72 return -1;
75 f = SMB_MALLOC_P(struct smbc_compat_fdlist);
76 if (!f) {
77 errno = ENOMEM;
78 return -1;
81 f->fd = SMBC_BASE_FD + smbc_compat_nextfd++;
84 f->file = file;
85 DLIST_ADD(smbc_compat_fd_in_use, f);
87 return f->fd;
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;
97 while (f) {
98 if (f->fd == fd)
99 break;
100 f = f->next;
103 if (f) {
104 /* found */
105 DLIST_REMOVE(smbc_compat_fd_in_use, f);
106 f->file = NULL;
107 DLIST_ADD(smbc_compat_fd_avail, f);
108 return 0;
110 return 1;
115 int smbc_init(smbc_get_auth_data_fn fn, int debug)
117 if (!smbc_compat_initialized) {
118 statcont = smbc_new_context();
119 if (!statcont)
120 return -1;
122 statcont->debug = debug;
123 statcont->callbacks.auth_fn = fn;
125 if (!smbc_init_context(statcont)) {
126 smbc_free_context(statcont, False);
127 return -1;
130 smbc_compat_initialized = 1;
132 return 0;
134 return 0;
138 SMBCCTX *smbc_set_context(SMBCCTX * context)
140 SMBCCTX *old_context = statcont;
142 if (context) {
143 /* Save provided context. It must have been initialized! */
144 statcont = context;
146 /* You'd better know what you're doing. We won't help you. */
147 smbc_compat_initialized = 1;
150 return old_context;
154 int smbc_open(const char *furl, int flags, mode_t mode)
156 SMBCFILE * file;
157 int fd;
159 file = (statcont->open)(statcont, furl, flags, mode);
160 if (!file)
161 return -1;
163 fd = add_fd(file);
164 if (fd == -1)
165 (statcont->close_fn)(statcont, file);
166 return fd;
170 int smbc_creat(const char *furl, mode_t mode)
172 SMBCFILE * file;
173 int fd;
175 file = (statcont->creat)(statcont, furl, mode);
176 if (!file)
177 return -1;
179 fd = add_fd(file);
180 if (fd == -1) {
181 /* Hmm... should we delete the file too ? I guess we could try */
182 (statcont->close_fn)(statcont, file);
183 (statcont->unlink)(statcont, furl);
185 return fd;
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);
210 del_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)
226 SMBCFILE * file;
227 int fd;
229 file = (statcont->opendir)(statcont, durl);
230 if (!file)
231 return -1;
233 fd = add_fd(file);
234 if (fd == -1)
235 (statcont->closedir)(statcont, file);
237 return fd;
240 int smbc_closedir(int dh)
242 SMBCFILE * file = find_fd(dh);
243 del_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);
302 #ifdef HAVE_UTIME_H
303 int smbc_utime(const char *fname, struct utimbuf *utbuf)
305 struct timeval tv[2];
307 if (utbuf == NULL)
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);
316 #endif
318 int smbc_setxattr(const char *fname,
319 const char *name,
320 const void *value,
321 size_t size,
322 int flags)
324 return (statcont->setxattr)(statcont, fname, name, value, size, flags);
327 int smbc_lsetxattr(const char *fname,
328 const char *name,
329 const void *value,
330 size_t size,
331 int flags)
333 return (statcont->setxattr)(statcont, fname, name, value, size, flags);
336 int smbc_fsetxattr(int fd,
337 const char *name,
338 const void *value,
339 size_t size,
340 int flags)
342 SMBCFILE * file = find_fd(fd);
343 if (file == NULL) {
344 errno = EBADF;
345 return -1;
347 return (statcont->setxattr)(statcont, file->fname,
348 name, value, size, flags);
351 int smbc_getxattr(const char *fname,
352 const char *name,
353 const void *value,
354 size_t size)
356 return (statcont->getxattr)(statcont, fname, name, value, size);
359 int smbc_lgetxattr(const char *fname,
360 const char *name,
361 const void *value,
362 size_t size)
364 return (statcont->getxattr)(statcont, fname, name, value, size);
367 int smbc_fgetxattr(int fd,
368 const char *name,
369 const void *value,
370 size_t size)
372 SMBCFILE * file = find_fd(fd);
373 if (file == NULL) {
374 errno = EBADF;
375 return -1;
377 return (statcont->getxattr)(statcont, file->fname, name, value, size);
380 int smbc_removexattr(const char *fname,
381 const char *name)
383 return (statcont->removexattr)(statcont, fname, name);
386 int smbc_lremovexattr(const char *fname,
387 const char *name)
389 return (statcont->removexattr)(statcont, fname, name);
392 int smbc_fremovexattr(int fd,
393 const char *name)
395 SMBCFILE * file = find_fd(fd);
396 if (file == NULL) {
397 errno = EBADF;
398 return -1;
400 return (statcont->removexattr)(statcont, file->fname, name);
403 int smbc_listxattr(const char *fname,
404 char *list,
405 size_t size)
407 return (statcont->listxattr)(statcont, fname, list, size);
410 int smbc_llistxattr(const char *fname,
411 char *list,
412 size_t size)
414 return (statcont->listxattr)(statcont, fname, list, size);
417 int smbc_flistxattr(int fd,
418 char *list,
419 size_t size)
421 SMBCFILE * file = find_fd(fd);
422 if (file == NULL) {
423 errno = EBADF;
424 return -1;
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;
438 return file->cli_fd;
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);