~youscribe/parted/3.1

« back to all changes in this revision

Viewing changes to tests/dup-clobber.c

  • Committer: Guilhem Lettron
  • Date: 2012-10-22 14:37:59 UTC
  • Revision ID: guilhem+ubuntu@lettron.fr-20121022143759-m403kecgz13sknvp
3.1 from tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.  */
 
4
#include <config.h>
 
5
#include <parted/parted.h>
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <assert.h>
 
9
#include <sys/types.h>
 
10
#include <unistd.h>
 
11
#include <fcntl.h>
 
12
 
 
13
#include "closeout.h"
 
14
#include "progname.h"
 
15
 
 
16
static void
 
17
seek_to_final_sector (int fd, PedSector ss)
 
18
{
 
19
  /* Seek to EOF.  */
 
20
  off_t off = lseek (fd, 0, SEEK_END);
 
21
 
 
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);
 
26
 
 
27
  /* Back up one sector.  */
 
28
  off = lseek (fd, -ss, SEEK_CUR);
 
29
  assert (0 < off);
 
30
}
 
31
 
 
32
static void
 
33
scribble_on_final_sector (char const *file_name, PedSector ss)
 
34
{
 
35
  assert (0 < ss);
 
36
  assert (ss % 512 == 0);
 
37
  int fd = open (file_name, O_WRONLY);
 
38
  assert (0 <= fd);
 
39
 
 
40
  seek_to_final_sector (fd, ss);
 
41
 
 
42
  /* Fill the final sector with ascii 'G's.  */
 
43
  char *buf = malloc (ss);
 
44
  assert (buf);
 
45
  memset (buf, 'G', ss);
 
46
  assert (write (fd, buf, ss) == ss);
 
47
  free (buf);
 
48
  assert (close (fd) == 0);
 
49
}
 
50
 
 
51
int
 
52
main (int argc, char **argv)
 
53
{
 
54
  atexit (close_stdout);
 
55
  set_program_name (argv[0]);
 
56
 
 
57
  if (argc != 1)
 
58
    return EXIT_FAILURE;
 
59
 
 
60
  char const *dev_name = "dev-file";
 
61
 
 
62
  /* Create a file.  */
 
63
  int fd = open (dev_name, O_CREAT|O_TRUNC|O_WRONLY, 0644);
 
64
  assert (0 <= fd);
 
65
  off_t size = 8 * 1024 * 1024;
 
66
  assert (ftruncate (fd, size) == 0);
 
67
  assert (close (fd) == 0);
 
68
 
 
69
  PedDevice *dev = ped_device_get (dev_name);
 
70
  assert (dev);
 
71
 
 
72
  PedDisk *disk = ped_disk_new_fresh (dev, ped_disk_type_get ("msdos"));
 
73
  assert (disk);
 
74
 
 
75
  assert (ped_disk_commit(disk));
 
76
 
 
77
  PedSector ss = dev->sector_size;
 
78
  scribble_on_final_sector (dev_name, ss);
 
79
 
 
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));
 
85
 
 
86
  ped_disk_destroy (copy);
 
87
  ped_disk_destroy (disk);
 
88
  ped_device_destroy (dev);
 
89
 
 
90
  /* Read the final sector and ensure it's still all 'G's.  */
 
91
  fd = open (dev_name, O_RDONLY);
 
92
  assert (0 <= fd);
 
93
  seek_to_final_sector (fd, ss);
 
94
  char *buf = malloc (ss);
 
95
  assert (buf);
 
96
  assert (read (fd, buf, ss) == ss);
 
97
  unsigned int i;
 
98
  for (i = 0; i < ss; i++)
 
99
    assert (buf[i] == 'G');
 
100
  free (buf);
 
101
 
 
102
  return EXIT_SUCCESS;
 
103
}