Whamcloud - gitweb
LU-6245 libcfs: remove libcfsutil.h
[fs/lustre-release.git] / lustre / utils / lustre_rsync.c
index dee27b0..e7b3830 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, Intel Corporation.
+ * Copyright (c) 2012, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <utime.h>
 #include <sys/xattr.h>
 
-#include <libcfs/libcfsutil.h>
+#include <libcfs/util/parser.h>
 #include <lustre/lustreapi.h>
 #include <lustre/lustre_idl.h>
 #include "lustre_rsync.h"
@@ -674,11 +674,11 @@ void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
                                         info->src, d, errno);
                                 errors++;
                         }
-                        lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
                         if (curr == parents)
                                 parents = curr->pc_next;
                         else
                                 prev->pc_next = curr->pc_next;
+                       lr_cascade_move(curr->pc_log.pcl_tfid, d, info);
                         free(curr);
                         prev = curr = parents;
 
@@ -1005,7 +1005,7 @@ int lr_link(struct lr_info *info)
                                 break;
 
                         len = strlen(info->path) - strlen(info->name);
-                        if (len > 0 && strcmp(info->path + len,
+                       if (len >= 0 && strcmp(info->path + len,
                                               info->name) == 0)
                                 snprintf(info->dest, PATH_MAX, "%s/%s",
                                         status->ls_targets[info->target_no],
@@ -1111,25 +1111,34 @@ int lr_setxattr(struct lr_info *info)
 /* Parse a line of changelog entry */
 int lr_parse_line(void *priv, struct lr_info *info)
 {
-       struct changelog_ext_rec *rec;
-
-        if (llapi_changelog_recv(priv, &rec) != 0)
-                return -1;
-
-       info->is_extended = CHANGELOG_REC_EXTENDED(rec);
-        info->recno = rec->cr_index;
-        info->type = rec->cr_type;
-        sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
-        sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
-       strncpy(info->name, rec->cr_name, rec->cr_namelen);
-       info->name[rec->cr_namelen] = '\0';
-
-       if (fid_is_sane(&rec->cr_sfid)) {
-               sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
-               sprintf(info->spfid, DFID, PFID(&rec->cr_spfid));
-               strncpy(info->sname, changelog_rec_sname(rec),
-                       changelog_rec_snamelen(rec));
-               info->sname[changelog_rec_snamelen(rec)] = '\0';
+       struct changelog_rec            *rec;
+       struct changelog_ext_rename     *rnm;
+       size_t                           namelen;
+       size_t                           copylen;
+
+       if (llapi_changelog_recv(priv, &rec) != 0)
+               return -1;
+
+       info->is_extended = !!(rec->cr_flags & CLF_RENAME);
+       info->recno = rec->cr_index;
+       info->type = rec->cr_type;
+       snprintf(info->tfid, sizeof(info->tfid), DFID, PFID(&rec->cr_tfid));
+       snprintf(info->pfid, sizeof(info->pfid), DFID, PFID(&rec->cr_pfid));
+
+       namelen = strnlen(changelog_rec_name(rec), rec->cr_namelen);
+       copylen = min(sizeof(info->name), namelen + 1);
+       strlcpy(info->name, changelog_rec_name(rec), copylen);
+
+       /* Don't use rnm if CLF_RENAME isn't set */
+       rnm = changelog_rec_rename(rec);
+       if (rec->cr_flags & CLF_RENAME && !fid_is_zero(&rnm->cr_sfid)) {
+               snprintf(info->sfid, sizeof(info->sfid), DFID,
+                        PFID(&rnm->cr_sfid));
+               snprintf(info->spfid, sizeof(info->spfid), DFID,
+                        PFID(&rnm->cr_spfid));
+               namelen = changelog_rec_snamelen(rec);
+               copylen = min(sizeof(info->sname), namelen + 1);
+               strlcpy(info->sname, changelog_rec_sname(rec), copylen);
 
                if (verbose > 1)
                        printf("Rec %lld: %d %s %s\n", info->recno, info->type,
@@ -1235,31 +1244,50 @@ int lr_read_log()
                 return 0;
 
         s = calloc(1, read_size);
-        if (s == NULL)
-                GOTO(out, rc = -ENOMEM);
-
-        fd = open(statuslog, O_RDONLY);
-        if (fd == -1)
-                GOTO(out, rc = -errno);
-        size = read(fd, s, read_size);
-        if (size != read_size)
-                GOTO(out, rc = -EINVAL);
-        if (read_size < s->ls_size) {
-                read_size = s->ls_size;
-                s = lr_grow_buf(s, read_size);
-                if (s == NULL)
-                        GOTO(out, rc = -ENOMEM);
-                if (lseek(fd, 0, SEEK_SET) == -1)
-                        GOTO(out, rc = -errno);
-                size = read(fd, s, read_size);
-                if (size != read_size)
-                        GOTO(out, rc = -EINVAL);
-        }
+       if (s == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       fd = open(statuslog, O_RDONLY);
+       if (fd == -1) {
+               rc = -errno;
+               goto out;
+       }
+
+       size = read(fd, s, read_size);
+       if (size != read_size) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (read_size < s->ls_size) {
+               read_size = s->ls_size;
+               s = lr_grow_buf(s, read_size);
+               if (s == NULL) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
+
+               if (lseek(fd, 0, SEEK_SET) == -1) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
+
+               size = read(fd, s, read_size);
+               if (size != read_size) {
+                       rc = -EINVAL;
+                       goto out;
+               }
+       }
+
+       while (read(fd, &rec, sizeof(rec)) != 0) {
+               tmp = calloc(1, sizeof(*tmp));
+               if (!tmp) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
 
-        while (read(fd, &rec, sizeof(rec)) != 0) {
-                tmp = calloc(1, sizeof(*tmp));
-                if (!tmp)
-                        GOTO(out, rc = -ENOMEM);
                 tmp->pc_log = rec;
                 tmp->pc_next = parents;
                 parents = tmp;
@@ -1269,8 +1297,11 @@ int lr_read_log()
         if (status->ls_num_targets == 0) {
                 if (status->ls_size != s->ls_size) {
                         status = lr_grow_buf(status, s->ls_size);
-                        if (status == NULL)
-                                GOTO(out, rc = -ENOMEM);
+                       if (status == NULL) {
+                               rc = -ENOMEM;
+                               goto out;
+                       }
+
                         status->ls_size = s->ls_size;
                 }
                 status->ls_num_targets = s->ls_num_targets;
@@ -1473,9 +1504,10 @@ int lr_replicate()
 
         lr_print_status(info);
 
-        /* Open changelogs for consumption*/
-        rc = llapi_changelog_start(&changelog_priv, CHANGELOG_FLAG_BLOCK,
-                                   status->ls_source_fs, status->ls_last_recno);
+       /* Open changelogs for consumption*/
+       rc = llapi_changelog_start(&changelog_priv,
+                                  CHANGELOG_FLAG_BLOCK | CHANGELOG_FLAG_JOBID,
+                                  status->ls_source_fs, status->ls_last_recno);
         if (rc < 0) {
                 fprintf(stderr, "Error opening changelog file for fs %s.\n",
                         status->ls_source_fs);