1 /* 2 * Copyright 2004-2007, Haiku, Inc. All rights reserved. 3 * Copyright 2003-2004, Ingo Weinhold, bonefish@cs.tu-berlin.de. All rights reserved. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 #ifndef _K_DISK_DEVICE_MANAGER_H 8 #define _K_DISK_DEVICE_MANAGER_H 9 10 11 #include <disk_device_manager.h> 12 #include <Locker.h> 13 14 15 namespace BPrivate { 16 namespace DiskDevice { 17 18 class KDiskDevice; 19 class KDiskSystem; 20 class KFileDiskDevice; 21 class KPartition; 22 23 class KDiskDeviceManager { 24 public: 25 KDiskDeviceManager(); 26 ~KDiskDeviceManager(); 27 28 status_t InitCheck() const; 29 30 // Singleton Creation, Deletion, and Access 31 32 static status_t CreateDefault(); 33 static void DeleteDefault(); 34 static KDiskDeviceManager *Default(); 35 36 // Locking 37 38 bool Lock(); 39 void Unlock(); 40 41 // Disk Device / Partition Management 42 43 // manager must be locked 44 KDiskDevice *FindDevice(const char *path); 45 KDiskDevice *FindDevice(partition_id id, bool deviceOnly = true); 46 KPartition *FindPartition(const char *path); 47 KPartition *FindPartition(partition_id id); 48 KFileDiskDevice *FindFileDevice(const char *filePath); 49 50 KDiskDevice *RegisterDevice(const char *path); 51 KDiskDevice *RegisterDevice(partition_id id, bool deviceOnly = true); 52 KDiskDevice *RegisterNextDevice(int32 *cookie); 53 KPartition *RegisterPartition(const char *path); 54 KPartition *RegisterPartition(partition_id id); 55 KFileDiskDevice *RegisterFileDevice(const char *filePath); 56 57 KDiskDevice *ReadLockDevice(partition_id id, bool deviceOnly = true); 58 KDiskDevice *WriteLockDevice(partition_id id, bool deviceOnly = true); 59 // The device is also registered and must be unregistered by the 60 // caller. 61 KPartition *ReadLockPartition(partition_id id); 62 KPartition *WriteLockPartition(partition_id id); 63 // Both the device and the partition is also registered and must be 64 // unregistered by the caller. 65 66 status_t ScanPartition(KPartition* partition); 67 68 partition_id CreateDevice(const char *path, bool *newlyCreated = NULL); 69 status_t DeleteDevice(const char *path); 70 71 partition_id CreateFileDevice(const char* filePath, 72 bool* newlyCreated = NULL); 73 status_t DeleteFileDevice(const char *filePath); 74 status_t DeleteFileDevice(partition_id id); 75 76 // manager must be locked 77 int32 CountDevices(); 78 KDiskDevice *NextDevice(int32 *cookie); 79 80 bool PartitionAdded(KPartition *partition); // implementation internal 81 bool PartitionRemoved(KPartition *partition); // 82 bool DeletePartition(KPartition *partition); // 83 84 // Disk Systems 85 86 // manager must be locked 87 KDiskSystem *FindDiskSystem(const char *name, bool byPrettyName = false); 88 KDiskSystem *FindDiskSystem(disk_system_id id); 89 int32 CountDiskSystems(); 90 KDiskSystem *NextDiskSystem(int32 *cookie); 91 92 KDiskSystem *LoadDiskSystem(const char *name, bool byPrettyName = false); 93 KDiskSystem *LoadDiskSystem(disk_system_id id); 94 KDiskSystem *LoadNextDiskSystem(int32 *cookie); 95 96 status_t InitialDeviceScan(); 97 status_t RescanDiskSystems(); 98 status_t StartMonitoring(); 99 100 private: 101 struct DeviceMap; 102 struct DiskSystemMap; 103 struct PartitionMap; 104 struct PartitionSet; 105 class DiskSystemWatcher; 106 class DeviceWatcher; 107 108 static status_t _CheckMediaStatusDaemon(void* self); 109 status_t _CheckMediaStatus(); 110 111 status_t _RescanDiskSystems(DiskSystemMap& addedSystems, bool fileSystems); 112 113 status_t _AddPartitioningSystem(const char *name); 114 status_t _AddFileSystem(const char *name); 115 status_t _AddDiskSystem(KDiskSystem *diskSystem); 116 117 bool _AddDevice(KDiskDevice *device); 118 bool _RemoveDevice(KDiskDevice *device); 119 120 status_t _Scan(const char *path); 121 status_t _ScanPartition(KPartition *partition, bool async, 122 DiskSystemMap* restrictScan = NULL); 123 // the manager must be locked and the device write locked 124 status_t _ScanPartition(KPartition *partition, 125 DiskSystemMap* restrictScan); 126 127 status_t _AddRemoveMonitoring(const char *path, bool add); 128 129 BLocker fLock; 130 DeviceMap *fDevices; 131 PartitionMap *fPartitions; 132 DiskSystemMap *fDiskSystems; 133 PartitionSet *fObsoletePartitions; 134 thread_id fMediaChecker; 135 volatile bool fTerminating; 136 DiskSystemWatcher *fDiskSystemWatcher; 137 DeviceWatcher *fDeviceWatcher; 138 139 static KDiskDeviceManager *sDefaultManager; 140 }; 141 142 } // namespace DiskDevice 143 } // namespace BPrivate 144 145 using BPrivate::DiskDevice::KDiskDeviceManager; 146 147 #endif // _K_DISK_DEVICE_MANAGER_H 148