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