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
18BDiskSystem::BDiskSystem()
19	:
20	fID(B_NO_INIT),
21	fFlags(0)
22{
23}
24
25
26// copy constructor
27BDiskSystem::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
39BDiskSystem::~BDiskSystem()
40{
41}
42
43
44// InitCheck
45status_t
46BDiskSystem::InitCheck() const
47{
48	return fID > 0 ? B_OK : fID;
49}
50
51
52// Name
53const char*
54BDiskSystem::Name() const
55{
56	return fName.String();
57}
58
59
60// ShortName
61const char*
62BDiskSystem::ShortName() const
63{
64	return fShortName.String();
65}
66
67
68// PrettyName
69const char*
70BDiskSystem::PrettyName() const
71{
72	return fPrettyName.String();
73}
74
75
76// SupportsDefragmenting
77bool
78BDiskSystem::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
97bool
98BDiskSystem::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
122bool
123BDiskSystem::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
142bool
143BDiskSystem::SupportsResizingChild() const
144{
145	return (InitCheck() == B_OK && IsPartitioningSystem()
146		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_RESIZING_CHILD));
147}
148
149
150// SupportsMoving
151bool
152BDiskSystem::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
171bool
172BDiskSystem::SupportsMovingChild() const
173{
174	return (InitCheck() == B_OK && IsPartitioningSystem()
175		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_MOVING_CHILD));
176}
177
178
179// SupportsName
180bool
181BDiskSystem::SupportsName() const
182{
183	return (InitCheck() == B_OK && IsPartitioningSystem()
184		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_NAME));
185}
186
187
188// SupportsContentName
189bool
190BDiskSystem::SupportsContentName() const
191{
192	return (InitCheck() == B_OK
193		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME));
194}
195
196
197// SupportsSettingName
198bool
199BDiskSystem::SupportsSettingName() const
200{
201	return (InitCheck() == B_OK && IsPartitioningSystem()
202		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_NAME));
203}
204
205
206// SupportsSettingContentName
207bool
208BDiskSystem::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
228bool
229BDiskSystem::SupportsSettingType() const
230{
231	return (InitCheck() == B_OK && IsPartitioningSystem()
232		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_TYPE));
233}
234
235
236// SupportsSettingParameters
237bool
238BDiskSystem::SupportsSettingParameters() const
239{
240	return (InitCheck() == B_OK && IsPartitioningSystem()
241		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_SETTING_PARAMETERS));
242}
243
244
245// SupportsSettingContentParameters
246bool
247BDiskSystem::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
267bool
268BDiskSystem::SupportsCreatingChild() const
269{
270	return (InitCheck() == B_OK && IsPartitioningSystem()
271		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD));
272}
273
274
275// SupportsDeletingChild
276bool
277BDiskSystem::SupportsDeletingChild() const
278{
279	return (InitCheck() == B_OK && IsPartitioningSystem()
280		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_DELETING_CHILD));
281}
282
283
284// SupportsInitializing
285bool
286BDiskSystem::SupportsInitializing() const
287{
288	return (InitCheck() == B_OK
289		&& (fFlags & B_DISK_SYSTEM_SUPPORTS_INITIALIZING));
290}
291
292
293bool
294BDiskSystem::SupportsWriting() const
295{
296	if (InitCheck() != B_OK
297		|| !IsFileSystem())
298		return false;
299
300	return (fFlags & B_DISK_SYSTEM_SUPPORTS_WRITING) != 0;
301}
302
303
304// GetTypeForContentType
305status_t
306BDiskSystem::GetTypeForContentType(const char* contentType, BString* type) const
307{
308	if (InitCheck() != B_OK)
309		return InitCheck();
310
311	if (!contentType || !type || !IsPartitioningSystem())
312		return B_BAD_VALUE;
313
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;
319
320	status_t result = addOn->GetTypeForContentType(contentType, type);
321
322	// put the add-on
323	manager->PutAddOn(addOn);
324
325	return result;
326}
327
328
329// IsPartitioningSystem
330bool
331BDiskSystem::IsPartitioningSystem() const
332{
333	return InitCheck() == B_OK && !(fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
334}
335
336
337// IsFileSystem
338bool
339BDiskSystem::IsFileSystem() const
340{
341	return InitCheck() == B_OK && (fFlags & B_DISK_SYSTEM_IS_FILE_SYSTEM);
342}
343
344
345// =
346BDiskSystem&
347BDiskSystem::operator=(const BDiskSystem& other)
348{
349	fID = other.fID;
350	fName = other.fName;
351	fShortName = other.fShortName;
352	fPrettyName = other.fPrettyName;
353	fFlags = other.fFlags;
354
355	return *this;
356}
357
358
359// _SetTo
360status_t
361BDiskSystem::_SetTo(disk_system_id id)
362{
363	_Unset();
364
365	if (id < 0)
366		return fID;
367
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);
372
373	return _SetTo(&info);
374}
375
376
377// _SetTo
378status_t
379BDiskSystem::_SetTo(const user_disk_system_info* info)
380{
381	_Unset();
382
383	if (!info)
384		return (fID = B_BAD_VALUE);
385
386	fID = info->id;
387	fName = info->name;
388	fShortName = info->short_name;
389	fPrettyName = info->pretty_name;
390	fFlags = info->flags;
391
392	return B_OK;
393}
394
395
396// _Unset
397void
398BDiskSystem::_Unset()
399{
400	fID = B_NO_INIT;
401	fName = (const char*)NULL;
402	fPrettyName = (const char*)NULL;
403	fFlags = 0;
404}
405