/** * Initializes a pre-allocated piece of memory to contain a constraint * with the supplied default values. * * \return \c 0 on failure. */ int ped_constraint_init ( PedConstraint* constraint, const PedAlignment* start_align, const PedAlignment* end_align, const PedGeometry* start_range, const PedGeometry* end_range, PedSector min_size, PedSector max_size) { PED_ASSERT (constraint != NULL); PED_ASSERT (start_range != NULL); PED_ASSERT (end_range != NULL); PED_ASSERT (min_size > 0); PED_ASSERT (max_size > 0); constraint->start_align = ped_alignment_duplicate (start_align); constraint->end_align = ped_alignment_duplicate (end_align); constraint->start_range = ped_geometry_duplicate (start_range); constraint->end_range = ped_geometry_duplicate (end_range); constraint->min_size = min_size; constraint->max_size = max_size; return 1; }
/* 1:1 function mappings for geom.h in libparted */ PyObject *py_ped_geometry_duplicate(PyObject *s, PyObject *args) { PedGeometry *geometry = NULL, *geom = NULL; _ped_Geometry *ret = NULL; geometry = _ped_Geometry2PedGeometry(s); if (geometry == NULL) { return NULL; } geom = ped_geometry_duplicate(geometry); if (geom) { ret = PedGeometry2_ped_Geometry(geom); } else { if (partedExnRaised) { partedExnRaised = 0; if (!PyErr_ExceptionMatches(PartedException) && !PyErr_ExceptionMatches(PyExc_NotImplementedError)) PyErr_SetString(CreateException, partedExnMessage); } else PyErr_SetString(CreateException, "Could not duplicate geometry"); return NULL; } return (PyObject *) ret; }
static PedGeometry* _generic_apfs_probe (PedGeometry* geom, uint32_t kind) { uint32_t *block; PedSector root; struct PartitionBlock * part; uint32_t blocksize = 1, reserved = 2; PED_ASSERT (geom != NULL); PED_ASSERT (geom->dev != NULL); if (geom->dev->sector_size != 512) return NULL; /* Finds the blocksize and reserved values of the partition block */ if (!(part = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("%s : Failed to allocate partition block\n"), __func__); goto error_part; } if (amiga_find_part(geom, part) != NULL) { reserved = PED_BE32_TO_CPU (part->de_Reserved); blocksize = PED_BE32_TO_CPU (part->de_SizeBlock) * PED_BE32_TO_CPU (part->de_SectorPerBlock) / 128; } free (part); /* Test boot block */ if (!(block = ped_malloc (PED_SECTOR_SIZE_DEFAULT*blocksize))) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("%s : Failed to allocate block\n"), __func__); goto error_block; } if (!ped_device_read (geom->dev, block, geom->start, blocksize)) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("%s : Couldn't read boot block %llu\n"), __func__, geom->start); goto error; } if (PED_BE32_TO_CPU (block[0]) != kind) { goto error; } /* Find and test the root block */ root = geom->start+reserved*blocksize; if (!ped_device_read (geom->dev, block, root, blocksize)) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("%s : Couldn't read root block %llu\n"), __func__, root); goto error; } if (_apfs_probe_root(block, blocksize, kind) == 1) { free(block); return ped_geometry_duplicate (geom); } error: free (block); error_block: error_part: return NULL; }
/* This function erases all signatures that indicate the presence of * a file system in a particular region, without erasing any data * contained inside the "exclude" region. */ static int ped_file_system_clobber_exclude (PedGeometry* geom, const PedGeometry* exclude) { PedGeometry* clobber_geom; int status; if (ped_geometry_test_sector_inside (exclude, geom->start)) return 1; clobber_geom = ped_geometry_duplicate (geom); if (ped_geometry_test_overlap (clobber_geom, exclude)) ped_geometry_set_end (clobber_geom, exclude->start - 1); status = ped_file_system_clobber (clobber_geom); ped_geometry_destroy (clobber_geom); return status; }
static PedFileSystem *reiserfs_open(PedGeometry *geom) { PedFileSystem *fs; PedGeometry *fs_geom; dal_t *dal; reiserfs_fs_t *fs_info; PED_ASSERT(geom != NULL); if (!(fs_geom = ped_geometry_duplicate(geom))) goto error; if (! (dal = geom_dal_create(fs_geom, DEFAULT_BLOCK_SIZE, O_RDONLY))) goto error_fs_geom_free; /* We are passing NULL as DAL for journal. Therefore we let libreiserfs know, that journal not available and parted will be working fine for reiserfs with relocated journal too. */ if (!(fs = (PedFileSystem *) ped_malloc(sizeof(PedFileSystem)))) goto error_free_dal; if (!(fs_info = reiserfs_fs_open(dal, NULL))) goto error_free_fs; fs->type = reiserfs_type; fs->geom = fs_geom; fs->type_specific = (void *) fs_info; return fs; error_free_fs: free(fs); error_free_dal: geom_dal_free(dal); error_fs_geom_free: ped_geometry_destroy(fs_geom); error: return NULL; }
PedFileSystem * hfs_open (PedGeometry* geom) { uint8_t buf[PED_SECTOR_SIZE_DEFAULT]; PedFileSystem* fs; HfsMasterDirectoryBlock* mdb; HfsPrivateFSData* priv_data; if (!hfsc_can_use_geom (geom)) return NULL; /* Read MDB */ if (!ped_geometry_read (geom, buf, 2, 1)) return NULL; /* Allocate memory */ fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem)); if (!fs) goto ho; mdb = (HfsMasterDirectoryBlock*) ped_malloc (sizeof (HfsMasterDirectoryBlock)); if (!mdb) goto ho_fs; priv_data = (HfsPrivateFSData*) ped_malloc (sizeof (HfsPrivateFSData)); if (!priv_data) goto ho_mdb; memcpy (mdb, buf, sizeof (HfsMasterDirectoryBlock)); /* init structures */ priv_data->mdb = mdb; priv_data->bad_blocks_loaded = 0; priv_data->bad_blocks_xtent_nb = 0; priv_data->bad_blocks_xtent_list = NULL; priv_data->extent_file = hfs_file_open (fs, PED_CPU_TO_BE32 (HFS_XTENT_ID), mdb->extents_file_rec, PED_CPU_TO_BE32 (mdb->extents_file_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->extent_file) goto ho_pd; priv_data->catalog_file = hfs_file_open (fs, PED_CPU_TO_BE32 (HFS_CATALOG_ID), mdb->catalog_file_rec, PED_CPU_TO_BE32 (mdb->catalog_file_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->catalog_file) goto ho_ce; /* Read allocation blocks */ if (!ped_geometry_read(geom, priv_data->alloc_map, PED_BE16_TO_CPU (mdb->volume_bitmap_block), ( PED_BE16_TO_CPU (mdb->total_blocks) + PED_SECTOR_SIZE_DEFAULT * 8 - 1 ) / (PED_SECTOR_SIZE_DEFAULT * 8) ) ) goto ho_cf; fs->type = &hfs_type; fs->geom = ped_geometry_duplicate (geom); if (!fs->geom) goto ho_cf; fs->type_specific = (void*) priv_data; fs->checked = ( PED_BE16_TO_CPU (mdb->volume_attributes) >> HFS_UNMOUNTED ) & 1; return fs; /*--- clean error handling ---*/ ho_cf: hfs_file_close(priv_data->catalog_file); ho_ce: hfs_file_close(priv_data->extent_file); ho_pd: free(priv_data); ho_mdb: free(mdb); ho_fs: free(fs); ho: return NULL; }
PedFileSystem* hfsplus_open (PedGeometry* geom) { uint8_t buf[PED_SECTOR_SIZE_DEFAULT]; PedFileSystem* fs; HfsPVolumeHeader* vh; HfsPPrivateFSData* priv_data; PedGeometry* wrapper_geom; unsigned int map_sectors; if (!hfsc_can_use_geom (geom)) return NULL; fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem)); if (!fs) goto hpo; vh = (HfsPVolumeHeader*) ped_malloc (sizeof (HfsPVolumeHeader)); if (!vh) goto hpo_fs; priv_data = (HfsPPrivateFSData*)ped_malloc (sizeof (HfsPPrivateFSData)); if (!priv_data) goto hpo_vh; fs->geom = ped_geometry_duplicate (geom); if (!fs->geom) goto hpo_pd; fs->type_specific = (void*) priv_data; if ((wrapper_geom = hfs_and_wrapper_probe (geom))) { HfsPrivateFSData* hfs_priv_data; PedSector abs_sect, length; unsigned int bs; ped_geometry_destroy (wrapper_geom); priv_data->wrapper = hfs_open(geom); if (!priv_data->wrapper) goto hpo_gm; hfs_priv_data = (HfsPrivateFSData*) priv_data->wrapper->type_specific; bs = PED_BE32_TO_CPU (hfs_priv_data->mdb->block_size) / PED_SECTOR_SIZE_DEFAULT; abs_sect = (PedSector) geom->start + (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->start_block) + (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new .embedded.location.start_block ) * bs; length = (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new .embedded.location.block_count) * bs; priv_data->plus_geom = ped_geometry_new (geom->dev, abs_sect, length); if (!priv_data->plus_geom) goto hpo_wr; priv_data->free_geom = 1; } else { priv_data->wrapper = NULL; priv_data->plus_geom = fs->geom; priv_data->free_geom = 0; } if (!ped_geometry_read (priv_data->plus_geom, buf, 2, 1)) goto hpo_pg; memcpy (vh, buf, sizeof (HfsPVolumeHeader)); priv_data->vh = vh; if (vh->signature != PED_CPU_TO_BE16(HFSP_SIGNATURE) && vh->signature != PED_CPU_TO_BE16(HFSX_SIGNATURE)) { ped_exception_throw ( PED_EXCEPTION_BUG, PED_EXCEPTION_CANCEL, _("No valid HFS[+X] signature has been found while " "opening.")); goto hpo_pg; } if (vh->signature == PED_CPU_TO_BE16(HFSP_SIGNATURE) && vh->version != PED_CPU_TO_BE16(HFSP_VERSION)) { if (ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_IGNORE_CANCEL, _("Version %d of HFS+ isn't supported."), PED_BE16_TO_CPU(vh->version)) != PED_EXCEPTION_IGNORE) goto hpo_pg; } if (vh->signature == PED_CPU_TO_BE16(HFSX_SIGNATURE) && vh->version != PED_CPU_TO_BE16(HFSX_VERSION)) { if (ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_IGNORE_CANCEL, _("Version %d of HFSX isn't supported."), PED_BE16_TO_CPU(vh->version)) != PED_EXCEPTION_IGNORE) goto hpo_pg; } priv_data->jib_start_block = 0; priv_data->jl_start_block = 0; if (vh->attributes & PED_CPU_TO_BE32(1<<HFSP_JOURNALED)) { if (!hfsj_replay_journal(fs)) goto hpo_pg; } priv_data->bad_blocks_loaded = 0; priv_data->bad_blocks_xtent_nb = 0; priv_data->bad_blocks_xtent_list = NULL; priv_data->extents_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFS_XTENT_ID), vh->extents_file.extents, PED_BE64_TO_CPU ( vh->extents_file.logical_size ) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->extents_file) goto hpo_pg; priv_data->catalog_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFS_CATALOG_ID), vh->catalog_file.extents, PED_BE64_TO_CPU ( vh->catalog_file.logical_size ) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->catalog_file) goto hpo_ce; priv_data->attributes_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFSP_ATTRIB_ID), vh->attributes_file.extents, PED_BE64_TO_CPU ( vh->attributes_file.logical_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->attributes_file) goto hpo_cc; map_sectors = ( PED_BE32_TO_CPU (vh->total_blocks) + PED_SECTOR_SIZE_DEFAULT * 8 - 1 ) / (PED_SECTOR_SIZE_DEFAULT * 8); priv_data->dirty_alloc_map = (uint8_t*) ped_malloc ((map_sectors + 7) / 8); if (!priv_data->dirty_alloc_map) goto hpo_cl; memset(priv_data->dirty_alloc_map, 0, (map_sectors + 7) / 8); priv_data->alloc_map = (uint8_t*) ped_malloc (map_sectors * PED_SECTOR_SIZE_DEFAULT); if (!priv_data->alloc_map) goto hpo_dm; priv_data->allocation_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFSP_ALLOC_ID), vh->allocation_file.extents, PED_BE64_TO_CPU ( vh->allocation_file.logical_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->allocation_file) goto hpo_am; if (!hfsplus_file_read (priv_data->allocation_file, priv_data->alloc_map, 0, map_sectors)) { hfsplus_close(fs); return NULL; } fs->type = &hfsplus_type; fs->checked = ((PED_BE32_TO_CPU (vh->attributes) >> HFS_UNMOUNTED) & 1) && !((PED_BE32_TO_CPU (vh->attributes) >> HFSP_INCONSISTENT) & 1); return fs; /*--- clean error handling ---*/ hpo_am: free(priv_data->alloc_map); hpo_dm: free(priv_data->dirty_alloc_map); hpo_cl: hfsplus_file_close (priv_data->attributes_file); hpo_cc: hfsplus_file_close (priv_data->catalog_file); hpo_ce: hfsplus_file_close (priv_data->extents_file); hpo_pg: if (priv_data->free_geom) ped_geometry_destroy (priv_data->plus_geom); hpo_wr: if (priv_data->wrapper) hfs_close(priv_data->wrapper); hpo_gm: ped_geometry_destroy (fs->geom); hpo_pd: free(priv_data); hpo_vh: free(vh); hpo_fs: free(fs); hpo: return NULL; }
const PedAlignment* start_align, const PedAlignment* end_align, const PedGeometry* start_range, const PedGeometry* end_range, PedSector min_size, PedSector max_size) { PED_ASSERT (constraint != NULL, return 0); PED_ASSERT (start_range != NULL, return 0); PED_ASSERT (end_range != NULL, return 0); PED_ASSERT (min_size > 0, return 0); PED_ASSERT (max_size > 0, return 0); constraint->start_align = ped_alignment_duplicate (start_align); constraint->end_align = ped_alignment_duplicate (end_align); constraint->start_range = ped_geometry_duplicate (start_range); constraint->end_range = ped_geometry_duplicate (end_range); constraint->min_size = min_size; constraint->max_size = max_size; return 1; } /** * Convenience wrapper for ped_constraint_init(). * * Allocates a new piece of memory and initializes the constraint. * * \return \c NULL on failure. */ PedConstraint*
static PedFileSystem *reiserfs_copy(const PedFileSystem *fs, PedGeometry *geom, PedTimer *timer) { dal_t *dal; PedGeometry *fs_geom; PedFileSystem *new_fs; blk_t fs_len, min_needed_blk; reiserfs_fs_t *dest_fs, *src_fs; reiserfs_gauge_t *gauge = NULL; fs_geom = ped_geometry_duplicate(geom); if (!(dal = geom_dal_create(fs_geom, DEFAULT_BLOCK_SIZE, O_RDWR))) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Couldn't create reiserfs device " "abstraction handler.")); goto error_free_fs_geom; } src_fs = fs->type_specific; fs_len = (geom->length / (reiserfs_fs_block_size(src_fs) / PED_SECTOR_SIZE_DEFAULT)); min_needed_blk = reiserfs_fs_bitmap_used(src_fs); if (fs_len <= min_needed_blk) { ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Device is too small for %lu blocks."), min_needed_blk); goto error_free_dal; } if (! (new_fs = (PedFileSystem *) ped_malloc(sizeof(PedFileSystem)))) goto error_free_dal; ped_timer_reset(timer); ped_timer_set_state_name(timer, _("copying")); ped_timer_update(timer, 0.0); if (libreiserfs_gauge_create && libreiserfs_gauge_free) { if (! (gauge = libreiserfs_gauge_create(NULL, gauge_handler, timer))) goto error_free_new_fs; } if (!(dest_fs = reiserfs_fs_copy(src_fs, dal, gauge))) goto error_free_gauge; ped_timer_update(timer, 1.0); if (gauge) libreiserfs_gauge_free(gauge); new_fs->type = reiserfs_type; new_fs->geom = fs_geom; new_fs->type_specific = (void *) dest_fs; return new_fs; error_free_gauge: if (gauge) libreiserfs_gauge_free(gauge); error_free_new_fs: free(new_fs); error_free_dal: geom_dal_free(dal); error_free_fs_geom: ped_geometry_destroy(fs_geom); return NULL; }
static PedFileSystem *reiserfs_create(PedGeometry *geom, PedTimer *timer) { dal_t *dal; uuid_t uuid; PedFileSystem *fs; PedGeometry *fs_geom; reiserfs_fs_t *fs_info; reiserfs_gauge_t *gauge = NULL; PED_ASSERT(geom != NULL); fs_geom = ped_geometry_duplicate(geom); if (!(dal = geom_dal_create(fs_geom, DEFAULT_BLOCK_SIZE, O_RDWR))) goto error_fs_geom_free; memset(uuid, 0, sizeof(uuid)); uuid_generate(uuid); ped_timer_reset(timer); ped_timer_set_state_name(timer, _("creating")); if (libreiserfs_gauge_create && libreiserfs_gauge_free) { if (! (gauge = libreiserfs_gauge_create(NULL, gauge_handler, timer))) goto error_free_dal; } if (!(fs_info = reiserfs_fs_create(dal, dal, 0, JOURNAL_MAX_TRANS, DEFAULT_JOURNAL_SIZE, DEFAULT_BLOCK_SIZE, FS_FORMAT_3_6, R5_HASH, NULL, (char *) uuid, dal_len(dal), gauge))) goto error_free_gauge; ped_timer_update(timer, 1.0); if (gauge) libreiserfs_gauge_free(gauge); if (!(fs = (PedFileSystem *) ped_malloc(sizeof(PedFileSystem)))) goto error_free_fs_info; fs->type = reiserfs_type; fs->geom = fs_geom; fs->type_specific = (void *) fs_info; return fs; error_free_fs_info: free(fs_info); error_free_gauge: if (gauge) libreiserfs_gauge_free(gauge); error_free_dal: geom_dal_free(dal); error_fs_geom_free: ped_geometry_destroy(fs_geom); return NULL; }