author | David Powell <David.Powell@sun.com> |
Wed, 16 Dec 2009 19:06:12 -0800 | |
changeset 401 | fc1223edbd8d |
parent 391 | 71abce159a62 |
child 500 | d134d555588f |
permissions | -rw-r--r-- |
391 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
23 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
|
24 |
* Use is subject to license terms. |
|
25 |
*/ |
|
26 |
||
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
27 |
/* |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
28 |
* Unfortunately, the behavior this module has inherited is not one |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
29 |
* of "get me information about file X or directory Y", but "tell me |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
30 |
* what calling 'new File(X)' would do in Java". This includes |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
31 |
* nonsense like manufacturing correct fake data for nonexistent |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
32 |
* files. |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
33 |
* |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
34 |
* This sorely needs to be ripped out and replaced with a sane |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
35 |
* interface. |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
36 |
*/ |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
37 |
|
391 | 38 |
#include <sys/stat.h> |
39 |
#include <sys/types.h> |
|
40 |
#include <string.h> |
|
41 |
#include <stdio.h> |
|
42 |
#include <stdlib.h> |
|
43 |
#include <unistd.h> |
|
44 |
#include <time.h> |
|
45 |
#include <dirent.h> |
|
46 |
#include <libgen.h> |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
47 |
#include <limits.h> |
391 | 48 |
|
49 |
#include "rad_adr.h" |
|
50 |
#include "rad_object.h" |
|
51 |
#include "rad_modapi.h" |
|
52 |
||
53 |
#include "api_file.h" |
|
54 |
||
55 |
static data_t * |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
56 |
empty_file(data_t *data, const char *apath, const char *cpath) |
391 | 57 |
{ |
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
58 |
struct_set(data, "absolutePath", data_new_string(apath, lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
59 |
struct_set(data, "canonicalPath", data_new_string(cpath, lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
60 |
struct_set(data, "canonical", data_new_boolean(B_TRUE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
61 |
struct_set(data, "baseName", data_new_string("", lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
62 |
struct_set(data, "exists", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
63 |
struct_set(data, "readable", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
64 |
struct_set(data, "writable", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
65 |
struct_set(data, "hidden", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
66 |
struct_set(data, "directory", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
67 |
struct_set(data, "file", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
68 |
struct_set(data, "lastModified", data_new_time(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
69 |
struct_set(data, "length", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
70 |
struct_set(data, "freeSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
71 |
struct_set(data, "totalSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
72 |
struct_set(data, "usableSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
73 |
return (data_purify(data)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
74 |
} |
391 | 75 |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
76 |
static data_t * |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
77 |
read_file(const char *path, const char *file) |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
78 |
{ |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
79 |
struct stat64 st; |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
80 |
char apath[PATH_MAX] = ""; |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
81 |
char cpath[PATH_MAX] = ""; |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
82 |
const char *name = file != NULL ? file : path; |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
83 |
|
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
84 |
data_t *data = data_new_struct(&t__FileSnapshot); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
85 |
struct_set(data, "path", data_new_string(name, lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
86 |
struct_set(data, "absolute", data_new_boolean(name[0] == '/')); |
391 | 87 |
|
88 |
if (path[0] != '/') { |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
89 |
if (getcwd(apath, PATH_MAX) == NULL) { |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
90 |
data_free(data); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
91 |
return (NULL); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
92 |
} |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
93 |
if (apath[1] != '\0') |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
94 |
strlcat(apath, "/", PATH_MAX); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
95 |
strlcat(apath, path, PATH_MAX); |
391 | 96 |
} else { |
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
97 |
strlcpy(apath, path, PATH_MAX); |
391 | 98 |
} |
99 |
||
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
100 |
if (resolvepath(apath, cpath, PATH_MAX) == -1 || |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
101 |
stat64(path, &st) == -1) |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
102 |
return (empty_file(data, apath, apath)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
103 |
|
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
104 |
struct_set(data, "absolutePath", data_new_string(apath, lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
105 |
struct_set(data, "canonicalPath", data_new_string(cpath, lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
106 |
struct_set(data, "canonical", |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
107 |
data_new_boolean(strcmp(apath, cpath) == 0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
108 |
struct_set(data, "baseName", data_new_string(basename(apath), lt_copy)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
109 |
struct_set(data, "exists", data_new_boolean(B_TRUE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
110 |
struct_set(data, "readable", data_new_boolean(access(path, R_OK) == 0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
111 |
struct_set(data, "writable", data_new_boolean(access(path, W_OK) == 0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
112 |
struct_set(data, "hidden", data_new_boolean(B_FALSE)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
113 |
struct_set(data, "directory", data_new_boolean(S_ISDIR(st.st_mode))); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
114 |
struct_set(data, "file", data_new_boolean(S_ISREG(st.st_mode))); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
115 |
struct_set(data, "lastModified", data_new_time(st.st_mtime)); |
391 | 116 |
/* XXX: 64-bitify */ |
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
117 |
struct_set(data, "length", data_new_long(st.st_size)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
118 |
struct_set(data, "freeSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
119 |
struct_set(data, "totalSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
120 |
struct_set(data, "usableSpace", data_new_long(0)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
121 |
return (data_purify(data)); |
391 | 122 |
} |
123 |
||
124 |
/* ARGSUSED */ |
|
125 |
conerr_t |
|
126 |
api_fileBrowser_read_Roots(struct instance *inst, struct attribute *attr, |
|
127 |
data_t **data, data_t **error) |
|
128 |
{ |
|
129 |
data_t *result = data_new_array(&t_array__FileSnapshot, 1); |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
130 |
array_add(result, read_file("/", NULL)); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
131 |
*data = data_purify(result); |
391 | 132 |
return (ce_ok); |
133 |
} |
|
134 |
||
135 |
/* ARGSUSED */ |
|
136 |
conerr_t |
|
137 |
api_fileBrowser_invoke_getFile(struct instance *inst, struct method *meth, |
|
138 |
data_t **ret, data_t **args, int count, data_t **error) |
|
139 |
{ |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
140 |
*ret = read_file(args[0]->d_data.string, NULL); |
391 | 141 |
return (ce_ok); |
142 |
} |
|
143 |
||
144 |
/* ARGSUSED */ |
|
145 |
conerr_t |
|
146 |
api_fileBrowser_invoke_getFiles(struct instance *inst, struct method *meth, |
|
147 |
data_t **ret, data_t **args, int count, data_t **error) |
|
148 |
{ |
|
149 |
const char *path = args[0]->d_data.string; |
|
150 |
DIR *d = opendir(path); |
|
151 |
struct dirent *ent; |
|
152 |
data_t *result; |
|
153 |
||
154 |
if (d == NULL) |
|
155 |
return (ce_object); |
|
156 |
||
157 |
result = data_new_array(&t_array__FileSnapshot, 1); |
|
158 |
while ((ent = readdir(d)) != NULL) { |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
159 |
char buf[PATH_MAX]; |
391 | 160 |
if (strcmp(ent->d_name, ".") == 0 || |
161 |
strcmp(ent->d_name, "..") == 0) |
|
162 |
continue; |
|
163 |
int len = strlen(path); |
|
164 |
while (len > 0 && path[len - 1] == '/') |
|
165 |
len--; |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
166 |
snprintf(buf, PATH_MAX, "%.*s/%s", len, path, ent->d_name); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
167 |
data_t *file = read_file(buf, NULL); |
391 | 168 |
if (file != NULL) |
169 |
array_add(result, file); |
|
170 |
} |
|
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
171 |
(void) closedir(d); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
172 |
*ret = data_purify(result); |
391 | 173 |
|
174 |
return (ce_ok); |
|
175 |
} |
|
176 |
||
177 |
static rad_modinfo_t modinfo = { "files", "File Browser module" }; |
|
178 |
||
179 |
int |
|
180 |
_rad_init(void *handle) |
|
181 |
{ |
|
182 |
if (rad_module_register(handle, RAD_MODVERSION, &modinfo) == -1) |
|
183 |
return (-1); |
|
184 |
||
185 |
if (rad_isproxy) |
|
186 |
return (0); |
|
187 |
||
188 |
(void) cont_insert_singleton(&rad_container, |
|
189 |
"org.opensolaris.os.vp.filebrowser:type=Files", |
|
190 |
&api_fileBrowser, NULL); |
|
191 |
||
192 |
return (0); |
|
193 |
} |