cpp

Coverage Report

Created: 2023-03-07 20:24

/home/andy/git/oilshell/oil/cpp/osh.cc
Line
Count
Source (jump to first uncovered line)
1
// osh.cc
2
3
#include "cpp/osh.h"
4
5
#include <fcntl.h>  // AT_* Constants
6
#include <sys/stat.h>
7
#include <unistd.h>
8
9
#include "mycpp/gc_builtins.h"
10
// To avoid circular dependency with e_die()
11
#include "prebuilt/core/error.mycpp.h"
12
13
using id_kind_asdl::Id;  // used below
14
using pyerror::e_die;
15
using syntax_asdl::loc;
16
17
namespace arith_parse {
18
19
tdop::ParserSpec kArithSpec;
20
21
}  // namespace arith_parse
22
23
namespace bool_stat {
24
25
2
bool isatty(Str* fd_str, word_t* blame_word) {
26
2
  int fd;
27
2
  try {
28
2
    fd = to_int(fd_str);
29
2
  } catch (ValueError* e) {
30
1
    e_die(StrFormat("Invalid file descriptor %r", fd_str),
31
1
          Alloc<loc::Word>(blame_word));
32
1
  }
33
  // note: we don't check errno
34
2
  int result = ::isatty(fd);
35
1
  return result;
36
2
}
37
38
0
bool DoUnaryOp(Id_t op_id, Str* s) {
39
0
  const char* zPath = s->data_;
40
41
0
  if (op_id == Id::BoolUnary_h || op_id == Id::BoolUnary_L) {
42
0
    struct stat st;
43
0
    if (lstat(zPath, &st) < 0) {
44
0
      return false;
45
0
    }
46
47
0
    return S_ISLNK(st.st_mode);
48
0
  } else {
49
0
    struct stat st;
50
0
    if (stat(zPath, &st) < 0) {
51
0
      return false;
52
0
    }
53
54
0
    auto mode = st.st_mode;
55
56
0
    switch (op_id) {
57
    // synonyms for existence
58
0
    case Id::BoolUnary_a:
59
0
    case Id::BoolUnary_e:
60
0
      return true;
61
62
0
    case Id::BoolUnary_s:
63
0
      return st.st_size != 0;
64
65
0
    case Id::BoolUnary_d:
66
0
      return S_ISDIR(mode);
67
68
0
    case Id::BoolUnary_f:
69
0
      return S_ISREG(mode);
70
71
0
    case Id::BoolUnary_k:
72
0
      return (mode & S_ISVTX) != 0;
73
74
0
    case Id::BoolUnary_p:
75
0
      return S_ISFIFO(mode);
76
77
0
    case Id::BoolUnary_O:
78
0
      return st.st_uid == geteuid();
79
80
0
    case Id::BoolUnary_G:
81
0
      return st.st_gid == getegid();
82
83
0
    case Id::BoolUnary_u:
84
0
      return mode & S_ISUID;
85
86
0
    case Id::BoolUnary_g:
87
0
      return mode & S_ISGID;
88
89
      // NOTE(Jesse): This implementation MAY have a bug.  On my system (Ubuntu
90
      // 20.04) it returns a correct result if the user is root (elevated with
91
      // sudo) and no execute bits are set for a file.
92
      //
93
      // A bug worked around in the python `posix` module here is that the above
94
      // (working) scenario is not always the case.
95
      //
96
      // https://github.com/python/cpython/blob/8d999cbf4adea053be6dbb612b9844635c4dfb8e/Modules/posixmodule.c#L2547
97
      //
98
      // As well as the dash source code found here (relative to this repo
99
      // root):
100
      //
101
      // _cache/spec-bin/dash-0.5.10.2/src/bltin/test.c
102
      // See `test_file_access()`
103
      //
104
      // We could also use the `stat` struct to manually compute the
105
      // permissions, as shown in the above `test.c`, though the code is
106
      // somewhat obtuse.
107
      //
108
      // There is further discussion of this issue in:
109
      // https://github.com/oilshell/oil/pull/1168
110
      //
111
      // And a bug filed for it at:
112
      //
113
      // https://github.com/oilshell/oil/issues/1170
114
      //
115
0
    case Id::BoolUnary_x:
116
0
      return faccessat(AT_FDCWD, zPath, X_OK, AT_EACCESS) == 0;
117
      //
118
119
0
    case Id::BoolUnary_r:
120
0
      return faccessat(AT_FDCWD, zPath, R_OK, AT_EACCESS) == 0;
121
122
0
    case Id::BoolUnary_w:
123
0
      return faccessat(AT_FDCWD, zPath, W_OK, AT_EACCESS) == 0;
124
0
    }
125
0
  }
126
127
0
  FAIL(kShouldNotGetHere);
128
0
}
129
130
0
bool DoBinaryOp(Id_t op_id, Str* s1, Str* s2) {
131
0
  int m1 = 0;
132
0
  struct stat st1;
133
0
  if (stat(s1->data_, &st1) == 0) {
134
0
    m1 = st1.st_mtime;
135
0
  }
136
137
0
  int m2 = 0;
138
0
  struct stat st2;
139
0
  if (stat(s2->data_, &st2) == 0) {
140
0
    m2 = st2.st_mtime;
141
0
  }
142
143
0
  switch (op_id) {
144
0
  case Id::BoolBinary_nt:
145
0
    return m1 > m2;
146
0
  case Id::BoolBinary_ot:
147
0
    return m1 < m2;
148
0
  case Id::BoolBinary_ef:
149
0
    return st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
150
0
  }
151
152
0
  FAIL(kShouldNotGetHere);
153
0
}
154
155
}  // namespace bool_stat