summaryrefslogtreecommitdiff
path: root/src/framework/client/canonicalize.cpp
blob: bb412dd95d97edeb8c01151763d86ee3697b4543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        canonicalize.cpp
 * @author      Kyungwook Tak (k.tak@samsung.com)
 * @version     1.0
 * @brief       Canonicalize file and directory paths
 */
#include "client/canonicalize.h"

#include <utility>
#include <cerrno>
#include <unistd.h>

#include "common/exception.h"

namespace Csr {
namespace Client {

std::string getAbsolutePath(const std::string &path)
{
	if (path.empty() || path.front() == '/')
		return path;

	size_t bufsize = 1024;
	std::vector<char> buf(bufsize, 0);

	char *result = nullptr;

	while ((result = ::getcwd(buf.data(), bufsize)) == nullptr && errno == ERANGE) {
		bufsize <<= 1;
		buf.resize(bufsize, 0);
	}

	if (result == nullptr)
		ThrowExc(CSR_ERROR_FILE_SYSTEM, "Failed to getcwd. errno: " << errno);

	std::string apath(buf.data());
	apath += '/';
	apath += path;

	return apath;
}

}
}