]>
Commit | Line | Data |
---|---|---|
2e4fb373 | 1 | /* |
22e6c827 | 2 | * Copyright (c) 2001,2002 Damien Miller. All rights reserved. |
2e4fb373 | 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" | |
00b3ad3e | 26 | RCSID("$OpenBSD: sftp-glob.c,v 1.13 2002/09/11 22:41:50 djm Exp $"); |
2e4fb373 | 27 | |
2e4fb373 | 28 | #include "buffer.h" |
29 | #include "bufaux.h" | |
2e4fb373 | 30 | #include "xmalloc.h" |
31 | #include "log.h" | |
2e4fb373 | 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 { | |
22e6c827 | 44 | struct sftp_conn *conn; |
2e4fb373 | 45 | } cur; |
46 | ||
396c147e | 47 | static void * |
48 | fudge_opendir(const char *path) | |
2e4fb373 | 49 | { |
50 | struct SFTP_OPENDIR *r; | |
184eed6a | 51 | |
2e4fb373 | 52 | r = xmalloc(sizeof(*r)); |
184eed6a | 53 | |
da2499f5 | 54 | if (do_readdir(cur.conn, (char *)path, &r->dir)) { |
55 | xfree(r); | |
2e4fb373 | 56 | return(NULL); |
da2499f5 | 57 | } |
2e4fb373 | 58 | |
59 | r->offset = 0; | |
60 | ||
343288b8 | 61 | return((void *)r); |
2e4fb373 | 62 | } |
63 | ||
396c147e | 64 | static struct dirent * |
65 | fudge_readdir(struct SFTP_OPENDIR *od) | |
2e4fb373 | 66 | { |
edbe6722 | 67 | /* Solaris needs sizeof(dirent) + path length (see below) */ |
68 | static char buf[sizeof(struct dirent) + MAXPATHLEN]; | |
69 | struct dirent *ret = (struct dirent *)buf; | |
2e4fb373 | 70 | #ifdef __GNU_LIBRARY__ |
71 | static int inum = 1; | |
72 | #endif /* __GNU_LIBRARY__ */ | |
b6453d99 | 73 | |
2e4fb373 | 74 | if (od->dir[od->offset] == NULL) |
75 | return(NULL); | |
76 | ||
edbe6722 | 77 | memset(buf, 0, sizeof(buf)); |
2e4fb373 | 78 | |
edbe6722 | 79 | /* |
80 | * Solaris defines dirent->d_name as a one byte array and expects | |
81 | * you to hack around it. | |
82 | */ | |
83 | #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME | |
84 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN); | |
85 | #else | |
cd332296 | 86 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, |
edbe6722 | 87 | sizeof(ret->d_name)); |
88 | #endif | |
2e4fb373 | 89 | #ifdef __GNU_LIBRARY__ |
90 | /* | |
91 | * Idiot glibc uses extensions to struct dirent for readdir with | |
aff51935 | 92 | * ALTDIRFUNCs. Not that this is documented anywhere but the |
2e4fb373 | 93 | * source... Fake an inode number to appease it. |
94 | */ | |
edbe6722 | 95 | ret->d_ino = inum++; |
2e4fb373 | 96 | if (!inum) |
97 | inum = 1; | |
98 | #endif /* __GNU_LIBRARY__ */ | |
99 | ||
edbe6722 | 100 | return(ret); |
2e4fb373 | 101 | } |
102 | ||
396c147e | 103 | static void |
104 | fudge_closedir(struct SFTP_OPENDIR *od) | |
2e4fb373 | 105 | { |
106 | free_sftp_dirents(od->dir); | |
894c5fa6 | 107 | xfree(od); |
2e4fb373 | 108 | } |
109 | ||
396c147e | 110 | static int |
111 | fudge_lstat(const char *path, struct stat *st) | |
2e4fb373 | 112 | { |
113 | Attrib *a; | |
184eed6a | 114 | |
343288b8 | 115 | if (!(a = do_lstat(cur.conn, (char *)path, 0))) |
2e4fb373 | 116 | return(-1); |
184eed6a | 117 | |
2e4fb373 | 118 | attrib_to_stat(a, st); |
184eed6a | 119 | |
2e4fb373 | 120 | return(0); |
121 | } | |
122 | ||
396c147e | 123 | static int |
124 | fudge_stat(const char *path, struct stat *st) | |
2e4fb373 | 125 | { |
126 | Attrib *a; | |
184eed6a | 127 | |
343288b8 | 128 | if (!(a = do_stat(cur.conn, (char *)path, 0))) |
2e4fb373 | 129 | return(-1); |
184eed6a | 130 | |
2e4fb373 | 131 | attrib_to_stat(a, st); |
184eed6a | 132 | |
2e4fb373 | 133 | return(0); |
134 | } | |
135 | ||
136 | int | |
22e6c827 | 137 | remote_glob(struct sftp_conn *conn, const char *pattern, int flags, |
ec1f12d3 | 138 | int (*errfunc)(const char *, int), glob_t *pglob) |
2e4fb373 | 139 | { |
29009047 | 140 | pglob->gl_opendir = fudge_opendir; |
141 | pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; | |
142 | pglob->gl_closedir = (void (*)(void *))fudge_closedir; | |
2e4fb373 | 143 | pglob->gl_lstat = fudge_lstat; |
144 | pglob->gl_stat = fudge_stat; | |
184eed6a | 145 | |
2e4fb373 | 146 | memset(&cur, 0, sizeof(cur)); |
22e6c827 | 147 | cur.conn = conn; |
2e4fb373 | 148 | |
22e6c827 | 149 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob)); |
2e4fb373 | 150 | } |