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