Annotation of src/usr.bin/ssh/sftp-glob.c, Revision 1.6
1.1 djm 1: /*
2: * Copyright (c) 2001 Damien Miller. All rights reserved.
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.6 ! itojun 26: RCSID("$OpenBSD: sftp-glob.c,v 1.5 2001/04/15 08:43:46 markus Exp $");
1.1 djm 27:
28: #include <glob.h>
29:
30: #include "ssh.h"
31: #include "buffer.h"
32: #include "bufaux.h"
33: #include "getput.h"
34: #include "xmalloc.h"
35: #include "log.h"
36: #include "atomicio.h"
37: #include "pathnames.h"
38:
39: #include "sftp.h"
40: #include "sftp-common.h"
41: #include "sftp-client.h"
42: #include "sftp-glob.h"
43:
44: struct SFTP_OPENDIR {
45: SFTP_DIRENT **dir;
46: int offset;
47: };
48:
49: static struct {
50: int fd_in;
51: int fd_out;
52: } cur;
53:
1.6 ! itojun 54: static void *
! 55: fudge_opendir(const char *path)
1.1 djm 56: {
57: struct SFTP_OPENDIR *r;
58:
59: r = xmalloc(sizeof(*r));
60:
61: if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir))
62: return(NULL);
63:
64: r->offset = 0;
65:
66: return((void*)r);
67: }
68:
1.6 ! itojun 69: static struct dirent *
! 70: fudge_readdir(struct SFTP_OPENDIR *od)
1.1 djm 71: {
72: static struct dirent ret;
73:
74: if (od->dir[od->offset] == NULL)
75: return(NULL);
76:
77: memset(&ret, 0, sizeof(ret));
1.4 markus 78: strlcpy(ret.d_name, od->dir[od->offset++]->filename,
1.1 djm 79: sizeof(ret.d_name));
80:
81: return(&ret);
82: }
83:
1.6 ! itojun 84: static void
! 85: fudge_closedir(struct SFTP_OPENDIR *od)
1.1 djm 86: {
87: free_sftp_dirents(od->dir);
1.3 stevesk 88: xfree(od);
1.1 djm 89: }
90:
1.6 ! itojun 91: static void
! 92: attrib_to_stat(Attrib *a, struct stat *st)
1.1 djm 93: {
94: memset(st, 0, sizeof(*st));
95:
96: if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
97: st->st_size = a->size;
98: if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
99: st->st_uid = a->uid;
100: st->st_gid = a->gid;
101: }
102: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
103: st->st_mode = a->perm;
104: if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
105: st->st_atime = a->atime;
106: st->st_mtime = a->mtime;
107: }
108: }
109:
1.6 ! itojun 110: static int
! 111: fudge_lstat(const char *path, struct stat *st)
1.1 djm 112: {
113: Attrib *a;
114:
1.2 djm 115: if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0)))
1.1 djm 116: return(-1);
117:
118: attrib_to_stat(a, st);
119:
120: return(0);
121: }
122:
1.6 ! itojun 123: static int
! 124: fudge_stat(const char *path, struct stat *st)
1.1 djm 125: {
126: Attrib *a;
127:
1.2 djm 128: if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0)))
1.1 djm 129: return(-1);
130:
131: attrib_to_stat(a, st);
132:
133: return(0);
134: }
135:
136: int
1.4 markus 137: remote_glob(int fd_in, int fd_out, const char *pattern, int flags,
1.5 markus 138: int (*errfunc)(const char *, int), glob_t *pglob)
1.1 djm 139: {
140: pglob->gl_opendir = (void*)fudge_opendir;
141: pglob->gl_readdir = (void*)fudge_readdir;
142: pglob->gl_closedir = (void*)fudge_closedir;
143: pglob->gl_lstat = fudge_lstat;
144: pglob->gl_stat = fudge_stat;
145:
146: memset(&cur, 0, sizeof(cur));
147: cur.fd_in = fd_in;
148: cur.fd_out = fd_out;
149:
1.4 markus 150: return(glob(pattern, flags | GLOB_ALTDIRFUNC, (void*)errfunc,
1.1 djm 151: pglob));
152: }