Modified the visitor implementation a bit. The Visit() with the BPartition* argument now also gets the level of the partition in the hierarchy.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3900 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2003-07-08 18:26:15 +00:00
parent c54d68a84e
commit 671a745561
10 changed files with 134 additions and 90 deletions
+4 -4
View File
@@ -25,8 +25,6 @@ public:
virtual status_t GetPath(BPath *path) const; virtual status_t GetPath(BPath *path) const;
virtual BPartition *VisitEachDescendent(BDiskDeviceVisitor *visitor);
bool IsModified() const; bool IsModified() const;
status_t PrepareModifications(); status_t PrepareModifications();
status_t CommitModifications(bool synchronously = true, status_t CommitModifications(bool synchronously = true,
@@ -38,8 +36,10 @@ private:
friend class BDiskDeviceList; friend class BDiskDeviceList;
friend class BDiskDeviceRoster; friend class BDiskDeviceRoster;
status_t SetTo(partition_id id, size_t neededSize = 0); status_t _SetTo(partition_id id, size_t neededSize = 0);
status_t SetTo(user_disk_device_data *data); status_t _SetTo(user_disk_device_data *data);
virtual bool _AcceptVisitor(BDiskDeviceVisitor *visitor, int32 level);
user_disk_device_data *fDeviceData; user_disk_device_data *fDeviceData;
}; };
+3 -3
View File
@@ -16,7 +16,7 @@ namespace BPrivate {
// PartitionFilter // PartitionFilter
class PartitionFilter { class PartitionFilter {
public: public:
virtual bool Filter(BPartition *partition) = 0; virtual bool Filter(BPartition *partition, int32 level) = 0;
}; };
// PartitionFilterVisitor // PartitionFilterVisitor
@@ -26,7 +26,7 @@ public:
PartitionFilter *filter); PartitionFilter *filter);
virtual bool Visit(BDiskDevice *device); virtual bool Visit(BDiskDevice *device);
virtual bool Visit(BPartition *partition); virtual bool Visit(BPartition *partition, int32 level);
private: private:
BDiskDeviceVisitor *fVisitor; BDiskDeviceVisitor *fVisitor;
@@ -39,7 +39,7 @@ public:
IDFinderVisitor(partition_id id); IDFinderVisitor(partition_id id);
virtual bool Visit(BDiskDevice *device); virtual bool Visit(BDiskDevice *device);
virtual bool Visit(BPartition *partition); virtual bool Visit(BPartition *partition, int32 level);
private: private:
partition_id fID; partition_id fID;
+3 -1
View File
@@ -6,6 +6,8 @@
#ifndef _DISK_DEVICE_VISITOR_H #ifndef _DISK_DEVICE_VISITOR_H
#define _DISK_DEVICE_VISITOR_H #define _DISK_DEVICE_VISITOR_H
#include <SupportDefs.h>
class BDiskDevice; class BDiskDevice;
class BPartition; class BPartition;
@@ -17,7 +19,7 @@ public:
// return true to abort iteration // return true to abort iteration
virtual bool Visit(BDiskDevice *device); virtual bool Visit(BDiskDevice *device);
virtual bool Visit(BPartition *partition); virtual bool Visit(BPartition *partition, int32 level);
}; };
#endif _DISK_DEVICE_VISITOR_H #endif _DISK_DEVICE_VISITOR_H
+9 -4
View File
@@ -64,7 +64,7 @@ public:
status_t GetPartitioningInfo(BPartitioningInfo *info) const; status_t GetPartitioningInfo(BPartitioningInfo *info) const;
BPartition *VisitEachChild(BDiskDeviceVisitor *visitor); BPartition *VisitEachChild(BDiskDeviceVisitor *visitor);
virtual BPartition *VisitEachDescendent(BDiskDeviceVisitor *visitor); virtual BPartition *VisitEachDescendant(BDiskDeviceVisitor *visitor);
// Self Modification // Self Modification
@@ -113,9 +113,14 @@ private:
BPartition(const Partition &); BPartition(const Partition &);
virtual ~BPartition(); virtual ~BPartition();
status_t SetTo(BDiskDevice *device, BPartition *parent, status_t _SetTo(BDiskDevice *device, BPartition *parent,
user_partition_data *data); user_partition_data *data);
void Unset(); void _Unset();
int32 _Level() const;
virtual bool _AcceptVisitor(BDiskDeviceVisitor *visitor, int32 level);
BPartition *_VisitEachDescendant(BDiskDeviceVisitor *visitor,
int32 level = -1);
friend class BDiskDevice; friend class BDiskDevice;
+15 -19
View File
@@ -181,17 +181,6 @@ BDiskDevice::GetPath(BPath *path) const
return path->SetTo(fDeviceData->path); return path->SetTo(fDeviceData->path);
} }
// VisitEachDescendent
BPartition *
BDiskDevice::VisitEachDescendent(BDiskDeviceVisitor *visitor)
{
if (!visitor)
return NULL;
if (visitor->Visit(this))
return this;
return BPartition::VisitEachDescendent(visitor);
}
// IsModified // IsModified
bool bool
BDiskDevice::IsModified() const BDiskDevice::IsModified() const
@@ -226,9 +215,9 @@ BDiskDevice::CancelModifications()
return B_ERROR; return B_ERROR;
} }
// SetTo // _SetTo
status_t status_t
BDiskDevice::SetTo(partition_id id, size_t neededSize) BDiskDevice::_SetTo(partition_id id, size_t neededSize)
{ {
Unset(); Unset();
// get the device data // get the device data
@@ -259,25 +248,25 @@ BDiskDevice::SetTo(partition_id id, size_t neededSize)
} while (error == B_BUFFER_OVERFLOW); } while (error == B_BUFFER_OVERFLOW);
// set the data // set the data
if (error == B_OK) if (error == B_OK)
error = SetTo((user_disk_device_data*)buffer); error = _SetTo((user_disk_device_data*)buffer);
// cleanup on error // cleanup on error
if (error != B_OK) if (error != B_OK)
free(buffer); free(buffer);
return error; return error;
} }
// SetTo // _SetTo
status_t status_t
BDiskDevice::SetTo(user_disk_device_data *data) BDiskDevice::_SetTo(user_disk_device_data *data)
{ {
Unset(); Unset();
if (!data) if (!data)
return B_BAD_VALUE; return B_BAD_VALUE;
fDeviceData = data; fDeviceData = data;
status_t error = BPartition::SetTo(this, NULL, status_t error = BPartition::_SetTo(this, NULL,
&fDeviceData->device_partition_data); &fDeviceData->device_partition_data);
if (error != B_OK) { if (error != B_OK) {
// Don't call Unset() here. If SetTo() fails, the caller retains // Don't call Unset() here. If _SetTo() fails, the caller retains
// ownership of the supplied data. // ownership of the supplied data.
// TODO: Maybe introduce a _Unset() to avoid potential future // TODO: Maybe introduce a _Unset() to avoid potential future
// problems. // problems.
@@ -286,6 +275,13 @@ BDiskDevice::SetTo(user_disk_device_data *data)
return error; return error;
} }
// _AcceptVisitor
bool
BDiskDevice::_AcceptVisitor(BDiskDeviceVisitor *visitor, int32 level)
{
return visitor->Visit(this);
}
#if 0 #if 0
+4 -4
View File
@@ -27,10 +27,10 @@ PartitionFilterVisitor::Visit(BDiskDevice *device)
// Visit // Visit
bool bool
PartitionFilterVisitor::Visit(BPartition *partition) PartitionFilterVisitor::Visit(BPartition *partition, int32 level)
{ {
if (fFilter->Filter(partition)) if (fFilter->Filter(partition, level))
return fVisitor->Visit(partition); return fVisitor->Visit(partition, level);
return false; return false;
} }
@@ -53,7 +53,7 @@ IDFinderVisitor::Visit(BDiskDevice *device)
// Visit // Visit
bool bool
IDFinderVisitor::Visit(BPartition *partition) IDFinderVisitor::Visit(BPartition *partition, int32 level)
{ {
return (partition->UniqueID() == fID); return (partition->UniqueID() == fID);
} }
+8 -8
View File
@@ -82,7 +82,7 @@ BDiskDeviceRoster::GetNextDevice(BDiskDevice *device)
partition_id id = _kern_get_next_disk_device_id(&fCookie, &neededSize); partition_id id = _kern_get_next_disk_device_id(&fCookie, &neededSize);
if (id < 0) if (id < 0)
return id; return id;
return device->SetTo(id, neededSize); return device->_SetTo(id, neededSize);
} }
// RewindDevices // RewindDevices
@@ -214,8 +214,8 @@ BDiskDeviceRoster::VisitEachPartition(BDiskDeviceVisitor *visitor,
BDiskDevice *useDevice = (device ? device : &deviceOnStack); BDiskDevice *useDevice = (device ? device : &deviceOnStack);
BPartition *foundPartition = NULL; BPartition *foundPartition = NULL;
while (!foundPartition && GetNextDevice(useDevice) == B_OK) while (!foundPartition && GetNextDevice(useDevice) == B_OK)
foundPartition = useDevice->VisitEachDescendent(visitor); foundPartition = useDevice->VisitEachDescendant(visitor);
// TODO: That probably not correct. VisitEachDescendent() // TODO: That probably not correct. VisitEachDescendant()
// should also invoke Visit(BDiskDevice*). // should also invoke Visit(BDiskDevice*).
fCookie = oldCookie; fCookie = oldCookie;
if (!terminatedEarly) if (!terminatedEarly)
@@ -247,7 +247,7 @@ BDiskDeviceRoster::VisitAll(BDiskDeviceVisitor *visitor)
fCookie = 0; fCookie = 0;
BDiskDevice device; BDiskDevice device;
while (!terminatedEarly && GetNextDevice(&device) == B_OK) while (!terminatedEarly && GetNextDevice(&device) == B_OK)
terminatedEarly = device.VisitEachDescendent(visitor); terminatedEarly = device.VisitEachDescendant(visitor);
fCookie = oldCookie; fCookie = oldCookie;
} }
return terminatedEarly; return terminatedEarly;
@@ -279,7 +279,7 @@ BDiskDeviceRoster::VisitEachMountedPartition(BDiskDeviceVisitor *visitor,
bool terminatedEarly = false; bool terminatedEarly = false;
if (visitor) { if (visitor) {
struct MountedPartitionFilter : public PartitionFilter { struct MountedPartitionFilter : public PartitionFilter {
virtual bool Filter(BPartition *partition) virtual bool Filter(BPartition *partition, int32)
{ return partition->IsMounted(); } { return partition->IsMounted(); }
} filter; } filter;
PartitionFilterVisitor filterVisitor(visitor, &filter); PartitionFilterVisitor filterVisitor(visitor, &filter);
@@ -315,7 +315,7 @@ BDiskDeviceRoster::VisitEachMountablePartition(BDiskDeviceVisitor *visitor,
bool terminatedEarly = false; bool terminatedEarly = false;
if (visitor) { if (visitor) {
struct MountablePartitionFilter : public PartitionFilter { struct MountablePartitionFilter : public PartitionFilter {
virtual bool Filter(BPartition *partition) virtual bool Filter(BPartition *partition, int32)
{ return partition->ContainsFileSystem(); } { return partition->ContainsFileSystem(); }
} filter; } filter;
PartitionFilterVisitor filterVisitor(visitor, &filter); PartitionFilterVisitor filterVisitor(visitor, &filter);
@@ -351,7 +351,7 @@ BDiskDeviceRoster::VisitEachInitializablePartition(BDiskDeviceVisitor *visitor,
/* bool terminatedEarly = false; /* bool terminatedEarly = false;
if (visitor) { if (visitor) {
struct InitializablePartitionFilter : public PartitionFilter { struct InitializablePartitionFilter : public PartitionFilter {
virtual bool Filter(BPartition *partition) virtual bool Filter(BPartition *partition, int32)
{ return !partition->CanInitialize(NULL); } { return !partition->CanInitialize(NULL); }
// TODO: ??? // TODO: ???
} filter; } filter;
@@ -375,7 +375,7 @@ BDiskDeviceRoster::VisitEachPartitionablePartition(BDiskDeviceVisitor *visitor,
bool terminatedEarly = false; bool terminatedEarly = false;
if (visitor) { if (visitor) {
struct PartitionablePartitionFilter : public PartitionFilter { struct PartitionablePartitionFilter : public PartitionFilter {
virtual bool Filter(BPartition *partition) virtual bool Filter(BPartition *partition, int32)
{ return partition->ContainsPartitioningSystem(); } { return partition->ContainsPartitioningSystem(); }
} filter; } filter;
PartitionFilterVisitor filterVisitor(visitor, &filter); PartitionFilterVisitor filterVisitor(visitor, &filter);
+4 -1
View File
@@ -42,6 +42,7 @@ BDiskDeviceVisitor::~BDiskDeviceVisitor()
Overridden by derived classes. Overridden by derived classes.
This class' version does nothing and it returns \c false. This class' version does nothing and it returns \c false.
\param device The visited disk device.
\return \c true, if the iteration shall be terminated at this point, \return \c true, if the iteration shall be terminated at this point,
\c false otherwise. \c false otherwise.
*/ */
@@ -60,11 +61,13 @@ BDiskDeviceVisitor::Visit(BDiskDevice *device)
Overridden by derived classes. Overridden by derived classes.
This class' version does nothing and it returns \c false. This class' version does nothing and it returns \c false.
\param partition The visited partition.
\param level The level of the partition in the partition tree.
\return \c true, if the iteration shall be terminated at this point, \return \c true, if the iteration shall be terminated at this point,
\c false otherwise. \c false otherwise.
*/ */
bool bool
BDiskDeviceVisitor::Visit(BPartition *partition) BDiskDeviceVisitor::Visit(BPartition *partition, int32 level)
{ {
return false; return false;
} }
+50 -20
View File
@@ -37,7 +37,7 @@ BPartition::BPartition()
*/ */
BPartition::~BPartition() BPartition::~BPartition()
{ {
Unset(); _Unset();
} }
// Offset // Offset
@@ -362,35 +362,30 @@ BPartition *
BPartition::VisitEachChild(BDiskDeviceVisitor *visitor) BPartition::VisitEachChild(BDiskDeviceVisitor *visitor)
{ {
if (visitor) { if (visitor) {
int32 level = _Level();
for (int32 i = 0; BPartition *child = ChildAt(i); i++) { for (int32 i = 0; BPartition *child = ChildAt(i); i++) {
if (visitor->Visit(child)) if (child->_AcceptVisitor(visitor, level))
return child; return child;
} }
} }
return NULL; return NULL;
} }
// VisitEachDescendent // VisitEachDescendant
BPartition * BPartition *
BPartition::VisitEachDescendent(BDiskDeviceVisitor *visitor) BPartition::VisitEachDescendant(BDiskDeviceVisitor *visitor)
{ {
if (visitor) { if (visitor)
if (visitor->Visit(this)) return _VisitEachDescendant(visitor);
return this;
for (int32 i = 0; BPartition *child = ChildAt(i); i++) {
if (BPartition *result = child->VisitEachDescendent(visitor))
return result;
}
}
return NULL; return NULL;
} }
// SetTo // _SetTo
status_t status_t
BPartition::SetTo(BDiskDevice *device, BPartition *parent, BPartition::_SetTo(BDiskDevice *device, BPartition *parent,
user_partition_data *data) user_partition_data *data)
{ {
Unset(); _Unset();
if (!device || !data) if (!device || !data)
return B_BAD_VALUE; return B_BAD_VALUE;
fPartitionData = data; fPartitionData = data;
@@ -402,7 +397,7 @@ BPartition::SetTo(BDiskDevice *device, BPartition *parent,
for (int32 i = 0; error == B_OK && i < fPartitionData->child_count; i++) { for (int32 i = 0; error == B_OK && i < fPartitionData->child_count; i++) {
BPartition *child = new(nothrow) BPartition; BPartition *child = new(nothrow) BPartition;
if (child) { if (child) {
error = child->SetTo(fDevice, this, fPartitionData->children[i]); error = child->_SetTo(fDevice, this, fPartitionData->children[i]);
if (error != B_OK) if (error != B_OK)
delete child; delete child;
} else } else
@@ -410,13 +405,13 @@ BPartition::SetTo(BDiskDevice *device, BPartition *parent,
} }
// cleanup on error // cleanup on error
if (error != B_OK) if (error != B_OK)
Unset(); _Unset();
return error; return error;
} }
// Unset // _Unset
void void
BPartition::Unset() BPartition::_Unset()
{ {
// delete children // delete children
if (fPartitionData) { if (fPartitionData) {
@@ -431,6 +426,41 @@ BPartition::Unset()
fPartitionData = NULL; fPartitionData = NULL;
} }
// _Level
int32
BPartition::_Level() const
{
int32 level = 0;
const BPartition *ancestor = this;
while ((ancestor = ancestor->Parent()))
level++;
return level;
}
// _AcceptVisitor
bool
BPartition::_AcceptVisitor(BDiskDeviceVisitor *visitor, int32 level)
{
return visitor->Visit(this, level);
}
// _VisitEachDescendant
BPartition *
BPartition::_VisitEachDescendant(BDiskDeviceVisitor *visitor, int32 level)
{
if (level < 0)
level = _Level();
if (_AcceptVisitor(visitor, level))
return this;
for (int32 i = 0; BPartition *child = ChildAt(i); i++) {
if (BPartition *result = child->_VisitEachDescendant(visitor,
level + 1)) {
return result;
}
}
return NULL;
}
#if 0 #if 0
@@ -32,37 +32,45 @@ public:
else else
pathString = strerror(error); pathString = strerror(error);
printf("device %ld: `%s'\n", device->UniqueID(), pathString); printf("device %ld: `%s'\n", device->UniqueID(), pathString);
printf(" removable: %d\n", device->IsRemovable()); printf(" removable: %d\n", device->IsRemovable());
printf(" has media: %d\n", device->HasMedia()); printf(" has media: %d\n", device->HasMedia());
printf(" ---\n");
Visit(device, 0);
return false; return false;
} }
virtual bool Visit(BPartition *partition) virtual bool Visit(BPartition *partition, int32 level)
{ {
BPath path; char prefix[128];
status_t error = partition->GetPath(&path); sprintf(prefix, "%*s", 2 * (int)level, "");
const char *pathString = NULL; if (level > 0) {
if (error == B_OK) BPath path;
pathString = path.Path(); status_t error = partition->GetPath(&path);
else const char *pathString = NULL;
pathString = strerror(error); if (error == B_OK)
printf(" partition %ld: `%s'\n", partition->UniqueID(), pathString); pathString = path.Path();
printf(" offset: %lld\n", partition->Offset()); else
printf(" size: %lld\n", partition->Size()); pathString = strerror(error);
printf(" block size: %lu\n", partition->BlockSize()); printf("%spartition %ld: `%s'\n", prefix, partition->UniqueID(),
printf(" index: %ld\n", partition->Index()); pathString);
printf(" status: %lu\n", partition->Status()); }
printf(" file system: %d\n", partition->ContainsFileSystem()); printf("%s offset: %lld\n", prefix, partition->Offset());
printf(" part. system: %d\n", printf("%s size: %lld\n", prefix, partition->Size());
printf("%s block size: %lu\n", prefix, partition->BlockSize());
printf("%s index: %ld\n", prefix, partition->Index());
printf("%s status: %lu\n", prefix, partition->Status());
printf("%s file system: %d\n", prefix,
partition->ContainsFileSystem());
printf("%s part. system: %d\n", prefix,
partition->ContainsPartitioningSystem()); partition->ContainsPartitioningSystem());
printf(" device: %d\n", partition->IsDevice()); printf("%s device: %d\n", prefix, partition->IsDevice());
printf(" read only: %d\n", partition->IsReadOnly()); printf("%s read only: %d\n", prefix, partition->IsReadOnly());
printf(" mounted: %d\n", partition->IsMounted()); printf("%s mounted: %d\n", prefix, partition->IsMounted());
printf(" flags: %lx\n", partition->Flags()); printf("%s flags: %lx\n", prefix, partition->Flags());
printf(" name: `%s'\n", partition->Name()); printf("%s name: `%s'\n", prefix, partition->Name());
printf(" content name: `%s'\n", partition->ContentName()); printf("%s content name: `%s'\n", prefix, partition->ContentName());
printf(" type: `%s'\n", partition->Type()); printf("%s type: `%s'\n", prefix, partition->Type());
printf(" content type: `%s'\n", partition->ContentType()); printf("%s content type: `%s'\n", prefix, partition->ContentType());
// volume, icon,... // volume, icon,...
return false; return false;
} }