4491746f751f23ef161bdb804d312d3746b300f4
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

1) #include <permissioner/Config.h>
2) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

3) #include <boost/filesystem.hpp>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

4) #include <cstdlib>
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

5) #include <grp.h>
6) #include <iomanip>
7) #include <iostream>
8) #include <pwd.h>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

9) #include <sys/stat.h>
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

10) #include <sys/types.h>
11) #include <unistd.h>
12) #include <vector>
13) 
14) struct LogLchown {
15)   std::string pathname;
16)   uid_t owner;
17)   gid_t group;
18) };
19) std::vector<LogLchown> logs_lchown;
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

20) 
21) // mock version of lchown, to see if right files get right owners
22) extern "C" int lchown(const char *pathname, uid_t owner, gid_t group) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

23)   logs_lchown.emplace_back(LogLchown { pathname, owner, group });
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

24)   return 0;
25) }
26) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

27) bool check_lchown(unsigned int idx, std::string const &path,
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

28)                   uid_t owner, gid_t group) {
29)   bool ret = true;
30)   if (idx > logs_lchown.size()) {
31)     std::cerr << "no such lchown call #" << idx << std::endl;
32)     return false;
33)   }
34)   LogLchown const & log_lchown = logs_lchown.at(idx);
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

35)   if (log_lchown.pathname != path) {
36)     std::cerr << "lchown call #" << idx << ": unexpected path \""
37)               << log_lchown.pathname << "\" != ...\"" << path << "\""
38)               << std::endl;
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

39)     ret = false;
40)   }
41)   if (log_lchown.owner != owner) {
42)     std::cerr << "lchown call #" << idx << ": unexpected owner "
43)               << log_lchown.owner << " != " << owner << std::endl;
44)     ret = false;
45)   }
46)   if (log_lchown.group != group) {
47)     std::cerr << "lchown call #" << idx << ": unexpected group "
48)               << log_lchown.group << " != " << group << std::endl;
49)     ret = false;
50)   }
51)   return ret;
52) }
53) 
54) struct LogChmod {
55)   std::string pathname;
56)   mode_t mode;
57) };
58) std::vector<LogChmod> logs_chmod;
59) 
60) // mock version of chmod, to see if right files get right permissions
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

61) extern "C" int chmod(const char *pathname, mode_t mode) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

62)   logs_chmod.emplace_back(LogChmod { pathname, mode });
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

63)   return 0;
64) }
65) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

66) bool check_chmod(unsigned int idx, std::string const &path, mode_t mode) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

67)   bool ret = true;
68)   if (idx > logs_chmod.size()) {
69)     std::cerr << "no such chmod call #" << idx << std::endl;
70)     return false;
71)   }
72)   LogChmod const & log_chmod = logs_chmod.at(idx);
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

73)   if (log_chmod.pathname != path) {
74)     std::cerr << "chmod call #" << idx << ": unexpected path \""
75)               << log_chmod.pathname << "\" != ...\"" << path << "\""
76)               << std::endl;
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

77)     ret = false;
78)   }
79)   if (log_chmod.mode != mode) {
80)     std::cerr << "chmod call #" << idx << ": unexpected mode "
81)               << std::oct << log_chmod.mode << " != " << mode
82)               << std::dec << std::endl;
83)     ret = false;
84)   }
85)   return ret;
86) }
87) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

88) bool check(unsigned int idx, std::string const &rel_path,
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

89)            uid_t owner, gid_t group, mode_t mode) {
90)   bool ret = true;
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

91)   std::string path = boost::filesystem::canonical(rel_path).string();
92)   if (! check_lchown(idx, path, owner, group)) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

93)     ret = false;
94)   }
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

95)   if (! check_chmod(idx, path, mode)) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

96)     ret = false;
97)   }
98)   return ret;
99) }
100) 
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

101) int main(int argc, char const **argv) {
102)   (void)argc;
103)   Config config;
104)   config.parseFile(argv[1]);
105)   config.setPermissions();
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

106) 
107)   int ret = EXIT_SUCCESS;
108) 
109)   uid_t nobody = getpwnam("nobody")->pw_uid;
110)   gid_t nogroup = getgrnam("nogroup")->gr_gid;
111) 
112)   if (! check(0, "work", nobody, nogroup, 0775)) {
113)     ret = EXIT_FAILURE;
114)   }
115)   if (! check(1, "work/file", nobody, nogroup, 0664)) {
116)     ret = EXIT_FAILURE;
117)   }
118)   if (! check(2, "work/nested", -1, -1, 0757)) {
119)     ret = EXIT_FAILURE;
120)   }
121)   if (! check(3, "work/nested/other", -1, -1, 0646)) {
122)     ret = EXIT_FAILURE;
123)   }
124)   unsigned int size = 4;
125)   if (logs_lchown.size() != size) {
126)     std::cerr << "unexpected size of logs_lchown: " << logs_lchown.size()
127)               << " != " << size << std::endl;
128)     ret = EXIT_FAILURE;
129)   }
130)   if (logs_chmod.size() != size) {
131)     std::cerr << "unexpected size of logs_chmod: " << logs_chmod.size()
132)               << " != " << size << std::endl;
133)     ret = EXIT_FAILURE;
134)   }
135) 
136)   return ret;