source: trunk-3.0/source/libsmb/libsmb_compat.c@ 102

Last change on this file since 102 was 71, checked in by Paul Smedley, 18 years ago

Update source to 3.0.26a

File size: 10.7 KB
Line 
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
9
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 2 of the License, or
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25
26#include "includes.h"
27
28#include "include/libsmb_internal.h"
29
30struct smbc_compat_fdlist {
31 SMBCFILE * file;
32 int fd;
33 struct smbc_compat_fdlist *next, *prev;
34};
35
36static SMBCCTX * statcont = NULL;
37static int smbc_compat_initialized = 0;
38static int smbc_compat_nextfd = 0;
39static struct smbc_compat_fdlist * smbc_compat_fd_in_use = NULL;
40static struct smbc_compat_fdlist * smbc_compat_fd_avail = NULL;
41
42/* Find an fd and return the SMBCFILE * or NULL on failure */
43static SMBCFILE * find_fd(int fd)
44{
45 struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
46 while (f) {
47 if (f->fd == fd)
48 return f->file;
49 f = f->next;
50 }
51 return NULL;
52}
53
54/* Add an fd, returns 0 on success, -1 on error with errno set */
55static int add_fd(SMBCFILE * file)
56{
57 struct smbc_compat_fdlist * f = smbc_compat_fd_avail;
58
59 if (f) {
60 /* We found one that's available */
61 DLIST_REMOVE(smbc_compat_fd_avail, f);
62
63 } else {
64 /*
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
69 * be returned.
70 */
71 if (smbc_compat_nextfd >= FD_SETSIZE) {
72 errno = EMFILE;
73 return -1;
74 }
75
76 f = SMB_MALLOC_P(struct smbc_compat_fdlist);
77 if (!f) {
78 errno = ENOMEM;
79 return -1;
80 }
81
82 f->fd = SMBC_BASE_FD + smbc_compat_nextfd++;
83 }
84
85 f->file = file;
86 DLIST_ADD(smbc_compat_fd_in_use, f);
87
88 return f->fd;
89}
90
91
92
93/* Delete an fd, returns 0 on success */
94static int del_fd(int fd)
95{
96 struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
97
98 while (f) {
99 if (f->fd == fd)
100 break;
101 f = f->next;
102 }
103
104 if (f) {
105 /* found */
106 DLIST_REMOVE(smbc_compat_fd_in_use, f);
107 f->file = NULL;
108 DLIST_ADD(smbc_compat_fd_avail, f);
109 return 0;
110 }
111 return 1;
112}
113
114
115
116int smbc_init(smbc_get_auth_data_fn fn, int debug)
117{
118 if (!smbc_compat_initialized) {
119 statcont = smbc_new_context();
120 if (!statcont)
121 return -1;
122
123 statcont->debug = debug;
124 statcont->callbacks.auth_fn = fn;
125
126 if (!smbc_init_context(statcont)) {
127 smbc_free_context(statcont, False);
128 return -1;
129 }
130
131 smbc_compat_initialized = 1;
132
133 return 0;
134 }
135 return 0;
136}
137
138
139SMBCCTX *smbc_set_context(SMBCCTX * context)
140{
141 SMBCCTX *old_context = statcont;
142
143 if (context) {
144 /* Save provided context. It must have been initialized! */
145 statcont = context;
146
147 /* You'd better know what you're doing. We won't help you. */
148 smbc_compat_initialized = 1;
149 }
150
151 return old_context;
152}
153
154
155int smbc_open(const char *furl, int flags, mode_t mode)
156{
157 SMBCFILE * file;
158 int fd;
159
160 file = (statcont->open)(statcont, furl, flags, mode);
161 if (!file)
162 return -1;
163
164 fd = add_fd(file);
165 if (fd == -1)
166 (statcont->close_fn)(statcont, file);
167 return fd;
168}
169
170
171int smbc_creat(const char *furl, mode_t mode)
172{
173 SMBCFILE * file;
174 int fd;
175
176 file = (statcont->creat)(statcont, furl, mode);
177 if (!file)
178 return -1;
179
180 fd = add_fd(file);
181 if (fd == -1) {
182 /* Hmm... should we delete the file too ? I guess we could try */
183 (statcont->close_fn)(statcont, file);
184 (statcont->unlink)(statcont, furl);
185 }
186 return fd;
187}
188
189
190ssize_t smbc_read(int fd, void *buf, size_t bufsize)
191{
192 SMBCFILE * file = find_fd(fd);
193 return (statcont->read)(statcont, file, buf, bufsize);
194}
195
196ssize_t smbc_write(int fd, void *buf, size_t bufsize)
197{
198 SMBCFILE * file = find_fd(fd);
199 return (statcont->write)(statcont, file, buf, bufsize);
200}
201
202off_t smbc_lseek(int fd, off_t offset, int whence)
203{
204 SMBCFILE * file = find_fd(fd);
205 return (statcont->lseek)(statcont, file, offset, whence);
206}
207
208int smbc_close(int fd)
209{
210 SMBCFILE * file = find_fd(fd);
211 del_fd(fd);
212 return (statcont->close_fn)(statcont, file);
213}
214
215int smbc_unlink(const char *fname)
216{
217 return (statcont->unlink)(statcont, fname);
218}
219
220int smbc_rename(const char *ourl, const char *nurl)
221{
222 return (statcont->rename)(statcont, ourl, statcont, nurl);
223}
224
225int smbc_opendir(const char *durl)
226{
227 SMBCFILE * file;
228 int fd;
229
230 file = (statcont->opendir)(statcont, durl);
231 if (!file)
232 return -1;
233
234 fd = add_fd(file);
235 if (fd == -1)
236 (statcont->closedir)(statcont, file);
237
238 return fd;
239}
240
241int smbc_closedir(int dh)
242{
243 SMBCFILE * file = find_fd(dh);
244 del_fd(dh);
245 return (statcont->closedir)(statcont, file);
246}
247
248int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
249{
250 SMBCFILE * file = find_fd(dh);
251 return (statcont->getdents)(statcont, file,dirp, count);
252}
253
254struct smbc_dirent* smbc_readdir(unsigned int dh)
255{
256 SMBCFILE * file = find_fd(dh);
257 return (statcont->readdir)(statcont, file);
258}
259
260off_t smbc_telldir(int dh)
261{
262 SMBCFILE * file = find_fd(dh);
263 return (statcont->telldir)(statcont, file);
264}
265
266int smbc_lseekdir(int fd, off_t offset)
267{
268 SMBCFILE * file = find_fd(fd);
269 return (statcont->lseekdir)(statcont, file, offset);
270}
271
272int smbc_mkdir(const char *durl, mode_t mode)
273{
274 return (statcont->mkdir)(statcont, durl, mode);
275}
276
277int smbc_rmdir(const char *durl)
278{
279 return (statcont->rmdir)(statcont, durl);
280}
281
282int smbc_stat(const char *url, struct stat *st)
283{
284 return (statcont->stat)(statcont, url, st);
285}
286
287int smbc_fstat(int fd, struct stat *st)
288{
289 SMBCFILE * file = find_fd(fd);
290 return (statcont->fstat)(statcont, file, st);
291}
292
293int smbc_chmod(const char *url, mode_t mode)
294{
295 return (statcont->chmod)(statcont, url, mode);
296}
297
298int smbc_utimes(const char *fname, struct timeval *tbuf)
299{
300 return (statcont->utimes)(statcont, fname, tbuf);
301}
302
303#ifdef HAVE_UTIME_H
304int smbc_utime(const char *fname, struct utimbuf *utbuf)
305{
306 struct timeval tv[2];
307
308 if (utbuf == NULL)
309 return (statcont->utimes)(statcont, fname, NULL);
310
311 tv[0].tv_sec = utbuf->actime;
312 tv[1].tv_sec = utbuf->modtime;
313 tv[0].tv_usec = tv[1].tv_usec = 0;
314
315 return (statcont->utimes)(statcont, fname, tv);
316}
317#endif
318
319int smbc_setxattr(const char *fname,
320 const char *name,
321 const void *value,
322 size_t size,
323 int flags)
324{
325 return (statcont->setxattr)(statcont, fname, name, value, size, flags);
326}
327
328int smbc_lsetxattr(const char *fname,
329 const char *name,
330 const void *value,
331 size_t size,
332 int flags)
333{
334 return (statcont->setxattr)(statcont, fname, name, value, size, flags);
335}
336
337int smbc_fsetxattr(int fd,
338 const char *name,
339 const void *value,
340 size_t size,
341 int flags)
342{
343 SMBCFILE * file = find_fd(fd);
344 if (file == NULL) {
345 errno = EBADF;
346 return -1;
347 }
348 return (statcont->setxattr)(statcont, file->fname,
349 name, value, size, flags);
350}
351
352int smbc_getxattr(const char *fname,
353 const char *name,
354 const void *value,
355 size_t size)
356{
357 return (statcont->getxattr)(statcont, fname, name, value, size);
358}
359
360int smbc_lgetxattr(const char *fname,
361 const char *name,
362 const void *value,
363 size_t size)
364{
365 return (statcont->getxattr)(statcont, fname, name, value, size);
366}
367
368int smbc_fgetxattr(int fd,
369 const char *name,
370 const void *value,
371 size_t size)
372{
373 SMBCFILE * file = find_fd(fd);
374 if (file == NULL) {
375 errno = EBADF;
376 return -1;
377 }
378 return (statcont->getxattr)(statcont, file->fname, name, value, size);
379}
380
381int smbc_removexattr(const char *fname,
382 const char *name)
383{
384 return (statcont->removexattr)(statcont, fname, name);
385}
386
387int smbc_lremovexattr(const char *fname,
388 const char *name)
389{
390 return (statcont->removexattr)(statcont, fname, name);
391}
392
393int smbc_fremovexattr(int fd,
394 const char *name)
395{
396 SMBCFILE * file = find_fd(fd);
397 if (file == NULL) {
398 errno = EBADF;
399 return -1;
400 }
401 return (statcont->removexattr)(statcont, file->fname, name);
402}
403
404int smbc_listxattr(const char *fname,
405 char *list,
406 size_t size)
407{
408 return (statcont->listxattr)(statcont, fname, list, size);
409}
410
411int smbc_llistxattr(const char *fname,
412 char *list,
413 size_t size)
414{
415 return (statcont->listxattr)(statcont, fname, list, size);
416}
417
418int smbc_flistxattr(int fd,
419 char *list,
420 size_t size)
421{
422 SMBCFILE * file = find_fd(fd);
423 if (file == NULL) {
424 errno = EBADF;
425 return -1;
426 }
427 return (statcont->listxattr)(statcont, file->fname, list, size);
428}
429
430int smbc_print_file(const char *fname, const char *printq)
431{
432 return (statcont->print_file)(statcont, fname, statcont, printq);
433}
434
435int smbc_open_print_job(const char *fname)
436{
437 SMBCFILE * file = (statcont->open_print_job)(statcont, fname);
438 if (!file) return -1;
439 return file->cli_fd;
440}
441
442int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
443{
444 return (statcont->list_print_jobs)(statcont, purl, fn);
445}
446
447int smbc_unlink_print_job(const char *purl, int id)
448{
449 return (statcont->unlink_print_job)(statcont, purl, id);
450}
451
452
Note: See TracBrowser for help on using the repository browser.