]>
Commit | Line | Data |
---|---|---|
2e4fb373 | 1 | /* |
ab3932ab | 2 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
2e4fb373 | 3 | * |
ab3932ab | 4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
2e4fb373 | 7 | * |
ab3932ab | 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
2e4fb373 | 15 | */ |
16 | ||
17 | #include "includes.h" | |
68e39d38 | 18 | RCSID("$OpenBSD: sftp-glob.c,v 1.16 2006/02/08 23:51:24 stevesk Exp $"); |
19 | ||
20 | #include <dirent.h> | |
2e4fb373 | 21 | |
2e4fb373 | 22 | #include "buffer.h" |
23 | #include "bufaux.h" | |
2e4fb373 | 24 | #include "xmalloc.h" |
25 | #include "log.h" | |
2e4fb373 | 26 | |
27 | #include "sftp.h" | |
28 | #include "sftp-common.h" | |
29 | #include "sftp-client.h" | |
caf1e9f0 | 30 | |
31 | int remote_glob(struct sftp_conn *, const char *, int, | |
32 | int (*)(const char *, int), glob_t *); | |
2e4fb373 | 33 | |
34 | struct SFTP_OPENDIR { | |
35 | SFTP_DIRENT **dir; | |
36 | int offset; | |
37 | }; | |
38 | ||
39 | static struct { | |
22e6c827 | 40 | struct sftp_conn *conn; |
2e4fb373 | 41 | } cur; |
42 | ||
396c147e | 43 | static void * |
44 | fudge_opendir(const char *path) | |
2e4fb373 | 45 | { |
46 | struct SFTP_OPENDIR *r; | |
184eed6a | 47 | |
2e4fb373 | 48 | r = xmalloc(sizeof(*r)); |
184eed6a | 49 | |
da2499f5 | 50 | if (do_readdir(cur.conn, (char *)path, &r->dir)) { |
51 | xfree(r); | |
2e4fb373 | 52 | return(NULL); |
da2499f5 | 53 | } |
2e4fb373 | 54 | |
55 | r->offset = 0; | |
56 | ||
343288b8 | 57 | return((void *)r); |
2e4fb373 | 58 | } |
59 | ||
396c147e | 60 | static struct dirent * |
61 | fudge_readdir(struct SFTP_OPENDIR *od) | |
2e4fb373 | 62 | { |
edbe6722 | 63 | /* Solaris needs sizeof(dirent) + path length (see below) */ |
64 | static char buf[sizeof(struct dirent) + MAXPATHLEN]; | |
65 | struct dirent *ret = (struct dirent *)buf; | |
2e4fb373 | 66 | #ifdef __GNU_LIBRARY__ |
67 | static int inum = 1; | |
68 | #endif /* __GNU_LIBRARY__ */ | |
b6453d99 | 69 | |
2e4fb373 | 70 | if (od->dir[od->offset] == NULL) |
71 | return(NULL); | |
72 | ||
edbe6722 | 73 | memset(buf, 0, sizeof(buf)); |
2e4fb373 | 74 | |
edbe6722 | 75 | /* |
76 | * Solaris defines dirent->d_name as a one byte array and expects | |
77 | * you to hack around it. | |
78 | */ | |
79 | #ifdef BROKEN_ONE_BYTE_DIRENT_D_NAME | |
80 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, MAXPATHLEN); | |
81 | #else | |
cd332296 | 82 | strlcpy(ret->d_name, od->dir[od->offset++]->filename, |
edbe6722 | 83 | sizeof(ret->d_name)); |
84 | #endif | |
2e4fb373 | 85 | #ifdef __GNU_LIBRARY__ |
86 | /* | |
87 | * Idiot glibc uses extensions to struct dirent for readdir with | |
aff51935 | 88 | * ALTDIRFUNCs. Not that this is documented anywhere but the |
2e4fb373 | 89 | * source... Fake an inode number to appease it. |
90 | */ | |
edbe6722 | 91 | ret->d_ino = inum++; |
2e4fb373 | 92 | if (!inum) |
93 | inum = 1; | |
94 | #endif /* __GNU_LIBRARY__ */ | |
95 | ||
edbe6722 | 96 | return(ret); |
2e4fb373 | 97 | } |
98 | ||
396c147e | 99 | static void |
100 | fudge_closedir(struct SFTP_OPENDIR *od) | |
2e4fb373 | 101 | { |
102 | free_sftp_dirents(od->dir); | |
894c5fa6 | 103 | xfree(od); |
2e4fb373 | 104 | } |
105 | ||
396c147e | 106 | static int |
107 | fudge_lstat(const char *path, struct stat *st) | |
2e4fb373 | 108 | { |
109 | Attrib *a; | |
184eed6a | 110 | |
343288b8 | 111 | if (!(a = do_lstat(cur.conn, (char *)path, 0))) |
2e4fb373 | 112 | return(-1); |
184eed6a | 113 | |
2e4fb373 | 114 | attrib_to_stat(a, st); |
184eed6a | 115 | |
2e4fb373 | 116 | return(0); |
117 | } | |
118 | ||
396c147e | 119 | static int |
120 | fudge_stat(const char *path, struct stat *st) | |
2e4fb373 | 121 | { |
122 | Attrib *a; | |
184eed6a | 123 | |
343288b8 | 124 | if (!(a = do_stat(cur.conn, (char *)path, 0))) |
2e4fb373 | 125 | return(-1); |
184eed6a | 126 | |
2e4fb373 | 127 | attrib_to_stat(a, st); |
184eed6a | 128 | |
2e4fb373 | 129 | return(0); |
130 | } | |
131 | ||
132 | int | |
22e6c827 | 133 | remote_glob(struct sftp_conn *conn, const char *pattern, int flags, |
ec1f12d3 | 134 | int (*errfunc)(const char *, int), glob_t *pglob) |
2e4fb373 | 135 | { |
29009047 | 136 | pglob->gl_opendir = fudge_opendir; |
137 | pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; | |
138 | pglob->gl_closedir = (void (*)(void *))fudge_closedir; | |
2e4fb373 | 139 | pglob->gl_lstat = fudge_lstat; |
140 | pglob->gl_stat = fudge_stat; | |
184eed6a | 141 | |
2e4fb373 | 142 | memset(&cur, 0, sizeof(cur)); |
22e6c827 | 143 | cur.conn = conn; |
2e4fb373 | 144 | |
22e6c827 | 145 | return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob)); |
2e4fb373 | 146 | } |