tFunction dosyncÃ(Ã) outputs the commands to run - synk - synchronize files between hosts
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit fa827f5f90ee4bb0bc0ebf47bebaab48045d62ed
(DIR) parent 55f96b7e9985c6d3449d57bdd37b7720c6f5a4c1
(HTM) Author: Willy <willyatmailoodotorg>
Date: Thu, 1 Sep 2016 14:10:58 +0200
Function dosyncÃ(Ã) outputs the commands to run
Diffstat:
M synk.c | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)
---
(DIR) diff --git a/synk.c b/synk.c
t@@ -15,6 +15,8 @@
#include "arg.h"
#include "sha512.h"
+#define IS_LOOPBACK(p) ((p)->peer.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
+
#define SERVER_HOST "127.0.0.1"
#define SERVER_PORT 9723
t@@ -66,6 +68,7 @@ int syncwithmaster(struct peer_t *master, struct peers_t *plist);
int dosync(struct peer_t *master, struct peer_t *slave);
const char *rsync_cmd[] = { "rsync", "-azEq", "--delete", NULL };
+const char *ssh_cmd[] = { "ssh", NULL };
void
usage(char *name)
t@@ -413,19 +416,24 @@ dosync(struct peer_t *master, struct peer_t *slave)
{
char **cmd = NULL;
char *args[] = { NULL, NULL, NULL };
- char local[_POSIX_ARG_MAX], remote[_POSIX_ARG_MAX];
-
- snprintf(local, _POSIX_ARG_MAX, "%s", master->meta.path);
- snprintf(remote, _POSIX_ARG_MAX, "%s:%s", inet_ntoa(slave->peer.sin_addr),
- slave->meta.path);
+ char source[_POSIX_ARG_MAX] = "";
+ char destination[_POSIX_ARG_MAX] = "";
+
+ if (IS_LOOPBACK(slave)) {
+ snprintf(source, _POSIX_ARG_MAX, "%s:%s", inet_ntoa(master->peer.sin_addr), slave->meta.path);
+ snprintf(destination, _POSIX_ARG_MAX, "%s", master->meta.path);
+ } else {
+ snprintf(source, _POSIX_ARG_MAX, "%s", master->meta.path);
+ snprintf(destination, _POSIX_ARG_MAX, "%s:%s", inet_ntoa(slave->peer.sin_addr), slave->meta.path);
+ }
- args[0] = local;
- args[1] = remote;
+ args[0] = source;
+ args[1] = destination;
cmd = concat(2, rsync_cmd, args);
- if (master->peer.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
- cmd = concat(1, (char *[]){ "ssh", inet_ntoa(master->peer.sin_addr), echo(cmd), NULL });
+ if (!IS_LOOPBACK(master) && !IS_LOOPBACK(slave)) {
+ cmd = concat(2, ssh_cmd, (char *[]){ inet_ntoa(master->peer.sin_addr), echo(cmd), NULL });
}
puts(echo(cmd));