xref: /haiku/src/apps/drivesetup/Support.cpp (revision cda5b8808fd0262f0fac472f6cfa809f846a83cf)
1 /*
2  * Copyright 2002-2007 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Erik Jaesler <ejakowatz@users.sourceforge.net>
7  *		Ithamar R. Adema <ithamar@unet.nl>
8  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 #include "Support.h"
11 
12 #include <Partition.h>
13 #include <String.h>
14 
15 
16 const char*
17 string_for_size(off_t size, char *string)
18 {
19 	double kb = size / 1024.0;
20 	if (kb < 1.0) {
21 		sprintf(string, "%Ld B", size);
22 		return string;
23 	}
24 	float mb = kb / 1024.0;
25 	if (mb < 1.0) {
26 		sprintf(string, "%3.1f KB", kb);
27 		return string;
28 	}
29 	float gb = mb / 1024.0;
30 	if (gb < 1.0) {
31 		sprintf(string, "%3.1f MB", mb);
32 		return string;
33 	}
34 	float tb = gb / 1024.0;
35 	if (tb < 1.0) {
36 		sprintf(string, "%3.1f GB", gb);
37 		return string;
38 	}
39 	sprintf(string, "%.1f TB", tb);
40 	return string;
41 }
42 
43 
44 void
45 dump_partition_info(const BPartition* partition)
46 {
47 	char size[1024];
48 	printf("\tOffset(): %Ld\n", partition->Offset());
49 	printf("\tSize(): %s\n", string_for_size(partition->Size(),size));
50 	printf("\tContentSize(): %s\n", string_for_size(partition->ContentSize(),
51 		size));
52 	printf("\tBlockSize(): %ld\n", partition->BlockSize());
53 	printf("\tIndex(): %ld\n", partition->Index());
54 	printf("\tStatus(): %ld\n\n", partition->Status());
55 	printf("\tContainsFileSystem(): %s\n",
56 		partition->ContainsFileSystem() ? "true" : "false");
57 	printf("\tContainsPartitioningSystem(): %s\n\n",
58 		partition->ContainsPartitioningSystem() ? "true" : "false");
59 	printf("\tIsDevice(): %s\n", partition->IsDevice() ? "true" : "false");
60 	printf("\tIsReadOnly(): %s\n", partition->IsReadOnly() ? "true" : "false");
61 	printf("\tIsMounted(): %s\n", partition->IsMounted() ? "true" : "false");
62 	printf("\tIsBusy(): %s\n\n", partition->IsBusy() ? "true" : "false");
63 	printf("\tFlags(): %lx\n\n", partition->Flags());
64 	printf("\tName(): %s\n", partition->Name());
65 	printf("\tContentName(): %s\n", partition->ContentName());
66 	printf("\tType(): %s\n", partition->Type());
67 	printf("\tContentType(): %s\n", partition->ContentType());
68 	printf("\tID(): %lx\n\n", partition->ID());
69 }
70 
71 
72 bool
73 is_valid_partitionable_space(size_t size)
74 {
75 	// TODO: remove this again, the DiskDeviceAPI should
76 	// not even show these spaces to begin with
77 	return size >= 8 * 1024 * 1024;
78 }
79 
80 
81 // #pragma mark -SpaceIDMap
82 
83 SpaceIDMap::SpaceIDMap()
84 	: HashMap<HashString, partition_id>()
85 	, fNextSpaceID(-2)
86 {
87 }
88 
89 
90 SpaceIDMap::~SpaceIDMap()
91 {
92 }
93 
94 
95 partition_id
96 SpaceIDMap::SpaceIDFor(partition_id parentID, off_t spaceOffset)
97 {
98 	BString key;
99 	key << parentID << ':' << (uint64)spaceOffset;
100 
101 	if (ContainsKey(key.String()))
102 		return Get(key.String());
103 
104 	partition_id newID = fNextSpaceID--;
105 	Put(key.String(), newID);
106 
107 	return newID;
108 }
109 
110 
111