OSDN Git Service

factor out the read_sector function
authorJim Meyering <meyering@redhat.com>
Tue, 15 Jul 2008 12:47:22 +0000 (14:47 +0200)
committerJim Meyering <meyering@redhat.com>
Fri, 24 Jul 2009 13:04:41 +0000 (15:04 +0200)
* libparted/labels/pt-tools.c (ptt_read_sector): New function.
Factored out of...
* libparted/labels/aix.c (aix_probe, aix_clobber, read_sector):
* libparted/labels/bsd.c (bsd_probe, bsd_clobber, bsd_read)
(_probe_and_add_boot_code, read_sector):
* libparted/labels/dos.c (_, msdos_probe, msdos_clobber)
(read_table, msdos_write, msdos_disk_type, read_sector):
* libparted/labels/gpt.c (gpt_probe, gpt_disk_type, read_sector):
* libparted/labels/loop.c (loop_probe, loop_read, loop_disk_type)
(read_sector):
* libparted/labels/mac.c (_, mac_probe, mac_read)
(write_block_zero, mac_disk_type, read_sector):
* libparted/labels/pt-tools.h: Declare.

libparted/labels/aix.c
libparted/labels/bsd.c
libparted/labels/dos.c
libparted/labels/gpt.c
libparted/labels/loop.c
libparted/labels/mac.c
libparted/labels/pt-tools.c
libparted/labels/pt-tools.h

index de81270..eb2a14b 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
     libparted - a library for manipulating disk partitions
-    Copyright (C) 2000, 2001, 2007, 2009 Free Software Foundation, Inc.
+    Copyright (C) 2000-2001, 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
@@ -25,6 +25,7 @@
 #include <parted/debug.h>
 #include <parted/endian.h>
 #include <stdbool.h>
+#include "pt-tools.h"
 
 #if ENABLE_NLS
 #  include <libintl.h>
@@ -50,29 +51,13 @@ aix_label_magic_set (char *label, int magic_val)
        *(unsigned int *)label = magic_val;
 }
 
-/* Read a single sector, of 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
-read_sector (const PedDevice *dev, char **buf)
-{
-       char *b = ped_malloc (dev->sector_size);
-       PED_ASSERT (b != NULL, return 0);
-       if (!ped_device_read (dev, b, 0, 1)) {
-               free (b);
-               return 0;
-       }
-       *buf = b;
-       return 1;
-}
-
 static int
 aix_probe (const PedDevice *dev)
 {
        PED_ASSERT (dev != NULL, return 0);
 
-       char *label;
-       if (!read_sector (dev, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
        unsigned int magic = aix_label_magic_get (label);
        free (label);
@@ -88,8 +73,8 @@ aix_clobber (PedDevice* dev)
        if (!aix_probe (dev))
                return 0;
 
-       char *label;
-       if (!read_sector (dev, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
        aix_label_magic_set (label, 0);
index b59a06e..99e63e0 100644 (file)
@@ -148,23 +148,6 @@ alpha_bootblock_checksum (char *boot) {
        dp[63] = sum;
 }
 
-/* FIXME: factor out this function: copied from dos.c
-   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
-read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
-{
-       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;
-}
-
 static int
 bsd_probe (const PedDevice *dev)
 {
@@ -175,11 +158,11 @@ bsd_probe (const PedDevice *dev)
         if (dev->sector_size < 512)
                 return 0;
 
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
-       partition = (BSDRawLabel *) (label + BSD_LABEL_OFFSET);
+       partition = (BSDRawLabel *) ((char *) label + BSD_LABEL_OFFSET);
 
        alpha_bootblock_checksum(label);
 
@@ -275,10 +258,11 @@ bsd_free (PedDisk* disk)
 static int
 bsd_clobber (PedDevice* dev)
 {
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
-       BSDRawLabel *rawlabel = (BSDRawLabel *) (label + BSD_LABEL_OFFSET);
+       BSDRawLabel *rawlabel
+         = (BSDRawLabel *) ((char *) label + BSD_LABEL_OFFSET);
        rawlabel->d_magic = 0;
        return ped_device_write (dev, label, 0, 1);
 }
@@ -293,8 +277,8 @@ bsd_read (PedDisk* disk)
 
        ped_disk_delete_all (disk);
 
-       char *s0;
-       if (!read_sector (disk->dev, 0, &s0))
+       void *s0;
+       if (!ptt_read_sector (disk->dev, 0, &s0))
                return 0;
 
        memcpy (bsd_specific->boot_code, s0, sizeof (bsd_specific->boot_code));
@@ -339,8 +323,8 @@ error:
 static void
 _probe_and_add_boot_code (const PedDisk* disk)
 {
-       char *s0;
-       if (!read_sector (disk->dev, 0, &s0))
+       void *s0;
+       if (!ptt_read_sector (disk->dev, 0, &s0))
                return;
        char *old_boot_code = s0;
        BSDRawLabel *old_label
index c318604..a0fdcac 100644 (file)
@@ -33,6 +33,7 @@
 #endif /* ENABLE_NLS */
 
 #include "misc.h"
+#include "pt-tools.h"
 
 /* this MBR boot code is loaded into 0000:7c00 by the BIOS.  See mbr.s for
  * the source, and how to build it
@@ -153,24 +154,6 @@ typedef struct {
 
 static PedDiskType msdos_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
-read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
-{
-       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;
-}
-
 static int
 msdos_probe (const PedDevice *dev)
 {
@@ -183,8 +166,8 @@ msdos_probe (const PedDevice *dev)
         if (dev->sector_size < sizeof *part_table)
                 return 0;
 
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
        part_table = (DosRawTable *) label;
@@ -274,8 +257,8 @@ msdos_clobber (PedDevice* dev)
        PED_ASSERT (dev != NULL, return 0);
        PED_ASSERT (msdos_probe (dev), return 0);
 
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
        DosRawTable *table = (DosRawTable *) label;
@@ -846,8 +829,8 @@ read_table (PedDisk* disk, PedSector sector, int is_extended_table)
        PED_ASSERT (disk != NULL, return 0);
        PED_ASSERT (disk->dev != NULL, return 0);
 
-       char *label = NULL;
-       if (!read_sector (disk->dev, sector, &label))
+       void *label = NULL;
+       if (!ptt_read_sector (disk->dev, sector, &label))
                goto error;
 
         table = (DosRawTable *) label;
@@ -1142,8 +1125,8 @@ msdos_write (const PedDisk* disk)
        PED_ASSERT (disk != NULL, return 0);
        PED_ASSERT (disk->dev != NULL, return 0);
 
-       char *s0 = NULL;
-       if (!read_sector (disk->dev, 0, &s0))
+       void *s0 = NULL;
+       if (!ptt_read_sector (disk->dev, 0, &s0))
                return 0;
        DosRawTable *table = (DosRawTable *) s0;
 
index a29af87..f69c556 100644 (file)
@@ -40,6 +40,8 @@
 #include <stdbool.h>
 #include "xalloc.h"
 
+#include "pt-tools.h"
+
 #if ENABLE_NLS
 #  include <libintl.h>
 #  define _(String) gettext (String)
@@ -259,23 +261,6 @@ typedef struct _GPTPartitionData {
 static PedDiskType gpt_disk_type;
 
 
-/* FIXME: factor out this function: copied from dos.c
-   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
-read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
-{
-       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;
-}
-
 static inline uint32_t
 pth_get_size (const PedDevice* dev)
 {
@@ -468,8 +453,8 @@ gpt_probe (const PedDevice * dev)
                return 0;
 
 
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
         int ok = 1;
index 165de38..44b73f0 100644 (file)
@@ -23,6 +23,8 @@
 #include <parted/endian.h>
 #include <stdbool.h>
 
+#include "pt-tools.h"
+
 #if ENABLE_NLS
 #  include <libintl.h>
 #  define _(String) dgettext (PACKAGE, String)
@@ -37,23 +39,6 @@ static PedDiskType loop_disk_type;
 static PedDisk* loop_alloc (const PedDevice* dev);
 static void loop_free (PedDisk* disk);
 
-/* FIXME: factor out this function: copied from dos.c
-   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
-read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
-{
-       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;
-}
-
 static int
 loop_probe (const PedDevice* dev)
 {
@@ -61,8 +46,8 @@ loop_probe (const PedDevice* dev)
        if (!disk)
                goto error;
 
-       char *buf;
-       if (!read_sector (dev, 0, &buf))
+       void *buf;
+       if (!ptt_read_sector (dev, 0, &buf))
                goto error_destroy_disk;
         int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE));
         free (buf);
@@ -146,8 +131,8 @@ loop_read (PedDisk* disk)
 
        ped_disk_delete_all (disk);
 
-       char *buf;
-       if (!read_sector (dev, 0, &buf))
+       void *buf;
+       if (!ptt_read_sector (dev, 0, &buf))
                goto error;
 
         int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE));
index e098497..bb9595c 100644 (file)
@@ -31,6 +31,7 @@
 #endif /* ENABLE_NLS */
 
 #include "misc.h"
+#include "pt-tools.h"
 
 /* struct's hacked from Linux source:  fs/partitions/mac.h
  * I believe it was originally written by Paul Mackerras (from comments in
@@ -170,23 +171,6 @@ struct _MacDiskData {
 
 static PedDiskType mac_disk_type;
 
-/* FIXME: factor out this function: copied from dos.c
-   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
-read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
-{
-       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;
-}
-
 static int
 _check_signature (MacRawDisk const *raw_disk)
 {
@@ -221,8 +205,8 @@ mac_probe (const PedDevice * dev)
         if (dev->sector_size < sizeof (MacRawDisk))
                 return 0;
 
-       char *label;
-       if (!read_sector (dev, 0, &label))
+       void *label;
+       if (!ptt_read_sector (dev, 0, &label))
                return 0;
 
        int valid = _check_signature ((MacRawDisk *) &label);
@@ -777,8 +761,8 @@ mac_read (PedDisk* disk)
        mac_disk_data = disk->disk_specific;
        mac_disk_data->part_map_entry_num = 0;          /* 0 == none */
 
-       char *s0;
-       if (!read_sector (disk->dev, 0, &s0))
+       void *s0;
+       if (!ptt_read_sector (disk->dev, 0, &s0))
                return 0;
 
        MacRawDisk *raw_disk = (MacRawDisk *) s0;
@@ -1055,8 +1039,8 @@ static int
 write_block_zero (PedDisk* disk, MacDiskData* mac_driverdata)
 {
        PedDevice*      dev = disk->dev;
-       char *s0;
-       if (!read_sector (dev, 0, &s0))
+       void *s0;
+       if (!ptt_read_sector (dev, 0, &s0))
                return 0;
        MacRawDisk *raw_disk = (MacRawDisk *) s0;
 
index 1ecdee5..50fa55d 100644 (file)
@@ -46,3 +46,19 @@ 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.  */
+int
+ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
+{
+  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;
+}
index ec870ec..0af8e73 100644 (file)
@@ -18,3 +18,4 @@
 #include <parted/disk.h>
 
 int ptt_write_sector (PedDisk const *disk, void const *buf, size_t buflen);
+int ptt_read_sector (const PedDevice *dev, PedSector sector_num, void **buf);