-
Notifications
You must be signed in to change notification settings - Fork 0
/
symlink.c
272 lines (240 loc) · 7.29 KB
/
symlink.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Demonstrate symbolc link creation on Windows.
//
// This shows CreateSymbolicLinkW and CreateSymbolicLinkA explicitly, creating
// both file and directory symbolic links and reporting any errors.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
static const wchar_t *const symlink_file_w = L"symlink-file-w";
static const wchar_t *const symlink_dir_w = L"symlink-dir-w";
static const wchar_t *const target_file_w = L"target-file-w";
static const wchar_t *const target_dir_w = L"target-dir-w";
static const char *const symlink_file_a = "symlink-file-a";
static const char *const symlink_dir_a = "symlink-dir-a";
static const char *const target_file_a = "target-file-a";
static const char *const target_dir_a = "target-dir-a";
enum status {
status_success = 0,
status_symlink_creation_failed = 1,
status_error_setting_up_experiment = 2,
};
__declspec(noreturn) static void wdie(wchar_t *__restrict format, ...)
{
fputws(L"fatal: ", stderr);
va_list args;
va_start(args, format);
vfwprintf(stderr, format, args);
va_end(args);
fputwc(L'\n', stderr);
exit(status_error_setting_up_experiment);
}
__declspec(noreturn) static void die(char *__restrict format, ...)
{
fputs("fatal: ", stderr);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr);
exit(status_error_setting_up_experiment);
}
static void wmsg(wchar_t *__restrict format, ...)
{
fputws(L"note: ", stderr);
va_list args;
va_start(args, format);
vfwprintf(stderr, format, args);
va_end(args);
fputwc(L'\n', stderr);
}
static void msg(char *__restrict format, ...)
{
fputs("note: ", stderr);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr);
}
static inline enum status maxval(enum status lhs, enum status rhs)
{
return lhs < rhs ? rhs : lhs;
}
static void check_clean_w(void)
{
const wchar_t *wide_paths[] = {
symlink_file_w,
symlink_dir_w,
target_file_w,
target_dir_w,
};
for (int i = 0; i < _countof(wide_paths); ++i) {
if (GetFileAttributesW(wide_paths[i]) != INVALID_FILE_ATTRIBUTES
|| GetLastError() != ERROR_FILE_NOT_FOUND) {
wdie(L"workspace not clean: \"%s\" may exist", wide_paths[i]);
}
}
}
static void check_clean_a(void)
{
const char *narrow_paths[] = {
symlink_file_a,
symlink_dir_a,
target_file_a,
target_dir_a,
};
for (int i = 0; i < _countof(narrow_paths); ++i) {
if (GetFileAttributesA(narrow_paths[i]) != INVALID_FILE_ATTRIBUTES
|| GetLastError() != ERROR_FILE_NOT_FOUND) {
die("workspace not clean: \"%s\" may exist", narrow_paths[i]);
}
}
}
// Make sure none of the files or directories exist yet. This catches the most
// likely "user error," giving clear messages. Files and directories do not
// come in two flavors--they are all Unicode (UTF-16LE) really--but using the
// same data types that will be passed to encoding-specific functions in the
// test makes it easier to understand what is going on and to inspect the test.
static void check_clean(void)
{
check_clean_w();
check_clean_a();
}
static void create_target_file_w(void)
{
HANDLE tfwh = CreateFileW(
target_file_w,
GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (tfwh == INVALID_HANDLE_VALUE) {
wdie(L"can't create \"%s\" (error %d)", target_file_w, GetLastError());
}
if (!CloseHandle(tfwh)) {
wdie(L"BUG: can't close handle after creating \"%s\" (error %d)",
target_file_w, GetLastError());
}
}
static void create_target_dir_w(void)
{
if (!CreateDirectoryW(target_dir_w, NULL)) {
wdie(L"can't create \"%s\" (error %d)", target_dir_w, GetLastError());
}
}
static void create_target_file_a(void)
{
HANDLE tfah = CreateFileA(
target_file_a,
GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (tfah == INVALID_HANDLE_VALUE) {
die("can't create \"%s\" (error %d)", target_file_a, GetLastError());
}
if (!CloseHandle(tfah)) {
die("BUG: can't close handle after creating \"%s\" (error %d)",
target_file_a, GetLastError());
}
}
static void create_target_dir_a(void)
{
if (!CreateDirectoryA(target_dir_a, NULL)) {
die("can't create \"%s\" (error %d)", target_dir_a, GetLastError());
}
}
// Dangling symlinks could be created, but would not test or demonstrate ordinary
// usage. So we create the target files and directories.
static void create_targets(void)
{
create_target_file_w();
create_target_dir_w();
create_target_file_a();
create_target_dir_a();
}
static enum status create_symlink_file_w(void)
{
if (CreateSymbolicLinkW(
symlink_file_w,
target_file_w,
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
)) {
wmsg(L"created file symlink \"%s\" to \"%s\"",
symlink_file_w, target_file_w);
return status_success;
}
wmsg(L"can't create file symlink \"%s\" to \"%s\" (error %d)",
symlink_file_w, target_file_w, GetLastError());
return status_symlink_creation_failed;
}
static enum status create_symlink_dir_w(void)
{
if (CreateSymbolicLinkW(
symlink_dir_w,
target_dir_w,
SYMBOLIC_LINK_FLAG_DIRECTORY
| SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
)) {
wmsg(L"created directory symlink \"%s\" to \"%s\"",
symlink_dir_w, target_dir_w);
return status_success;
}
wmsg(L"can't create directory symlink \"%s\" to \"%s\" (error %d)",
symlink_dir_w, target_dir_w, GetLastError());
return status_symlink_creation_failed;
}
static enum status create_symlink_file_a(void)
{
if (CreateSymbolicLinkA(
symlink_file_a,
target_file_a,
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
)) {
msg("created file symlink \"%s\" to \"%s\"",
symlink_file_a, target_file_a);
return status_success;
}
msg("can't create file symlink \"%s\" to \"%s\" (error %d)",
symlink_file_a, target_file_a, GetLastError());
return status_symlink_creation_failed;
}
static enum status create_symlink_dir_a(void)
{
if (CreateSymbolicLinkA(
symlink_dir_a,
target_dir_a,
SYMBOLIC_LINK_FLAG_DIRECTORY
| SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
)) {
msg("created directory symlink \"%s\" to \"%s\"",
symlink_dir_a, target_dir_a);
return status_success;
}
msg("can't create directory symlink \"%s\" to \"%s\" (error %d)",
symlink_dir_a, target_dir_a, GetLastError());
return status_symlink_creation_failed;
}
static enum status create_symlinks(void)
{
enum status status = status_success;
status = maxval(status, create_symlink_file_w());
status = maxval(status, create_symlink_dir_w());
status = maxval(status, create_symlink_file_a());
status = maxval(status, create_symlink_dir_a());
return status;
}
int wmain(void)
{
check_clean();
create_targets();
return create_symlinks();
}