1/*
2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <new>
8
9#include <DiskDeviceDefs.h>
10#include <DiskSystemAddOn.h>
11
12#include <AutoDeleter.h>
13#include <MutablePartition.h>
14
15#include "checksumfs.h"
16
17
18static const uint32 kDiskSystemFlags =
19    0
20//  | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME
21//  | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS
22    | B_DISK_SYSTEM_SUPPORTS_INITIALIZING
23    | B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME
24//  | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME_WHILE_MOUNTED
25//  | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS_WHILE_MOUNTED
26;
27
28
29class CheckSumFSAddOn : public BDiskSystemAddOn {
30public:
31								CheckSumFSAddOn();
32
33	virtual	status_t			CreatePartitionHandle(
34									BMutablePartition* partition,
35									BPartitionHandle** _handle);
36
37	virtual	bool				CanInitialize(
38									const BMutablePartition* partition);
39	virtual	status_t			GetInitializationParameterEditor(
40									const BMutablePartition* partition,
41									BPartitionParameterEditor** _editor);
42	virtual	status_t			ValidateInitialize(
43									const BMutablePartition* partition,
44									BString* name, const char* parameters);
45	virtual	status_t			Initialize(BMutablePartition* partition,
46									const char* name, const char* parameters,
47									BPartitionHandle** _handle);
48};
49
50
51class CheckSumFSPartitionHandle : public BPartitionHandle {
52public:
53								CheckSumFSPartitionHandle(
54									BMutablePartition* partition);
55								~CheckSumFSPartitionHandle();
56
57			status_t			Init();
58
59	virtual	uint32				SupportedOperations(uint32 mask);
60};
61
62
63// #pragma mark - CheckSumFSAddOn
64
65
66CheckSumFSAddOn::CheckSumFSAddOn()
67	:
68	BDiskSystemAddOn(CHECK_SUM_FS_PRETTY_NAME, kDiskSystemFlags)
69{
70}
71
72
73status_t
74CheckSumFSAddOn::CreatePartitionHandle(BMutablePartition* partition,
75	BPartitionHandle** _handle)
76{
77debug_printf("CheckSumFSAddOn::CreatePartitionHandle()\n");
78	CheckSumFSPartitionHandle* handle
79		= new(std::nothrow) CheckSumFSPartitionHandle(partition);
80	if (handle == NULL)
81		return B_NO_MEMORY;
82
83	status_t error = handle->Init();
84	if (error != B_OK) {
85		delete handle;
86		return error;
87	}
88
89	*_handle = handle;
90	return B_OK;
91}
92
93
94bool
95CheckSumFSAddOn::CanInitialize(const BMutablePartition* partition)
96{
97debug_printf("CheckSumFSAddOn::CanInitialize()\n");
98	return (uint64)partition->Size() >= kCheckSumFSMinSize;
99}
100
101
102status_t
103CheckSumFSAddOn::GetInitializationParameterEditor(
104	const BMutablePartition* partition, BPartitionParameterEditor** _editor)
105{
106debug_printf("CheckSumFSAddOn::GetInitializationParameterEditor()\n");
107	*_editor = NULL;
108	return B_OK;
109}
110
111
112status_t
113CheckSumFSAddOn::ValidateInitialize(const BMutablePartition* partition,
114	BString* name, const char* parameters)
115{
116debug_printf("CheckSumFSAddOn::ValidateInitialize()\n");
117	if (!CanInitialize(partition) || name == NULL)
118		return B_BAD_VALUE;
119
120	// truncate name, if too long
121	if ((uint64)name->Length() >= kCheckSumFSNameLength)
122		name->Truncate(kCheckSumFSNameLength - 1);
123
124	// replace '/' by '-'
125	name->ReplaceAll('/', '-');
126
127	return B_OK;
128}
129
130
131status_t
132CheckSumFSAddOn::Initialize(BMutablePartition* partition, const char* name,
133	const char* parameters, BPartitionHandle** _handle)
134{
135debug_printf("CheckSumFSAddOn::Initialize()\n");
136	if (!CanInitialize(partition) || name == NULL
137		|| strlen(name) >= kCheckSumFSNameLength) {
138		return B_BAD_VALUE;
139	}
140
141	CheckSumFSPartitionHandle* handle
142		= new(std::nothrow) CheckSumFSPartitionHandle(partition);
143	if (handle == NULL)
144		return B_NO_MEMORY;
145	ObjectDeleter<CheckSumFSPartitionHandle> handleDeleter(handle);
146
147	status_t error = partition->SetContentType(Name());
148	if (error != B_OK)
149		return error;
150
151	partition->SetContentName(name);
152	partition->SetContentParameters(parameters);
153	partition->SetBlockSize(B_PAGE_SIZE);
154	partition->SetContentSize(partition->Size() / B_PAGE_SIZE * B_PAGE_SIZE);
155	partition->Changed(B_PARTITION_CHANGED_INITIALIZATION);
156
157	*_handle = handleDeleter.Detach();
158
159	return B_OK;
160}
161
162
163// #pragma mark - CheckSumFSPartitionHandle
164
165
166CheckSumFSPartitionHandle::CheckSumFSPartitionHandle(
167	BMutablePartition* partition)
168	:
169	BPartitionHandle(partition)
170{
171}
172
173
174CheckSumFSPartitionHandle::~CheckSumFSPartitionHandle()
175{
176}
177
178
179status_t
180CheckSumFSPartitionHandle::Init()
181{
182	return B_OK;
183}
184
185
186uint32
187CheckSumFSPartitionHandle::SupportedOperations(uint32 mask)
188{
189	return kDiskSystemFlags & mask;
190}
191
192
193
194// #pragma mark -
195
196
197status_t
198get_disk_system_add_ons(BList* addOns)
199{
200debug_printf("checksumfs: get_disk_system_add_ons()\n");
201    CheckSumFSAddOn* addOn = new(std::nothrow) CheckSumFSAddOn;
202    if (addOn == NULL)
203        return B_NO_MEMORY;
204
205    if (!addOns->AddItem(addOn)) {
206        delete addOn;
207        return B_NO_MEMORY;
208    }
209
210debug_printf("checksumfs: get_disk_system_add_ons() done ok\n");
211    return B_OK;
212}
213