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