OSDN Git Service

tests: avoid spurious failure due to extra space in diagnostic
[android-x86/external-parted.git] / libparted / labels / dvh.c
index 3e377ef..c92709e 100644 (file)
@@ -1,6 +1,6 @@
 /*
     libparted - a library for manipulating disk partitions
-    Copyright (C) 2001, 2002, 2005, 2007-2008 Free Software Foundation, Inc.
+    Copyright (C) 2001, 2002, 2005, 2007-2009 Free Software Foundation, Inc.
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include <parted/parted.h>
 #include <parted/debug.h>
 #include <parted/endian.h>
+#include <stdbool.h>
 
 #include "dvh.h"
+#include "pt-tools.h"
 
 #if ENABLE_NLS
 #  include <libintl.h>
@@ -58,28 +60,50 @@ typedef struct _DVHPartData {
 
 static PedDiskType dvh_disk_type;
 
+/* FIXME: factor out this function: copied from aix.c, with changes to
+   the description, and an added sector number argument.
+   Read sector, SECTOR_NUM (which has length DEV->sector_size) into malloc'd
+   storage.  If the read fails, free the memory and return zero without
+   modifying *BUF.  Otherwise, set *BUF to the new buffer and return 1.  */
 static int
-dvh_probe (const PedDevice *dev)
+read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
 {
-       struct volume_header    vh;
+       char *b = ped_malloc (dev->sector_size);
+       PED_ASSERT (b != NULL, return 0);
+       if (!ped_device_read (dev, b, sector_num, 1)) {
+               free (b);
+               return 0;
+       }
+       *buf = b;
+       return 1;
+}
 
-        if (dev->sector_size != 512)
-                return 0;
+static int
+dvh_probe (const PedDevice *dev)
+{
+       struct volume_header *vh;
 
-       if (!ped_device_read (dev, &vh, 0, 1))
+       char *label;
+       if (!read_sector (dev, 0, &label))
                return 0;
 
-       return PED_BE32_TO_CPU (vh.vh_magic) == VHMAGIC;
+       vh = (struct volume_header *) label;
+
+        bool found = PED_BE32_TO_CPU (vh->vh_magic) == VHMAGIC;
+       free (label);
+       return found;
 }
 
 #ifndef DISCOVER_ONLY
 static int
 dvh_clobber (PedDevice* dev)
 {
-       char    zeros[512];
-
-       memset (zeros, 0, 512);
-       return ped_device_write (dev, zeros, 0, 1);
+       char *zeros = ped_calloc (dev->sector_size);
+       if (zeros == NULL)
+                return 0;
+       int ok = ped_device_write (dev, zeros, 0, 1);
+       free (zeros);
+       return ok;
 }
 #endif /* !DISCOVER_ONLY */
 
@@ -121,9 +145,9 @@ error_destroy_constraint_any:
        ped_constraint_destroy (constraint_any);
        ped_partition_destroy (volume_part);
 error_free_disk_specific:
-       ped_free (disk->disk_specific);
+       free (disk->disk_specific);
 error_free_disk:
-       ped_free (disk);
+       free (disk);
 error:
        return NULL;
 }
@@ -137,7 +161,7 @@ dvh_duplicate (const PedDisk* disk)
 
        PED_ASSERT (old_dvh_disk_data != NULL, goto error);
 
-       new_disk = _ped_disk_alloc (disk->dev, &dvh_disk_type);
+       new_disk = ped_disk_new_fresh (disk->dev, &dvh_disk_type);
        if (!new_disk)
                goto error;
 
@@ -150,7 +174,7 @@ dvh_duplicate (const PedDisk* disk)
        return new_disk;
 
 error_free_new_disk:
-       ped_free (new_disk);
+       free (new_disk);
 error:
        return NULL;
 }
@@ -158,7 +182,7 @@ error:
 static void
 dvh_free (PedDisk* disk)
 {
-       ped_free (disk->disk_specific);
+       free (disk->disk_specific);
        _ped_disk_free (disk);
 }
 
@@ -302,8 +326,11 @@ dvh_read (PedDisk* disk)
 
        ped_disk_delete_all (disk);
 
-       if (!ped_device_read (disk->dev, &vh, 0, 1))
+       char *s0;
+       if (!read_sector (disk->dev, 0, &s0))
                return 0;
+       memcpy (&vh, s0, sizeof vh);
+       free (s0);
 
        if (_checksum ((uint32_t*) &vh, sizeof (struct volume_header))) {
                if (ped_exception_throw (
@@ -474,8 +501,8 @@ dvh_write (const PedDisk* disk)
        }
 
        /* whole disk partition
-        * This is only ever written here, and never modified 
-        * (or even shown) as it must contain the entire disk, 
+        * This is only ever written here, and never modified
+        * (or even shown) as it must contain the entire disk,
         * and parted does not like overlapping partitions
         */
        vh.vh_pt[PNUM_VOLUME].pt_nblks = PED_CPU_TO_BE32 (disk->dev->length);
@@ -493,8 +520,8 @@ dvh_write (const PedDisk* disk)
        vh.vh_csum = PED_CPU_TO_BE32 (_checksum ((uint32_t*) &vh,
                                      sizeof (struct volume_header)));
 
-       return ped_device_write (disk->dev, &vh, 0, 1)
-              && ped_device_sync (disk->dev);
+        return (ptt_write_sector (disk, &vh, sizeof vh)
+                && ped_device_sync (disk->dev));
 }
 #endif /* !DISCOVER_ONLY */
 
@@ -572,7 +599,7 @@ dvh_partition_destroy (PedPartition* part)
 {
        if (ped_partition_is_active (part)) {
                PED_ASSERT (part->disk_specific != NULL, return);
-               ped_free (part->disk_specific);
+               free (part->disk_specific);
        }
        _ped_partition_free (part);
 }
@@ -822,6 +849,14 @@ dvh_get_max_primary_partition_count (const PedDisk* disk)
        return NPARTAB;
 }
 
+static bool
+dvh_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
+{
+       *max_n = NPARTAB;
+       return true;
+}
+
+
 static int
 dvh_alloc_metadata (PedDisk* disk)
 {
@@ -857,6 +892,21 @@ error:
        return 0;
 }
 
+/*
+ * Enforce some restrictions inherent in the dvh partition table format.
+ * 1. Partition size must be smaller than 2^32 (unsigned int) sectors.
+ *    If sector size is 512 bytes, this results in 2T aprox.
+ * 2. Partition starting sector number must be smaller than 2^32.
+ */
+static bool
+dvh_partition_check (const PedPartition* part)
+{
+       if (!ptt_partition_max_start_len("dvh", part))
+               return false;
+
+       return true;
+}
+
 static PedDiskOps dvh_disk_ops = {
        probe:                  dvh_probe,
 #ifndef DISCOVER_ONLY
@@ -885,10 +935,11 @@ static PedDiskOps dvh_disk_ops = {
        partition_get_name:     dvh_partition_get_name,
        partition_align:        dvh_partition_align,
        partition_enumerate:    dvh_partition_enumerate,
+       partition_check:        dvh_partition_check,
 
        alloc_metadata:         dvh_alloc_metadata,
-       get_max_primary_partition_count:
-                               dvh_get_max_primary_partition_count
+       get_max_primary_partition_count: dvh_get_max_primary_partition_count,
+       get_max_supported_partition_count: dvh_get_max_supported_partition_count
 };
 
 static PedDiskType dvh_disk_type = {