xref: /haiku/src/kits/storage/disk_device/DiskSystem.cpp (revision f75a7bf508f3156d63a14f8fd77c5e0ca4d08c42)
1 /*
2  * Copyright 2003-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <DiskSystem.h>
7 
8 #include <DiskSystemAddOn.h>
9 #include <Partition.h>
10 
11 #include <ddm_userland_interface_defs.h>
12 #include <syscalls.h>
13 
14 #include "DiskSystemAddOnManager.h"
15 
16 
17 // constructor
18 BDiskSystem::BDiskSystem()
19 	:
20 	fID(B_NO_INIT),
21 	fFlags(0)
22 {
23 }
24 
25 
26 // copy constructor
27 BDiskSystem::BDiskSystem(const BDiskSystem& other)
28 	:
29 	fID(other.fID),
30 	fName(other.fName),
31 	fShortName(other.fShortName),
32 	fPrettyName(other.fPrettyName),
33 	fFlags(other.fFlags)
34 {
35 }
36 
37 
38 // destructor
39 BDiskSystem::~BDiskSystem()
40 {
41 }
42 
43 
44 // InitCheck
45 status_t
46 BDiskSystem::InitCheck() const
47 {
48 	return fID > 0 ? B_OK : fID;
49 }
50 
51 
52 // Name
53 const char*
54 BDiskSystem::Name() const
55 {
56 	return fName.String();
57 }
58 
59 
60 // ShortName
61 const char*
62 BDiskSystem::ShortName() const
63 {
64 	return fShortName.String();
65 }
66 
67 
68 // PrettyName
69 const char*
70 BDiskSystem::PrettyName() const
71 {
72 	return fPrettyName.String();
73 }
74 
75 
76 // SupportsDefragmenting
77 bool
78 BDiskSystem::SupportsDefragmenting(bool* whileMounted) const
79 {
80 	if (InitCheck() != B_OK
81 		|| !(fFlags & B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING)) {
82 		if (whileMounted)
83 			*whileMounted = false;
84 		return false;
85 	}
86 
87 	if (whileMounted) {
88 		*whileMounted = IsFileSystem() && (fFlags
89 				& B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING_WHILE_MOUNTED) != 0;
90 	}
91 
92 	return true;
93 }
94 
95 
96 // SupportsRepairing
97 bool
98 BDiskSystem::SupportsRepairing(bool checkOnly, bool* whileMounted) const
99 {
100 	uint32 mainBit = B_DISK_SYSTEM_SUPPORTS_REPAIRING;
101 	uint32 mountedBit = B_DISK_SYSTEM_SUPPORTS_REPAIRING_WHILE_MOUNTED;
102 
103 	if (checkOnly) {
104 		mainBit = B_DISK_SYSTEM_SUPPORTS_CHECKING;
105 		mountedBit = B_DISK_SYSTEM_SUPPORTS_CHECKING_WHILE_MOUNTED;
106 	}
107 
108 	if (InitCheck() != B_OK || !(fFlags & mainBit)) {
109 		if (whileMounted)
110 			*whileMounted = false;
111 		return false;
112 	}
113 
114 	if (whileMounted)
115 		*whileMounted = (IsFileSystem() && (fFlags & mountedBit));
116 
117 	return true;
118 }
119 
120 
121 // SupportsResizing
122 bool
123 BDiskSystem::SupportsResizing(bool* whileMounted) const
124 {
125 	if (InitCheck() != B_OK
126 		|| !(fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING)) {
127 		if (whileMounted)
128 			*whileMounted = false;
129 		return false;
130 	}
131 
132 	if (whileMounted) {
133 		*whileMounted = (IsFileSystem()
134 			&& (fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING_WHILE_MOUNTED));
135 	}
136 
137 	return true;
138 }
139 
140 
141 // SupportsResizingChild
142 bool
143 BDiskSystem::SupportsResizingChild() const
144 {
145 	return (InitCheck() == B_OK && IsPartitioningSystem()
146 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING_CHILD));
147 }
148 
149 
150 // SupportsMoving
151 bool
152 BDiskSystem::SupportsMoving(bool* whileMounted) const
153 {
154 	if (InitCheck() != B_OK
155 		|| !(fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING)) {
156 		if (whileMounted)
157 			*whileMounted = false;
158 		return false;
159 	}
160 
161 	if (whileMounted) {
162 		*whileMounted = (IsFileSystem()
163 			&& (fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING_WHILE_MOUNTED));
164 	}
165 
166 	return true;
167 }
168 
169 
170 // SupportsMovingChild
171 bool
172 BDiskSystem::SupportsMovingChild() const
173 {
174 	return (InitCheck() == B_OK && IsPartitioningSystem()
175 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING_CHILD));
176 }
177 
178 
179 // SupportsName
180 bool
181 BDiskSystem::SupportsName() const
182 {
183 	return (InitCheck() == B_OK && IsPartitioningSystem()
184 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_NAME));
185 }
186 
187 
188 // SupportsContentName
189 bool
190 BDiskSystem::SupportsContentName() const
191 {
192 	return (InitCheck() == B_OK
193 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME));
194 }
195 
196 
197 // SupportsSettingName
198 bool
199 BDiskSystem::SupportsSettingName() const
200 {
201 	return (InitCheck() == B_OK && IsPartitioningSystem()
202 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_NAME));
203 }
204 
205 
206 // SupportsSettingContentName
207 bool
208 BDiskSystem::SupportsSettingContentName(bool* whileMounted) const
209 {
210 	if (InitCheck() != B_OK
211 		|| !(fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME)) {
212 		if (whileMounted)
213 			*whileMounted = false;
214 		return false;
215 	}
216 
217 	if (whileMounted) {
218 		*whileMounted = (IsFileSystem()
219 			&& (fFlags
220 				& B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME_WHILE_MOUNTED));
221 	}
222 
223 	return true;
224 }
225 
226 
227 // SupportsSettingType
228 bool
229 BDiskSystem::SupportsSettingType() const
230 {
231 	return (InitCheck() == B_OK && IsPartitioningSystem()
232 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_TYPE));
233 }
234 
235 
236 // SupportsSettingParameters
237 bool
238 BDiskSystem::SupportsSettingParameters() const
239 {
240 	return (InitCheck() == B_OK && IsPartitioningSystem()
241 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_PARAMETERS));
242 }
243 
244 
245 // SupportsSettingContentParameters
246 bool
247 BDiskSystem::SupportsSettingContentParameters(bool* whileMounted) const
248 {
249 	if (InitCheck() != B_OK
250 		|| !(fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS)) {
251 		if (whileMounted)
252 			*whileMounted = false;
253 		return false;
254 	}
255 
256 	if (whileMounted) {
257 		uint32 whileMountedFlag
258 			= B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS_WHILE_MOUNTED;
259 		*whileMounted = (IsFileSystem() && (fFlags & whileMountedFlag));
260 	}
261 
262 	return true;
263 }
264 
265 
266 // SupportsCreatingChild
267 bool
268 BDiskSystem::SupportsCreatingChild() const
269 {
270 	return (InitCheck() == B_OK && IsPartitioningSystem()
271 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD));
272 }
273 
274 
275 // SupportsDeletingChild
276 bool
277 BDiskSystem::SupportsDeletingChild() const
278 {
279 	return (InitCheck() == B_OK && IsPartitioningSystem()
280 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_DELETING_CHILD));
281 }
282 
283 
284 // SupportsInitializing
285 bool
286 BDiskSystem::SupportsInitializing() const
287 {
288 	return (InitCheck() == B_OK
289 		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_INITIALIZING));
290 }
291 
292 
293 // GetTypeForContentType
294 status_t
295 BDiskSystem::GetTypeForContentType(const char* contentType, BString* type) const
296 {
297 	if (InitCheck() != B_OK)
298 		return InitCheck();
299 
300 	if (!contentType || !type || !IsPartitioningSystem())
301 		return B_BAD_VALUE;
302 
303 	// get the disk system add-on
304 	DiskSystemAddOnManager* manager = DiskSystemAddOnManager::Default();
305 	BDiskSystemAddOn* addOn = manager->GetAddOn(fName.String());
306 	if (!addOn)
307 		return B_ENTRY_NOT_FOUND;
308 
309 	status_t result = addOn->GetTypeForContentType(contentType, type);
310 
311 	// put the add-on
312 	manager->PutAddOn(addOn);
313 
314 	return result;
315 }
316 
317 
318 // IsPartitioningSystem
319 bool
320 BDiskSystem::IsPartitioningSystem() const
321 {
322 	return InitCheck() == B_OK && !(fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
323 }
324 
325 
326 // IsFileSystem
327 bool
328 BDiskSystem::IsFileSystem() const
329 {
330 	return InitCheck() == B_OK && (fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
331 }
332 
333 
334 // =
335 BDiskSystem&
336 BDiskSystem::operator=(const BDiskSystem& other)
337 {
338 	fID = other.fID;
339 	fName = other.fName;
340 	fShortName = other.fShortName;
341 	fPrettyName = other.fPrettyName;
342 	fFlags = other.fFlags;
343 
344 	return *this;
345 }
346 
347 
348 // _SetTo
349 status_t
350 BDiskSystem::_SetTo(disk_system_id id)
351 {
352 	_Unset();
353 
354 	if (id < 0)
355 		return fID;
356 
357 	user_disk_system_info info;
358 	status_t error = _kern_get_disk_system_info(id, &info);
359 	if (error != B_OK)
360 		return (fID = error);
361 
362 	return _SetTo(&info);
363 }
364 
365 
366 // _SetTo
367 status_t
368 BDiskSystem::_SetTo(const user_disk_system_info* info)
369 {
370 	_Unset();
371 
372 	if (!info)
373 		return (fID = B_BAD_VALUE);
374 
375 	fID = info->id;
376 	fName = info->name;
377 	fShortName = info->short_name;
378 	fPrettyName = info->pretty_name;
379 	fFlags = info->flags;
380 
381 	return B_OK;
382 }
383 
384 
385 // _Unset
386 void
387 BDiskSystem::_Unset()
388 {
389 	fID = B_NO_INIT;
390 	fName = (const char*)NULL;
391 	fPrettyName = (const char*)NULL;
392 	fFlags = 0;
393 }
394