Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.12
1.1 djm 1: /*
1.10 djm 2: * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
1.1 djm 3: *
4: * Redistribution and use in source and binary forms, with or without
5: * modification, are permitted provided that the following conditions
6: * are met:
7: * 1. Redistributions of source code must retain the above copyright
8: * notice, this list of conditions and the following disclaimer.
9: * 2. Redistributions in binary form must reproduce the above copyright
10: * notice, this list of conditions and the following disclaimer in the
11: * documentation and/or other materials provided with the distribution.
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: */
24:
25: #include "includes.h"
1.12 ! deraadt 26: RCSID("$OpenBSD: sftp-glob.c,v 1.11 2002/06/30 21:59:45 deraadt Exp $");
1.1 djm 27:
28: #include <glob.h>
29:
30: #include "buffer.h"
31: #include "bufaux.h"
32: #include "xmalloc.h"
33: #include "log.h"
34:
35: #include "sftp.h"
36: #include "sftp-common.h"
37: #include "sftp-client.h"
38: #include "sftp-glob.h"
39:
40: struct SFTP_OPENDIR {
41: SFTP_DIRENT **dir;
42: int offset;
43: };
44:
45: static struct {
1.10 djm 46: struct sftp_conn *conn;
1.1 djm 47: } cur;
48:
1.6 itojun 49: static void *
50: fudge_opendir(const char *path)
1.1 djm 51: {
52: struct SFTP_OPENDIR *r;
1.9 deraadt 53:
1.1 djm 54: r = xmalloc(sizeof(*r));
1.9 deraadt 55:
1.12 ! deraadt 56: if (do_readdir(cur.conn, (char *)path, &r->dir)) {
! 57: xfree(r);
1.1 djm 58: return(NULL);
1.12 ! deraadt 59: }
1.1 djm 60:
61: r->offset = 0;
62:
1.11 deraadt 63: return((void *)r);
1.1 djm 64: }
65:
1.6 itojun 66: static struct dirent *
67: fudge_readdir(struct SFTP_OPENDIR *od)
1.1 djm 68: {
69: static struct dirent ret;
1.9 deraadt 70:
1.1 djm 71: if (od->dir[od->offset] == NULL)
72: return(NULL);
73:
74: memset(&ret, 0, sizeof(ret));
1.4 markus 75: strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1 djm 76: sizeof(ret.d_name));
77:
78: return(&ret);
79: }
80:
1.6 itojun 81: static void
82: fudge_closedir(struct SFTP_OPENDIR *od)
1.1 djm 83: {
84: free_sftp_dirents(od->dir);
1.3 stevesk 85: xfree(od);
1.1 djm 86: }
87:
1.6 itojun 88: static void
89: attrib_to_stat(Attrib *a, struct stat *st)
1.1 djm 90: {
91: memset(st, 0, sizeof(*st));
1.9 deraadt 92:
1.1 djm 93: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
94: st->st_size = a->size;
95: if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
96: st->st_uid = a->uid;
97: st->st_gid = a->gid;
98: }
99: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
100: st->st_mode = a->perm;
101: if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
102: st->st_atime = a->atime;
103: st->st_mtime = a->mtime;
104: }
105: }
106:
1.6 itojun 107: static int
108: fudge_lstat(const char *path, struct stat *st)
1.1 djm 109: {
110: Attrib *a;
1.9 deraadt 111:
1.11 deraadt 112: if (!(a = do_lstat(cur.conn, (char *)path, 0)))
1.1 djm 113: return(-1);
1.9 deraadt 114:
1.1 djm 115: attrib_to_stat(a, st);
1.9 deraadt 116:
1.1 djm 117: return(0);
118: }
119:
1.6 itojun 120: static int
121: fudge_stat(const char *path, struct stat *st)
1.1 djm 122: {
123: Attrib *a;
1.9 deraadt 124:
1.11 deraadt 125: if (!(a = do_stat(cur.conn, (char *)path, 0)))
1.1 djm 126: return(-1);
1.9 deraadt 127:
1.1 djm 128: attrib_to_stat(a, st);
1.9 deraadt 129:
1.1 djm 130: return(0);
131: }
132:
133: int
1.10 djm 134: remote_glob(struct sftp_conn *conn, const char *pattern, int flags,
1.5 markus 135: int (*errfunc)(const char *, int), glob_t *pglob)
1.1 djm 136: {
1.7 espie 137: pglob->gl_opendir = fudge_opendir;
138: pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir;
139: pglob->gl_closedir = (void (*)(void *))fudge_closedir;
1.1 djm 140: pglob->gl_lstat = fudge_lstat;
141: pglob->gl_stat = fudge_stat;
1.9 deraadt 142:
1.1 djm 143: memset(&cur, 0, sizeof(cur));
1.10 djm 144: cur.conn = conn;
1.1 djm 145:
1.10 djm 146: return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob));
1.1 djm 147: }