]>
Commit | Line | Data |
---|---|---|
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.8 2001/07/14 15:10:17 stevesk Exp $"); | |
27 | ||
28 | #include "buffer.h" | |
29 | #include "bufaux.h" | |
30 | #include "xmalloc.h" | |
31 | #include "log.h" | |
32 | ||
33 | #include "sftp.h" | |
34 | #include "sftp-common.h" | |
35 | #include "sftp-client.h" | |
36 | #include "sftp-glob.h" | |
37 | ||
38 | struct SFTP_OPENDIR { | |
39 | SFTP_DIRENT **dir; | |
40 | int offset; | |
41 | }; | |
42 | ||
43 | static struct { | |
44 | int fd_in; | |
45 | int fd_out; | |
46 | } cur; | |
47 | ||
48 | static void * | |
49 | fudge_opendir(const char *path) | |
50 | { | |
51 | struct SFTP_OPENDIR *r; | |
52 | ||
53 | r = xmalloc(sizeof(*r)); | |
54 | ||
55 | if (do_readdir(cur.fd_in, cur.fd_out, (char*)path, &r->dir)) | |
56 | return(NULL); | |
57 | ||
58 | r->offset = 0; | |
59 | ||
60 | return((void*)r); | |
61 | } | |
62 | ||
63 | static struct dirent * | |
64 | fudge_readdir(struct SFTP_OPENDIR *od) | |
65 | { | |
66 | /* Solaris needs sizeof(dirent) + path length (see below) */ | |
67 | static char buf[sizeof(struct dirent) + MAXPATHLEN]; | |
68 | struct dirent *ret = (struct dirent *)buf; | |
69 | #ifdef __GNU_LIBRARY__ | |
70 | static int inum = 1; | |
71 | #endif /* __GNU_LIBRARY__ */ | |
72 | ||
73 | if (od->dir[od->offset] == NULL) | |
74 | return(NULL); | |
75 | ||
76 | memset(buf, 0, sizeof(buf)); | |
77 | ||
78 | /* | |
79 | * Solaris defines dirent->d_name as a one byte array and expects | |
80 | * you to hack around it. | |
81 | */ | |
82 | #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME | |
83 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN); | |
84 | #else | |
85 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, | |
86 | sizeof(ret->d_name)); | |
87 | #endif | |
88 | #ifdef __GNU_LIBRARY__ | |
89 | /* | |
90 | * Idiot glibc uses extensions to struct dirent for readdir with | |
91 | * ALTDIRFUNCs. Not that this is documented anywhere but the | |
92 | * source... Fake an inode number to appease it. | |
93 | */ | |
94 | ret->d_ino = inum++; | |
95 | if (!inum) | |
96 | inum = 1; | |
97 | #endif /* __GNU_LIBRARY__ */ | |
98 | ||
99 | return(ret); | |
100 | } | |
101 | ||
102 | static void | |
103 | fudge_closedir(struct SFTP_OPENDIR *od) | |
104 | { | |
105 | free_sftp_dirents(od->dir); | |
106 | xfree(od); | |
107 | } | |
108 | ||
109 | static void | |
110 | attrib_to_stat(Attrib *a, struct stat *st) | |
111 | { | |
112 | memset(st, 0, sizeof(*st)); | |
113 | ||
114 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) | |
115 | st->st_size = a->size; | |
116 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { | |
117 | st->st_uid = a->uid; | |
118 | st->st_gid = a->gid; | |
119 | } | |
120 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) | |
121 | st->st_mode = a->perm; | |
122 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { | |
123 | st->st_atime = a->atime; | |
124 | st->st_mtime = a->mtime; | |
125 | } | |
126 | } | |
127 | ||
128 | static int | |
129 | fudge_lstat(const char *path, struct stat *st) | |
130 | { | |
131 | Attrib *a; | |
132 | ||
133 | if (!(a = do_lstat(cur.fd_in, cur.fd_out, (char*)path, 0))) | |
134 | return(-1); | |
135 | ||
136 | attrib_to_stat(a, st); | |
137 | ||
138 | return(0); | |
139 | } | |
140 | ||
141 | static int | |
142 | fudge_stat(const char *path, struct stat *st) | |
143 | { | |
144 | Attrib *a; | |
145 | ||
146 | if (!(a = do_stat(cur.fd_in, cur.fd_out, (char*)path, 0))) | |
147 | return(-1); | |
148 | ||
149 | attrib_to_stat(a, st); | |
150 | ||
151 | return(0); | |
152 | } | |
153 | ||
154 | int | |
155 | remote_glob(int fd_in, int fd_out, const char *pattern, int flags, | |
156 | int (*errfunc)(const char *, int), glob_t *pglob) | |
157 | { | |
158 | pglob->gl_opendir = fudge_opendir; | |
159 | pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; | |
160 | pglob->gl_closedir = (void (*)(void *))fudge_closedir; | |
161 | pglob->gl_lstat = fudge_lstat; | |
162 | pglob->gl_stat = fudge_stat; | |
163 | ||
164 | memset(&cur, 0, sizeof(cur)); | |
165 | cur.fd_in = fd_in; | |
166 | cur.fd_out = fd_out; | |
167 | ||
168 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, | |
169 | pglob)); | |
170 | } |