File: FileInformation.cpp

package info (click to toggle)
watchman 4.9.0-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,936 kB
  • sloc: cpp: 27,450; python: 6,534; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (79 lines) | stat: -rw-r--r-- 2,366 bytes parent folder | download | duplicates (3)
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
/* 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

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
}
}