r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[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_chmod(const char *url, mode_t mode)
293 {
294         return statcont->chmod(statcont, url, mode);
295 }
296
297 int smbc_utimes(const char *fname, struct timeval *tbuf)
298 {
299         return statcont->utimes(statcont, fname, tbuf);
300 }
301
302 #ifdef HAVE_UTIME_H
303 int smbc_utime(const char *fname, struct utimbuf *utbuf)
304 {
305         struct timeval tv[2];
306
307         if (utbuf == NULL)
308                 return statcont->utimes(statcont, fname, NULL);
309
310         tv[0].tv_sec = utbuf->actime;
311         tv[1].tv_sec = utbuf->modtime;
312         tv[0].tv_usec = tv[1].tv_usec = 0;
313
314         return statcont->utimes(statcont, fname, tv);
315 }
316 #endif
317
318 int smbc_setxattr(const char *fname,
319                   const char *name,
320                   const void *value,
321                   size_t size,
322                   int flags)
323 {
324         return statcont->setxattr(statcont, fname, name, value, size, flags);
325 }
326
327 int smbc_lsetxattr(const char *fname,
328                    const char *name,
329                    const void *value,
330                    size_t size,
331                    int flags)
332 {
333         return statcont->setxattr(statcont, fname, name, value, size, flags);
334 }
335
336 int smbc_fsetxattr(int fd,
337                    const char *name,
338                    const void *value,
339                    size_t size,
340                    int flags)
341 {
342         SMBCFILE * file = find_fd(fd);
343         if (file == NULL) {
344                 errno = EBADF;
345                 return -1;
346         }
347         return statcont->setxattr(statcont, file->fname,
348                                   name, value, size, flags);
349 }
350
351 int smbc_getxattr(const char *fname,
352                   const char *name,
353                   const void *value,
354                   size_t size)
355 {
356         return statcont->getxattr(statcont, fname, name, value, size);
357 }
358
359 int smbc_lgetxattr(const char *fname,
360                    const char *name,
361                    const void *value,
362                    size_t size)
363 {
364         return statcont->getxattr(statcont, fname, name, value, size);
365 }
366
367 int smbc_fgetxattr(int fd,
368                    const char *name,
369                    const void *value,
370                    size_t size)
371 {
372         SMBCFILE * file = find_fd(fd);
373         if (file == NULL) {
374                 errno = EBADF;
375                 return -1;
376         }
377         return statcont->getxattr(statcont, file->fname, name, value, size);
378 }
379
380 int smbc_removexattr(const char *fname,
381                      const char *name)
382 {
383         return statcont->removexattr(statcont, fname, name);
384 }
385
386 int smbc_lremovexattr(const char *fname,
387                       const char *name)
388 {
389         return statcont->removexattr(statcont, fname, name);
390 }
391
392 int smbc_fremovexattr(int fd,
393                       const char *name)
394 {
395         SMBCFILE * file = find_fd(fd);
396         if (file == NULL) {
397                 errno = EBADF;
398                 return -1;
399         }
400         return statcont->removexattr(statcont, file->fname, name);
401 }
402
403 int smbc_listxattr(const char *fname,
404                    char *list,
405                    size_t size)
406 {
407         return statcont->listxattr(statcont, fname, list, size);
408 }
409
410 int smbc_llistxattr(const char *fname,
411                     char *list,
412                     size_t size)
413 {
414         return statcont->listxattr(statcont, fname, list, size);
415 }
416
417 int smbc_flistxattr(int fd,
418                     char *list,
419                     size_t size)
420 {
421         SMBCFILE * file = find_fd(fd);
422         if (file == NULL) {
423                 errno = EBADF;
424                 return -1;
425         }
426         return statcont->listxattr(statcont, file->fname, list, size);
427 }
428
429 int smbc_print_file(const char *fname, const char *printq)
430 {
431         return statcont->print_file(statcont, fname, statcont, printq);
432 }
433
434 int smbc_open_print_job(const char *fname)
435 {
436         SMBCFILE * file = statcont->open_print_job(statcont, fname);
437         if (!file) return -1;
438         return file->cli_fd;
439 }
440
441 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
442 {
443         return statcont->list_print_jobs(statcont, purl, fn);
444 }
445
446 int smbc_unlink_print_job(const char *purl, int id)
447 {
448         return statcont->unlink_print_job(statcont, purl, id);
449 }
450
451