This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.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    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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_currentfd = 10000;
38 static struct smbc_compat_fdlist * smbc_compat_fdlist = NULL;
39
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_fdlist;
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 = malloc(sizeof(struct smbc_compat_fdlist));
57         if (!f) {
58                 errno = ENOMEM;
59                 return -1;
60         }
61         
62         f->fd = smbc_currentfd++;
63         f->file = file;
64         
65         DLIST_ADD(smbc_compat_fdlist, f);
66
67         return f->fd;
68 }
69
70
71
72 /* Delete an fd, returns 0 on success */
73 static int del_fd(int fd)
74 {
75         struct smbc_compat_fdlist * f = smbc_compat_fdlist;
76         while (f) {
77                 if (f->fd == fd) 
78                         break;
79                 f = f->next;
80         }
81         if (f) {
82                 /* found */
83                 DLIST_REMOVE(smbc_compat_fdlist, f);
84                 SAFE_FREE(f);
85                 return 0;
86         }
87         return 1;
88 }
89  
90
91
92 int smbc_init(smbc_get_auth_data_fn fn, int debug)
93 {
94         if (!smbc_compat_initialized) {
95                 statcont = smbc_new_context();
96                 if (!statcont) 
97                         return -1;
98
99                 statcont->debug = debug;
100                 statcont->callbacks.auth_fn = fn;
101                 
102                 if (!smbc_init_context(statcont)) {
103                         smbc_free_context(statcont, False);
104                         return -1;
105                 }
106
107                 smbc_compat_initialized = 1;
108
109                 return 0;
110         }
111         return 0;
112 }
113
114
115 int smbc_open(const char *furl, int flags, mode_t mode)
116 {
117         SMBCFILE * file;
118         int fd;
119
120         file = statcont->open(statcont, furl, flags, mode);
121         if (!file)
122                 return -1;
123
124         fd = add_fd(file);
125         if (fd == -1) 
126                 statcont->close(statcont, file);
127         return fd;
128 }
129
130
131 int smbc_creat(const char *furl, mode_t mode)
132 {
133         SMBCFILE * file;
134         int fd;
135
136         file = statcont->creat(statcont, furl, mode);
137         if (!file)
138                 return -1;
139
140         fd = add_fd(file);
141         if (fd == -1) {
142                 /* Hmm... should we delete the file too ? I guess we could try */
143                 statcont->close(statcont, file);
144                 statcont->unlink(statcont, furl);
145         }
146         return fd;
147 }
148
149
150 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
151 {
152         SMBCFILE * file = find_fd(fd);
153         return statcont->read(statcont, file, buf, bufsize);
154 }
155
156 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
157 {
158         SMBCFILE * file = find_fd(fd);
159         return statcont->write(statcont, file, buf, bufsize);
160 }
161
162 off_t smbc_lseek(int fd, off_t offset, int whence)
163 {
164         SMBCFILE * file = find_fd(fd);
165         return statcont->lseek(statcont, file, offset, whence);
166 }
167
168 int smbc_close(int fd)
169 {
170         SMBCFILE * file = find_fd(fd);
171         del_fd(fd);
172         return statcont->close(statcont, file);
173 }
174
175 int smbc_unlink(const char *fname)
176 {
177         return statcont->unlink(statcont, fname);
178 }
179
180 int smbc_rename(const char *ourl, const char *nurl)
181 {
182         return statcont->rename(statcont, ourl, statcont, nurl);
183 }
184
185 int smbc_opendir(const char *durl)
186 {
187         SMBCFILE * file;
188         int fd;
189
190         file = statcont->opendir(statcont, durl);
191         if (!file)
192                 return -1;
193
194         fd = add_fd(file);
195         if (fd == -1) 
196                 statcont->closedir(statcont, file);
197
198         return fd;
199 }
200
201 int smbc_closedir(int dh) 
202 {
203         SMBCFILE * file = find_fd(dh);
204         del_fd(dh);
205         return statcont->closedir(statcont, file);
206 }
207
208 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
209 {
210         SMBCFILE * file = find_fd(dh);
211         return statcont->getdents(statcont, file,dirp, count);
212 }
213
214 struct smbc_dirent* smbc_readdir(unsigned int dh)
215 {
216         SMBCFILE * file = find_fd(dh);
217         return statcont->readdir(statcont, file);
218 }
219
220 off_t smbc_telldir(int dh)
221 {
222         SMBCFILE * file = find_fd(dh);
223         return statcont->telldir(statcont, file);
224 }
225
226 int smbc_lseekdir(int fd, off_t offset)
227 {
228         SMBCFILE * file = find_fd(fd);
229         return statcont->lseekdir(statcont, file, offset);
230 }
231
232 int smbc_mkdir(const char *durl, mode_t mode)
233 {
234         return statcont->mkdir(statcont, durl, mode);
235 }
236
237 int smbc_rmdir(const char *durl)
238 {
239         return statcont->rmdir(statcont, durl);
240 }
241
242 int smbc_stat(const char *url, struct stat *st)
243 {
244         return statcont->stat(statcont, url, st);
245 }
246
247 int smbc_fstat(int fd, struct stat *st)
248 {
249         SMBCFILE * file = find_fd(fd);
250         return statcont->fstat(statcont, file, st);
251 }
252
253 int smbc_chmod(const char *url, mode_t mode)
254 {
255         /* NOT IMPLEMENTED IN LIBSMBCLIENT YET */
256         return -1;
257 }
258
259 int smbc_print_file(const char *fname, const char *printq)
260 {
261         return statcont->print_file(statcont, fname, statcont, printq);
262 }
263
264 int smbc_open_print_job(const char *fname)
265 {
266         SMBCFILE * file = statcont->open_print_job(statcont, fname);
267         if (!file) return -1;
268         return (int) file;
269 }
270
271 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
272 {
273         return statcont->list_print_jobs(statcont, purl, fn);
274 }
275
276 int smbc_unlink_print_job(const char *purl, int id)
277 {
278         return statcont->unlink_print_job(statcont, purl, id);
279 }
280
281