xref: /haiku/src/add-ons/kernel/file_systems/exfat/Utility.cpp (revision f7c507c3a6fbf3a44c59500543926a9088724968)
1 /*
2  * Copyright 2011, Jérôme Duval, korli@users.berlios.de.
3  * Copyright 2014 Haiku, Inc. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  *
7  * Authors:
8  *		Jérôme Duval, korli@users.berlios.de
9  *		John Scipione, jscipione@gmail.com
10  */
11 
12 
13 #include "Utility.h"
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <Errors.h>
20 
21 #include "convertutf.h"
22 
23 
24 status_t
25 get_volume_name(struct exfat_entry* entry, char* name, size_t length)
26 {
27 	if (entry == NULL || name == NULL)
28 		return B_BAD_VALUE;
29 
30 	if (entry->type == EXFAT_ENTRY_TYPE_NOT_IN_USE)
31 		strlcpy(name, "", length);
32 	else if (entry->type == EXFAT_ENTRY_TYPE_LABEL) {
33 		ssize_t utf8Length = utf16le_to_utf8(entry->volume_label.name,
34 			entry->volume_label.length, name, length);
35 		if (utf8Length < 0)
36 			return (status_t)utf8Length;
37 	} else
38 		return B_NAME_NOT_FOUND;
39 
40 	return B_OK;
41 }
42 
43 
44 void
45 get_default_volume_name(off_t partitionSize, char* name, size_t length)
46 {
47 	off_t divisor = 1ULL << 40;
48 	char unit = 'T';
49 	if (partitionSize < divisor) {
50 		divisor = 1UL << 30;
51 		unit = 'G';
52 		if (partitionSize < divisor) {
53 			divisor = 1UL << 20;
54 			unit = 'M';
55 		}
56 	}
57 
58 	double size = double((10 * partitionSize + divisor - 1) / divisor);
59 		// %g in the kernel does not support precision...
60 
61 	snprintf(name, length, "%g%ciB ExFAT Volume", size / 10, unit);
62 }
63