1 /* Demonstrate that setting disk->needs_clobber in ped_disk_duplicate
2 is necessary. With that, this test passes. Without it, the last
3 sectors of the disk are cleared, and this test fails. */
5 #include <parted/parted.h>
17 seek_to_final_sector (int fd, PedSector ss)
20 off_t off = lseek (fd, 0, SEEK_END);
22 /* That had better succeed and determine that the size is > 2 sectors
23 and an exact multiple of ss. */
24 assert (2 * ss < off);
25 assert (off % ss == 0);
27 /* Back up one sector. */
28 off = lseek (fd, -ss, SEEK_CUR);
33 scribble_on_final_sector (char const *file_name, PedSector ss)
36 assert (ss % 512 == 0);
37 int fd = open (file_name, O_WRONLY);
40 seek_to_final_sector (fd, ss);
42 /* Fill the final sector with ascii 'G's. */
43 char *buf = malloc (ss);
45 memset (buf, 'G', ss);
46 assert (write (fd, buf, ss) == ss);
48 assert (close (fd) == 0);
52 main (int argc, char **argv)
54 atexit (close_stdout);
55 set_program_name (argv[0]);
60 char const *dev_name = "dev-file";
63 int fd = open (dev_name, O_CREAT|O_TRUNC|O_WRONLY, 0644);
65 off_t size = 8 * 1024 * 1024;
66 assert (ftruncate (fd, size) == 0);
67 assert (close (fd) == 0);
69 PedDevice *dev = ped_device_get (dev_name);
72 PedDisk *disk = ped_disk_new_fresh (dev, ped_disk_type_get ("msdos"));
75 assert (ped_disk_commit(disk));
77 PedSector ss = dev->sector_size;
78 scribble_on_final_sector (dev_name, ss);
80 /* Before the fix, this ped_disk_duplicate call would always set
81 copy->needs_clobber, thus causing the subsequent commit to
82 mistakenly clobber 9KiB at each end of the disk. */
83 PedDisk *copy = ped_disk_duplicate (disk);
84 assert (ped_disk_commit(copy));
86 ped_disk_destroy (copy);
87 ped_disk_destroy (disk);
88 ped_device_destroy (dev);
90 /* Read the final sector and ensure it's still all 'G's. */
91 fd = open (dev_name, O_RDONLY);
93 seek_to_final_sector (fd, ss);
94 char *buf = malloc (ss);
96 assert (read (fd, buf, ss) == ss);
98 for (i = 0; i < ss; i++)
99 assert (buf[i] == 'G');