* 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.
/* -*- 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
#include <parted/debug.h>
#include <parted/endian.h>
#include <stdbool.h>
+#include "pt-tools.h"
#if ENABLE_NLS
# include <libintl.h>
*(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);
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);
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)
{
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);
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);
}
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));
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
#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
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)
{
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;
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;
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;
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;
#include <stdbool.h>
#include "xalloc.h"
+#include "pt-tools.h"
+
#if ENABLE_NLS
# include <libintl.h>
# define _(String) gettext (String)
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)
{
return 0;
- char *label;
- if (!read_sector (dev, 0, &label))
+ void *label;
+ if (!ptt_read_sector (dev, 0, &label))
return 0;
int ok = 1;
#include <parted/endian.h>
#include <stdbool.h>
+#include "pt-tools.h"
+
#if ENABLE_NLS
# include <libintl.h>
# define _(String) dgettext (PACKAGE, String)
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)
{
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);
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));
#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
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)
{
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);
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;
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;
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;
+}
#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);