Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

vinil can decrease file size

  • Loading branch information...
commit a90895bca2d5e4fd9683006f34ef86f943cbc389 1 parent c05393a
@igorbonadio authored
View
8 src/crossplatform.c
@@ -28,4 +28,12 @@ VINILAPI int64_t vinil_ftell(FILE *fd) {
#else
return ftello(fd);
#endif
+}
+
+VINILAPI int vinil_truncate(FILE *fd, int64_t new_length) {
+#ifdef _WIN32
+ return _chsize(fileno(fd), new_length) == 0 ? TRUE : FALSE;
+#else
+ return ftruncate(fileno(fd), new_length) == 0 ? TRUE : FALSE;
+#endif
}
View
2  src/crossplatform.h
@@ -34,5 +34,7 @@ typedef uuid_t vinil_uuid;
VINILAPI void vinil_uuid_generate(vinil_uuid* uuid);
VINILAPI int vinil_fseek(FILE *fd, int64_t offset, int origin);
VINILAPI int64_t vinil_ftell(FILE *fd);
+VINILAPI int vinil_truncate(FILE *fd, int64_t new_length);
+
#endif
View
6 src/vhd.c
@@ -245,10 +245,12 @@ int vinil_vhd_flush(VinilVHD* vhd) {
}
int vinil_vhd_commit_structural_changes(VinilVHD* vhd) {
+ if (!vinil_truncate(vhd->fd, vhd->footer->current_size))
+ return FALSE;
+
int error = vinil_fseek(vhd->fd, vhd->footer->current_size, SEEK_SET);
- if (error) {
+ if (error)
return FALSE;
- }
vhd->footer->checksum = vinil_checksum_vhd_footer(vhd->footer);
View
24 tests/check_vhd.c
@@ -202,7 +202,7 @@ START_TEST (test_vinil_vhd_commit_structural_changes) {
vhd->footer->disk_type = 2; // Fixed
vinil_uuid_generate(&vhd->footer->uuid);
vhd->footer->saved_state = 0;
- vhd->footer->checksum = vinil_checksum_vhd_footer(vhd->footer);
+ //vhd->footer->checksum = vinil_checksum_vhd_footer(vhd->footer);
fail_unless(vinil_vhd_commit_structural_changes(vhd), "Cannot commit changes in new_vhd_file.vhd");
@@ -230,8 +230,30 @@ START_TEST (test_vinil_vhd_commit_structural_changes) {
// checking the new file...
VinilVHD* vhd2 = vinil_vhd_open(vhd_path);
fail_unless(vhd2 != NULL, "Cannot reopen new_vhd_file.vhd");
+
+ // decreasing vhd's size
+ vhd2->footer->current_size = 2*1024*1024; // 2MB
+
+ fail_unless(vinil_vhd_commit_structural_changes(vhd2), "Cannot commit changes in new_vhd_file.vhd");
+
vinil_vhd_close(vhd2);
+ // checking the new file...
+ VinilVHD* vhd3 = vinil_vhd_open(vhd_path);
+ fail_unless(vhd3 != NULL, "Cannot reopen new_vhd_file.vhd");
+
+ fail_unless(vinil_vhd_seek(vhd3, 0, SEEK_SET), "Cannot execute vinil_vhd_seek in new_vhd_file.vhd");
+
+ count = 0;
+ while (vinil_vhd_write(vhd3, buffer, 1))
+ count++;
+
+ fail_unless(count*512 == vhd->footer->current_size, "Wrong number of sectors in new_vhd_file.vhd");
+
+ fail_unless(2*1024*1024 == vhd->footer->current_size, "Wrong number of sectors in new_vhd_file.vhd");
+
+ vinil_vhd_close(vhd3);
+
} END_TEST
START_TEST (test_vinil_geometry_encode) {
Please sign in to comment.
Something went wrong with that request. Please try again.