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