diff --git a/headers/private/storage/DiskDevicePrivate.h b/headers/private/storage/DiskDevicePrivate.h index 0f786495ee..3939a003f5 100644 --- a/headers/private/storage/DiskDevicePrivate.h +++ b/headers/private/storage/DiskDevicePrivate.h @@ -10,6 +10,7 @@ #include class BMessenger; +class BPath; namespace BPrivate { @@ -45,10 +46,14 @@ private: partition_id fID; }; +status_t get_unique_partition_mount_point(BPartition *partition, + BPath *mountPoint); + } // namespace BPrivate using BPrivate::PartitionFilter; using BPrivate::PartitionFilterVisitor; using BPrivate::IDFinderVisitor; +using BPrivate::get_unique_partition_mount_point; #endif // _DISK_DEVICE_PRIVATE_H diff --git a/src/kits/storage/DiskDevicePrivate.cpp b/src/kits/storage/DiskDevicePrivate.cpp index d001553d0b..0510d6f7ca 100644 --- a/src/kits/storage/DiskDevicePrivate.cpp +++ b/src/kits/storage/DiskDevicePrivate.cpp @@ -5,7 +5,10 @@ #include #include +#include #include +#include +#include // PartitionFilterVisitor @@ -37,6 +40,8 @@ PartitionFilterVisitor::Visit(BPartition *partition, int32 level) } +// #pragma mark - + // IDFinderVisitor // constructor @@ -60,3 +65,46 @@ IDFinderVisitor::Visit(BPartition *partition, int32 level) return (partition->ID() == fID); } + +// #pragma mark - + +// get_unique_partition_mount_point +status_t +BPrivate::get_unique_partition_mount_point(BPartition *partition, + BPath *mountPoint) +{ + if (!partition || !mountPoint) + return B_BAD_VALUE; + + // get the volume name + const char *volumeName = partition->ContentName(); + if (volumeName || strlen(volumeName) == 0) + volumeName = partition->Name(); + if (!volumeName || strlen(volumeName) == 0) + volumeName = "unnamed volume"; + + // construct a path name from the volume name + // replace '/'s and prepend a '/' + BString mountPointPath(volumeName); + mountPointPath.ReplaceAll('/', '-'); + mountPointPath.Insert("/", 0); + + // make the name unique + BString basePath(mountPointPath); + int counter = 1; + while (true) { + BEntry entry; + status_t error = entry.SetTo(mountPointPath.String()); + if (error != B_OK) + return error; + + if (!entry.Exists()) + break; + mountPointPath = basePath; + mountPointPath << counter; + counter++; + } + + return mountPoint->SetTo(mountPointPath.String()); +} + diff --git a/src/kits/storage/Partition.cpp b/src/kits/storage/Partition.cpp index fabd27fddc..fdcd692b3d 100644 --- a/src/kits/storage/Partition.cpp +++ b/src/kits/storage/Partition.cpp @@ -387,7 +387,7 @@ status_t BPartition::Mount(const char *mountPoint, uint32 mountFlags, const char *parameters) { - if (!fPartitionData || IsMounted()) + if (!fPartitionData || IsMounted() || !ContainsFileSystem()) return B_BAD_VALUE; // get the partition path @@ -398,38 +398,14 @@ BPartition::Mount(const char *mountPoint, uint32 mountFlags, // create a mount point, if none is given bool deleteMountPoint = false; - BString mountPointPath; + BPath mountPointPath; if (!mountPoint) { - // get the volume name - const char *volumeName = ContentName(); - if (volumeName || strlen(volumeName) == 0) - volumeName = Name(); - if (!volumeName || strlen(volumeName) == 0) - volumeName = "unnamed volume"; + // get a unique mount point + error = get_unique_partition_mount_point(this, &mountPointPath); + if (error != B_OK) + return error; - // construct a path name from the volume name - // replace '/'s and prepend a '/' - mountPointPath = volumeName; - mountPointPath.ReplaceAll('/', '-'); - mountPointPath.Insert("/", 0); - - // make the name unique - BString basePath(mountPointPath); - int counter = 1; - while (true) { - BEntry entry; - error = entry.SetTo(mountPointPath.String()); - if (error != B_OK) - return error; - - if (!entry.Exists()) - break; - mountPointPath = basePath; - mountPointPath << counter; - counter++; - } - - mountPoint = mountPointPath.String(); + mountPoint = mountPointPath.Path(); // create the directory if (mkdir(mountPoint, S_IRWXU | S_IRWXG | S_IRWXO) < 0)