Skip to content

Commit

Permalink
Example: file attributes of symlinks et.al
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlocsomor committed Jan 14, 2019
1 parent 81325c4 commit 8af1378
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bazel/file-attributes/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cc_binary(
name = "opensym",
srcs = ["opensym.cc"],
)
56 changes: 56 additions & 0 deletions bazel/file-attributes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Contents:

```
2019-01-14 15:28 66 BUILD
2019-01-14 15:39 <SYMLINKD> dir.dangl [hello]
2019-01-14 14:33 <DIR> examples
2019-01-14 15:39 <SYMLINK> file.dangl [hello]
2019-01-14 15:25 <SYMLINKD> foo.dir [examples\py_native]
2019-01-14 15:25 <SYMLINKD> foo.dir.bad [examples\py_native\bin.py]
2019-01-14 14:35 <SYMLINK> foo.sym [examples\py_native\bin.py]
2019-01-14 15:26 <SYMLINK> foo.sym.bad [examples\py_native]
2019-01-14 15:48 <JUNCTION> junc [D:\src\tmp\examples]
2019-01-14 15:38 <JUNCTION> junc.dangl [D:\src\tmp\hello]
2019-01-14 15:49 <JUNCTION> junc.file [D:\src\tmp\BUILD]
```

CreateFileW and attributes:

- open1: CreateFileW with `FILE_FLAG_BACKUP_SEMANTICS` (follows reparse points)
- open2: CreateFileW with `FILE_ATTRIBUTE_NORMAL`

```
\\?\D:\src\tmp\BUILD (regular file)
open1: succeeded, attr: 0x00000020, info.attr: 0x00000020, is_dir: 0
open2: succeeded, attr: 0x00000020, info.attr: 0x00000020, is_dir: 0
\\?\D:\src\tmp\examples (directory)
open1: succeeded, attr: 0x00000010, info.attr: 0x00000010, is_dir: 1
open2: failed, error: 5
\\?\D:\src\tmp\junc (junction)
open1: succeeded, attr: 0x00000410, info.attr: 0x00000010, is_dir: 1
open2: failed, error: 5
\\?\D:\src\tmp\foo.sym (file symlink)
open1: succeeded, attr: 0x00000420, info.attr: 0x00000020, is_dir: 0
open2: succeeded, attr: 0x00000420, info.attr: 0x00000020, is_dir: 0
\\?\D:\src\tmp\foo.dir (directory symlink)
open1: succeeded, attr: 0x00000410, info.attr: 0x00000010, is_dir: 1
open2: failed, error: 5
\\?\D:\src\tmp\foo.sym.bad (file symlink pointing to directory)
open1: succeeded, attr: 0x00000420, info.attr: 0x00000010, is_dir: 1
open2: failed, error: 5
\\?\D:\src\tmp\foo.dir.bad (directory symlink pointing to file)
open1: succeeded, attr: 0x00000410, info.attr: 0x00000020, is_dir: 0
open2: failed, error: 5
\\?\D:\src\tmp\junc.dangl (dangling junction)
open1: failed, error: 2
open2: failed, error: 5
\\?\D:\src\tmp\file.dangl (dangling file symlink)
open1: failed, error: 2
open2: failed, error: 2
\\?\D:\src\tmp\dir.dangl (dangling directory symlink)
open1: failed, error: 2
open2: failed, error: 5
\\?\D:\src\tmp\junc.file (junction pointing to file)
open1: succeeded, attr: 0x00000410, info.attr: 0x00000020, is_dir: 0
open2: failed, error: 5
```
Empty file added bazel/file-attributes/WORKSPACE
Empty file.
53 changes: 53 additions & 0 deletions bazel/file-attributes/opensym.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <windows.h>
#include <stdio.h>
#include <vector>

int main(void) {
static constexpr DWORD kAllShare =
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

for (const wchar_t* path : {
L"\\\\?\\D:\\src\\tmp\\BUILD",
L"\\\\?\\D:\\src\\tmp\\examples",
L"\\\\?\\D:\\src\\tmp\\junc",
L"\\\\?\\D:\\src\\tmp\\foo.sym",
L"\\\\?\\D:\\src\\tmp\\foo.dir",
L"\\\\?\\D:\\src\\tmp\\foo.sym.bad",
L"\\\\?\\D:\\src\\tmp\\foo.dir.bad",
L"\\\\?\\D:\\src\\tmp\\junc.dangl",
L"\\\\?\\D:\\src\\tmp\\file.dangl",
L"\\\\?\\D:\\src\\tmp\\dir.dangl",
L"\\\\?\\D:\\src\\tmp\\junc.file",
}) {
wprintf(L"%s\n", path);

HANDLE h = CreateFileW(path, 0, kAllShare, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h == INVALID_HANDLE_VALUE) {
printf(" open1: failed, error: %d\n", GetLastError());
} else {
BY_HANDLE_FILE_INFORMATION info;
GetFileInformationByHandle(h, &info);
printf(" open1: succeeded, attr: 0x%08x, info.attr: 0x%08x, is_dir: %d\n",
GetFileAttributesW(path),
info.dwFileAttributes,
info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0);
CloseHandle(h);
}

h = CreateFileW(path, 0, kAllShare, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
printf(" open2: failed, error: %d\n", GetLastError());
} else {
BY_HANDLE_FILE_INFORMATION info;
GetFileInformationByHandle(h, &info);
printf(" open2: succeeded, attr: 0x%08x, info.attr: 0x%08x, is_dir: %d\n",
GetFileAttributesW(path),
info.dwFileAttributes,
info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0);
CloseHandle(h);
}
}
return 0;
}

0 comments on commit 8af1378

Please sign in to comment.