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
);
65 * None were available, so allocate one. Keep the number of
66 * file descriptors determinate. This allows the application
67 * to allocate bitmaps or mapping of file descriptors based on
68 * a known maximum number of file descriptors that will ever
71 if (smbc_compat_nextfd
>= FD_SETSIZE
) {
76 f
= SMB_MALLOC_P(struct smbc_compat_fdlist
);
82 f
->fd
= SMBC_BASE_FD
+ smbc_compat_nextfd
++;
86 DLIST_ADD(smbc_compat_fd_in_use
, f
);
93 /* Delete an fd, returns 0 on success */
97 struct smbc_compat_fdlist
* f
= smbc_compat_fd_in_use
;
107 DLIST_REMOVE(smbc_compat_fd_in_use
, f
);
109 DLIST_ADD(smbc_compat_fd_avail
, f
);
118 smbc_init(smbc_get_auth_data_fn fn
,
121 if (!smbc_compat_initialized
) {
122 statcont
= smbc_new_context();
126 smbc_setDebug(statcont
, debug
);
127 smbc_setFunctionAuthData(statcont
, fn
);
129 if (!smbc_init_context(statcont
)) {
130 smbc_free_context(statcont
, False
);
134 smbc_compat_initialized
= 1;
143 smbc_set_context(SMBCCTX
* context
)
145 SMBCCTX
*old_context
= statcont
;
148 /* Save provided context. It must have been initialized! */
151 /* You'd better know what you're doing. We won't help you. */
152 smbc_compat_initialized
= 1;
160 smbc_open(const char *furl
,
167 file
= smbc_getFunctionOpen(statcont
)(statcont
, furl
, flags
, mode
);
173 smbc_getFunctionClose(statcont
)(statcont
, file
);
179 smbc_creat(const char *furl
,
185 file
= smbc_getFunctionCreat(statcont
)(statcont
, furl
, mode
);
191 /* Hmm... should we delete the file too ? I guess we could try */
192 smbc_getFunctionClose(statcont
)(statcont
, file
);
193 smbc_getFunctionUnlink(statcont
)(statcont
, furl
);
204 SMBCFILE
* file
= find_fd(fd
);
205 return smbc_getFunctionRead(statcont
)(statcont
, file
, buf
, bufsize
);
213 SMBCFILE
* file
= find_fd(fd
);
214 return smbc_getFunctionWrite(statcont
)(statcont
, file
, buf
, bufsize
);
222 SMBCFILE
* file
= find_fd(fd
);
223 return smbc_getFunctionLseek(statcont
)(statcont
, file
, offset
, whence
);
229 SMBCFILE
* file
= find_fd(fd
);
231 return smbc_getFunctionClose(statcont
)(statcont
, file
);
235 smbc_unlink(const char *fname
)
237 return smbc_getFunctionUnlink(statcont
)(statcont
, fname
);
241 smbc_rename(const char *ourl
,
244 return smbc_getFunctionRename(statcont
)(statcont
, ourl
,
249 smbc_opendir(const char *durl
)
254 file
= smbc_getFunctionOpendir(statcont
)(statcont
, durl
);
260 smbc_getFunctionClosedir(statcont
)(statcont
, file
);
266 smbc_closedir(int dh
)
268 SMBCFILE
* file
= find_fd(dh
);
270 return smbc_getFunctionClosedir(statcont
)(statcont
, file
);
274 smbc_getdents(unsigned int dh
,
275 struct smbc_dirent
*dirp
,
278 SMBCFILE
* file
= find_fd(dh
);
279 return smbc_getFunctionGetdents(statcont
)(statcont
, file
, dirp
, count
);
283 smbc_readdir(unsigned int dh
)
285 SMBCFILE
* file
= find_fd(dh
);
286 return smbc_getFunctionReaddir(statcont
)(statcont
, file
);
292 SMBCFILE
* file
= find_fd(dh
);
293 return smbc_getFunctionTelldir(statcont
)(statcont
, file
);
297 smbc_lseekdir(int fd
,
300 SMBCFILE
* file
= find_fd(fd
);
301 return smbc_getFunctionLseekdir(statcont
)(statcont
, file
, offset
);
305 smbc_mkdir(const char *durl
,
308 return smbc_getFunctionMkdir(statcont
)(statcont
, durl
, mode
);
312 smbc_rmdir(const char *durl
)
314 return smbc_getFunctionRmdir(statcont
)(statcont
, durl
);
318 smbc_stat(const char *url
,
321 return smbc_getFunctionStat(statcont
)(statcont
, url
, st
);
328 SMBCFILE
* file
= find_fd(fd
);
329 return smbc_getFunctionFstat(statcont
)(statcont
, file
, st
);
333 smbc_statvfs(char *path
,
336 return smbc_getFunctionStatVFS(statcont
)(statcont
, path
, st
);
340 smbc_fstatvfs(int fd
,
343 SMBCFILE
* file
= find_fd(fd
);
344 return smbc_getFunctionFstatVFS(statcont
)(statcont
, file
, st
);
348 smbc_ftruncate(int fd
,
351 SMBCFILE
* file
= find_fd(fd
);
352 return smbc_getFunctionFtruncate(statcont
)(statcont
, file
, size
);
356 smbc_chmod(const char *url
,
359 return smbc_getFunctionChmod(statcont
)(statcont
, url
, mode
);
363 smbc_utimes(const char *fname
,
364 struct timeval
*tbuf
)
366 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, tbuf
);
371 smbc_utime(const char *fname
,
372 struct utimbuf
*utbuf
)
374 struct timeval tv
[2];
377 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, NULL
);
379 tv
[0].tv_sec
= utbuf
->actime
;
380 tv
[1].tv_sec
= utbuf
->modtime
;
381 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
383 return smbc_getFunctionUtimes(statcont
)(statcont
, fname
, tv
);
388 smbc_setxattr(const char *fname
,
394 return smbc_getFunctionSetxattr(statcont
)(statcont
,
400 smbc_lsetxattr(const char *fname
,
406 return smbc_getFunctionSetxattr(statcont
)(statcont
,
412 smbc_fsetxattr(int fd
,
418 SMBCFILE
* file
= find_fd(fd
);
423 return smbc_getFunctionSetxattr(statcont
)(statcont
,
429 smbc_getxattr(const char *fname
,
434 return smbc_getFunctionGetxattr(statcont
)(statcont
,
440 smbc_lgetxattr(const char *fname
,
445 return smbc_getFunctionGetxattr(statcont
)(statcont
,
451 smbc_fgetxattr(int fd
,
456 SMBCFILE
* file
= find_fd(fd
);
461 return smbc_getFunctionGetxattr(statcont
)(statcont
,
467 smbc_removexattr(const char *fname
,
470 return smbc_getFunctionRemovexattr(statcont
)(statcont
, fname
, name
);
474 smbc_lremovexattr(const char *fname
,
477 return smbc_getFunctionRemovexattr(statcont
)(statcont
, fname
, name
);
481 smbc_fremovexattr(int fd
,
484 SMBCFILE
* file
= find_fd(fd
);
489 return smbc_getFunctionRemovexattr(statcont
)(statcont
,
494 smbc_listxattr(const char *fname
,
498 return smbc_getFunctionListxattr(statcont
)(statcont
,
503 smbc_llistxattr(const char *fname
,
507 return smbc_getFunctionListxattr(statcont
)(statcont
,
512 smbc_flistxattr(int fd
,
516 SMBCFILE
* file
= find_fd(fd
);
521 return smbc_getFunctionListxattr(statcont
)(statcont
,
522 file
->fname
, list
, size
);
526 smbc_print_file(const char *fname
,
529 return smbc_getFunctionPrintFile(statcont
)(statcont
, fname
,
534 smbc_open_print_job(const char *fname
)
538 file
= smbc_getFunctionOpenPrintJob(statcont
)(statcont
, fname
);
539 if (!file
) return -1;
544 smbc_list_print_jobs(const char *purl
,
545 smbc_list_print_job_fn fn
)
547 return smbc_getFunctionListPrintJobs(statcont
)(statcont
, purl
, fn
);
551 smbc_unlink_print_job(const char *purl
,
554 return smbc_getFunctionUnlinkPrintJob(statcont
)(statcont
, purl
, id
);