--file-{read,write,append}

This commit is contained in:
Ian Gulliver
2016-02-28 13:08:04 -08:00
parent fdafe2e055
commit e86ea97671
4 changed files with 60 additions and 0 deletions

View File

@@ -32,6 +32,9 @@ static void print_usage(const char *name) {
"\t--connect-send=FORMAT=HOST/PORT\n" "\t--connect-send=FORMAT=HOST/PORT\n"
"\t--listen-receive=[HOST/]PORT\n" "\t--listen-receive=[HOST/]PORT\n"
"\t--listen-send=FORMAT=[HOST/]PORT\n" "\t--listen-send=FORMAT=[HOST/]PORT\n"
"\t--file-read=PATH\n"
"\t--file-write=FORMAT=PATH\n"
"\t--file-append=FORMAT=PATH\n"
"\t--stdin\n" "\t--stdin\n"
"\t--stdout=FORMAT\n" "\t--stdout=FORMAT\n"
, name); , name);
@@ -45,6 +48,9 @@ static bool parse_opts(int argc, char *argv[]) {
{"connect-send", required_argument, 0, 's'}, {"connect-send", required_argument, 0, 's'},
{"listen-receive", required_argument, 0, 'l'}, {"listen-receive", required_argument, 0, 'l'},
{"listen-send", required_argument, 0, 'm'}, {"listen-send", required_argument, 0, 'm'},
{"file-read", required_argument, 0, 'r'},
{"file-write", required_argument, 0, 'w'},
{"file-append", required_argument, 0, 'a'},
{"stdin", no_argument, 0, 'i'}, {"stdin", no_argument, 0, 'i'},
{"stdout", required_argument, 0, 'o'}, {"stdout", required_argument, 0, 'o'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
@@ -71,6 +77,18 @@ static bool parse_opts(int argc, char *argv[]) {
handler = opts_add_listen_send; handler = opts_add_listen_send;
break; break;
case 'r':
handler = opts_add_file_read;
break;
case 'w':
handler = opts_add_file_write;
break;
case 'a':
handler = opts_add_file_append;
break;
case 'i': case 'i':
handler = opts_add_stdin; handler = opts_add_stdin;
break; break;

View File

@@ -1,5 +1,8 @@
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "buf.h" #include "buf.h"
@@ -88,6 +91,41 @@ bool opts_add_listen_send(char *arg) {
return true; return true;
} }
bool opts_add_file_read(char *arg) {
int fd = open(arg, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return false;
}
receive_new(fd, NULL, NULL);
return true;
}
bool opts_add_file_write(char *arg) {
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
return NULL;
}
int fd = open(arg, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, S_IRWXU);
if (fd == -1) {
return false;
}
return send_new_hello(fd, serializer, NULL);
}
bool opts_add_file_append(char *arg) {
struct serializer *serializer = opts_get_serializer(&arg);
if (!serializer) {
return NULL;
}
int fd = open(arg, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU);
if (fd == -1) {
return false;
}
return send_new_hello(fd, serializer, NULL);
}
bool opts_add_stdin(char __attribute__((unused)) *arg) { bool opts_add_stdin(char __attribute__((unused)) *arg) {
receive_new(dup(0), NULL, NULL); receive_new(dup(0), NULL, NULL);
return true; return true;

View File

@@ -6,5 +6,8 @@ bool opts_add_connect_receive(char *);
bool opts_add_connect_send(char *); bool opts_add_connect_send(char *);
bool opts_add_listen_receive(char *); bool opts_add_listen_receive(char *);
bool opts_add_listen_send(char *); bool opts_add_listen_send(char *);
bool opts_add_file_read(char *);
bool opts_add_file_write(char *);
bool opts_add_file_append(char *);
bool opts_add_stdout(char *); bool opts_add_stdout(char *);
bool opts_add_stdin(char *); bool opts_add_stdin(char *);

View File

@@ -141,6 +141,7 @@ bool send_new_hello(int fd, struct serializer *serializer, struct peer *on_close
send_hello(&buf_ptr, serializer); send_hello(&buf_ptr, serializer);
if (buf_ptr->length) { if (buf_ptr->length) {
if (write(fd, buf_at(buf_ptr, 0), buf_ptr->length) != (ssize_t) buf_ptr->length) { if (write(fd, buf_at(buf_ptr, 0), buf_ptr->length) != (ssize_t) buf_ptr->length) {
assert(!close(fd));
return false; return false;
} }
} }