forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatehardlink.cpp
More file actions
115 lines (108 loc) · 3.11 KB
/
Copy pathcreatehardlink.cpp
File metadata and controls
115 lines (108 loc) · 3.11 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! @file createsymlink.cpp
//! @author George FLeming <v-geflem@microsoft.com>
//! @brief create new hard link
#include <errno.h>
#include <unistd.h>
#include <string>
#include "createhardlink.h"
//! @brief Createhardlink create new symbolic link
//!
//! Createhardlink
//!
//! @param[in] link
//! @parblock
//! A pointer to the buffer that contains the symbolic link to create
//!
//! char* is marshaled as an LPStr, which on Linux is UTF-8.
//! @endparblock
//!
//! @param[in] target
//! @parblock
//! A pointer to the buffer that contains the existing file
//!
//! char* is marshaled as an LPStr, which on Linux is UTF-8.
//! @endparblock
//!
//! @exception errno Passes these errors via errno to GetLastError:
//! - ERROR_INVALID_PARAMETER: parameter is not valid
//! - ERROR_FILE_NOT_FOUND: file does not exist
//! - ERROR_ACCESS_DENIED: access is denied
//! - ERROR_FILE_NOT_FOUND: the system cannot find the file specified
//! - ERROR_INVALID_ADDRESS: attempt to access invalid address
//! - ERROR_TOO_MANY_LINK: max number of hard links has been exceeded
//! - ERROR_GEN_FAILURE: device attached to the system is not functioning
//! - ERROR_NO_SUCH_USER: there was no corresponding entry in the utmp-file
//! - ERROR_INVALID_NAME: filename, directory name, or volume label syntax is incorrect
//! - ERROR_BUFFER_OVERFLOW: file name is too long
//! - ERROR_INVALID_FUNCTION: incorrect function
//! - ERROR_BAD_PATH_NAME: pathname is too long, or contains invalid characters
//!
//! @retval 1 if creation is successful
//! @retval 0 if creation failed
//!
int32_t CreateHardLink(const char *newlink, const char *target)
{
errno = 0;
// Check parameters
if (!newlink || !target)
{
errno = ERROR_INVALID_PARAMETER;
return 0;
}
int returnCode = link(target, newlink);
if (returnCode == 0)
{
return 1;
}
switch(errno)
{
case EACCES:
errno = ERROR_ACCESS_DENIED;
break;
case EDQUOT:
errno = ERROR_DISK_FULL;
break;
case EEXIST:
errno = ERROR_FILE_EXISTS;
break;
case EFAULT:
errno = ERROR_INVALID_ADDRESS;
break;
case EIO:
errno = ERROR_GEN_FAILURE;
break;
case ELOOP:
errno = ERROR_TOO_MANY_LINKS;
break;
case EMLINK:
errno = ERROR_TOO_MANY_LINKS;
break;
case ENAMETOOLONG:
errno = ERROR_BAD_PATH_NAME;
break;
case ENOENT:
errno = ERROR_FILE_NOT_FOUND;
break;
case ENOMEM:
errno = ERROR_OUTOFMEMORY;
break;
case ENOTDIR:
errno = ERROR_INVALID_NAME;
break;
case ENOSPC:
errno = ERROR_DISK_FULL;
break;
case EPERM:
errno = ERROR_ACCESS_DENIED;
break;
case EROFS:
errno = ERROR_ACCESS_DENIED;
break;
case EXDEV:
errno = ERROR_GEN_FAILURE;
break;
default:
errno = ERROR_INVALID_FUNCTION;
}
return 0;
}