kernel/disk_device_manager: Cleanup code style, fix some minor TODOs.

This commit is contained in:
Augustin Cavalier
2024-12-03 12:31:20 -05:00
parent 1a98f27639
commit 896f7fdb75
5 changed files with 35 additions and 62 deletions
@@ -27,8 +27,6 @@ public:
status_t SetTo(const char *path); status_t SetTo(const char *path);
void Unset(); void Unset();
virtual status_t InitCheck() const;
// TODO: probably superfluous
// A read lock owner can be sure that the device (incl. all of its // A read lock owner can be sure that the device (incl. all of its
// partitions won't be changed). // partitions won't be changed).
@@ -60,16 +58,11 @@ public:
void UpdateGeometry(); void UpdateGeometry();
status_t SetPath(const char *path);
// TODO: Remove this method or make it private. Once initialized the
// path must not be changed.
const char *Path() const; const char *Path() const;
virtual status_t GetFileName(char* buffer, size_t size) const; virtual status_t GetFileName(char* buffer, size_t size) const;
virtual status_t GetPath(KPath *path) const; virtual status_t GetPath(KPath *path) const;
// File descriptor: Set only from a kernel thread, valid only for // File descriptor: valid only for kernel threads.
// kernel threads.
void SetFD(int fd);
int FD() const; int FD() const;
// access to C style device data // access to C style device data
@@ -101,6 +94,8 @@ private:
} // namespace DiskDevice } // namespace DiskDevice
} // namespace BPrivate } // namespace BPrivate
using BPrivate::DiskDevice::KDiskDevice; using BPrivate::DiskDevice::KDiskDevice;
#endif // _K_DISK_DEVICE_H #endif // _K_DISK_DEVICE_H
@@ -1,26 +1,30 @@
// KFileDiskDevice.h /*
* Copyright 2003-2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef _K_FILE_DISK_DEVICE_H #ifndef _K_FILE_DISK_DEVICE_H
#define _K_FILE_DISK_DEVICE_H #define _K_FILE_DISK_DEVICE_H
#include <OS.h> #include <OS.h>
#include "KDiskDevice.h" #include "KDiskDevice.h"
namespace BPrivate { namespace BPrivate {
namespace DiskDevice { namespace DiskDevice {
class KPath; class KPath;
class KFileDiskDevice : public KDiskDevice {
class KFileDiskDevice final : public KDiskDevice {
public: public:
KFileDiskDevice(partition_id id = -1); KFileDiskDevice(partition_id id = -1);
virtual ~KFileDiskDevice(); virtual ~KFileDiskDevice();
status_t SetTo(const char *filePath, const char *devicePath = NULL); status_t SetTo(const char *filePath, const char *devicePath = NULL);
void Unset(); void Unset();
virtual status_t InitCheck() const;
// TODO: probably superfluous
const char *FilePath() const; const char *FilePath() const;
@@ -40,9 +44,12 @@ private:
char *fFilePath; char *fFilePath;
}; };
} // namespace DiskDevice } // namespace DiskDevice
} // namespace BPrivate } // namespace BPrivate
using BPrivate::DiskDevice::KFileDiskDevice; using BPrivate::DiskDevice::KFileDiskDevice;
#endif // _K_FILE_DISK_DEVICE_H #endif // _K_FILE_DISK_DEVICE_H
@@ -8,14 +8,18 @@
#ifndef _K_DISK_DEVICE_PARTITION_H #ifndef _K_DISK_DEVICE_PARTITION_H
#define _K_DISK_DEVICE_PARTITION_H #define _K_DISK_DEVICE_PARTITION_H
#include <disk_device_manager.h> #include <disk_device_manager.h>
#include <Vector.h> #include <Vector.h>
struct user_partition_data; struct user_partition_data;
namespace BPrivate { namespace BPrivate {
namespace DiskDevice { namespace DiskDevice {
class UserDataWriter; class UserDataWriter;
class KDiskDevice; class KDiskDevice;
@@ -25,6 +29,7 @@ class KPartitionVisitor;
class KPath; class KPath;
class KPhysicalPartition; class KPhysicalPartition;
//! \brief Class representing a single partition. //! \brief Class representing a single partition.
class KPartition { class KPartition {
public: public:
@@ -235,9 +240,12 @@ protected:
static int32 sNextID; static int32 sNextID;
}; };
} // namespace DiskDevice } // namespace DiskDevice
} // namespace BPrivate } // namespace BPrivate
using BPrivate::DiskDevice::KPartition; using BPrivate::DiskDevice::KPartition;
#endif // _K_DISK_DEVICE_PARTITION_H #endif // _K_DISK_DEVICE_PARTITION_H
@@ -51,17 +51,14 @@ KDiskDevice::~KDiskDevice()
status_t status_t
KDiskDevice::SetTo(const char* path) KDiskDevice::SetTo(const char* path)
{ {
// check initialization and parameter
status_t error = InitCheck();
if (error != B_OK)
return error;
if (!path) if (!path)
return B_BAD_VALUE; return B_BAD_VALUE;
Unset(); Unset();
// set the path
error = set_string(fDeviceData.path, path); status_t error = set_string(fDeviceData.path, path);
if (error != B_OK) if (error != B_OK)
return error; return error;
// open the device // open the device
fFD = open(path, O_RDONLY); fFD = open(path, O_RDONLY);
if (fFD < 0) if (fFD < 0)
@@ -72,6 +69,7 @@ KDiskDevice::SetTo(const char* path)
return error; return error;
if (fMediaStatus == B_DEV_MEDIA_CHANGED) if (fMediaStatus == B_DEV_MEDIA_CHANGED)
fMediaStatus = B_OK; fMediaStatus = B_OK;
// get device geometry // get device geometry
if (fMediaStatus == B_OK) { if (fMediaStatus == B_OK) {
error = GetGeometry(&fDeviceData.geometry); error = GetGeometry(&fDeviceData.geometry);
@@ -82,9 +80,7 @@ KDiskDevice::SetTo(const char* path)
_ResetGeometry(); _ResetGeometry();
} }
// set device flags
_UpdateDeviceFlags(); _UpdateDeviceFlags();
// update partition data
_InitPartitionData(); _InitPartitionData();
return B_OK; return B_OK;
} }
@@ -108,13 +104,6 @@ KDiskDevice::Unset()
} }
status_t
KDiskDevice::InitCheck() const
{
return B_OK;
}
bool bool
KDiskDevice::ReadLock() KDiskDevice::ReadLock()
{ {
@@ -257,13 +246,6 @@ KDiskDevice::UpdateGeometry()
} }
status_t
KDiskDevice::SetPath(const char* path)
{
return set_string(fDeviceData.path, path);
}
const char* const char*
KDiskDevice::Path() const KDiskDevice::Path() const
{ {
@@ -291,13 +273,6 @@ KDiskDevice::GetPath(KPath* path) const
} }
void
KDiskDevice::SetFD(int fd)
{
fFD = fd;
}
int int
KDiskDevice::FD() const KDiskDevice::FD() const
{ {
@@ -40,11 +40,11 @@ KFileDiskDevice::~KFileDiskDevice()
status_t status_t
KFileDiskDevice::SetTo(const char* filePath, const char* devicePath) KFileDiskDevice::SetTo(const char* filePath, const char* devicePath)
{ {
// check params
if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH
|| (devicePath && strlen(devicePath) > B_PATH_NAME_LENGTH)) { || (devicePath && strlen(devicePath) > B_PATH_NAME_LENGTH)) {
return B_BAD_VALUE; return B_BAD_VALUE;
} }
// normalize the file path // normalize the file path
// (should actually not be necessary, since this method is only invoked // (should actually not be necessary, since this method is only invoked
// by the DDM, which has already normalized the path) // by the DDM, which has already normalized the path)
@@ -52,35 +52,32 @@ KFileDiskDevice::SetTo(const char* filePath, const char* devicePath)
status_t error = tmpFilePath.SetTo(filePath, KPath::NORMALIZE); status_t error = tmpFilePath.SetTo(filePath, KPath::NORMALIZE);
if (error != B_OK) if (error != B_OK)
return error; return error;
// check the file // check the file
struct stat st; struct stat st;
if (stat(filePath, &st) != 0) if (stat(filePath, &st) != 0)
return errno; return errno;
if (!S_ISREG(st.st_mode)) if (!S_ISREG(st.st_mode))
return B_BAD_VALUE; return B_BAD_VALUE;
// create the device, if requested // create the device, if requested
KPath tmpDevicePath; KPath tmpDevicePath;
if (devicePath == NULL) { if (devicePath == NULL) {
// no device path: we shall create a new device entry // no device path: we shall create a new device entry
if (tmpDevicePath.InitCheck() != B_OK) if (tmpDevicePath.InitCheck() != B_OK)
return tmpDevicePath.InitCheck(); return tmpDevicePath.InitCheck();
// TODO: Cleanup. The directory creation is done automatically by the devfs.
// // make the file devices dir
// if (mkdir(kFileDevicesDir, 0777) != 0) {
// if (errno != B_FILE_EXISTS)
// return errno;
// }
// make the directory // make the directory
status_t error = _GetDirectoryPath(ID(), &tmpDevicePath); status_t error = _GetDirectoryPath(ID(), &tmpDevicePath);
if (error != B_OK) if (error != B_OK)
return error; return error;
// if (mkdir(tmpDevicePath.Path(), 0777) != 0)
// return errno;
// get the device path name // get the device path name
error = tmpDevicePath.Append("raw"); error = tmpDevicePath.Append("raw");
if (error != B_OK) if (error != B_OK)
return error; return error;
devicePath = tmpDevicePath.Path(); devicePath = tmpDevicePath.Path();
// register the file as virtual disk device // register the file as virtual disk device
error = _RegisterDevice(filePath, devicePath); error = _RegisterDevice(filePath, devicePath);
if (error != B_OK) if (error != B_OK)
@@ -94,8 +91,7 @@ KFileDiskDevice::SetTo(const char* filePath, const char* devicePath)
if (error != B_OK) if (error != B_OK)
return error; return error;
// reset the B_DISK_DEVICE_IS_FILE flag -- KDiskDevice::SetTo() has cleared // reset the B_DISK_DEVICE_IS_FILE flag -- KDiskDevice::SetTo() has cleared it
// it
SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE); SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE);
return B_OK; return B_OK;
@@ -113,19 +109,12 @@ KFileDiskDevice::Unset()
// if (_GetDirectoryPath(ID(), &dirPath) == B_OK) // if (_GetDirectoryPath(ID(), &dirPath) == B_OK)
// rmdir(dirPath.Path()); // rmdir(dirPath.Path());
} }
// free file path
free(fFilePath); free(fFilePath);
fFilePath = NULL; fFilePath = NULL;
} }
status_t
KFileDiskDevice::InitCheck() const
{
return KDiskDevice::InitCheck();
}
const char* const char*
KFileDiskDevice::FilePath() const KFileDiskDevice::FilePath() const
{ {
@@ -211,4 +200,3 @@ KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath* path)
} }
return error; return error;
} }