aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/createHardLink.cpp
blob: 238851d7f01bbaecea7b2c2a3f52c53226502415 (plain) (blame)
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
#include <Common/createHardLink.h>
#include <Common/Exception.h>
#include <cerrno>
#include <unistd.h>
#include <sys/stat.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int CANNOT_STAT;
    extern const int CANNOT_LINK;
}

void createHardLink(const String & source_path, const String & destination_path)
{
    if (0 != link(source_path.c_str(), destination_path.c_str()))
    {
        if (errno == EEXIST)
        {
            auto link_errno = errno;

            struct stat source_descr;
            struct stat destination_descr;

            if (0 != lstat(source_path.c_str(), &source_descr))
                throwFromErrnoWithPath("Cannot stat " + source_path, source_path, ErrorCodes::CANNOT_STAT);

            if (0 != lstat(destination_path.c_str(), &destination_descr))
                throwFromErrnoWithPath("Cannot stat " + destination_path, destination_path, ErrorCodes::CANNOT_STAT);

            if (source_descr.st_ino != destination_descr.st_ino)
                throwFromErrnoWithPath(
                        "Destination file " + destination_path + " is already exist and have different inode.",
                        destination_path, ErrorCodes::CANNOT_LINK, link_errno);
        }
        else
            throwFromErrnoWithPath("Cannot link " + source_path + " to " + destination_path, destination_path,
                                   ErrorCodes::CANNOT_LINK);
    }
}

}