2d4d15040014e6fbdd4e66c77fcd87b2534a3d8f
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 complete config text, fix c...

Stefan Schuermans authored 4 years ago

9) #include <string>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

21) 
22) // mock version of lchown, to see if right files get right owners
23) 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

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

67) 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

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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