forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileInformation.cpp
107 lines (99 loc) · 2.81 KB
/
FileInformation.cpp
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
/* Copyright 2017-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "FileInformation.h"
#include "watchman_time.h"
namespace watchman {
#ifndef _WIN32
FileInformation::FileInformation(const struct stat& st)
: mode(st.st_mode),
size(off_t(st.st_size)),
uid(st.st_uid),
gid(st.st_gid),
ino(st.st_ino),
dev(st.st_dev),
nlink(st.st_nlink) {
memcpy(&atime, &st.WATCHMAN_ST_TIMESPEC(a), sizeof(atime));
memcpy(&mtime, &st.WATCHMAN_ST_TIMESPEC(m), sizeof(mtime));
memcpy(&ctime, &st.WATCHMAN_ST_TIMESPEC(c), sizeof(ctime));
}
#endif
#ifdef _WIN32
FileInformation::FileInformation(uint32_t dwFileAttributes)
: fileAttributes(dwFileAttributes) {
if (fileAttributes & FILE_ATTRIBUTE_READONLY) {
mode = 0444;
} else {
mode = 0666;
}
if (fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
// It's a symlink, but to be msvc compatible, we report
// this as a file. Note that a reparse point can also
// have FILE_ATTRIBUTE_DIRECTORY set if the symlink was
// created with the intention of it appearing as a file.
mode |= _S_IFREG;
} else if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
mode |= _S_IFDIR | 0111 /* executable/searchable */;
} else {
mode |= _S_IFREG;
}
}
#endif
DType FileInformation::dtype() const {
#ifdef DTTOIF
return static_cast<DType>(IFTODT(mode));
#else
// Windows, Solaris
if (isSymlink()) {
return DType::Symlink;
}
if (isDir()) {
return DType::Dir;
}
if (isFile()) {
return DType::Regular;
}
return DType::Unknown;
#endif
}
bool FileInformation::isSymlink() const {
#ifdef _WIN32
// We treat all reparse points as equivalent to symlinks
return fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT;
#else
return S_ISLNK(mode);
#endif
}
bool FileInformation::isDir() const {
#ifdef _WIN32
// Note that junctions have both DIRECTORY and REPARSE_POINT set,
// so we have to check both bits to make sure that we only report
// this as a dir if it isn't a junction, otherwise we will fail to
// opendir.
return (fileAttributes &
(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) ==
FILE_ATTRIBUTE_DIRECTORY;
#else
return S_ISDIR(mode);
#endif
}
bool FileInformation::isFile() const {
#ifdef _WIN32
// We can't simply test for FILE_ATTRIBUTE_NORMAL as that is only
// valid when no other bits are set. Instead we check for the absence
// of DIRECTORY and REPARSE_POINT to decide that it is a regular file.
return (fileAttributes &
(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) == 0;
#else
return S_ISREG(mode);
#endif
}
FileInformation FileInformation::makeDeletedFileInformation() {
FileInformation info;
#ifndef _WIN32
info.mode = S_IFREG;
#else
info.mode = _S_IFREG;
#endif
return info;
}
} // namespace watchman