cpp

Coverage Report

Created: 2023-09-13 01:07

/home/andy/git/oilshell/oil/cpp/pylib.cc
Line
Count
Source (jump to first uncovered line)
1
// pylib.cc
2
3
#include "pylib.h"
4
5
#include <sys/stat.h>
6
7
namespace os_path {
8
9
5
Str* rstrip_slashes(Str* s) {
10
  // Strip all the rightmost slashes, but not if it's ALL slashes
11
5
  int n = len(s);
12
5
  if (n == 0) {
13
1
    return s;
14
1
  }
15
16
4
  int new_len = n;
17
9
  for (int i = n - 1; i >= 0; i--) {
18
8
    char c = s->data_[i];
19
8
    if (c == '/') {
20
5
      new_len--;
21
5
    } else {
22
3
      break;
23
3
    }
24
8
  }
25
26
4
  if (new_len == 0) {  // it was all slashes, don't strip
27
1
    return s;
28
1
  }
29
30
  // Truncate to new_len
31
3
  Str* result = NewStr(new_len);
32
3
  memcpy(result->data_, s->data_, new_len);
33
3
  result->data_[new_len] = '\0';
34
3
  return result;
35
4
}
36
37
}  // namespace os_path
38
39
namespace path_stat {
40
41
2
bool exists(Str* path) {
42
2
  struct stat st;
43
2
  if (::stat(path->data_, &st) < 0) {
44
1
    return false;
45
1
  } else {
46
1
    return true;
47
1
  }
48
2
}
49
50
2
bool isdir(Str* path) {
51
2
  struct stat st;
52
2
  if (::stat(path->data_, &st) < 0) {
53
0
    return false;
54
0
  }
55
2
  return S_ISDIR(st.st_mode);
56
2
}
57
58
}  // namespace path_stat