Whamcloud - gitweb
LU-3867 utils: llapi_create_volatile_idx uses 0 instead idx
[fs/lustre-release.git] / lustre / utils / lhsmtool_posix.c
index c283e63..f182dac 100644 (file)
@@ -507,7 +507,7 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        __u64                    offset = hai->hai_extent.offset;
        struct stat              src_st;
        struct stat              dst_st;
-       char                    *buf;
+       char                    *buf = NULL;
        __u64                    write_total = 0;
        __u64                    length;
        time_t                   last_print_time = time(NULL);
@@ -515,10 +515,6 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        CT_TRACE("going to copy data from '%s' to '%s'", src, dst);
 
-       buf = malloc(opt.o_chunk_size);
-       if (buf == NULL)
-               return -ENOMEM;
-
        if (fstat(src_fd, &src_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", src);
@@ -545,11 +541,11 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
        if (rc < 0) {
-               CT_ERROR(errno,
+               rc = -errno;
+               CT_ERROR(rc,
                         "cannot seek for read to "LPU64" (len %jd) in '%s'",
                         hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
-               rc = -errno;
-               goto out;
+               return rc;
        }
 
        he.offset = offset;
@@ -567,6 +563,12 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
        /* Don't read beyond a given extent */
        length = min(hai->hai_extent.length, src_st.st_size);
 
+       buf = malloc(opt.o_chunk_size);
+       if (buf == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
        CT_DEBUG("Going to copy "LPU64" bytes %s -> %s\n", length, src, dst);
 
        while (write_total < length) {
@@ -631,14 +633,14 @@ out:
                rc = ftruncate(dst_fd, src_st.st_size);
                if (rc < 0) {
                        rc = -errno;
-                       CT_ERROR(rc,
-                                "cannot truncate '%s' to size %jd",
+                       CT_ERROR(rc, "cannot truncate '%s' to size %jd",
                                 dst, (intmax_t)src_st.st_size);
                        err_major++;
                }
        }
 
-       free(buf);
+       if (buf != NULL)
+               free(buf);
 
        return rc;
 }
@@ -934,7 +936,7 @@ static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
                int              linkno = 0;
                char            *ptr;
                int              depth = 0;
-               int              sz;
+               ssize_t          sz;
 
                sprintf(buf, DFID, PFID(&hai->hai_fid));
                sprintf(src, "%s/shadow/", opt.o_hsm_root);
@@ -968,6 +970,12 @@ static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
                }
                /* symlink already exists ? */
                sz = readlink(src, buf, sizeof(buf));
+               /* detect truncation */
+               if (sz == sizeof(buf)) {
+                       rcf = rcf ? rcf : -E2BIG;
+                       CT_ERROR(rcf, "readlink '%s' truncated", src);
+                       goto fini_minor;
+               }
                if (sz >= 0) {
                        buf[sz] = '\0';
                        if (sz == 0 || strncmp(buf, dst, sz) != 0) {