OSDN Git Service

maint: update all copyright year number ranges
[android-x86/external-parted.git] / libparted / labels / pt-tools.c
index 622cedd..a8ac4e4 100644 (file)
@@ -1,5 +1,5 @@
 /* partition table tools
-   Copyright (C) 2008-2009 Free Software Foundation, Inc.
+   Copyright (C) 2008-2012 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
@@ -40,7 +40,7 @@ static char zero[16 * 1024];
 int
 ptt_write_sector (PedDisk const *disk, void const *buf, size_t buflen)
 {
-  PED_ASSERT (buflen <= disk->dev->sector_size, return 0);
+  PED_ASSERT (buflen <= disk->dev->sector_size);
   /* Allocate a big enough buffer for ped_device_write.  */
   char *s0 = ped_malloc (disk->dev->sector_size);
   if (s0 == NULL)
@@ -56,15 +56,17 @@ ptt_write_sector (PedDisk const *disk, void const *buf, size_t buflen)
   return write_ok;
 }
 
-/* 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.  */
+/* Read N sectors, starting with 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.  */
 int
-ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
+ptt_read_sectors (PedDevice const *dev, PedSector start_sector,
+                 PedSector n_sectors, void **buf)
 {
-  char *b = ped_malloc (dev->sector_size);
-  PED_ASSERT (b != NULL, return 0);
-  if (!ped_device_read (dev, b, sector_num, 1)) {
+  char *b = ped_malloc (n_sectors * dev->sector_size);
+  PED_ASSERT (b != NULL);
+  if (!ped_device_read (dev, b, start_sector, n_sectors)) {
     free (b);
     return 0;
   }
@@ -72,12 +74,21 @@ ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
   return 1;
 }
 
+/* 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.  */
+int
+ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
+{
+  return ptt_read_sectors (dev, sector_num, 1, buf);
+}
+
 /* Zero N sectors of DEV, starting with START.
    Return nonzero to indicate success, zero otherwise.  */
 int
 ptt_clear_sectors (PedDevice *dev, PedSector start, PedSector n)
 {
-  PED_ASSERT (dev->sector_size <= sizeof zero, return 0);
+  PED_ASSERT (dev->sector_size <= sizeof zero);
   PedSector n_z_sectors = sizeof zero / dev->sector_size;
   PedSector n_full = n / n_z_sectors;
   PedSector i;
@@ -92,46 +103,75 @@ ptt_clear_sectors (PedDevice *dev, PedSector start, PedSector n)
           ? 1 : ped_device_write (dev, zero, start + n_z_sectors * i, rem));
 }
 
+#include "pt-limit.c"
+
 /* Throw an exception and return 0 if PART's starting sector number or
    its length is greater than the maximum allowed value for LABEL_TYPE.
    Otherwise, return 1.  */
 int
-ptt_partition_max_start_len (char const *label_type, const PedPartition *part)
+ptt_partition_max_start_len (char const *pt_type, const PedPartition *part)
 {
-  static char const *const max_32[] = {"msdos", "dvh"};
-  unsigned int i;
+  struct partition_limit const *pt_lim
+    = pt_limit_lookup (pt_type, strlen (pt_type));
+
+  /* If we don't have info on the type, return "true".  */
+  if (pt_lim == NULL)
+    return 1;
 
-  for (i = 0; i < sizeof max_32 / sizeof *max_32; i++)
+  /* If the length in sectors exceeds the limit, you lose.  */
+  if (part->geom.length > pt_lim->max_length)
     {
-      if (strcmp (label_type, max_32[i]) == 0)
-        {
-          /* The starting sector length must fit in 32 bytes.  */
-          if (part->geom.length > UINT32_MAX)
-            {
-              ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-                                   _("partition length of %jd sectors exceeds"
-                                     " the %s-partition-table-imposed maximum"
-                                     " of %jd"),
-                                   part->geom.length,
-                                   label_type,
-                                   UINT32_MAX);
-              return 0;
-            }
-
-          /* The starting sector number must fit in 32 bytes.  */
-          if (part->geom.start > UINT32_MAX) {
-            ped_exception_throw (
-                                 PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-                                 _("starting sector number, %jd exceeds"
-                                   " the %s-partition-table-imposed maximum"
-                                   " of %jd"),
-                                 part->geom.start,
-                                 label_type,
-                                 UINT32_MAX);
-            return 0;
-          }
-        }
+      ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+                          _("partition length of %jd sectors exceeds"
+                            " the %s-partition-table-imposed maximum"
+                            " of %jd"),
+                          part->geom.length,
+                          pt_type,
+                          pt_lim->max_length);
+      return 0;
     }
 
+  /* If the starting sector exceeds the limit, you lose.  */
+  if (part->geom.start > pt_lim->max_start_sector) {
+    ped_exception_throw (
+                        PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+                        _("starting sector number, %jd exceeds"
+                          " the %s-partition-table-imposed maximum"
+                          " of %jd"),
+                        part->geom.start,
+                        pt_type,
+                        pt_lim->max_start_sector);
+    return 0;
+  }
+
   return 1;
 }
+
+/* Set *MAX to the largest representation-imposed starting sector number
+   of a partition of type PT_TYPE and return 0.  If PT_TYPE is not
+   recognized, return -1.  */
+int
+ptt_partition_max_start_sector (char const *pt_type, PedSector *max)
+{
+  struct partition_limit const *pt_lim
+    = pt_limit_lookup (pt_type, strlen (pt_type));
+  if (pt_lim == NULL)
+    return -1;
+
+  *max = pt_lim->max_start_sector;
+  return 0;
+}
+
+/* Set *MAX to the maximum representable length of a partition of type
+   PT_TYPE and return 0.  If PT_TYPE is not recognized, return -1.  */
+int
+ptt_partition_max_length (char const *pt_type, PedSector *max)
+{
+  struct partition_limit const *pt_lim
+    = pt_limit_lookup (pt_type, strlen (pt_type));
+  if (pt_lim == NULL)
+    return -1;
+
+  *max = pt_lim->max_length;
+  return 0;
+}