[BACK]Return to sftp-glob.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.18

1.1       djm         1: /*
1.14      djm         2:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         3:  *
1.14      djm         4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
1.1       djm         7:  *
1.14      djm         8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       djm        15:  */
                     16:
                     17: #include "includes.h"
1.17      stevesk    18:
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
1.1       djm        21:
1.16      stevesk    22: #include <dirent.h>
1.1       djm        23: #include <glob.h>
                     24:
                     25: #include "buffer.h"
                     26: #include "bufaux.h"
                     27: #include "xmalloc.h"
                     28: #include "log.h"
                     29:
                     30: #include "sftp.h"
                     31: #include "sftp-common.h"
                     32: #include "sftp-client.h"
1.15      djm        33:
                     34: int remote_glob(struct sftp_conn *, const char *, int,
                     35:     int (*)(const char *, int), glob_t *);
1.1       djm        36:
                     37: struct SFTP_OPENDIR {
                     38:        SFTP_DIRENT **dir;
                     39:        int offset;
                     40: };
                     41:
                     42: static struct {
1.10      djm        43:        struct sftp_conn *conn;
1.1       djm        44: } cur;
                     45:
1.6       itojun     46: static void *
                     47: fudge_opendir(const char *path)
1.1       djm        48: {
                     49:        struct SFTP_OPENDIR *r;
1.9       deraadt    50:
1.1       djm        51:        r = xmalloc(sizeof(*r));
1.9       deraadt    52:
1.12      deraadt    53:        if (do_readdir(cur.conn, (char *)path, &r->dir)) {
                     54:                xfree(r);
1.1       djm        55:                return(NULL);
1.12      deraadt    56:        }
1.1       djm        57:
                     58:        r->offset = 0;
                     59:
1.11      deraadt    60:        return((void *)r);
1.1       djm        61: }
                     62:
1.6       itojun     63: static struct dirent *
                     64: fudge_readdir(struct SFTP_OPENDIR *od)
1.1       djm        65: {
                     66:        static struct dirent ret;
1.9       deraadt    67:
1.1       djm        68:        if (od->dir[od->offset] == NULL)
                     69:                return(NULL);
                     70:
                     71:        memset(&ret, 0, sizeof(ret));
1.4       markus     72:        strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1       djm        73:            sizeof(ret.d_name));
                     74:
                     75:        return(&ret);
                     76: }
                     77:
1.6       itojun     78: static void
                     79: fudge_closedir(struct SFTP_OPENDIR *od)
1.1       djm        80: {
                     81:        free_sftp_dirents(od->dir);
1.3       stevesk    82:        xfree(od);
1.1       djm        83: }
                     84:
1.6       itojun     85: static int
                     86: fudge_lstat(const char *path, struct stat *st)
1.1       djm        87: {
                     88:        Attrib *a;
1.9       deraadt    89:
1.11      deraadt    90:        if (!(a = do_lstat(cur.conn, (char *)path, 0)))
1.1       djm        91:                return(-1);
1.9       deraadt    92:
1.1       djm        93:        attrib_to_stat(a, st);
1.9       deraadt    94:
1.1       djm        95:        return(0);
                     96: }
                     97:
1.6       itojun     98: static int
                     99: fudge_stat(const char *path, struct stat *st)
1.1       djm       100: {
                    101:        Attrib *a;
1.9       deraadt   102:
1.11      deraadt   103:        if (!(a = do_stat(cur.conn, (char *)path, 0)))
1.1       djm       104:                return(-1);
1.9       deraadt   105:
1.1       djm       106:        attrib_to_stat(a, st);
1.9       deraadt   107:
1.1       djm       108:        return(0);
                    109: }
                    110:
                    111: int
1.10      djm       112: remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1.5       markus    113:     int (*errfunc)(const char *, int), glob_t *pglob)
1.1       djm       114: {
1.7       espie     115:        pglob->gl_opendir = fudge_opendir;
                    116:        pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
                    117:        pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1.1       djm       118:        pglob->gl_lstat = fudge_lstat;
                    119:        pglob->gl_stat = fudge_stat;
1.9       deraadt   120:
1.1       djm       121:        memset(&cur, 0, sizeof(cur));
1.10      djm       122:        cur.conn = conn;
1.1       djm       123:
1.10      djm       124:        return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
1.1       djm       125: }