Skip to content

Commit 35077f1

Browse files
committed
dsync: fix positioning bug when using --contents
1 parent 381a2d1 commit 35077f1

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/dsync/dsync.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static int dsync_compare_data(
472472
const char* src_name,
473473
const char* dst_name,
474474
off_t offset,
475-
size_t length,
475+
off_t length,
476476
size_t buff_size,
477477
mfu_copy_opts_t* mfu_copy_opts,
478478
uint64_t* count_bytes_read,
@@ -485,7 +485,6 @@ static int dsync_compare_data(
485485
* src side */
486486
MFU_LOG(MFU_LOG_ERR, "Failed to open %s, error msg: %s",
487487
src_name, strerror(errno));
488-
mfu_close(src_name, src_fd);
489488
return -1;
490489
}
491490

@@ -506,8 +505,8 @@ static int dsync_compare_data(
506505
}
507506

508507
/* hint that we'll read from file sequentially */
509-
posix_fadvise(src_fd, offset, (off_t)length, POSIX_FADV_SEQUENTIAL);
510-
posix_fadvise(dst_fd, offset, (off_t)length, POSIX_FADV_SEQUENTIAL);
508+
posix_fadvise(src_fd, offset, length, POSIX_FADV_SEQUENTIAL);
509+
posix_fadvise(dst_fd, offset, length, POSIX_FADV_SEQUENTIAL);
511510

512511
/* assume we'll find that file contents are the same */
513512
int rc = 0;
@@ -516,7 +515,7 @@ static int dsync_compare_data(
516515
if (mfu_lseek(src_name, src_fd, offset, SEEK_SET) == (off_t)-1) {
517516
/* log error if there is an lseek failure on the
518517
* src side */
519-
MFU_LOG(MFU_LOG_ERR, "Failed to lseek %s, offset: %x, error msg: %s",
518+
MFU_LOG(MFU_LOG_ERR, "Failed to lseek %s, offset: %lx, error msg: %s",
520519
src_name, (unsigned long)offset, strerror(errno));
521520
mfu_close(dst_name, dst_fd);
522521
mfu_close(src_name, src_fd);
@@ -527,7 +526,7 @@ static int dsync_compare_data(
527526
if(mfu_lseek(dst_name, dst_fd, offset, SEEK_SET) == (off_t)-1) {
528527
/* log error if there is an lseek failure on the
529528
* dst side */
530-
MFU_LOG(MFU_LOG_ERR, "Failed to lseek %s, offset: %x, error msg: %s",
529+
MFU_LOG(MFU_LOG_ERR, "Failed to lseek %s, offset: %lx, error msg: %s",
531530
dst_name, (unsigned long)offset, strerror(errno));
532531
mfu_close(dst_name, dst_fd);
533532
mfu_close(src_name, src_fd);
@@ -539,19 +538,16 @@ static int dsync_compare_data(
539538
void* dest_buf = MFU_MALLOC(buff_size + 1);
540539

541540
/* read and compare data from files */
542-
size_t total_bytes = 0;
541+
off_t total_bytes = 0;
543542
while(length == 0 || total_bytes < length) {
544543
/* whether we should copy the source bytes to the destination as part of sync */
545544
int copy_src_to_dst = 0;
546545

547546
/* determine number of bytes to read in this iteration */
548-
size_t left_to_read;
549-
if (length == 0) {
550-
left_to_read = buff_size;
551-
} else {
552-
left_to_read = length - total_bytes;
553-
if (left_to_read > buff_size) {
554-
left_to_read = buff_size;
547+
size_t left_to_read = buff_size;
548+
if (length > 0) {
549+
if (length - total_bytes < (off_t)buff_size) {
550+
left_to_read = (size_t)(length - total_bytes);
555551
}
556552
}
557553

@@ -627,21 +623,29 @@ static int dsync_compare_data(
627623
* then copy the bytes from the source into the destination */
628624

629625
if (!options.dry_run && copy_src_to_dst == 1) {
630-
/* number of bytes to write */
631-
size_t bytes_to_write = (size_t) src_read;
626+
/* seek back to position to write to in destination file */
627+
off_t pos = offset + total_bytes;
628+
if (mfu_lseek(dst_name, dst_fd, pos, SEEK_SET) == (off_t)-1) {
629+
/* log error if there is an lseek failure on the dst side */
630+
MFU_LOG(MFU_LOG_ERR, "Failed to lseek %s, offset: %lx, error msg: %s",
631+
dst_name, (unsigned long)pos, strerror(errno));
632+
rc = -1;
633+
break;
634+
}
632635

633-
/* seek to position to write to in destination
634-
* file */
635-
mfu_lseek(dst_name, dst_fd, offset, SEEK_SET);
636-
637636
/* write data to destination file */
638-
ssize_t num_of_bytes_written = mfu_write(dst_name, dst_fd, src_buf,
639-
bytes_to_write);
637+
size_t bytes_to_write = (size_t) src_read;
638+
ssize_t bytes_written = mfu_write(dst_name, dst_fd, src_buf, bytes_to_write);
639+
if (bytes_written < 0) {
640+
/* hit a write error */
641+
MFU_LOG(MFU_LOG_ERR, "Failed to write %s, error msg: %s",
642+
dst_name, strerror(errno));
643+
rc = -1;
644+
break;
645+
}
640646

641647
/* tally up number of bytes written */
642-
if (num_of_bytes_written >= 0) {
643-
*count_bytes_written += (uint64_t) num_of_bytes_written;
644-
}
648+
*count_bytes_written += (uint64_t) bytes_written;
645649
}
646650

647651
/* add bytes to our total */
@@ -854,7 +858,7 @@ static void dsync_strmap_compare_data(
854858

855859
/* compare the contents of the files */
856860
int rc = dsync_compare_data(src_p->name, dst_p->name, offset,
857-
(size_t)length, 1048576, mfu_copy_opts,
861+
length, 1048576, mfu_copy_opts,
858862
count_bytes_read, count_bytes_written);
859863
if (rc == -1) {
860864
/* we hit an error while reading, consider files to be different,

0 commit comments

Comments
 (0)