OSDN Git Service

ptt_clear_sectors: new function
[android-x86/external-parted.git] / libparted / labels / pt-tools.c
1 /* partition table tools
2    Copyright (C) 2008-2009 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include <parted/parted.h>
23 #include <parted/debug.h>
24
25 #include "pt-tools.h"
26
27 static char zero[16 * 1024];
28
29 /* Write a single sector to DISK, filling the first BUFLEN
30    bytes of that sector with data from BUF, and NUL-filling
31    any remaining bytes.  Return nonzero to indicate success,
32    zero otherwise.  */
33 int
34 ptt_write_sector (PedDisk const *disk, void const *buf, size_t buflen)
35 {
36   PED_ASSERT (buflen <= disk->dev->sector_size, return 0);
37   /* Allocate a big enough buffer for ped_device_write.  */
38   char *s0 = ped_malloc (disk->dev->sector_size);
39   if (s0 == NULL)
40     return 0;
41   /* Copy boot_code into the first part.  */
42   memcpy (s0, buf, buflen);
43   char *p = s0 + buflen;
44   /* Fill the rest with zeros.  */
45   memset (p, 0, disk->dev->sector_size - buflen);
46   int write_ok = ped_device_write (disk->dev, s0, 0, 1);
47   free (s0);
48
49   return write_ok;
50 }
51
52 /* Read sector, SECTOR_NUM (which has length DEV->sector_size) into malloc'd
53    storage.  If the read fails, free the memory and return zero without
54    modifying *BUF.  Otherwise, set *BUF to the new buffer and return 1.  */
55 int
56 ptt_read_sector (PedDevice const *dev, PedSector sector_num, void **buf)
57 {
58   char *b = ped_malloc (dev->sector_size);
59   PED_ASSERT (b != NULL, return 0);
60   if (!ped_device_read (dev, b, sector_num, 1)) {
61     free (b);
62     return 0;
63   }
64   *buf = b;
65   return 1;
66 }
67
68 /* Zero N sectors of DEV, starting with START.
69    Return nonzero to indicate success, zero otherwise.  */
70 int
71 ptt_clear_sectors (PedDevice *dev, PedSector start, PedSector n)
72 {
73   PED_ASSERT (dev->sector_size <= sizeof zero, return 0);
74   PedSector n_z_sectors = sizeof zero / dev->sector_size;
75   PedSector n_full = n / n_z_sectors;
76   PedSector i;
77   for (i = 0; i < n_full; i++)
78     {
79       if (!ped_device_write (dev, zero, start + n_z_sectors * i, n_z_sectors))
80         return 0;
81     }
82
83   PedSector rem = n - n_z_sectors * i;
84   return (rem == 0
85           ? 1 : ped_device_write (dev, zero, start + n_z_sectors * i, rem));
86 }