libsmb/libsmb_compat.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    SMB client library implementation (Old interface compatibility)
00004    Copyright (C) Andrew Tridgell 1998
00005    Copyright (C) Richard Sharpe 2000
00006    Copyright (C) John Terpstra 2000
00007    Copyright (C) Tom Jansen (Ninja ISD) 2002 
00008    Copyright (C) Derrell Lipman 2003
00009    
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 2 of the License, or
00013    (at your option) any later version.
00014    
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019    
00020    You should have received a copy of the GNU General Public License
00021    along with this program; if not, write to the Free Software
00022    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00023 */
00024 
00025 
00026 #include "includes.h"
00027 
00028 #include "include/libsmb_internal.h"
00029 
00030 struct smbc_compat_fdlist {
00031         SMBCFILE * file;
00032         int fd;
00033         struct smbc_compat_fdlist *next, *prev;
00034 };
00035 
00036 static SMBCCTX * statcont = NULL;
00037 static int smbc_compat_initialized = 0;
00038 static int smbc_compat_nextfd = 0;
00039 static struct smbc_compat_fdlist * smbc_compat_fd_in_use = NULL;
00040 static struct smbc_compat_fdlist * smbc_compat_fd_avail = NULL;
00041 
00042 /* Find an fd and return the SMBCFILE * or NULL on failure */
00043 static SMBCFILE * find_fd(int fd)
00044 {
00045         struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
00046         while (f) {
00047                 if (f->fd == fd) 
00048                         return f->file;
00049                 f = f->next;
00050         }
00051         return NULL;
00052 }
00053 
00054 /* Add an fd, returns 0 on success, -1 on error with errno set */
00055 static int add_fd(SMBCFILE * file)
00056 {
00057         struct smbc_compat_fdlist * f = smbc_compat_fd_avail;
00058 
00059         if (f) {
00060                 /* We found one that's available */
00061                 DLIST_REMOVE(smbc_compat_fd_avail, f);
00062 
00063         } else {
00064                 /*
00065                  * None were available, so allocate one.  Keep the number of
00066                  * file descriptors determinate.  This allows the application
00067                  * to allocate bitmaps or mapping of file descriptors based on
00068                  * a known maximum number of file descriptors that will ever
00069                  * be returned.
00070                  */
00071                 if (smbc_compat_nextfd >= FD_SETSIZE) {
00072                         errno = EMFILE;
00073                         return -1;
00074                 }
00075 
00076                 f = SMB_MALLOC_P(struct smbc_compat_fdlist);
00077                 if (!f) {
00078                         errno = ENOMEM;
00079                         return -1;
00080                 }
00081         
00082                 f->fd = SMBC_BASE_FD + smbc_compat_nextfd++;
00083         }
00084 
00085         f->file = file;
00086         DLIST_ADD(smbc_compat_fd_in_use, f);
00087 
00088         return f->fd;
00089 }
00090 
00091 
00092 
00093 /* Delete an fd, returns 0 on success */
00094 static int del_fd(int fd)
00095 {
00096         struct smbc_compat_fdlist * f = smbc_compat_fd_in_use;
00097 
00098         while (f) {
00099                 if (f->fd == fd) 
00100                         break;
00101                 f = f->next;
00102         }
00103 
00104         if (f) {
00105                 /* found */
00106                 DLIST_REMOVE(smbc_compat_fd_in_use, f);
00107                 f->file = NULL;
00108                 DLIST_ADD(smbc_compat_fd_avail, f);
00109                 return 0;
00110         }
00111         return 1;
00112 }
00113  
00114 
00115 
00116 int smbc_init(smbc_get_auth_data_fn fn, int debug)
00117 {
00118         if (!smbc_compat_initialized) {
00119                 statcont = smbc_new_context();
00120                 if (!statcont) 
00121                         return -1;
00122 
00123                 statcont->debug = debug;
00124                 statcont->callbacks.auth_fn = fn;
00125                 
00126                 if (!smbc_init_context(statcont)) {
00127                         smbc_free_context(statcont, False);
00128                         return -1;
00129                 }
00130 
00131                 smbc_compat_initialized = 1;
00132 
00133                 return 0;
00134         }
00135         return 0;
00136 }
00137 
00138 
00139 SMBCCTX *smbc_set_context(SMBCCTX * context)
00140 {
00141         SMBCCTX *old_context = statcont;
00142 
00143         if (context) {
00144                 /* Save provided context.  It must have been initialized! */
00145                 statcont = context;
00146 
00147                 /* You'd better know what you're doing.  We won't help you. */
00148                 smbc_compat_initialized = 1;
00149         }
00150         
00151         return old_context;
00152 }
00153 
00154 
00155 int smbc_open(const char *furl, int flags, mode_t mode)
00156 {
00157         SMBCFILE * file;
00158         int fd;
00159 
00160         file = (statcont->open)(statcont, furl, flags, mode);
00161         if (!file)
00162                 return -1;
00163 
00164         fd = add_fd(file);
00165         if (fd == -1) 
00166                 (statcont->close_fn)(statcont, file);
00167         return fd;
00168 }
00169 
00170 
00171 int smbc_creat(const char *furl, mode_t mode)
00172 {
00173         SMBCFILE * file;
00174         int fd;
00175 
00176         file = (statcont->creat)(statcont, furl, mode);
00177         if (!file)
00178                 return -1;
00179 
00180         fd = add_fd(file);
00181         if (fd == -1) {
00182                 /* Hmm... should we delete the file too ? I guess we could try */
00183                 (statcont->close_fn)(statcont, file);
00184                 (statcont->unlink)(statcont, furl);
00185         }
00186         return fd;
00187 }
00188 
00189 
00190 ssize_t smbc_read(int fd, void *buf, size_t bufsize)
00191 {
00192         SMBCFILE * file = find_fd(fd);
00193         return (statcont->read)(statcont, file, buf, bufsize);
00194 }
00195 
00196 ssize_t smbc_write(int fd, void *buf, size_t bufsize)
00197 {
00198         SMBCFILE * file = find_fd(fd);
00199         return (statcont->write)(statcont, file, buf, bufsize);
00200 }
00201 
00202 off_t smbc_lseek(int fd, off_t offset, int whence)
00203 {
00204         SMBCFILE * file = find_fd(fd);
00205         return (statcont->lseek)(statcont, file, offset, whence);
00206 }
00207 
00208 int smbc_close(int fd)
00209 {
00210         SMBCFILE * file = find_fd(fd);
00211         del_fd(fd);
00212         return (statcont->close_fn)(statcont, file);
00213 }
00214 
00215 int smbc_unlink(const char *fname)
00216 {
00217         return (statcont->unlink)(statcont, fname);
00218 }
00219 
00220 int smbc_rename(const char *ourl, const char *nurl)
00221 {
00222         return (statcont->rename)(statcont, ourl, statcont, nurl);
00223 }
00224 
00225 int smbc_opendir(const char *durl)
00226 {
00227         SMBCFILE * file;
00228         int fd;
00229 
00230         file = (statcont->opendir)(statcont, durl);
00231         if (!file)
00232                 return -1;
00233 
00234         fd = add_fd(file);
00235         if (fd == -1) 
00236                 (statcont->closedir)(statcont, file);
00237 
00238         return fd;
00239 }
00240 
00241 int smbc_closedir(int dh) 
00242 {
00243         SMBCFILE * file = find_fd(dh);
00244         del_fd(dh);
00245         return (statcont->closedir)(statcont, file);
00246 }
00247 
00248 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count)
00249 {
00250         SMBCFILE * file = find_fd(dh);
00251         return (statcont->getdents)(statcont, file,dirp, count);
00252 }
00253 
00254 struct smbc_dirent* smbc_readdir(unsigned int dh)
00255 {
00256         SMBCFILE * file = find_fd(dh);
00257         return (statcont->readdir)(statcont, file);
00258 }
00259 
00260 off_t smbc_telldir(int dh)
00261 {
00262         SMBCFILE * file = find_fd(dh);
00263         return (statcont->telldir)(statcont, file);
00264 }
00265 
00266 int smbc_lseekdir(int fd, off_t offset)
00267 {
00268         SMBCFILE * file = find_fd(fd);
00269         return (statcont->lseekdir)(statcont, file, offset);
00270 }
00271 
00272 int smbc_mkdir(const char *durl, mode_t mode)
00273 {
00274         return (statcont->mkdir)(statcont, durl, mode);
00275 }
00276 
00277 int smbc_rmdir(const char *durl)
00278 {
00279         return (statcont->rmdir)(statcont, durl);
00280 }
00281 
00282 int smbc_stat(const char *url, struct stat *st)
00283 {
00284         return (statcont->stat)(statcont, url, st);
00285 }
00286 
00287 int smbc_fstat(int fd, struct stat *st)
00288 {
00289         SMBCFILE * file = find_fd(fd);
00290         return (statcont->fstat)(statcont, file, st);
00291 }
00292 
00293 int smbc_chmod(const char *url, mode_t mode)
00294 {
00295         return (statcont->chmod)(statcont, url, mode);
00296 }
00297 
00298 int smbc_utimes(const char *fname, struct timeval *tbuf)
00299 {
00300         return (statcont->utimes)(statcont, fname, tbuf);
00301 }
00302 
00303 #ifdef HAVE_UTIME_H
00304 int smbc_utime(const char *fname, struct utimbuf *utbuf)
00305 {
00306         struct timeval tv[2];
00307 
00308         if (utbuf == NULL)
00309                 return (statcont->utimes)(statcont, fname, NULL);
00310 
00311         tv[0].tv_sec = utbuf->actime;
00312         tv[1].tv_sec = utbuf->modtime;
00313         tv[0].tv_usec = tv[1].tv_usec = 0;
00314 
00315         return (statcont->utimes)(statcont, fname, tv);
00316 }
00317 #endif
00318 
00319 int smbc_setxattr(const char *fname,
00320                   const char *name,
00321                   const void *value,
00322                   size_t size,
00323                   int flags)
00324 {
00325         return (statcont->setxattr)(statcont, fname, name, value, size, flags);
00326 }
00327 
00328 int smbc_lsetxattr(const char *fname,
00329                    const char *name,
00330                    const void *value,
00331                    size_t size,
00332                    int flags)
00333 {
00334         return (statcont->setxattr)(statcont, fname, name, value, size, flags);
00335 }
00336 
00337 int smbc_fsetxattr(int fd,
00338                    const char *name,
00339                    const void *value,
00340                    size_t size,
00341                    int flags)
00342 {
00343         SMBCFILE * file = find_fd(fd);
00344         if (file == NULL) {
00345                 errno = EBADF;
00346                 return -1;
00347         }
00348         return (statcont->setxattr)(statcont, file->fname,
00349                                     name, value, size, flags);
00350 }
00351 
00352 int smbc_getxattr(const char *fname,
00353                   const char *name,
00354                   const void *value,
00355                   size_t size)
00356 {
00357         return (statcont->getxattr)(statcont, fname, name, value, size);
00358 }
00359 
00360 int smbc_lgetxattr(const char *fname,
00361                    const char *name,
00362                    const void *value,
00363                    size_t size)
00364 {
00365         return (statcont->getxattr)(statcont, fname, name, value, size);
00366 }
00367 
00368 int smbc_fgetxattr(int fd,
00369                    const char *name,
00370                    const void *value,
00371                    size_t size)
00372 {
00373         SMBCFILE * file = find_fd(fd);
00374         if (file == NULL) {
00375                 errno = EBADF;
00376                 return -1;
00377         }
00378         return (statcont->getxattr)(statcont, file->fname, name, value, size);
00379 }
00380 
00381 int smbc_removexattr(const char *fname,
00382                      const char *name)
00383 {
00384         return (statcont->removexattr)(statcont, fname, name);
00385 }
00386 
00387 int smbc_lremovexattr(const char *fname,
00388                       const char *name)
00389 {
00390         return (statcont->removexattr)(statcont, fname, name);
00391 }
00392 
00393 int smbc_fremovexattr(int fd,
00394                       const char *name)
00395 {
00396         SMBCFILE * file = find_fd(fd);
00397         if (file == NULL) {
00398                 errno = EBADF;
00399                 return -1;
00400         }
00401         return (statcont->removexattr)(statcont, file->fname, name);
00402 }
00403 
00404 int smbc_listxattr(const char *fname,
00405                    char *list,
00406                    size_t size)
00407 {
00408         return (statcont->listxattr)(statcont, fname, list, size);
00409 }
00410 
00411 int smbc_llistxattr(const char *fname,
00412                     char *list,
00413                     size_t size)
00414 {
00415         return (statcont->listxattr)(statcont, fname, list, size);
00416 }
00417 
00418 int smbc_flistxattr(int fd,
00419                     char *list,
00420                     size_t size)
00421 {
00422         SMBCFILE * file = find_fd(fd);
00423         if (file == NULL) {
00424                 errno = EBADF;
00425                 return -1;
00426         }
00427         return (statcont->listxattr)(statcont, file->fname, list, size);
00428 }
00429 
00430 int smbc_print_file(const char *fname, const char *printq)
00431 {
00432         return (statcont->print_file)(statcont, fname, statcont, printq);
00433 }
00434 
00435 int smbc_open_print_job(const char *fname)
00436 {
00437         SMBCFILE * file = (statcont->open_print_job)(statcont, fname);
00438         if (!file) return -1;
00439         return file->cli_fd;
00440 }
00441 
00442 int smbc_list_print_jobs(const char *purl, smbc_list_print_job_fn fn)
00443 {
00444         return (statcont->list_print_jobs)(statcont, purl, fn);
00445 }
00446 
00447 int smbc_unlink_print_job(const char *purl, int id)
00448 {
00449         return (statcont->unlink_print_job)(statcont, purl, id);
00450 }
00451 
00452 

Sambaに対してSat Aug 29 21:23:05 2009に生成されました。  doxygen 1.4.7