Skip to content

MassStorageCopy updated to manage several node names. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified linux/massStorageCopy
Binary file not shown.
Binary file modified linux64/massStorageCopy
Binary file not shown.
Binary file modified macosx/massStorageCopyMacOsX
Binary file not shown.
9 changes: 0 additions & 9 deletions src/massStorageCopy/massStorageCopy.bat

This file was deleted.

115 changes: 90 additions & 25 deletions src/massStorageCopy/massStorageCopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,65 @@
#include <stdlib.h>
#include <mntent.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>

static char *input_path = NULL;
static char *output_path = NULL;
static char *output_dev = NULL;
static char **list_output_dev = NULL;
static char *cmd = NULL;
static FILE *aFile = NULL;

void usage(char *name)
{
printf("Usage: %s [-I <filepath>] [-O <mountpoint> ]\n\n", name);
printf("Usage: %s [-I <filepath>] [-O <mountpoint(s)> ]\n\n", name);
printf("Mandatory options:\n");
printf("\t-I: filepath binary to copy\n");
printf("\t-O: mountpoint destination name\n");
printf("\t-O: mountpoint(s) destination name.\n");
printf("\t Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\"\n");
}

void free_ressource()
{
if(input_path)
free(input_path);
if(output_path)
free(output_path);
if(output_dev)
free(output_dev);
if(list_output_dev)
free(list_output_dev);
if(cmd)
free(cmd);
if(aFile)
endmntent(aFile);
}

int main(int argc, char *argv[])
{
int c, i;
int ret = 0;
int device_found = 0;
char input_path[256] = "";
char output_dev[256] = "";
char output_path[256] = "";
char cmd[512] = "";
struct mntent *ent = NULL;
FILE *aFile = NULL;
char *p = NULL;
int n_output_dev = 0;
char scp_cmd[]="scp";

opterr = 0;

while ((c = getopt (argc, argv, "I:O:")) != -1)
switch (c)
{
case 'I':
strcpy(input_path, optarg);
input_path = malloc(strlen(optarg)+1);
if(input_path != NULL)
strcpy(input_path, optarg);
break;
case 'O':
strcpy(output_dev, optarg);
output_dev = malloc(strlen(optarg)+1);
if(output_dev != NULL)
strcpy(output_dev, optarg);
break;
case '?':
if ((optopt == 'I') || (optopt == 'O'))
Expand All @@ -46,47 +74,84 @@ int main(int argc, char *argv[])
"Unknown option character `\\x%x'.\n",
optopt);
usage(argv[0]);
return 1;
free_ressource();
return EINVAL;
default:
abort ();
}

if((input_path == NULL) || (output_dev == NULL))
{
free_ressource();
exit(ENOMEM);
}

if (strlen(input_path) && strlen(output_dev))
{
//get the mounted devives list
/* get the mounted devices list */
aFile = setmntent("/proc/mounts", "r");
if (aFile == NULL) {
perror("setmntent");
exit(1);
free_ressource();
exit(ENOENT);
}

//now lets read the path of the device
while (NULL != (ent = getmntent(aFile))) {
if (strstr(ent->mnt_dir, output_dev)) {
sprintf(output_path, "%s", ent->mnt_dir);
device_found = 1;
}
p = strtok (output_dev, ",");

/* split output_dev and append tokens to list_output_dev */
while (p) {
list_output_dev = realloc (list_output_dev, sizeof (char*) * ++n_output_dev);

if (list_output_dev == NULL)
exit (ENOMEM);

list_output_dev[n_output_dev-1] = p;

p = strtok (NULL, ",");
}

endmntent(aFile);
/* realloc one extra element for the last NULL */
list_output_dev = realloc (list_output_dev, sizeof (char*) * (n_output_dev+1));
list_output_dev[n_output_dev] = 0;

/* now lets read the path of the device */
while ((NULL != (ent = getmntent(aFile))) && (!device_found)) {
for (i = 0; (i < (n_output_dev)) && (!device_found); ++i) {
if (strstr(ent->mnt_dir, list_output_dev[i])) {
output_path = malloc(strlen(ent->mnt_dir)+1);
if(output_path != NULL) {
sprintf(output_path, "%s", ent->mnt_dir);
} else {
free_ressource();
exit(ENOMEM);
}
device_found = 1;
}
}
}

if(device_found) {
printf("copying %s to %s\n", input_path, output_path);

sprintf(cmd, "scp %s %s", input_path, output_path);
system(cmd);
cmd = malloc(strlen(scp_cmd)+1+strlen(input_path)+1+strlen(output_path)+1);
if(cmd != NULL) {
sprintf(cmd, "%s %s %s", scp_cmd, input_path, output_path);
} else {
free_ressource();
exit(ENOMEM);
}
ret = system(cmd);

} else {
printf("%s not found. please ensure the device is correctly connected\n",
printf("%s not found. Please ensure the device is correctly connected\n",
output_dev);
ret = -1;
ret = ENODEV;
}
}
else
{
printf("Missing argument\n");
usage(argv[0]);
}

free_ressource();
return ret;
}
149 changes: 119 additions & 30 deletions src/massStorageCopy/massStorageCopyMacOsX.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,156 @@
#include <string.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <getopt.h>
#include <ctype.h>
#include <errno.h>

#define MAX_FS 128

static char *input_path = NULL;
static char *output_path = NULL;
static char *output_dev = NULL;
static char **list_output_dev = NULL;
static char *cmd = NULL;


void usage(char *name)
{
printf("Usage: %s [-I <filepath>] [-O <mountpoint(s)> ]\n\n", name);
printf("Mandatory options:\n");
printf("\t-I: filepath binary to copy\n");
printf("\t-O: mountpoint(s) destination name.\n");
printf("\t Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\"\n");
}


void free_ressource()
{
if(input_path)
free(input_path);
if(output_path)
free(output_path);
if(output_dev)
free(output_dev);
if(list_output_dev)
free(list_output_dev);
if(cmd)
free(cmd);
}

int main(int argc, char *argv[])
{
int i;
int c, i, n;
int ret = 0;
int device_found = 0;
char input_path[256];
char output_dev[256];
char output_path[256];
char cmd[512];
struct statfs buf[MAX_FS];
int fs_count;
char *p = NULL;
int n_output_dev = 0;
char scp_cmd[]="scp";

if(argc < 4) {
printf("error: missing parameters\n");
ret = -1;
}

for(i = 1; i < argc; i++) {
opterr = 0;

if((strcmp(argv[i], "-I") == 0)&&(i+1 < argc)) {
strcpy(input_path, argv[i+1]);
i++;
} else if((strcmp(argv[i], "-O") == 0)&&(i+1 < argc)) {
strcpy(output_dev, argv[i+1]);
i++;
} else {
printf("error: unknown option %s\n", argv[i]);
ret = -1;
while ((c = getopt (argc, argv, "I:O:")) != -1) {
switch (c)
{
case 'I':
input_path = malloc(strlen(optarg)+1);
if(input_path != NULL)
strcpy(input_path, optarg);
break;
case 'O':
output_dev = malloc(strlen(optarg)+1);
if(output_dev != NULL)
strcpy(output_dev, optarg);
break;
case '?':
if ((optopt == 'I') || (optopt == 'O'))
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
usage(argv[0]);
free_ressource();
return EINVAL;
default:
abort ();
}
}

if(ret == 0) {
if((input_path == NULL) || (output_dev == NULL))
{
free_ressource();
exit(ENOMEM);
}

if(strlen(input_path) && strlen(output_dev))
{
/* get the mounted devices list */
fs_count = getfsstat(NULL,0,MNT_WAIT);
if(fs_count < 0) {
perror("getfsstat");
exit(1);
free_ressource();
exit(ENOENT);
}

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

for(i = 0; i < fs_count; i++) {
if(strstr(buf[i].f_mntonname,output_dev)) {
sprintf(output_path, "%s", buf[i].f_mntonname);
device_found = 1;
/* " must be removed too */
p = strtok (output_dev, ",\"");

/* split output_dev and append tokens to list_output_dev */
while (p) {
list_output_dev = realloc (list_output_dev, sizeof (char*) * ++n_output_dev);

if (list_output_dev == NULL)
exit (ENOMEM);

list_output_dev[n_output_dev-1] = p;

p = strtok (NULL, ",\"");
}

/* realloc one extra element for the last NULL */
list_output_dev = realloc (list_output_dev, sizeof (char*) * (n_output_dev+1));
list_output_dev[n_output_dev] = 0;

for(n = 0; (n < fs_count) && (!device_found); n++) {
for(i = 0; (i < n_output_dev) && (!device_found); i++) {
if(strstr(buf[n].f_mntonname,list_output_dev[i])) {
output_path = malloc(strlen(buf[n].f_mntonname)+1);
if(output_path != NULL) {
sprintf(output_path, "%s", buf[n].f_mntonname);
} else {
free_ressource();
exit(ENOMEM);
}
device_found = 1;
}
}
}

if(device_found) {
printf("copying %s to %s\n", input_path, output_path);
sprintf(cmd, "scp %s %s", input_path, output_path);
system(cmd);
cmd = malloc(strlen(scp_cmd)+1+strlen(input_path)+1+strlen(output_path)+1);
if(cmd != NULL) {
sprintf(cmd, "%s %s %s", scp_cmd, input_path, output_path);
} else {
free_ressource();
exit(ENOMEM);
}
ret = system(cmd);
} else {
printf("%s not found. please ensure the device is correctly connected\n",
printf("%s not found. Please ensure the device is correctly connected\n",
output_dev);
ret = -1;
ret = ENODEV;
}
} else {
printf("Missing argument\n");
usage(argv[0]);
}

free_ressource();
return ret;
}
Loading