|
3 | 3 | #include <string.h>
|
4 | 4 | #include <stdlib.h>
|
5 | 5 | #include <sys/mount.h>
|
| 6 | +#include <getopt.h> |
| 7 | +#include <ctype.h> |
| 8 | +#include <errno.h> |
6 | 9 |
|
7 | 10 | #define MAX_FS 128
|
8 | 11 |
|
| 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 | + |
9 | 43 | int main(int argc, char *argv[])
|
10 | 44 | {
|
11 |
| - int i; |
| 45 | + int c, i, n; |
12 | 46 | int ret = 0;
|
13 | 47 | int device_found = 0;
|
14 |
| - char input_path[256]; |
15 |
| - char output_dev[256]; |
16 |
| - char output_path[256]; |
17 |
| - char cmd[512]; |
18 | 48 | struct statfs buf[MAX_FS];
|
19 | 49 | int fs_count;
|
| 50 | + char *p = NULL; |
| 51 | + int n_output_dev = 0; |
| 52 | + char scp_cmd[]="scp"; |
20 | 53 |
|
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; |
27 | 55 |
|
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 (); |
37 | 81 | }
|
38 | 82 | }
|
39 | 83 |
|
40 |
| - if(ret == 0) { |
| 84 | + if((input_path == NULL) || (output_dev == NULL)) |
| 85 | + { |
| 86 | + free_ressource(); |
| 87 | + exit(ENOMEM); |
| 88 | + } |
41 | 89 |
|
| 90 | + if(strlen(input_path) && strlen(output_dev)) |
| 91 | + { |
| 92 | + /* get the mounted devices list */ |
42 | 93 | fs_count = getfsstat(NULL,0,MNT_WAIT);
|
43 | 94 | if(fs_count < 0) {
|
44 | 95 | perror("getfsstat");
|
45 |
| - exit(1); |
| 96 | + free_ressource(); |
| 97 | + exit(ENOENT); |
46 | 98 | }
|
47 | 99 |
|
48 | 100 | getfsstat(buf,fs_count*sizeof(buf[0]),MNT_WAIT);
|
49 | 101 |
|
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 | + } |
54 | 133 | }
|
55 | 134 | }
|
56 | 135 |
|
57 | 136 | if(device_found) {
|
58 | 137 | 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); |
61 | 146 | } 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", |
63 | 148 | output_dev);
|
64 |
| - ret = -1; |
| 149 | + ret = ENODEV; |
65 | 150 | }
|
| 151 | + } else { |
| 152 | + printf("Missing argument\n"); |
| 153 | + usage(argv[0]); |
66 | 154 | }
|
67 | 155 |
|
| 156 | + free_ressource(); |
68 | 157 | return ret;
|
69 | 158 | }
|
0 commit comments