73428f06aed6730e79889edc86ea38f7e05eec74
[wimlib] / tests / set_reparse_point.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <windows.h>
4
5 static wchar_t *
6 win32_error_string(DWORD err_code)
7 {
8         static wchar_t buf[1024];
9         buf[0] = L'\0';
10         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err_code, 0,
11                       buf, 1024, NULL);
12         return buf;
13 }
14
15 static void
16 fail(const char *func, DWORD code)
17 {
18         fprintf(stderr, "%s (err 0x%08x: %ls)\n", func,
19                 (unsigned int)code, win32_error_string(code));
20         exit(1);
21 }
22
23 int
24 wmain(int argc, wchar_t **argv)
25 {
26         if (argc != 2) {
27                 fprintf(stderr, "Usage: %ls FILE\n", argv[0]);
28                 return 2;
29         }
30
31         HANDLE h = CreateFile(argv[1],
32                               GENERIC_WRITE,
33                               FILE_SHARE_VALID_FLAGS,
34                               NULL,
35                               OPEN_EXISTING,
36                               FILE_FLAG_BACKUP_SEMANTICS,
37                               NULL);
38         if (h == INVALID_HANDLE_VALUE)
39                 fail("CreateFile", GetLastError());
40
41         uint8_t in[128];
42         uint8_t *p = in;
43         *(uint32_t *)p = 0x80000000; /* rptag */
44         p += 4;
45         *(uint16_t *)p = 80; /* rpdatalen */
46         p += 2;
47         *(uint16_t *)p = 0; /* rpreserved */
48         p += 2;
49         memset(p, 0, 80); /* rpdata */
50         p += 80;
51
52         DWORD bytes_returned;
53
54         if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, in, p - in,
55                              NULL, 0, &bytes_returned, NULL))
56                 fail("DeviceIoControl", GetLastError());
57
58         CloseHandle(h);
59
60         return 0;
61 }