2 * Copyright 2003-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold <bonefish@cs.tu-berlin.de>
7 * Lubos Kulic <lubos@radical.ed>
10 /** \file KPartitioningSystem.cpp
12 * \brief Implementation of \ref KPartitioningSystem class
19 #include <ddm_modules.h>
20 #include <KDiskDevice.h>
21 //#include <KDiskDeviceJob.h>
22 #include <KDiskDeviceManager.h>
23 #include <KDiskDeviceUtils.h>
24 #include <KPartition.h>
25 #include <KPartitioningSystem.h>
29 KPartitioningSystem::KPartitioningSystem(const char *name
)
37 KPartitioningSystem::~KPartitioningSystem()
44 KPartitioningSystem::Init()
46 status_t error
= KDiskSystem::Init();
52 error
= SetShortName(fModule
->short_name
);
54 error
= SetPrettyName(fModule
->pretty_name
);
56 SetFlags(fModule
->flags
& ~(uint32
)B_DISK_SYSTEM_IS_FILE_SYSTEM
);
63 //! Try to identify a given partition
65 KPartitioningSystem::Identify(KPartition
*partition
, void **cookie
)
67 if (!partition
|| !cookie
|| !fModule
|| !fModule
->identify_partition
)
70 if (partition
->Open(O_RDONLY
, &fd
) != B_OK
)
72 if (partition
->BlockSize() == 0) {
77 float result
= fModule
->identify_partition(fd
, partition
->PartitionData(),
85 //! Scan the partition
87 KPartitioningSystem::Scan(KPartition
*partition
, void *cookie
)
89 if (!partition
|| !fModule
|| !fModule
->scan_partition
)
92 status_t result
= partition
->Open(O_RDONLY
, &fd
);
95 result
= fModule
->scan_partition(fd
, partition
->PartitionData(), cookie
);
101 // FreeIdentifyCookie
103 KPartitioningSystem::FreeIdentifyCookie(KPartition
*partition
, void *cookie
)
105 if (!partition
|| !fModule
|| !fModule
->free_identify_partition_cookie
)
107 fModule
->free_identify_partition_cookie(partition
->PartitionData(),
114 KPartitioningSystem::FreeCookie(KPartition
*partition
)
116 if (!partition
|| !fModule
|| !fModule
->free_partition_cookie
117 || partition
->ParentDiskSystem() != this) {
120 fModule
->free_partition_cookie(partition
->PartitionData());
121 partition
->SetCookie(NULL
);
127 KPartitioningSystem::FreeContentCookie(KPartition
*partition
)
129 if (!partition
|| !fModule
|| !fModule
->free_partition_content_cookie
130 || partition
->DiskSystem() != this) {
133 fModule
->free_partition_content_cookie(partition
->PartitionData());
134 partition
->SetContentCookie(NULL
);
139 //! Repairs a partition
141 KPartitioningSystem::Repair(KPartition
* partition
, bool checkOnly
,
150 //! Resizes a partition
152 KPartitioningSystem::Resize(KPartition
* partition
, off_t size
, disk_job_id job
)
155 if (!partition
|| size
< 0 || !fModule
)
157 if (!fModule
->resize
)
158 return B_NOT_SUPPORTED
;
160 // open partition device
162 status_t result
= partition
->Open(O_RDWR
, &fd
);
166 // let the module do its job
167 result
= fModule
->resize(fd
, partition
->ID(), size
, job
);
169 // cleanup and return
176 //! Resizes child of a partition
178 KPartitioningSystem::ResizeChild(KPartition
* child
, off_t size
, disk_job_id job
)
181 if (!child
|| !child
->Parent() || size
< 0 || !fModule
)
183 if (!fModule
->resize_child
)
184 return B_NOT_SUPPORTED
;
186 // open partition device
188 status_t result
= child
->Parent()->Open(O_RDWR
, &fd
);
192 // let the module do its job
193 result
= fModule
->resize_child(fd
, child
->ID(), size
, job
);
195 // cleanup and return
202 //! Moves a partition
204 KPartitioningSystem::Move(KPartition
* partition
, off_t offset
, disk_job_id job
)
210 return B_NOT_SUPPORTED
;
212 // open partition device
214 status_t result
= partition
->Open(O_RDWR
, &fd
);
218 // let the module do its job
219 result
= fModule
->move(fd
, partition
->ID(), offset
, job
);
221 // cleanup and return
228 //! Moves child of a partition
230 KPartitioningSystem::MoveChild(KPartition
* child
, off_t offset
, disk_job_id job
)
233 if (!child
|| !child
->Parent() || !fModule
)
235 if (!fModule
->move_child
)
236 return B_NOT_SUPPORTED
;
238 // open partition device
240 status_t result
= child
->Parent()->Open(O_RDWR
, &fd
);
244 // let the module do its job
245 result
= fModule
->move_child(fd
, child
->Parent()->ID(), child
->ID(), offset
,
248 // cleanup and return
255 //! Sets name of a partition
257 KPartitioningSystem::SetName(KPartition
* child
, const char* name
,
261 if (!child
|| !child
->Parent() || !fModule
)
263 if (!fModule
->set_name
)
264 return B_NOT_SUPPORTED
;
266 // open partition device
268 status_t result
= child
->Parent()->Open(O_RDWR
, &fd
);
272 // let the module do its job
273 result
= fModule
->set_name(fd
, child
->ID(), name
, job
);
274 // TODO: Change hook interface!
276 // cleanup and return
283 //! Sets name of the content of a partition
285 KPartitioningSystem::SetContentName(KPartition
* partition
, const char* name
,
289 if (!partition
|| !fModule
)
291 if (!fModule
->set_content_name
)
292 return B_NOT_SUPPORTED
;
294 // open partition device
296 status_t result
= partition
->Open(O_RDWR
, &fd
);
300 // let the module do its job
301 result
= fModule
->set_content_name(fd
, partition
->ID(), name
, job
);
303 // cleanup and return
310 //! Sets type of a partition
312 KPartitioningSystem::SetType(KPartition
* child
, const char* type
,
316 if (!child
|| !child
->Parent() || !type
|| !fModule
)
318 if (!fModule
->set_type
)
319 return B_NOT_SUPPORTED
;
321 // open partition device
323 status_t result
= child
->Parent()->Open(O_RDWR
, &fd
);
327 // let the module do its job
328 result
= fModule
->set_type(fd
, child
->Parent()->ID(), type
, job
);
329 // TODO: Change hook interface!
331 // cleanup and return
338 //! Sets parameters of a partition
340 KPartitioningSystem::SetParameters(KPartition
* child
, const char* parameters
,
344 if (!child
|| !child
->Parent() || !fModule
)
346 if (!fModule
->set_parameters
)
347 return B_NOT_SUPPORTED
;
349 // open partition device
351 status_t result
= child
->Parent()->Open(O_RDWR
, &fd
);
355 // let the module do its job
356 result
= fModule
->set_parameters(fd
, child
->ID(), parameters
, job
);
357 // TODO: Change hook interface!
359 // cleanup and return
365 // SetContentParameters
366 //! Sets parameters of the content of a partition
368 KPartitioningSystem::SetContentParameters(KPartition
* partition
,
369 const char* parameters
, disk_job_id job
)
372 if (!partition
|| !fModule
)
374 if (!fModule
->set_content_parameters
)
375 return B_NOT_SUPPORTED
;
377 // open partition device
379 status_t result
= partition
->Open(O_RDWR
, &fd
);
383 // let the module do its job
384 result
= fModule
->set_content_parameters(fd
, partition
->ID(), parameters
,
387 // cleanup and return
394 //! Initializes a partition with this partitioning system
396 KPartitioningSystem::Initialize(KPartition
* partition
, const char* name
,
397 const char* parameters
, disk_job_id job
)
400 if (!partition
|| !fModule
)
402 if (!fModule
->initialize
)
403 return B_NOT_SUPPORTED
;
405 // open partition device
407 status_t result
= partition
->Open(O_RDWR
, &fd
);
411 // let the module do its job
412 result
= fModule
->initialize(fd
, partition
->ID(), name
, parameters
,
413 partition
->Size(), job
);
415 // cleanup and return
422 KPartitioningSystem::Uninitialize(KPartition
* partition
, disk_job_id job
)
425 if (partition
== NULL
|| fModule
== NULL
)
427 if (fModule
->uninitialize
== NULL
)
428 return B_NOT_SUPPORTED
;
430 // open partition device
432 status_t result
= partition
->Open(O_RDWR
, &fd
);
436 // let the module do its job
437 result
= fModule
->uninitialize(fd
, partition
->ID(), partition
->Size(),
438 partition
->BlockSize(), job
);
440 // cleanup and return
447 //! Creates a child partition
449 KPartitioningSystem::CreateChild(KPartition
* partition
, off_t offset
,
450 off_t size
, const char* type
, const char* name
, const char* parameters
,
451 disk_job_id job
, KPartition
** child
, partition_id childID
)
454 if (!partition
|| !type
|| !parameters
|| !child
|| !fModule
)
456 if (!fModule
->create_child
)
457 return B_NOT_SUPPORTED
;
459 // open partition device
461 status_t result
= partition
->Open(O_RDWR
, &fd
);
465 // let the module do its job
466 result
= fModule
->create_child(fd
, partition
->ID(), offset
, size
,
467 type
, name
, parameters
, job
, &childID
);
469 // find and return the child
470 *child
= KDiskDeviceManager::Default()->FindPartition(childID
);
472 // cleanup and return
479 //! Deletes a child partition
481 KPartitioningSystem::DeleteChild(KPartition
* child
, disk_job_id job
)
483 if (!child
|| !child
->Parent())
485 if (!fModule
->delete_child
)
486 return B_NOT_SUPPORTED
;
489 KPartition
* parent
= child
->Parent();
490 status_t result
= parent
->Open(O_RDWR
, &fd
);
494 result
= fModule
->delete_child(fd
, parent
->ID(), child
->ID(), job
);
502 KPartitioningSystem::LoadModule()
504 if (fModule
) // shouldn't happen
506 return get_module(Name(), (module_info
**)&fModule
);
512 KPartitioningSystem::UnloadModule()
515 put_module(fModule
->module
.name
);