KPartition.h revision 4ed39e6a
1/*
2 * Copyright 2003-2007, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <bonefish@cs.tu-berlin.de>
7 */
8#ifndef _K_DISK_DEVICE_PARTITION_H
9#define _K_DISK_DEVICE_PARTITION_H
10
11#include <disk_device_manager.h>
12#include <Vector.h>
13
14struct user_partition_data;
15
16namespace BPrivate {
17namespace DiskDevice {
18
19class UserDataWriter;
20
21class KDiskDevice;
22class KDiskSystem;
23class KPartitionListener;
24class KPartitionVisitor;
25class KPath;
26class KPhysicalPartition;
27
28//!	\brief Class representing a single partition.
29class KPartition {
30public:
31	KPartition(partition_id id = -1);
32	virtual ~KPartition();
33
34	// Reference counting. As long as there's at least one referrer, the
35	// object won't be deleted.
36	// manager must be locked (Unregister() locks itself)
37	void Register();
38	void Unregister();
39	int32 CountReferences() const;
40
41	void MarkObsolete();
42		// called by the manager only
43	bool IsObsolete() const;
44
45	virtual bool PrepareForRemoval();
46	virtual bool PrepareForDeletion();
47
48	virtual status_t Open(int flags, int *fd);
49	virtual status_t PublishDevice();
50	virtual status_t UnpublishDevice();
51	virtual status_t RepublishDevice();
52	bool IsPublished() const;
53
54	void SetBusy(bool busy);
55	bool IsBusy() const;
56	bool IsBusy(bool includeDescendants);
57	bool CheckAndMarkBusy(bool includeDescendants);
58	void MarkBusy(bool includeDescendants);
59	void UnmarkBusy(bool includeDescendants);
60
61	void SetOffset(off_t offset);
62	off_t Offset() const;		// 0 for devices
63
64	void SetSize(off_t size);
65	off_t Size() const;
66
67	void SetContentSize(off_t size);
68	off_t ContentSize() const;
69
70	void SetBlockSize(uint32 blockSize);
71	uint32 BlockSize() const;
72
73	void SetIndex(int32 index);
74	int32 Index() const;		// 0 for devices
75
76	void SetStatus(uint32 status);
77	uint32 Status() const;
78	bool IsUninitialized() const;
79
80	void SetFlags(uint32 flags);	// comprises the ones below
81	void AddFlags(uint32 flags);
82	void ClearFlags(uint32 flags);
83	uint32 Flags() const;
84	bool ContainsFileSystem() const;
85	bool ContainsPartitioningSystem() const;
86	bool IsReadOnly() const;
87	bool IsMounted() const;
88	bool IsChildMounted();
89
90	bool IsDevice() const;
91
92	status_t SetName(const char *name);
93	const char *Name() const;
94
95	status_t SetContentName(const char *name);
96	const char *ContentName() const;
97
98	status_t SetType(const char *type);
99	const char *Type() const;
100
101	const char *ContentType() const;
102		// ContentType() == DiskSystem()->NamePretty()
103
104	// access to C style partition data
105	partition_data *PartitionData();
106	const partition_data *PartitionData() const;
107
108	virtual void SetID(partition_id id);
109	partition_id ID() const;
110
111	virtual	status_t GetFileName(char* buffer, size_t size) const;
112	virtual status_t GetPath(KPath *path) const;
113		// no setter (see BDiskDevice) -- built on the fly
114
115	void SetVolumeID(dev_t volumeID);
116	dev_t VolumeID() const;
117
118	void SetMountCookie(void *cookie);
119	void *MountCookie() const;
120
121	virtual status_t Mount(uint32 mountFlags, const char *parameters);
122	virtual status_t Unmount();
123
124	// Parameters
125
126	status_t SetParameters(const char *parameters);
127	const char *Parameters() const;
128
129	status_t SetContentParameters(const char *parameters);
130	const char *ContentParameters() const;
131
132	// Hierarchy
133
134	void SetDevice(KDiskDevice *device);
135	KDiskDevice *Device() const;
136
137	void SetParent(KPartition *parent);
138	KPartition *Parent() const;
139
140	status_t AddChild(KPartition *partition, int32 index = -1);
141	status_t CreateChild(partition_id id, int32 index, off_t offset, off_t size,
142		KPartition **child = NULL);
143	bool RemoveChild(int32 index);
144	bool RemoveChild(KPartition *child);
145	bool RemoveAllChildren();
146	KPartition *ChildAt(int32 index) const;
147	int32 CountChildren() const;
148	int32 CountDescendants() const;
149
150	KPartition *VisitEachDescendant(KPartitionVisitor *visitor);
151
152	// DiskSystem
153
154	void SetDiskSystem(KDiskSystem *diskSystem, float priority = 0.0f);
155	KDiskSystem *DiskSystem() const;
156	float DiskSystemPriority() const;
157	KDiskSystem *ParentDiskSystem() const;
158		// When setting a disk system, it must already be loaded.
159		// The partition will load it too, hence it won't be unloaded before
160		// it is unset here.
161
162	void SetCookie(void *cookie);
163	void *Cookie() const;
164
165	void SetContentCookie(void *cookie);
166	void *ContentCookie() const;
167
168	// Listener Support
169
170	bool AddListener(KPartitionListener *listener);
171	bool RemoveListener(KPartitionListener *listener);
172
173	// Change Tracking
174
175	void Changed(uint32 flags, uint32 clearFlags = 0);
176	void SetChangeFlags(uint32 flags);
177	uint32 ChangeFlags() const;
178	int32 ChangeCounter() const;
179	status_t UninitializeContents(bool logChanges = true);
180
181	void SetAlgorithmData(uint32 data);
182	uint32 AlgorithmData() const;
183		// temporary storage freely usable by algorithms
184
185	virtual void WriteUserData(UserDataWriter &writer,
186							   user_partition_data *data);
187
188	virtual void Dump(bool deep, int32 level);
189
190protected:
191	void FireOffsetChanged(off_t offset);
192	void FireSizeChanged(off_t size);
193	void FireContentSizeChanged(off_t size);
194	void FireBlockSizeChanged(uint32 blockSize);
195	void FireIndexChanged(int32 index);
196	void FireStatusChanged(uint32 status);
197	void FireFlagsChanged(uint32 flags);
198	void FireNameChanged(const char *name);
199	void FireContentNameChanged(const char *name);
200	void FireTypeChanged(const char *type);
201	void FireIDChanged(partition_id id);
202	void FireVolumeIDChanged(dev_t volumeID);
203	void FireMountCookieChanged(void *cookie);
204	void FireParametersChanged(const char *parameters);
205	void FireContentParametersChanged(const char *parameters);
206	void FireChildAdded(KPartition *child, int32 index);
207	void FireChildRemoved(KPartition *child, int32 index);
208	void FireDiskSystemChanged(KDiskSystem *diskSystem);
209	void FireCookieChanged(void *cookie);
210	void FireContentCookieChanged(void *cookie);
211
212private:
213	void _UpdateChildIndices(int32 start, int32 end);
214	static int32 _NextID();
215
216protected:
217	typedef Vector<KPartition*> PartitionVector;
218	struct ListenerSet;
219
220	partition_data		fPartitionData;
221	PartitionVector		fChildren;
222	KDiskDevice			*fDevice;
223	KPartition			*fParent;
224	KDiskSystem			*fDiskSystem;
225	float				fDiskSystemPriority;
226	ListenerSet			*fListeners;
227	uint32				fChangeFlags;
228	int32				fChangeCounter;
229	uint32				fAlgorithmData;
230	int32				fReferenceCount;
231	bool				fObsolete;
232	char				*fPublishedName;
233	static int32		sNextID;
234};
235
236} // namespace DiskDevice
237} // namespace BPrivate
238
239using BPrivate::DiskDevice::KPartition;
240
241#endif	// _K_DISK_DEVICE_PARTITION_H
242