Skip to content

Commit 68b6a77

Browse files
author
fpr
committed
MassStorage updated to manage several node name on MacOSX. Source file modified to be as close as possible than Linux source. Easier to maintain.
Signed-off-by: fpr <fabien.perroquin@wi6labs.com>
1 parent 82eb13a commit 68b6a77

File tree

2 files changed

+119
-30
lines changed

2 files changed

+119
-30
lines changed

macosx/massStorageCopyMacOsX

4.76 KB
Binary file not shown.

src/massStorageCopy/massStorageCopyMacOsX.c

+119-30
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,156 @@
33
#include <string.h>
44
#include <stdlib.h>
55
#include <sys/mount.h>
6+
#include <getopt.h>
7+
#include <ctype.h>
8+
#include <errno.h>
69

710
#define MAX_FS 128
811

12+
static char *input_path = NULL;
13+
static char *output_path = NULL;
14+
static char *output_dev = NULL;
15+
static char **list_output_dev = NULL;
16+
static char *cmd = NULL;
17+
18+
19+
void usage(char *name)
20+
{
21+
printf("Usage: %s [-I <filepath>] [-O <mountpoint(s)> ]\n\n", name);
22+
printf("Mandatory options:\n");
23+
printf("\t-I: filepath binary to copy\n");
24+
printf("\t-O: mountpoint(s) destination name.\n");
25+
printf("\t Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\"\n");
26+
}
27+
28+
29+
void free_ressource()
30+
{
31+
if(input_path)
32+
free(input_path);
33+
if(output_path)
34+
free(output_path);
35+
if(output_dev)
36+
free(output_dev);
37+
if(list_output_dev)
38+
free(list_output_dev);
39+
if(cmd)
40+
free(cmd);
41+
}
42+
943
int main(int argc, char *argv[])
1044
{
11-
int i;
45+
int c, i, n;
1246
int ret = 0;
1347
int device_found = 0;
14-
char input_path[256];
15-
char output_dev[256];
16-
char output_path[256];
17-
char cmd[512];
1848
struct statfs buf[MAX_FS];
1949
int fs_count;
50+
char *p = NULL;
51+
int n_output_dev = 0;
52+
char scp_cmd[]="scp";
2053

21-
if(argc < 4) {
22-
printf("error: missing parameters\n");
23-
ret = -1;
24-
}
25-
26-
for(i = 1; i < argc; i++) {
54+
opterr = 0;
2755

28-
if((strcmp(argv[i], "-I") == 0)&&(i+1 < argc)) {
29-
strcpy(input_path, argv[i+1]);
30-
i++;
31-
} else if((strcmp(argv[i], "-O") == 0)&&(i+1 < argc)) {
32-
strcpy(output_dev, argv[i+1]);
33-
i++;
34-
} else {
35-
printf("error: unknown option %s\n", argv[i]);
36-
ret = -1;
56+
while ((c = getopt (argc, argv, "I:O:")) != -1) {
57+
switch (c)
58+
{
59+
case 'I':
60+
input_path = malloc(strlen(optarg)+1);
61+
if(input_path != NULL)
62+
strcpy(input_path, optarg);
63+
break;
64+
case 'O':
65+
output_dev = malloc(strlen(optarg)+1);
66+
if(output_dev != NULL)
67+
strcpy(output_dev, optarg);
68+
break;
69+
case '?':
70+
if ((optopt == 'I') || (optopt == 'O'))
71+
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
72+
else if (isprint (optopt))
73+
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
74+
else
75+
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
76+
usage(argv[0]);
77+
free_ressource();
78+
return EINVAL;
79+
default:
80+
abort ();
3781
}
3882
}
3983

40-
if(ret == 0) {
84+
if((input_path == NULL) || (output_dev == NULL))
85+
{
86+
free_ressource();
87+
exit(ENOMEM);
88+
}
4189

90+
if(strlen(input_path) && strlen(output_dev))
91+
{
92+
/* get the mounted devices list */
4293
fs_count = getfsstat(NULL,0,MNT_WAIT);
4394
if(fs_count < 0) {
4495
perror("getfsstat");
45-
exit(1);
96+
free_ressource();
97+
exit(ENOENT);
4698
}
4799

48100
getfsstat(buf,fs_count*sizeof(buf[0]),MNT_WAIT);
49101

50-
for(i = 0; i < fs_count; i++) {
51-
if(strstr(buf[i].f_mntonname,output_dev)) {
52-
sprintf(output_path, "%s", buf[i].f_mntonname);
53-
device_found = 1;
102+
/* " must be removed too */
103+
p = strtok (output_dev, ",\"");
104+
105+
/* split output_dev and append tokens to list_output_dev */
106+
while (p) {
107+
list_output_dev = realloc (list_output_dev, sizeof (char*) * ++n_output_dev);
108+
109+
if (list_output_dev == NULL)
110+
exit (ENOMEM);
111+
112+
list_output_dev[n_output_dev-1] = p;
113+
114+
p = strtok (NULL, ",\"");
115+
}
116+
117+
/* realloc one extra element for the last NULL */
118+
list_output_dev = realloc (list_output_dev, sizeof (char*) * (n_output_dev+1));
119+
list_output_dev[n_output_dev] = 0;
120+
121+
for(n = 0; (n < fs_count) && (!device_found); n++) {
122+
for(i = 0; (i < n_output_dev) && (!device_found); i++) {
123+
if(strstr(buf[n].f_mntonname,list_output_dev[i])) {
124+
output_path = malloc(strlen(buf[n].f_mntonname)+1);
125+
if(output_path != NULL) {
126+
sprintf(output_path, "%s", buf[n].f_mntonname);
127+
} else {
128+
free_ressource();
129+
exit(ENOMEM);
130+
}
131+
device_found = 1;
132+
}
54133
}
55134
}
56135

57136
if(device_found) {
58137
printf("copying %s to %s\n", input_path, output_path);
59-
sprintf(cmd, "scp %s %s", input_path, output_path);
60-
system(cmd);
138+
cmd = malloc(strlen(scp_cmd)+1+strlen(input_path)+1+strlen(output_path)+1);
139+
if(cmd != NULL) {
140+
sprintf(cmd, "%s %s %s", scp_cmd, input_path, output_path);
141+
} else {
142+
free_ressource();
143+
exit(ENOMEM);
144+
}
145+
ret = system(cmd);
61146
} else {
62-
printf("%s not found. please ensure the device is correctly connected\n",
147+
printf("%s not found. Please ensure the device is correctly connected\n",
63148
output_dev);
64-
ret = -1;
149+
ret = ENODEV;
65150
}
151+
} else {
152+
printf("Missing argument\n");
153+
usage(argv[0]);
66154
}
67155

156+
free_ressource();
68157
return ret;
69158
}

0 commit comments

Comments
 (0)