2 libparted - a library for manipulating disk partitions
3 Copyright (C) 1999-2000, 2007-2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <parted/parted.h>
22 #include <parted/debug.h>
23 #include <parted/endian.h>
30 # define _(String) dgettext (PACKAGE, String)
32 # define _(String) (String)
33 #endif /* ENABLE_NLS */
35 #define LOOP_SIGNATURE "GNU Parted Loopback 0"
37 static PedDiskType loop_disk_type;
39 static PedDisk* loop_alloc (const PedDevice* dev);
40 static void loop_free (PedDisk* disk);
43 loop_probe (const PedDevice* dev)
45 PedDisk *disk = loop_alloc (dev);
50 if (!ptt_read_sector (dev, 0, &buf))
51 goto error_destroy_disk;
52 int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE));
61 geom = ped_geometry_new (dev, 0, disk->dev->length);
63 goto error_destroy_disk;
64 result = ped_file_system_probe (geom) != NULL;
65 ped_geometry_destroy (geom);
77 loop_alloc (const PedDevice* dev)
79 PED_ASSERT (dev != NULL, return 0);
81 if (dev->length < 256)
83 return _ped_disk_alloc ((PedDevice*)dev, &loop_disk_type);
87 loop_duplicate (const PedDisk* disk)
89 return ped_disk_new_fresh (disk->dev, &loop_disk_type);
93 loop_free (PedDisk* disk)
95 PED_ASSERT (disk != NULL, return);
97 _ped_disk_free (disk);
101 loop_read (PedDisk* disk)
103 PedDevice* dev = NULL;
105 PedFileSystemType* fs_type;
107 PedConstraint* constraint_any;
109 PED_ASSERT (disk != NULL, return 0);
111 constraint_any = ped_constraint_any (dev);
113 ped_disk_delete_all (disk);
116 if (!ptt_read_sector (dev, 0, &buf))
119 int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE));
123 ped_constraint_destroy (constraint_any);
127 geom = ped_geometry_new (dev, 0, dev->length);
131 fs_type = ped_file_system_probe (geom);
133 goto error_free_geom;
135 part = ped_partition_new (disk, PED_PARTITION_NORMAL,
136 fs_type, geom->start, geom->end);
137 ped_geometry_destroy (geom);
140 part->fs_type = fs_type;
142 if (!ped_disk_add_partition (disk, part, constraint_any))
144 ped_constraint_destroy (constraint_any);
148 ped_geometry_destroy (geom);
150 ped_constraint_destroy (constraint_any);
154 #ifndef DISCOVER_ONLY
156 loop_write (const PedDisk* disk)
158 size_t buflen = disk->dev->sector_size;
159 char *buf = ped_malloc (buflen);
163 if (ped_disk_get_partition (disk, 1)) {
164 if (!ped_device_read (disk->dev, buf, 0, 1)) {
168 if (strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE)) != 0) {
172 memset (buf, 0, strlen (LOOP_SIGNATURE));
173 return ped_device_write (disk->dev, buf, 0, 1);
176 memset (buf, 0, buflen);
177 strcpy (buf, LOOP_SIGNATURE);
179 int write_ok = ped_device_write (disk->dev, buf, 0, 1);
183 #endif /* !DISCOVER_ONLY */
186 loop_partition_new (const PedDisk* disk, PedPartitionType part_type,
187 const PedFileSystemType* fs_type,
188 PedSector start, PedSector end)
192 part = _ped_partition_alloc (disk, part_type, fs_type, start, end);
195 part->disk_specific = NULL;
200 loop_partition_duplicate (const PedPartition* part)
202 PedPartition* result;
204 result = ped_partition_new (part->disk, part->type, part->fs_type,
205 part->geom.start, part->geom.end);
206 result->num = part->num;
211 loop_partition_destroy (PedPartition* part)
217 loop_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
219 part->fs_type = fs_type;
224 loop_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state)
230 loop_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
236 loop_partition_align (PedPartition* part, const PedConstraint* constraint)
238 PedGeometry* new_geom;
240 new_geom = ped_constraint_solve_nearest (constraint, &part->geom);
242 ped_exception_throw (
244 PED_EXCEPTION_CANCEL,
245 _("Unable to satisfy all constraints on the "
249 ped_geometry_set (&part->geom, new_geom->start, new_geom->length);
250 ped_geometry_destroy (new_geom);
255 loop_partition_is_flag_available (const PedPartition* part,
256 PedPartitionFlag flag)
262 loop_partition_enumerate (PedPartition* part)
269 loop_alloc_metadata (PedDisk* disk)
275 loop_get_max_primary_partition_count (const PedDisk* disk)
281 loop_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
287 #include "pt-common.h"
288 PT_define_limit_functions (loop)
290 static PedDiskOps loop_disk_ops = {
292 write: NULL_IF_DISCOVER_ONLY (loop_write),
294 partition_set_name: NULL,
295 partition_get_name: NULL,
297 PT_op_function_initializers (loop)
300 static PedDiskType loop_disk_type = {
308 ped_disk_loop_init ()
310 ped_disk_type_register (&loop_disk_type);
314 ped_disk_loop_done ()
316 ped_disk_type_unregister (&loop_disk_type);