1 |
|
|
/* $OpenBSD: sftp-glob.c,v 1.27 2015/01/14 13:54:13 djm Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
4 |
|
|
* |
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. |
8 |
|
|
* |
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. |
16 |
|
|
*/ |
17 |
|
|
|
18 |
|
|
#include <sys/types.h> |
19 |
|
|
#include <sys/stat.h> |
20 |
|
|
|
21 |
|
|
#include <dirent.h> |
22 |
|
|
#include <glob.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
|
27 |
|
|
#include "xmalloc.h" |
28 |
|
|
#include "sftp.h" |
29 |
|
|
#include "sftp-common.h" |
30 |
|
|
#include "sftp-client.h" |
31 |
|
|
|
32 |
|
|
int remote_glob(struct sftp_conn *, const char *, int, |
33 |
|
|
int (*)(const char *, int), glob_t *); |
34 |
|
|
|
35 |
|
|
struct SFTP_OPENDIR { |
36 |
|
|
SFTP_DIRENT **dir; |
37 |
|
|
int offset; |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
static struct { |
41 |
|
|
struct sftp_conn *conn; |
42 |
|
|
} cur; |
43 |
|
|
|
44 |
|
|
static void * |
45 |
|
|
fudge_opendir(const char *path) |
46 |
|
|
{ |
47 |
|
|
struct SFTP_OPENDIR *r; |
48 |
|
|
|
49 |
|
|
r = xcalloc(1, sizeof(*r)); |
50 |
|
|
|
51 |
|
|
if (do_readdir(cur.conn, (char *)path, &r->dir)) { |
52 |
|
|
free(r); |
53 |
|
|
return(NULL); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
r->offset = 0; |
57 |
|
|
|
58 |
|
|
return((void *)r); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
static struct dirent * |
62 |
|
|
fudge_readdir(struct SFTP_OPENDIR *od) |
63 |
|
|
{ |
64 |
|
|
static struct dirent ret; |
65 |
|
|
|
66 |
|
|
if (od->dir[od->offset] == NULL) |
67 |
|
|
return(NULL); |
68 |
|
|
|
69 |
|
|
memset(&ret, 0, sizeof(ret)); |
70 |
|
|
strlcpy(ret.d_name, od->dir[od->offset++]->filename, |
71 |
|
|
sizeof(ret.d_name)); |
72 |
|
|
|
73 |
|
|
return(&ret); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
static void |
77 |
|
|
fudge_closedir(struct SFTP_OPENDIR *od) |
78 |
|
|
{ |
79 |
|
|
free_sftp_dirents(od->dir); |
80 |
|
|
free(od); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static int |
84 |
|
|
fudge_lstat(const char *path, struct stat *st) |
85 |
|
|
{ |
86 |
|
|
Attrib *a; |
87 |
|
|
|
88 |
|
|
if (!(a = do_lstat(cur.conn, (char *)path, 1))) |
89 |
|
|
return(-1); |
90 |
|
|
|
91 |
|
|
attrib_to_stat(a, st); |
92 |
|
|
|
93 |
|
|
return(0); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
static int |
97 |
|
|
fudge_stat(const char *path, struct stat *st) |
98 |
|
|
{ |
99 |
|
|
Attrib *a; |
100 |
|
|
|
101 |
|
|
if (!(a = do_stat(cur.conn, (char *)path, 1))) |
102 |
|
|
return(-1); |
103 |
|
|
|
104 |
|
|
attrib_to_stat(a, st); |
105 |
|
|
|
106 |
|
|
return(0); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
int |
110 |
|
|
remote_glob(struct sftp_conn *conn, const char *pattern, int flags, |
111 |
|
|
int (*errfunc)(const char *, int), glob_t *pglob) |
112 |
|
|
{ |
113 |
|
|
pglob->gl_opendir = fudge_opendir; |
114 |
|
|
pglob->gl_readdir = (struct dirent *(*)(void *))fudge_readdir; |
115 |
|
|
pglob->gl_closedir = (void (*)(void *))fudge_closedir; |
116 |
|
|
pglob->gl_lstat = fudge_lstat; |
117 |
|
|
pglob->gl_stat = fudge_stat; |
118 |
|
|
|
119 |
|
|
memset(&cur, 0, sizeof(cur)); |
120 |
|
|
cur.conn = conn; |
121 |
|
|
|
122 |
|
|
return(glob(pattern, flags | GLOB_ALTDIRFUNC, errfunc, pglob)); |
123 |
|
|
} |