]> andersk Git - openssh.git/blame - sftp-glob.c
- djm@cvs.openbsd.org 2006/03/25 01:13:23
[openssh.git] / sftp-glob.c
CommitLineData
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"
4095f623 18
19#include <sys/types.h>
20#ifdef HAVE_SYS_STAT_H
21# include <sys/stat.h>
22#endif
68e39d38 23
24#include <dirent.h>
2e4fb373 25
2e4fb373 26#include "buffer.h"
27#include "bufaux.h"
2e4fb373 28#include "xmalloc.h"
29#include "log.h"
2e4fb373 30
31#include "sftp.h"
32#include "sftp-common.h"
33#include "sftp-client.h"
caf1e9f0 34
35int remote_glob(struct sftp_conn *, const char *, int,
36 int (*)(const char *, int), glob_t *);
2e4fb373 37
38struct SFTP_OPENDIR {
39 SFTP_DIRENT **dir;
40 int offset;
41};
42
43static struct {
22e6c827 44 struct sftp_conn *conn;
2e4fb373 45} cur;
46
396c147e 47static void *
48fudge_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 64static struct dirent *
65fudge_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 103static void
104fudge_closedir(struct SFTP_OPENDIR *od)
2e4fb373 105{
106 free_sftp_dirents(od->dir);
894c5fa6 107 xfree(od);
2e4fb373 108}
109
396c147e 110static int
111fudge_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 123static int
124fudge_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
136int
22e6c827 137remote_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}
This page took 0.241808 seconds and 5 git commands to generate.