diff --git a/lib/libarchive/archive_write_disk.c b/lib/libarchive/archive_write_disk.c index f5fb3aa8937b..0c15b77f9a75 100644 --- a/lib/libarchive/archive_write_disk.c +++ b/lib/libarchive/archive_write_disk.c @@ -444,12 +444,14 @@ _archive_write_data_block(struct archive *_a, const void *buff, size_t size, off_t offset) { struct archive_write_disk *a = (struct archive_write_disk *)_a; - ssize_t bytes_written = 0; + ssize_t bytes_written = 0, total_written = 0; __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, ARCHIVE_STATE_DATA, "archive_write_disk_block"); - if (a->fd < 0) - return (ARCHIVE_OK); + if (a->fd < 0) { + archive_set_error(&a->archive, 0, "File not open"); + return (ARCHIVE_WARN); + } archive_clear_error(&a->archive); /* Seek if necessary to the specified offset. */ @@ -470,8 +472,9 @@ _archive_write_data_block(struct archive *_a, } size -= bytes_written; a->offset += bytes_written; + total_written += bytes_written; } - return (ARCHIVE_OK); + return (total_written); } static ssize_t diff --git a/lib/libarchive/test/test_write_disk.c b/lib/libarchive/test/test_write_disk.c index 6b28830ee6f9..d8ab7b970360 100644 --- a/lib/libarchive/test/test_write_disk.c +++ b/lib/libarchive/test/test_write_disk.c @@ -50,6 +50,31 @@ static void create(struct archive_entry *ae, const char *msg) st.st_mode, archive_entry_mode(ae)); assert(st.st_mode == (archive_entry_mode(ae) & ~UMASK)); } + +static void create_reg_file(struct archive_entry *ae, const char *msg) +{ + static const char data[]="abcdefghijklmnopqrstuvwxyz"; + struct archive *ad; + struct stat st; + + /* Write the entry to disk. */ + assert((ad = archive_write_disk_new()) != NULL); + failure("%s", msg); + assertEqualIntA(ad, 0, archive_write_header(ad, ae)); + assertEqualIntA(ad, sizeof(data), archive_write_data(ad, data, sizeof(data))); + assertEqualIntA(ad, 0, archive_write_finish_entry(ad)); +#if ARCHIVE_API_VERSION > 1 + assertEqualInt(0, archive_write_finish(ad)); +#else + archive_write_finish(ad); +#endif + /* Test the entries on disk. */ + assert(0 == stat(archive_entry_pathname(ae), &st)); + failure("st.st_mode=%o archive_entry_mode(ae)=%o", + st.st_mode, archive_entry_mode(ae)); + assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK)); + assertEqualInt(st.st_size, sizeof(data)); +} #endif DEFINE_TEST(test_write_disk) @@ -66,7 +91,7 @@ DEFINE_TEST(test_write_disk) assert((ae = archive_entry_new()) != NULL); archive_entry_copy_pathname(ae, "file"); archive_entry_set_mode(ae, S_IFREG | 0755); - create(ae, "Test creating a regular file"); + create_reg_file(ae, "Test creating a regular file"); archive_entry_free(ae); /* A regular file over an existing file */