comparison rvinterf/etmsync/fsread.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 46ad66a231af
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * Commands for reading the content of a GSM device's file system
3 */
4
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <sys/stat.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <unistd.h>
13 #include "etm.h"
14 #include "ffs.h"
15 #include "tmffs2.h"
16 #include "limits.h"
17 #include "ffslimits.h"
18 #include "localtypes.h"
19 #include "localstruct.h"
20 #include "exitcodes.h"
21
22 extern char *pathname_for_ffs_child();
23
24 void
25 ll_print_line(pathname, stat)
26 char *pathname;
27 struct stat_info *stat;
28 {
29 char readonly;
30 char rlbuf[256];
31
32 if (stat->flags & OF_READONLY)
33 readonly = 'r';
34 else
35 readonly = ' ';
36 switch (stat->type) {
37 case OT_FILE:
38 printf("f%c %7u %s\n", readonly, stat->size, pathname);
39 return;
40 case OT_DIR:
41 printf("d%c %s\n", readonly, pathname);
42 return;
43 case OT_LINK:
44 if (do_readlink_sancheck(pathname, rlbuf))
45 strcpy(rlbuf, "<invalid>");
46 printf("l%c %s -> %s\n", readonly, pathname, rlbuf);
47 return;
48 default:
49 printf("?%c %s\n", readonly, pathname);
50 }
51 }
52
53 cmd_ll(argc, argv)
54 char **argv;
55 {
56 struct stat_info stat;
57 u_char rdstate[4];
58 char rdbuf[MAX_FN_COMPONENT+1], childpath[MAX_FULL_PATHNAME+1], *childp;
59 int nument, i, rc;
60
61 if (validate_ffs_pathname(argv[1]) < 0)
62 return(ERROR_USAGE); /* err msg already printed */
63 rc = do_xlstat(argv[1], &stat);
64 if (rc)
65 return(rc);
66 if (stat.type != OT_DIR) {
67 ll_print_line(argv[1], &stat);
68 return(0);
69 }
70 rc = do_opendir(argv[1], rdstate, &nument);
71 if (rc)
72 return(rc);
73 if (!nument) {
74 printf("<empty dir>\n");
75 return(0);
76 }
77 childp = pathname_for_ffs_child(argv[1], childpath);
78 if (!childp) {
79 printf("error: non-empty dir at the limit of pathname depth\n");
80 return(ERROR_TARGET);
81 }
82 for (i = 0; i < nument; i++) {
83 rc = do_readdir(rdstate, rdbuf, MAX_FN_COMPONENT+1);
84 if (rc)
85 return(rc);
86 if (index(rdbuf, '/')) {
87 printf("error: readdir result contains a slash\n");
88 return(ERROR_TARGET);
89 }
90 strcpy(childp, rdbuf);
91 rc = do_xlstat(childpath, &stat);
92 if (rc) {
93 printf("xlstat failed on %s\n", childpath);
94 return(rc);
95 }
96 ll_print_line(childpath, &stat);
97 }
98 return(0);
99 }
100
101 void
102 hexdump_line(offset, buf, len)
103 u_char *buf;
104 {
105 int i, c;
106
107 printf("%02X: ", offset);
108 for (i = 0; i < 16; i++) {
109 if (i < len)
110 printf("%02X ", buf[i]);
111 else
112 fputs(" ", stdout);
113 if (i == 7 || i == 15)
114 putchar(' ');
115 }
116 for (i = 0; i < len; i++) {
117 c = buf[i];
118 if (c < ' ' || c > '~')
119 c = '.';
120 putchar(c);
121 }
122 putchar('\n');
123 }
124
125 cmd_hd(argc, argv)
126 char **argv;
127 {
128 u_char databuf[MAX_READ_DATA];
129 int rc, sz, off, l;
130
131 rc = do_file_read(argv[1], databuf, MAX_READ_DATA, &sz);
132 if (rc)
133 return(rc);
134 printf("%d bytes read\n", sz);
135 for (off = 0; off < sz; off += 16) {
136 l = sz - off;
137 if (l > 16)
138 l = 16;
139 hexdump_line(off, databuf + off, l);
140 }
141 return(0);
142 }
143
144 cpout_object(ffspath, hostpath)
145 char *ffspath, *hostpath;
146 {
147 struct stat_info stat;
148 int rc;
149
150 rc = do_xlstat(ffspath, &stat);
151 if (rc)
152 return(rc);
153 switch (stat.type) {
154 case OT_FILE:
155 return cpout_file(ffspath, hostpath);
156 case OT_DIR:
157 return cpout_dir(ffspath, hostpath);
158 case OT_LINK:
159 printf("skipping FFS symlink %s\n", ffspath);
160 return(0);
161 default:
162 printf("error: stat returned bad objtype for %s\n", ffspath);
163 return(ERROR_TARGET);
164 }
165 }
166
167 cpout_file(ffspath, hostpath)
168 char *ffspath, *hostpath;
169 {
170 int tfd;
171 FILE *of;
172 u_char buf[MAX_READ_DATA];
173 int rc, sz;
174
175 printf("copying %s\n", ffspath);
176 rc = fd_open(ffspath, FFS_O_RDONLY, &tfd);
177 if (rc)
178 return(rc);
179 of = fopen(hostpath, "w");
180 if (!of) {
181 perror(hostpath);
182 fd_close(tfd);
183 return(ERROR_UNIX);
184 }
185 for (;;) {
186 rc = fd_read(tfd, buf, MAX_READ_DATA, &sz);
187 if (rc) {
188 fd_close(tfd);
189 fclose(of);
190 return(rc);
191 }
192 if (!sz)
193 break;
194 fwrite(buf, 1, sz, of);
195 }
196 fclose(of);
197 return fd_close(tfd);
198 }
199
200 host_mkdir(pathname)
201 char *pathname;
202 {
203 int rc;
204 struct stat st;
205
206 rc = stat(pathname, &st);
207 if (rc < 0) {
208 rc = mkdir(pathname, 0777);
209 if (rc < 0) {
210 perror(pathname);
211 return(ERROR_UNIX);
212 }
213 return(0);
214 } else {
215 if (S_ISDIR(st.st_mode))
216 return(0);
217 else {
218 fprintf(stderr,
219 "error: %s already exists and is not a directory\n",
220 pathname);
221 return(ERROR_UNIX);
222 }
223 }
224 }
225
226 cpout_dir(ffspath_dir, hostpath_dir)
227 char *ffspath_dir, *hostpath_dir;
228 {
229 u_char rdstate[4];
230 char rdbuf[MAX_FN_COMPONENT+1], ffspath_child[MAX_FULL_PATHNAME+1];
231 char *childp;
232 char hostpath_child[MAXPATHLEN];
233 int nument, i, rc, childerr;
234
235 printf("dir %s\n", ffspath_dir);
236 rc = host_mkdir(hostpath_dir);
237 if (rc)
238 return(rc);
239 rc = do_opendir(ffspath_dir, rdstate, &nument);
240 if (rc)
241 return(rc);
242 if (!nument)
243 return(0);
244 childp = pathname_for_ffs_child(ffspath_dir, ffspath_child);
245 if (!childp) {
246 printf("error: non-empty dir at the limit of pathname depth\n");
247 return(ERROR_TARGET);
248 }
249 childerr = 0;
250 for (i = 0; i < nument; i++) {
251 rc = do_readdir(rdstate, rdbuf, MAX_FN_COMPONENT+1);
252 if (rc)
253 return(rc);
254 if (index(rdbuf, '/')) {
255 printf("error: readdir result contains a slash\n");
256 return(ERROR_TARGET);
257 }
258 strcpy(childp, rdbuf);
259 if (rdbuf[0] == '.') {
260 printf("skipping %s\n", ffspath_child);
261 continue;
262 }
263 if (strlen(hostpath_dir) + strlen(rdbuf) + 2 >
264 sizeof hostpath_child) {
265 fprintf(stderr,
266 "error: host side pathname buffer overflow\n");
267 return(ERROR_UNIX);
268 }
269 sprintf(hostpath_child, "%s/%s", hostpath_dir, rdbuf);
270 rc = cpout_object(ffspath_child, hostpath_child);
271 if (rc && rc != ERROR_TARGET)
272 return(rc);
273 if (rc)
274 childerr = rc;
275 }
276 return(childerr);
277 }
278
279 cmd_cpout(argc, argv)
280 char **argv;
281 {
282 if (validate_ffs_pathname(argv[1]) < 0)
283 return(ERROR_USAGE); /* err msg already printed */
284 return cpout_object(argv[1], argv[2]);
285 }
286
287 cmd_cpout_file(argc, argv)
288 char **argv;
289 {
290 return cpout_file(argv[1], argv[2]);
291 }