examples: Fix libssh_scp.c code style
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Этот коммит содержится в:
родитель
00e5ef1b3c
Коммит
6118628424
@ -55,6 +55,7 @@ static void usage(const char *argv0){
|
||||
|
||||
static int opts(int argc, char **argv) {
|
||||
int i;
|
||||
|
||||
while((i = getopt(argc, argv, "v")) != -1) {
|
||||
switch(i) {
|
||||
case 'v':
|
||||
@ -66,18 +67,23 @@ static int opts(int argc, char **argv){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
nsources = argc - optind - 1;
|
||||
if (nsources < 1) {
|
||||
usage(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sources = malloc((nsources + 1) * sizeof(char *));
|
||||
if(sources == NULL)
|
||||
if (sources == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(i = 0; i < nsources; ++i) {
|
||||
sources[i] = argv[optind];
|
||||
optind++;
|
||||
}
|
||||
|
||||
sources[i] = NULL;
|
||||
destination = argv[optind];
|
||||
return 0;
|
||||
@ -120,11 +126,13 @@ static struct location *parse_location(char *loc){
|
||||
|
||||
location->host = location->user = NULL;
|
||||
ptr = strchr(loc, ':');
|
||||
|
||||
if (ptr != NULL) {
|
||||
location->is_ssh = 1;
|
||||
location->path = strdup(ptr+1);
|
||||
*ptr = '\0';
|
||||
ptr = strchr(loc, '@');
|
||||
|
||||
if (ptr != NULL) {
|
||||
location->host = strdup(ptr+1);
|
||||
*ptr = '\0';
|
||||
@ -175,6 +183,7 @@ static int open_location(struct location *loc, int flag){
|
||||
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
||||
return -1;
|
||||
}
|
||||
|
||||
loc->scp = ssh_scp_new(loc->session, SSH_SCP_WRITE, loc->path);
|
||||
if (!loc->scp) {
|
||||
fprintf(stderr, "error : %s\n", ssh_get_error(loc->session));
|
||||
@ -183,6 +192,7 @@ static int open_location(struct location *loc, int flag){
|
||||
loc->session = NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ssh_scp_init(loc->scp) == SSH_ERROR) {
|
||||
fprintf(stderr, "error : %s\n", ssh_get_error(loc->session));
|
||||
ssh_scp_free(loc->scp);
|
||||
@ -199,6 +209,7 @@ static int open_location(struct location *loc, int flag){
|
||||
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
||||
return -1;
|
||||
}
|
||||
|
||||
loc->scp = ssh_scp_new(loc->session, SSH_SCP_READ, loc->path);
|
||||
if (!loc->scp) {
|
||||
fprintf(stderr, "error : %s\n", ssh_get_error(loc->session));
|
||||
@ -207,6 +218,7 @@ static int open_location(struct location *loc, int flag){
|
||||
loc->session = NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ssh_scp_init(loc->scp) == SSH_ERROR) {
|
||||
fprintf(stderr, "error : %s\n", ssh_get_error(loc->session));
|
||||
ssh_scp_free(loc->scp);
|
||||
@ -222,12 +234,16 @@ static int open_location(struct location *loc, int flag){
|
||||
if (!loc->file) {
|
||||
if (errno == EISDIR) {
|
||||
if (chdir(loc->path)) {
|
||||
fprintf(stderr,"Error changing directory to %s: %s\n",loc->path,strerror(errno));
|
||||
fprintf(stderr,
|
||||
"Error changing directory to %s: %s\n",
|
||||
loc->path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
fprintf(stderr,"Error opening %s: %s\n",loc->path,strerror(errno));
|
||||
fprintf(stderr,
|
||||
"Error opening %s: %s\n",
|
||||
loc->path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -255,7 +271,9 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
if (!src->is_ssh) {
|
||||
fd = fileno(src->file);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Invalid file pointer, error: %s\n", strerror(errno));
|
||||
fprintf(stderr,
|
||||
"Invalid file pointer, error: %s\n",
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
r = fstat(fd, &s);
|
||||
@ -281,7 +299,9 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
break;
|
||||
}
|
||||
if (r == SSH_ERROR) {
|
||||
fprintf(stderr,"Error: %s\n",ssh_get_error(src->session));
|
||||
fprintf(stderr,
|
||||
"Error: %s\n",
|
||||
ssh_get_error(src->session));
|
||||
ssh_string_free_char(filename);
|
||||
return -1;
|
||||
}
|
||||
@ -292,7 +312,9 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
r = ssh_scp_push_file(dest->scp, src->path, size, mode);
|
||||
// snprintf(buffer, sizeof(buffer), "C0644 %d %s\n", size, src->path);
|
||||
if (r == SSH_ERROR) {
|
||||
fprintf(stderr,"error: %s\n",ssh_get_error(dest->session));
|
||||
fprintf(stderr,
|
||||
"error: %s\n",
|
||||
ssh_get_error(dest->session));
|
||||
ssh_string_free_char(filename);
|
||||
ssh_scp_free(dest->scp);
|
||||
dest->scp = NULL;
|
||||
@ -302,9 +324,12 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
if (!dest->file) {
|
||||
dest->file = fopen(filename, "w");
|
||||
if (!dest->file) {
|
||||
fprintf(stderr,"Cannot open %s for writing: %s\n",filename,strerror(errno));
|
||||
if(src->is_ssh)
|
||||
fprintf(stderr,
|
||||
"Cannot open %s for writing: %s\n",
|
||||
filename, strerror(errno));
|
||||
if (src->is_ssh) {
|
||||
ssh_scp_deny_request(src->scp, "Cannot open local file");
|
||||
}
|
||||
ssh_string_free_char(filename);
|
||||
return -1;
|
||||
}
|
||||
@ -313,30 +338,42 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
ssh_scp_accept_request(src->scp);
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
if (src->is_ssh) {
|
||||
r = ssh_scp_read(src->scp, buffer, sizeof(buffer));
|
||||
if (r == SSH_ERROR) {
|
||||
fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(src->session));
|
||||
fprintf(stderr,
|
||||
"Error reading scp: %s\n",
|
||||
ssh_get_error(src->session));
|
||||
ssh_string_free_char(filename);
|
||||
return -1;
|
||||
}
|
||||
if(r==0)
|
||||
|
||||
if (r == 0) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
r = fread(buffer, 1, sizeof(buffer), src->file);
|
||||
if(r==0)
|
||||
if (r == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
fprintf(stderr,"Error reading file: %s\n",strerror(errno));
|
||||
fprintf(stderr,
|
||||
"Error reading file: %s\n",
|
||||
strerror(errno));
|
||||
ssh_string_free_char(filename);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (dest->is_ssh) {
|
||||
w = ssh_scp_write(dest->scp, buffer, r);
|
||||
if (w == SSH_ERROR) {
|
||||
fprintf(stderr,"Error writing in scp: %s\n",ssh_get_error(dest->session));
|
||||
fprintf(stderr,
|
||||
"Error writing in scp: %s\n",
|
||||
ssh_get_error(dest->session));
|
||||
ssh_scp_free(dest->scp);
|
||||
dest->scp = NULL;
|
||||
ssh_string_free_char(filename);
|
||||
@ -345,7 +382,9 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
} else {
|
||||
w = fwrite(buffer, r, 1, dest->file);
|
||||
if (w <= 0) {
|
||||
fprintf(stderr,"Error writing in local file: %s\n",strerror(errno));
|
||||
fprintf(stderr,
|
||||
"Error writing in local file: %s\n",
|
||||
strerror(errno));
|
||||
ssh_string_free_char(filename);
|
||||
return -1;
|
||||
}
|
||||
@ -353,6 +392,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
|
||||
total += r;
|
||||
|
||||
} while(total < size);
|
||||
|
||||
ssh_string_free_char(filename);
|
||||
printf("wrote %d bytes\n", total);
|
||||
return 0;
|
||||
@ -366,12 +406,14 @@ int main(int argc, char **argv){
|
||||
r = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
dest = parse_location(destination);
|
||||
if (open_location(dest, WRITE) < 0) {
|
||||
location_free(dest);
|
||||
r = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (i = 0; i < nsources; ++i) {
|
||||
src = parse_location(sources[i]);
|
||||
if (open_location(src, READ) < 0) {
|
||||
@ -379,14 +421,17 @@ int main(int argc, char **argv){
|
||||
r = EXIT_FAILURE;
|
||||
goto close_dest;
|
||||
}
|
||||
|
||||
if (do_copy(src, dest, 0) < 0) {
|
||||
close_location(src);
|
||||
location_free(src);
|
||||
break;
|
||||
}
|
||||
|
||||
close_location(src);
|
||||
location_free(src);
|
||||
}
|
||||
|
||||
r = 0;
|
||||
|
||||
close_dest:
|
||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user