tFactorize timestamps retrieval - synk - synchronize files between hosts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 4c40d8e0cfbe7589ccd89c8e97f1cbf11a5844cf
 (DIR) parent b3dca9f5dc37c9e6190b5f6532ce9099b49ad2e7
 (HTM) Author: Willy <willyatmailoodotorg>
       Date:   Tue, 16 Aug 2016 20:37:36 +0200
       
       Factorize timestamps retrieval
       
       Diffstat:
         M synk.c                              |      17 +++++++++++++----
       
       1 file changed, 13 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/synk.c b/synk.c
       t@@ -32,13 +32,20 @@ usage(char *name)
                exit(1);
        }
        
       +long
       +gettimestamp(const char *path)
       +{
       +        struct stat sb;
       +        stat(path, &sb);
       +        return sb.st_mtim.tv_sec;
       +}
       +
        int
        handleclient(struct client_t *c)
        {
                int i = 0;
                char path[PATH_MAX] = "", ts[32] = "";
                size_t len = 0;
       -        struct stat sb;
        
                printf("%s: connected\n", inet_ntoa(c->in));
                while ((len = read(c->fd, &path, PATH_MAX)) > 0) {
       t@@ -49,8 +56,7 @@ handleclient(struct client_t *c)
        
                        path[len] = '\0';
                        printf("%s: %s\n", inet_ntoa(c->in), path);
       -                stat(path, &sb);
       -                snprintf(ts, 32, "%lu", sb.st_mtim.tv_sec);
       +                snprintf(ts, 32, "%lu", gettimestamp(path));
                        len = strnlen(ts, 32);
                        write(c->fd, ts, len);
                        memset(path, 0, PATH_MAX);
       t@@ -114,6 +120,7 @@ int
        client(in_addr_t host, in_port_t port, const char *fn)
        {
                int cfd;
       +        long ltim, rtim;
                ssize_t len = 0;
                struct sockaddr_in clt;
                char path[PATH_MAX] = "", ts[TIMESTAMP_MAX] = "";
       t@@ -144,7 +151,9 @@ client(in_addr_t host, in_port_t port, const char *fn)
        
                /* ... which should return the timestamp of this file */
                read(cfd, ts, TIMESTAMP_MAX);
       -        printf("%s: %s\n", path, ts);
       +        ltim = gettimestamp(path);
       +        rtim = strtol(ts, NULL, 10);
       +        printf("%s:%s L:%lu R:%lu\n", path, ltim==rtim?"SYNKED":"TOSYNK",ltim, rtim);
        
                close(cfd);