1/*
2 * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2008-2012, Axel D��rfler, axeld@pinc-software.de.
4 * Copyright 2012, Gerasim Troeglazov (3dEyes**), 3dEyes@gmail.com
5 *
6 * Distributed under the terms of the MIT License.
7 */
8
9
10#include "NTFSAddOn.h"
11#include "InitializeParameterEditor.h"
12
13#include <new>
14
15#include <Directory.h>
16#include <List.h>
17#include <Path.h>
18#include <Volume.h>
19
20#include <DiskDeviceTypes.h>
21#include <MutablePartition.h>
22
23#include <AutoDeleter.h>
24#include <StringForSize.h>
25
26#include <debug.h>
27#include <stdio.h>
28
29#ifdef ASSERT
30#	undef ASSERT
31#endif
32
33
34using std::nothrow;
35
36#define kPartitionTypeNTFS "NT File System"
37
38static const uint32 kDiskSystemFlags =
39	0
40	| B_DISK_SYSTEM_SUPPORTS_INITIALIZING
41	| B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME
42;
43
44#define TRACE printf
45
46NTFSAddOn::NTFSAddOn()
47	: BDiskSystemAddOn(kPartitionTypeNTFS, kDiskSystemFlags)
48{
49}
50
51
52NTFSAddOn::~NTFSAddOn()
53{
54}
55
56
57status_t
58NTFSAddOn::CreatePartitionHandle(BMutablePartition* partition,
59	BPartitionHandle** _handle)
60{
61	NTFSPartitionHandle* handle = new(nothrow) NTFSPartitionHandle(partition);
62	if (!handle)
63		return B_NO_MEMORY;
64
65	status_t error = handle->Init();
66	if (error != B_OK) {
67		delete handle;
68		return error;
69	}
70
71	*_handle = handle;
72
73	return B_OK;
74}
75
76
77bool
78NTFSAddOn::CanInitialize(const BMutablePartition* partition)
79{
80	return true;
81}
82
83
84status_t
85NTFSAddOn::ValidateInitialize(const BMutablePartition* partition, BString* name,
86	const char* parameterString)
87{
88	if (!CanInitialize(partition) || !name)
89		return B_BAD_VALUE;
90
91	if (name->Length() >= MAX_PATH)
92		name->Truncate(MAX_PATH - 1);
93
94	name->ReplaceAll('/', '-');
95
96	return B_OK;
97}
98
99
100status_t
101NTFSAddOn::Initialize(BMutablePartition* partition, const char* name,
102	const char* parameterString, BPartitionHandle** _handle)
103{
104	if (!CanInitialize(partition) || name == NULL)
105		return B_BAD_VALUE;
106
107	NTFSPartitionHandle* handle = new(nothrow) NTFSPartitionHandle(partition);
108	if (!handle)
109		return B_NO_MEMORY;
110	ObjectDeleter<NTFSPartitionHandle> handleDeleter(handle);
111
112	status_t error = partition->SetContentType(Name());
113	if (error != B_OK)
114		return error;
115
116	partition->SetContentName(name);
117	partition->SetContentParameters(parameterString);
118	uint32 blockSize = 4096;
119	partition->SetBlockSize(blockSize);
120	partition->SetContentSize(partition->Size() / blockSize * blockSize);
121	partition->Changed(B_PARTITION_CHANGED_INITIALIZATION);
122
123	*_handle = handleDeleter.Detach();
124	return B_OK;
125}
126
127
128status_t
129NTFSAddOn::GetParameterEditor(B_PARAMETER_EDITOR_TYPE type,
130	BPartitionParameterEditor** editor)
131{
132	*editor = NULL;
133	if (type == B_INITIALIZE_PARAMETER_EDITOR) {
134		try {
135			*editor = new InitializeNTFSEditor();
136		} catch (std::bad_alloc&) {
137			return B_NO_MEMORY;
138		}
139		return B_OK;
140	}
141	return B_NOT_SUPPORTED;
142}
143
144
145NTFSPartitionHandle::NTFSPartitionHandle(BMutablePartition* partition)
146	: BPartitionHandle(partition)
147{
148}
149
150
151NTFSPartitionHandle::~NTFSPartitionHandle()
152{
153}
154
155
156status_t
157NTFSPartitionHandle::Init()
158{
159	return B_OK;
160}
161
162
163uint32
164NTFSPartitionHandle::SupportedOperations(uint32 mask)
165{
166	return kDiskSystemFlags & mask;
167}
168
169
170status_t
171get_disk_system_add_ons(BList* addOns)
172{
173	NTFSAddOn* addOn = new(nothrow) NTFSAddOn;
174	if (!addOn)
175		return B_NO_MEMORY;
176
177	if (!addOns->AddItem(addOn)) {
178		delete addOn;
179		return B_NO_MEMORY;
180	}
181	return B_OK;
182}
183