bdbf09afe8d2cd07513a908431a50e641cecc28d
Stefan Schuermans add copyright & license

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   1) /**
libpermissioner/src/Tree.cpp   2)  * Permissioner: set file ownerships and permissions
libpermissioner/src/Tree.cpp   3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
libpermissioner/src/Tree.cpp   4)  * Copyleft: GNU GENERAL PUBLIC LICENSE version 3 (see LICENSE)
libpermissioner/src/Tree.cpp   5)  */
libpermissioner/src/Tree.cpp   6) 
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   7) #include <permissioner/Tree.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp     8) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   9) #include <permissioner/Callback.h>
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  10) #include <permissioner/Group.h>
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  11) #include <permissioner/Permissions.h>
libpermissioner/src/Tree.cpp  12) #include <permissioner/StringUtils.h>
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  13) #include <permissioner/User.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    14) 
permissionerd/src/Tree.cpp    15) #include <boost/filesystem.hpp>
permissionerd/src/Tree.cpp    16) #include <sstream>
permissionerd/src/Tree.cpp    17) #include <stdexcept>
permissionerd/src/Tree.cpp    18) #include <string>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  19) #include <unistd.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    20) #include <vector>
permissionerd/src/Tree.cpp    21) 
permissionerd/src/Tree.cpp    22) void Tree::parseParams(std::string const &paramStr) {
permissionerd/src/Tree.cpp    23)   // format of paramStr is: <user> " " <group> " " <permissions> " " <root>
permissionerd/src/Tree.cpp    24)   // user, group, permissions may be - for no change
permissionerd/src/Tree.cpp    25)   std::string userStr, groupStr, permissionsStr, rootStr;
permissionerd/src/Tree.cpp    26) 
permissionerd/src/Tree.cpp    27)   std::string::size_type pos = 0;
permissionerd/src/Tree.cpp    28)   StringUtils::getNextField(paramStr, pos, userStr, "user");
permissionerd/src/Tree.cpp    29)   StringUtils::getNextField(paramStr, pos, groupStr, "group");
permissionerd/src/Tree.cpp    30)   StringUtils::getNextField(paramStr, pos, permissionsStr, "permissions");
permissionerd/src/Tree.cpp    31)   if (pos >= paramStr.length()) {
permissionerd/src/Tree.cpp    32)     std::stringstream msg;
permissionerd/src/Tree.cpp    33)     msg << "<root> field missing in \"" << paramStr << "\"";
permissionerd/src/Tree.cpp    34)     throw std::runtime_error(msg.str());
permissionerd/src/Tree.cpp    35)   }
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  36)   rootStr = paramStr.substr(pos);
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    37) 
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  38)   try {
libpermissioner/src/Tree.cpp  39)     user.parseUserName(userStr);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  40)   } catch (std::exception const &e) {
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  41)     std::stringstream msg;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  42)     msg << "invalid <user> field \"" << userStr << "\" in \"" << paramStr
libpermissioner/src/Tree.cpp  43)         << "\": " << e.what();
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  44)     throw std::runtime_error(msg.str());
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    45)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  46) 
libpermissioner/src/Tree.cpp  47)   try {
libpermissioner/src/Tree.cpp  48)     group.parseGroupName(groupStr);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  49)   } catch (std::exception const &e) {
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  50)     std::stringstream msg;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  51)     msg << "invalid <group> field \"" << groupStr << "\" in \"" << paramStr
libpermissioner/src/Tree.cpp  52)         << "\": " << e.what();
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  53)     throw std::runtime_error(msg.str());
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    54)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  55) 
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    56)   try {
permissionerd/src/Tree.cpp    57)     permissions.parseParams(permissionsStr);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  58)   } catch (std::exception const &e) {
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    59)     std::stringstream msg;
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  60)     msg << "invalid <permissions> field \"" << permissionsStr << "\" in \""
libpermissioner/src/Tree.cpp  61)         << paramStr << "\": " << e.what();
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    62)     throw std::runtime_error(msg.str());
permissionerd/src/Tree.cpp    63)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  64) 
Stefan Schuermans canonicalize root paths

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  65)   try {
libpermissioner/src/Tree.cpp  66)     root = boost::filesystem::canonical(rootStr);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  67)   } catch (std::exception const &e) {
Stefan Schuermans canonicalize root paths

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  68)     std::stringstream msg;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  69)     msg << "invalid <root> field \"" << rootStr << "\" in \"" << paramStr
libpermissioner/src/Tree.cpp  70)         << "\": " << e.what();
Stefan Schuermans canonicalize root paths

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  71)     throw std::runtime_error(msg.str());
libpermissioner/src/Tree.cpp  72)   }
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    73) }
Stefan Schuermans add config getters

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  74) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  75) User const &Tree::getUser() const { return user; }
Stefan Schuermans add config getters

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  76) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  77) Group const &Tree::getGroup() const { return group; }
Stefan Schuermans add config getters

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  78) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  79) Permissions const &Tree::getPermissions() const { return permissions; }
Stefan Schuermans add config getters

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  80) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  81) boost::filesystem::path const &Tree::getRoot() const { return root; }
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  82) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  83) bool Tree::setPermissions(TreeMap const &exclude,
libpermissioner/src/Tree.cpp  84)                           Callback &callback) const {
libpermissioner/src/Tree.cpp  85)   return setPermissionsInternal(root, exclude, callback);
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  86) }
libpermissioner/src/Tree.cpp  87) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  88) bool Tree::setPermissionsInternal(boost::filesystem::path const &path,
libpermissioner/src/Tree.cpp  89)                                   TreeMap const &exclude,
libpermissioner/src/Tree.cpp  90)                                   Callback &callback) const {
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  91)   try {
libpermissioner/src/Tree.cpp  92)     if (boost::filesystem::is_regular_file(path)) {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  93)       if (!setPermissionsOne(path, callback)) {
libpermissioner/src/Tree.cpp  94)         return false;
libpermissioner/src/Tree.cpp  95)       };
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  96)     } else if (boost::filesystem::is_directory(path)) {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  97)       if (!setPermissionsOne(path, callback)) {
libpermissioner/src/Tree.cpp  98)         return false;
libpermissioner/src/Tree.cpp  99)       };
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 100)       for (boost::filesystem::directory_entry entry :
libpermissioner/src/Tree.cpp 101)            boost::filesystem::directory_iterator(path)) {
libpermissioner/src/Tree.cpp 102)         if (exclude.find(entry) != exclude.end()) {
libpermissioner/src/Tree.cpp 103)           continue; // other tree -> skip here
libpermissioner/src/Tree.cpp 104)         }
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 105)         // recurse
libpermissioner/src/Tree.cpp 106)         if (!setPermissionsInternal(entry, exclude, callback)) {
libpermissioner/src/Tree.cpp 107)           return false;
libpermissioner/src/Tree.cpp 108)         }
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 109)       }
libpermissioner/src/Tree.cpp 110)     }
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 111)   } catch (boost::filesystem::filesystem_error const &e) {
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 112)     // ignore filesystem errors for now, as this runs in a daemon in background
libpermissioner/src/Tree.cpp 113)     (void)e;
libpermissioner/src/Tree.cpp 114)   }
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 115)   return true;
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 116) }
libpermissioner/src/Tree.cpp 117) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 118) bool Tree::setPermissionsOne(boost::filesystem::path const &path,
libpermissioner/src/Tree.cpp 119)                              Callback &callback) const {
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 120)   // change permissions
libpermissioner/src/Tree.cpp 121)   try {
libpermissioner/src/Tree.cpp 122)     permissions.apply(path);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 123)   } catch (boost::filesystem::filesystem_error const &e) {
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 124)     // ignore filesystem errors for now, as this runs in a daemon in background
libpermissioner/src/Tree.cpp 125)     (void)e;
libpermissioner/src/Tree.cpp 126)   }
libpermissioner/src/Tree.cpp 127) 
libpermissioner/src/Tree.cpp 128)   // change owner/group
Stefan Schuermans ignore return value of lcho...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 129)   int ret = lchown(path.string().c_str(), user.getUid(), group.getGid());
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 130)   // ignore error for now, as this runs in a daemon in background
Stefan Schuermans ignore return value of lcho...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 131)   (void)ret;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 132) 
libpermissioner/src/Tree.cpp 133)   // call callback and return whether to continue
libpermissioner/src/Tree.cpp 134)   return callback.callback();