~youscribe/parted/3.1

« back to all changes in this revision

Viewing changes to tests/duplicate.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 ped_disk_duplicate is working correctly.
 
2
*/
 
3
#include <config.h>
 
4
#include <parted/parted.h>
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <assert.h>
 
8
#include <sys/types.h>
 
9
#include <unistd.h>
 
10
#include <fcntl.h>
 
11
#include <string.h>
 
12
 
 
13
#include "closeout.h"
 
14
#include "progname.h"
 
15
 
 
16
int
 
17
main (int argc, char **argv)
 
18
{
 
19
  atexit (close_stdout);
 
20
  set_program_name (argv[0]);
 
21
 
 
22
  if (argc != 2)
 
23
    return EXIT_FAILURE;
 
24
 
 
25
  char const *dev_name = "dev-file";
 
26
 
 
27
  /* Create a file.  */
 
28
  int fd = open (dev_name, O_CREAT|O_TRUNC|O_WRONLY, 0644);
 
29
  assert (0 <= fd);
 
30
  off_t size = 8 * 1024 * 1024;
 
31
  assert (ftruncate (fd, size) == 0);
 
32
  assert (close (fd) == 0);
 
33
 
 
34
  PedDevice *dev = ped_device_get (dev_name);
 
35
  assert (dev);
 
36
 
 
37
  PedDisk *disk = ped_disk_new_fresh (dev, ped_disk_type_get (argv[1]));
 
38
  assert (disk);
 
39
  assert (ped_disk_commit(disk));
 
40
  ped_disk_destroy (disk);
 
41
 
 
42
  /* re-open the disk */
 
43
  disk = ped_disk_new (dev);
 
44
  assert (disk);
 
45
 
 
46
  /* Create a partition */
 
47
  const PedFileSystemType *fs_type = ped_file_system_type_get ("ext2");
 
48
  assert (fs_type);
 
49
  PedPartitionType part_type = PED_PARTITION_NORMAL;
 
50
  const PedGeometry *geometry = ped_geometry_new (dev, 34, 1024);
 
51
  assert (geometry);
 
52
  PedPartition *part = ped_partition_new (disk, part_type, fs_type,
 
53
                                          geometry->start, geometry->end);
 
54
  assert (part);
 
55
  PedConstraint *constraint = ped_constraint_exact (geometry);
 
56
  assert (constraint);
 
57
 
 
58
  if (ped_partition_is_flag_available (part, PED_PARTITION_BOOT))
 
59
    assert (ped_partition_set_flag (part, PED_PARTITION_BOOT, 1));
 
60
 
 
61
  assert (ped_disk_add_partition (disk, part, constraint));
 
62
  ped_constraint_destroy (constraint);
 
63
 
 
64
  assert (ped_partition_set_system (part, fs_type));
 
65
  if (ped_partition_is_flag_available (part, PED_PARTITION_LBA))
 
66
    ped_partition_set_flag (part, PED_PARTITION_LBA, 1);
 
67
 
 
68
  assert (ped_disk_commit(disk));
 
69
 
 
70
  /* Duplicate it */
 
71
  PedDisk *copy = ped_disk_duplicate (disk);
 
72
  assert (ped_disk_commit(copy));
 
73
 
 
74
  /* Compare the two copies */
 
75
 
 
76
  /* Check the device */
 
77
  assert (strcmp (disk->dev->model, copy->dev->model) == 0);
 
78
  assert (strcmp (disk->dev->path, copy->dev->path) == 0);
 
79
  assert (disk->dev->sector_size == copy->dev->sector_size);
 
80
  assert (disk->dev->phys_sector_size == copy->dev->phys_sector_size);
 
81
  assert (disk->dev->length == copy->dev->length);
 
82
 
 
83
  /* Check the type */
 
84
  assert (strcmp (disk->type->name, copy->type->name) == 0);
 
85
  assert (disk->type->features == copy->type->features);
 
86
 
 
87
  /* Check the flags */
 
88
  for (PedDiskFlag flag = PED_DISK_FIRST_FLAG; flag <= PED_DISK_LAST_FLAG;
 
89
       flag++) {
 
90
    if (!ped_disk_is_flag_available(disk, flag))
 
91
      continue;
 
92
    assert (ped_disk_get_flag (disk, flag) == ped_disk_get_flag (copy, flag));
 
93
  }
 
94
 
 
95
  /* Check the partitions */
 
96
  PedPartition *disk_part, *copy_part;
 
97
  for ( disk_part = disk->part_list, copy_part = copy->part_list;
 
98
        disk_part && copy_part;
 
99
        disk_part = disk_part->next, copy_part = copy_part->next)
 
100
  {
 
101
    /* Only active partitions are duplicated */
 
102
    if (!ped_partition_is_active (disk_part))
 
103
      continue;
 
104
 
 
105
    assert (disk_part->geom.start == copy_part->geom.start);
 
106
    assert (disk_part->geom.end == copy_part->geom.end);
 
107
    assert (disk_part->geom.length == copy_part->geom.length);
 
108
    assert (disk_part->num == copy_part->num);
 
109
    assert (disk_part->type == copy_part->type);
 
110
 
 
111
    if (disk_part->fs_type && disk_part->fs_type->name) {
 
112
      assert (strcmp (disk_part->fs_type->name, copy_part->fs_type->name) == 0);
 
113
    }
 
114
 
 
115
    /* Check the flags */
 
116
    for (PedPartitionFlag flag = PED_PARTITION_FIRST_FLAG;
 
117
         flag <= PED_PARTITION_LAST_FLAG; flag++) {
 
118
      if (!ped_partition_is_flag_available(disk_part, flag))
 
119
        continue;
 
120
      fprintf (stderr, "Checking partition flag %d\n", flag);
 
121
      fprintf (stderr, "%d ? %d\n", ped_partition_get_flag (disk_part, flag),
 
122
               ped_partition_get_flag (copy_part, flag));
 
123
      assert (ped_partition_get_flag (disk_part, flag)
 
124
              == ped_partition_get_flag (copy_part, flag));
 
125
    }
 
126
  }
 
127
 
 
128
  /* Cleanup the mess */
 
129
  ped_disk_destroy (copy);
 
130
  ped_disk_destroy (disk);
 
131
  ped_device_destroy (dev);
 
132
 
 
133
  return EXIT_SUCCESS;
 
134
}