1/*
2 * Copyright 2011-2012 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Hamish Morrison, hamish@lavabit.com
7 *		Alexander von Gluck IV, kallisti5@unixzen.com
8 */
9
10
11#include "VMUtils.h"
12
13#include <stdio.h>
14
15#include <StackOrHeapArray.h>
16
17
18status_t
19get_mount_point(KPartition* partition, KPath* mountPoint)
20{
21	if (!mountPoint || !partition->ContainsFileSystem())
22		return B_BAD_VALUE;
23
24	int nameLength = 0;
25	const char* volumeName = partition->ContentName();
26	if (volumeName != NULL)
27		nameLength = strlen(volumeName);
28	if (nameLength == 0) {
29		volumeName = partition->Name();
30		if (volumeName != NULL)
31			nameLength = strlen(volumeName);
32		if (nameLength == 0) {
33			volumeName = "unnamed volume";
34			nameLength = strlen(volumeName);
35		}
36	}
37
38	BStackOrHeapArray<char, 128> basePath(nameLength + 2);
39	if (!basePath.IsValid())
40		return B_NO_MEMORY;
41	int32 len = snprintf(basePath, nameLength + 2, "/%s", volumeName);
42	for (int32 i = 1; i < len; i++)
43		if (basePath[i] == '/')
44			basePath[i] = '-';
45	char* path = mountPoint->LockBuffer();
46	int32 pathLen = mountPoint->BufferSize();
47	strncpy(path, basePath, pathLen);
48
49	struct stat dummy;
50	for (int i = 1; ; i++) {
51		if (stat(path, &dummy) != 0)
52			break;
53		snprintf(path, pathLen, "%s%d", (char*)basePath, i);
54	}
55
56	mountPoint->UnlockBuffer();
57	return B_OK;
58}
59