diff options
Diffstat (limited to 'sftp-glob.c')
-rw-r--r-- | sftp-glob.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/sftp-glob.c b/sftp-glob.c new file mode 100644 index 000000000..17f46a151 --- /dev/null +++ b/sftp-glob.c | |||
@@ -0,0 +1,160 @@ | |||
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" | ||
26 | RCSID("$OpenBSD: sftp-glob.c,v 1.1 2001/03/13 22:42:54 djm Exp $"); | ||
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 | |||
54 | void *fudge_opendir(const char *path) | ||
55 | { | ||
56 | struct SFTP_OPENDIR *r; | ||
57 | |||
58 | r = xmalloc(sizeof(*r)); | ||
59 | |||
60 | if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir)) | ||
61 | return(NULL); | ||
62 | |||
63 | r->offset = 0; | ||
64 | |||
65 | return((void*)r); | ||
66 | } | ||
67 | |||
68 | struct dirent *fudge_readdir(struct SFTP_OPENDIR *od) | ||
69 | { | ||
70 | static struct dirent ret; | ||
71 | #ifdef __GNU_LIBRARY__ | ||
72 | static int inum = 1; | ||
73 | #endif /* __GNU_LIBRARY__ */ | ||
74 | |||
75 | if (od->dir[od->offset] == NULL) | ||
76 | return(NULL); | ||
77 | |||
78 | memset(&ret, 0, sizeof(ret)); | ||
79 | strlcpy(ret.d_name, od->dir[od->offset++]->filename, | ||
80 | sizeof(ret.d_name)); | ||
81 | |||
82 | #ifdef __GNU_LIBRARY__ | ||
83 | /* | ||
84 | * Idiot glibc uses extensions to struct dirent for readdir with | ||
85 | * ALTDIRFUNCs. Not that this is documented anywhere but the | ||
86 | * source... Fake an inode number to appease it. | ||
87 | */ | ||
88 | ret.d_ino = inum++; | ||
89 | if (!inum) | ||
90 | inum = 1; | ||
91 | #endif /* __GNU_LIBRARY__ */ | ||
92 | |||
93 | return(&ret); | ||
94 | } | ||
95 | |||
96 | void fudge_closedir(struct SFTP_OPENDIR *od) | ||
97 | { | ||
98 | free_sftp_dirents(od->dir); | ||
99 | free(od); | ||
100 | } | ||
101 | |||
102 | void attrib_to_stat(Attrib *a, struct stat *st) | ||
103 | { | ||
104 | memset(st, 0, sizeof(*st)); | ||
105 | |||
106 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) | ||
107 | st->st_size = a->size; | ||
108 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { | ||
109 | st->st_uid = a->uid; | ||
110 | st->st_gid = a->gid; | ||
111 | } | ||
112 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) | ||
113 | st->st_mode = a->perm; | ||
114 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { | ||
115 | st->st_atime = a->atime; | ||
116 | st->st_mtime = a->mtime; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | int fudge_lstat(const char *path, struct stat *st) | ||
121 | { | ||
122 | Attrib *a; | ||
123 | |||
124 | if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path))) | ||
125 | return(-1); | ||
126 | |||
127 | attrib_to_stat(a, st); | ||
128 | |||
129 | return(0); | ||
130 | } | ||
131 | |||
132 | int fudge_stat(const char *path, struct stat *st) | ||
133 | { | ||
134 | Attrib *a; | ||
135 | |||
136 | if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path))) | ||
137 | return(-1); | ||
138 | |||
139 | attrib_to_stat(a, st); | ||
140 | |||
141 | return(0); | ||
142 | } | ||
143 | |||
144 | int | ||
145 | remote_glob(int fd_in, int fd_out, const char *pattern, int flags, | ||
146 | const int (*errfunc)(const char *, int), glob_t *pglob) | ||
147 | { | ||
148 | pglob->gl_opendir = (void*)fudge_opendir; | ||
149 | pglob->gl_readdir = (void*)fudge_readdir; | ||
150 | pglob->gl_closedir = (void*)fudge_closedir; | ||
151 | pglob->gl_lstat = fudge_lstat; | ||
152 | pglob->gl_stat = fudge_stat; | ||
153 | |||
154 | memset(&cur, 0, sizeof(cur)); | ||
155 | cur.fd_in = fd_in; | ||
156 | cur.fd_out = fd_out; | ||
157 | |||
158 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, (void*)errfunc, | ||
159 | pglob)); | ||
160 | } | ||