comparison rvinterf/etmsync/fsupload.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
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * upload-fs implementation
3 */
4
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <sys/stat.h>
8 #include <dirent.h>
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include "etm.h"
15 #include "ffs.h"
16 #include "ffserr.h"
17 #include "tmffs2.h"
18 #include "limits.h"
19 #include "ffslimits.h"
20 #include "localtypes.h"
21 #include "localstruct.h"
22 #include "exitcodes.h"
23
24 uploadfs_level(srcpath, depth, prefix)
25 char *srcpath, *prefix;
26 {
27 char ffs_childpath[MAX_FULL_PATHNAME+1], *ffs_childp;
28 DIR *rdd;
29 struct dirent *dirent;
30 char hostpath_child[MAXPATHLEN];
31 struct stat hst;
32 int rc;
33
34 strcpy(ffs_childpath, prefix);
35 ffs_childp = index(ffs_childpath, '\0');
36 *ffs_childp++ = '/';
37 rdd = opendir(srcpath);
38 if (!rdd) {
39 perror(srcpath);
40 return(ERROR_UNIX);
41 }
42 while (dirent = readdir(rdd)) {
43 if (dirent->d_name[0] == '.')
44 continue;
45 if (strlen(dirent->d_name) > MAX_FN_COMPONENT) {
46 fprintf(stderr,
47 "error: \"%s\" in %s exceeds the FFS component name limit\n",
48 dirent->d_name, srcpath);
49 closedir(rdd);
50 return(ERROR_USAGE);
51 }
52 if (strlen(srcpath) + strlen(dirent->d_name) + 2 >
53 sizeof hostpath_child) {
54 fprintf(stderr,
55 "error: host side pathname buffer overflow\n");
56 closedir(rdd);
57 return(ERROR_UNIX);
58 }
59 sprintf(hostpath_child, "%s/%s", srcpath, dirent->d_name);
60 if (lstat(hostpath_child, &hst) < 0) {
61 perror(hostpath_child);
62 closedir(rdd);
63 return(ERROR_UNIX);
64 }
65 strcpy(ffs_childp, dirent->d_name);
66 switch (hst.st_mode & S_IFMT) {
67 case S_IFREG:
68 printf("uploading %s\n", ffs_childpath);
69 rc = fwrite_from_file(ffs_childpath, hostpath_child);
70 if (rc) {
71 closedir(rdd);
72 return(rc);
73 }
74 break;
75 case S_IFDIR:
76 if (depth >= MAX_NAME_DEPTH-1) {
77 fprintf(stderr,
78 "error: directory nesting too deep at %s\n",
79 hostpath_child);
80 closedir(rdd);
81 return(ERROR_USAGE);
82 }
83 printf("mkdir %s\n", ffs_childpath);
84 rc = do_mkdir_existok(ffs_childpath);
85 if (rc) {
86 closedir(rdd);
87 return(rc);
88 }
89 rc = uploadfs_level(hostpath_child, depth + 1,
90 ffs_childpath);
91 if (rc) {
92 closedir(rdd);
93 return(rc);
94 }
95 break;
96 default:
97 fprintf(stderr,
98 "error: %s is neither a regular file nor a directory\n",
99 hostpath_child);
100 closedir(rdd);
101 return(ERROR_USAGE);
102 }
103 }
104 closedir(rdd);
105 return(0);
106 }
107
108 cmd_uploadfs(argc, argv)
109 char **argv;
110 {
111 return uploadfs_level(argv[1], 0, "");
112 }
113
114 cmd_upload_file(argc, argv)
115 char **argv;
116 {
117 if (strlen(argv[2]) >= TMFFS_STRING_SIZE) {
118 fprintf(stderr,
119 "error: pathname arg exceeds string length limit\n");
120 return(ERROR_USAGE);
121 }
122 return fwrite_from_file(argv[2], argv[1]);
123 }
124
125 cmd_upload_subtree(argc, argv)
126 char **argv;
127 {
128 int rc, depth;
129
130 depth = validate_ffs_pathname(argv[2]);
131 if (depth < 0)
132 return(ERROR_USAGE); /* error msg already printed */
133 if (depth == 0) {
134 fprintf(stderr, "please use upload-fs command instead\n");
135 return(ERROR_USAGE);
136 }
137 if (depth >= MAX_NAME_DEPTH) {
138 fprintf(stderr, "cannot upload into max-depth directory\n");
139 return(ERROR_USAGE);
140 }
141 printf("mkdir %s\n", argv[2]);
142 rc = do_mkdir_existok(argv[2]);
143 if (rc)
144 return(rc);
145 return uploadfs_level(argv[1], depth, argv[2]);
146 }