Skip to content

Commit 5e8c676

Browse files
authored
Merge pull request #8 from stm32duino/issue_6
MassStorageCopy updated to manage several node names.
2 parents 1ba6db2 + e82d0f2 commit 5e8c676

File tree

7 files changed

+248
-73
lines changed

7 files changed

+248
-73
lines changed

linux/massStorageCopy

4.37 KB
Binary file not shown.

linux64/massStorageCopy

506 Bytes
Binary file not shown.

macosx/massStorageCopyMacOsX

4.76 KB
Binary file not shown.

src/massStorageCopy/massStorageCopy.bat

-9
This file was deleted.

src/massStorageCopy/massStorageCopy.c

+90-25
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,65 @@
44
#include <stdlib.h>
55
#include <mntent.h>
66
#include <unistd.h>
7+
#include <errno.h>
8+
#include <ctype.h>
9+
10+
static char *input_path = NULL;
11+
static char *output_path = NULL;
12+
static char *output_dev = NULL;
13+
static char **list_output_dev = NULL;
14+
static char *cmd = NULL;
15+
static FILE *aFile = NULL;
716

817
void usage(char *name)
918
{
10-
printf("Usage: %s [-I <filepath>] [-O <mountpoint> ]\n\n", name);
19+
printf("Usage: %s [-I <filepath>] [-O <mountpoint(s)> ]\n\n", name);
1120
printf("Mandatory options:\n");
1221
printf("\t-I: filepath binary to copy\n");
13-
printf("\t-O: mountpoint destination name\n");
22+
printf("\t-O: mountpoint(s) destination name.\n");
23+
printf("\t Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\"\n");
24+
}
25+
26+
void free_ressource()
27+
{
28+
if(input_path)
29+
free(input_path);
30+
if(output_path)
31+
free(output_path);
32+
if(output_dev)
33+
free(output_dev);
34+
if(list_output_dev)
35+
free(list_output_dev);
36+
if(cmd)
37+
free(cmd);
38+
if(aFile)
39+
endmntent(aFile);
1440
}
1541

1642
int main(int argc, char *argv[])
1743
{
1844
int c, i;
1945
int ret = 0;
2046
int device_found = 0;
21-
char input_path[256] = "";
22-
char output_dev[256] = "";
23-
char output_path[256] = "";
24-
char cmd[512] = "";
2547
struct mntent *ent = NULL;
26-
FILE *aFile = NULL;
48+
char *p = NULL;
49+
int n_output_dev = 0;
50+
char scp_cmd[]="scp";
2751

2852
opterr = 0;
2953

3054
while ((c = getopt (argc, argv, "I:O:")) != -1)
3155
switch (c)
3256
{
3357
case 'I':
34-
strcpy(input_path, optarg);
58+
input_path = malloc(strlen(optarg)+1);
59+
if(input_path != NULL)
60+
strcpy(input_path, optarg);
3561
break;
3662
case 'O':
37-
strcpy(output_dev, optarg);
63+
output_dev = malloc(strlen(optarg)+1);
64+
if(output_dev != NULL)
65+
strcpy(output_dev, optarg);
3866
break;
3967
case '?':
4068
if ((optopt == 'I') || (optopt == 'O'))
@@ -46,47 +74,84 @@ int main(int argc, char *argv[])
4674
"Unknown option character `\\x%x'.\n",
4775
optopt);
4876
usage(argv[0]);
49-
return 1;
77+
free_ressource();
78+
return EINVAL;
5079
default:
5180
abort ();
5281
}
5382

83+
if((input_path == NULL) || (output_dev == NULL))
84+
{
85+
free_ressource();
86+
exit(ENOMEM);
87+
}
88+
5489
if (strlen(input_path) && strlen(output_dev))
5590
{
56-
//get the mounted devives list
91+
/* get the mounted devices list */
5792
aFile = setmntent("/proc/mounts", "r");
5893
if (aFile == NULL) {
5994
perror("setmntent");
60-
exit(1);
95+
free_ressource();
96+
exit(ENOENT);
6197
}
6298

63-
//now lets read the path of the device
64-
while (NULL != (ent = getmntent(aFile))) {
65-
if (strstr(ent->mnt_dir, output_dev)) {
66-
sprintf(output_path, "%s", ent->mnt_dir);
67-
device_found = 1;
68-
}
99+
p = strtok (output_dev, ",");
100+
101+
/* split output_dev and append tokens to list_output_dev */
102+
while (p) {
103+
list_output_dev = realloc (list_output_dev, sizeof (char*) * ++n_output_dev);
104+
105+
if (list_output_dev == NULL)
106+
exit (ENOMEM);
107+
108+
list_output_dev[n_output_dev-1] = p;
109+
110+
p = strtok (NULL, ",");
69111
}
70112

71-
endmntent(aFile);
113+
/* realloc one extra element for the last NULL */
114+
list_output_dev = realloc (list_output_dev, sizeof (char*) * (n_output_dev+1));
115+
list_output_dev[n_output_dev] = 0;
116+
117+
/* now lets read the path of the device */
118+
while ((NULL != (ent = getmntent(aFile))) && (!device_found)) {
119+
for (i = 0; (i < (n_output_dev)) && (!device_found); ++i) {
120+
if (strstr(ent->mnt_dir, list_output_dev[i])) {
121+
output_path = malloc(strlen(ent->mnt_dir)+1);
122+
if(output_path != NULL) {
123+
sprintf(output_path, "%s", ent->mnt_dir);
124+
} else {
125+
free_ressource();
126+
exit(ENOMEM);
127+
}
128+
device_found = 1;
129+
}
130+
}
131+
}
72132

73133
if(device_found) {
74134
printf("copying %s to %s\n", input_path, output_path);
75-
76-
sprintf(cmd, "scp %s %s", input_path, output_path);
77-
system(cmd);
135+
cmd = malloc(strlen(scp_cmd)+1+strlen(input_path)+1+strlen(output_path)+1);
136+
if(cmd != NULL) {
137+
sprintf(cmd, "%s %s %s", scp_cmd, input_path, output_path);
138+
} else {
139+
free_ressource();
140+
exit(ENOMEM);
141+
}
142+
ret = system(cmd);
78143

79144
} else {
80-
printf("%s not found. please ensure the device is correctly connected\n",
145+
printf("%s not found. Please ensure the device is correctly connected\n",
81146
output_dev);
82-
ret = -1;
147+
ret = ENODEV;
83148
}
84149
}
85150
else
86151
{
87152
printf("Missing argument\n");
88153
usage(argv[0]);
89154
}
90-
155+
free_ressource();
91156
return ret;
92157
}

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)