headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / storage / disk_device / DiskSystem.cpp
blob7dffe31a996ab719808a12e34dfe0315b42e8839
1 /*
2 * Copyright 2003-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <DiskSystem.h>
8 #include <DiskSystemAddOn.h>
9 #include <Partition.h>
11 #include <ddm_userland_interface_defs.h>
12 #include <syscalls.h>
14 #include "DiskSystemAddOnManager.h"
17 // constructor
18 BDiskSystem::BDiskSystem()
20 fID(B_NO_INIT),
21 fFlags(0)
26 // copy constructor
27 BDiskSystem::BDiskSystem(const BDiskSystem& other)
29 fID(other.fID),
30 fName(other.fName),
31 fShortName(other.fShortName),
32 fPrettyName(other.fPrettyName),
33 fFlags(other.fFlags)
38 // destructor
39 BDiskSystem::~BDiskSystem()
44 // InitCheck
45 status_t
46 BDiskSystem::InitCheck() const
48 return fID > 0 ? B_OK : fID;
52 // Name
53 const char*
54 BDiskSystem::Name() const
56 return fName.String();
60 // ShortName
61 const char*
62 BDiskSystem::ShortName() const
64 return fShortName.String();
68 // PrettyName
69 const char*
70 BDiskSystem::PrettyName() const
72 return fPrettyName.String();
76 // SupportsDefragmenting
77 bool
78 BDiskSystem::SupportsDefragmenting(bool* whileMounted) const
80 if (InitCheck() != B_OK
81 || !(fFlags & B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING)) {
82 if (whileMounted)
83 *whileMounted = false;
84 return false;
87 if (whileMounted) {
88 *whileMounted = IsFileSystem() && (fFlags
89 & B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING_WHILE_MOUNTED) != 0;
92 return true;
96 // SupportsRepairing
97 bool
98 BDiskSystem::SupportsRepairing(bool checkOnly, bool* whileMounted) const
100 uint32 mainBit = B_DISK_SYSTEM_SUPPORTS_REPAIRING;
101 uint32 mountedBit = B_DISK_SYSTEM_SUPPORTS_REPAIRING_WHILE_MOUNTED;
103 if (checkOnly) {
104 mainBit = B_DISK_SYSTEM_SUPPORTS_CHECKING;
105 mountedBit = B_DISK_SYSTEM_SUPPORTS_CHECKING_WHILE_MOUNTED;
108 if (InitCheck() != B_OK || !(fFlags & mainBit)) {
109 if (whileMounted)
110 *whileMounted = false;
111 return false;
114 if (whileMounted)
115 *whileMounted = (IsFileSystem() && (fFlags & mountedBit));
117 return true;
121 // SupportsResizing
122 bool
123 BDiskSystem::SupportsResizing(bool* whileMounted) const
125 if (InitCheck() != B_OK
126 || !(fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING)) {
127 if (whileMounted)
128 *whileMounted = false;
129 return false;
132 if (whileMounted) {
133 *whileMounted = (IsFileSystem()
134 && (fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING_WHILE_MOUNTED));
137 return true;
141 // SupportsResizingChild
142 bool
143 BDiskSystem::SupportsResizingChild() const
145 return (InitCheck() == B_OK && IsPartitioningSystem()
146 && (fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING_CHILD));
150 // SupportsMoving
151 bool
152 BDiskSystem::SupportsMoving(bool* whileMounted) const
154 if (InitCheck() != B_OK
155 || !(fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING)) {
156 if (whileMounted)
157 *whileMounted = false;
158 return false;
161 if (whileMounted) {
162 *whileMounted = (IsFileSystem()
163 && (fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING_WHILE_MOUNTED));
166 return true;
170 // SupportsMovingChild
171 bool
172 BDiskSystem::SupportsMovingChild() const
174 return (InitCheck() == B_OK && IsPartitioningSystem()
175 && (fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING_CHILD));
179 // SupportsName
180 bool
181 BDiskSystem::SupportsName() const
183 return (InitCheck() == B_OK && IsPartitioningSystem()
184 && (fFlags & B_DISK_SYSTEM_SUPPORTS_NAME));
188 // SupportsContentName
189 bool
190 BDiskSystem::SupportsContentName() const
192 return (InitCheck() == B_OK
193 && (fFlags & B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME));
197 // SupportsSettingName
198 bool
199 BDiskSystem::SupportsSettingName() const
201 return (InitCheck() == B_OK && IsPartitioningSystem()
202 && (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_NAME));
206 // SupportsSettingContentName
207 bool
208 BDiskSystem::SupportsSettingContentName(bool* whileMounted) const
210 if (InitCheck() != B_OK
211 || !(fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME)) {
212 if (whileMounted)
213 *whileMounted = false;
214 return false;
217 if (whileMounted) {
218 *whileMounted = (IsFileSystem()
219 && (fFlags
220 & B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME_WHILE_MOUNTED));
223 return true;
227 // SupportsSettingType
228 bool
229 BDiskSystem::SupportsSettingType() const
231 return (InitCheck() == B_OK && IsPartitioningSystem()
232 && (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_TYPE));
236 // SupportsSettingParameters
237 bool
238 BDiskSystem::SupportsSettingParameters() const
240 return (InitCheck() == B_OK && IsPartitioningSystem()
241 && (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_PARAMETERS));
245 // SupportsSettingContentParameters
246 bool
247 BDiskSystem::SupportsSettingContentParameters(bool* whileMounted) const
249 if (InitCheck() != B_OK
250 || !(fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS)) {
251 if (whileMounted)
252 *whileMounted = false;
253 return false;
256 if (whileMounted) {
257 uint32 whileMountedFlag
258 = B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS_WHILE_MOUNTED;
259 *whileMounted = (IsFileSystem() && (fFlags & whileMountedFlag));
262 return true;
266 // SupportsCreatingChild
267 bool
268 BDiskSystem::SupportsCreatingChild() const
270 return (InitCheck() == B_OK && IsPartitioningSystem()
271 && (fFlags & B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD));
275 // SupportsDeletingChild
276 bool
277 BDiskSystem::SupportsDeletingChild() const
279 return (InitCheck() == B_OK && IsPartitioningSystem()
280 && (fFlags & B_DISK_SYSTEM_SUPPORTS_DELETING_CHILD));
284 // SupportsInitializing
285 bool
286 BDiskSystem::SupportsInitializing() const
288 return (InitCheck() == B_OK
289 && (fFlags & B_DISK_SYSTEM_SUPPORTS_INITIALIZING));
293 bool
294 BDiskSystem::SupportsWriting() const
296 if (InitCheck() != B_OK
297 || !IsFileSystem())
298 return false;
300 return (fFlags & B_DISK_SYSTEM_SUPPORTS_WRITING) != 0;
304 // GetTypeForContentType
305 status_t
306 BDiskSystem::GetTypeForContentType(const char* contentType, BString* type) const
308 if (InitCheck() != B_OK)
309 return InitCheck();
311 if (!contentType || !type || !IsPartitioningSystem())
312 return B_BAD_VALUE;
314 // get the disk system add-on
315 DiskSystemAddOnManager* manager = DiskSystemAddOnManager::Default();
316 BDiskSystemAddOn* addOn = manager->GetAddOn(fName.String());
317 if (!addOn)
318 return B_ENTRY_NOT_FOUND;
320 status_t result = addOn->GetTypeForContentType(contentType, type);
322 // put the add-on
323 manager->PutAddOn(addOn);
325 return result;
329 // IsPartitioningSystem
330 bool
331 BDiskSystem::IsPartitioningSystem() const
333 return InitCheck() == B_OK && !(fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
337 // IsFileSystem
338 bool
339 BDiskSystem::IsFileSystem() const
341 return InitCheck() == B_OK && (fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
345 // =
346 BDiskSystem&
347 BDiskSystem::operator=(const BDiskSystem& other)
349 fID = other.fID;
350 fName = other.fName;
351 fShortName = other.fShortName;
352 fPrettyName = other.fPrettyName;
353 fFlags = other.fFlags;
355 return *this;
359 // _SetTo
360 status_t
361 BDiskSystem::_SetTo(disk_system_id id)
363 _Unset();
365 if (id < 0)
366 return fID;
368 user_disk_system_info info;
369 status_t error = _kern_get_disk_system_info(id, &info);
370 if (error != B_OK)
371 return (fID = error);
373 return _SetTo(&info);
377 // _SetTo
378 status_t
379 BDiskSystem::_SetTo(const user_disk_system_info* info)
381 _Unset();
383 if (!info)
384 return (fID = B_BAD_VALUE);
386 fID = info->id;
387 fName = info->name;
388 fShortName = info->short_name;
389 fPrettyName = info->pretty_name;
390 fFlags = info->flags;
392 return B_OK;
396 // _Unset
397 void
398 BDiskSystem::_Unset()
400 fID = B_NO_INIT;
401 fName = (const char*)NULL;
402 fPrettyName = (const char*)NULL;
403 fFlags = 0;