LCOV - code coverage report
Current view: top level - common/source - LinuxUtils.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 85.0 % 40 34
Test Date: 2025-03-21 11:02:39 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :  * If not stated otherwise in this file or this component's LICENSE file the
       3              :  * following copyright and licenses apply:
       4              :  *
       5              :  * Copyright 2025 Sky UK
       6              :  *
       7              :  * Licensed under the Apache License, Version 2.0 (the "License");
       8              :  * you may not use this file except in compliance with the License.
       9              :  * You may obtain a copy of the License at
      10              :  *
      11              :  * http://www.apache.org/licenses/LICENSE-2.0
      12              :  *
      13              :  * Unless required by applicable law or agreed to in writing, software
      14              :  * distributed under the License is distributed on an "AS IS" BASIS,
      15              :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16              :  * See the License for the specific language governing permissions and
      17              :  * limitations under the License.
      18              :  */
      19              : 
      20              : #include "LinuxUtils.h"
      21              : #include "RialtoCommonLogging.h"
      22              : #include <grp.h>
      23              : #include <pwd.h>
      24              : #include <sys/stat.h>
      25              : #include <unistd.h>
      26              : 
      27              : namespace
      28              : {
      29              : constexpr uid_t kNoOwnerChange = -1; // -1 means chown() won't change the owner
      30              : constexpr gid_t kNoGroupChange = -1; // -1 means chown() won't change the group
      31              : 
      32           20 : uid_t getFileOwnerId(const std::string &fileOwner)
      33              : {
      34           20 :     uid_t ownerId = kNoOwnerChange;
      35           20 :     const size_t kBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX);
      36           20 :     if (!fileOwner.empty() && kBufferSize > 0)
      37              :     {
      38            1 :         errno = 0;
      39            1 :         passwd passwordStruct{};
      40            1 :         passwd *passwordResult = nullptr;
      41            1 :         char buffer[kBufferSize];
      42            1 :         int result = getpwnam_r(fileOwner.c_str(), &passwordStruct, buffer, kBufferSize, &passwordResult);
      43            1 :         if (result == 0 && passwordResult)
      44              :         {
      45            0 :             ownerId = passwordResult->pw_uid;
      46              :         }
      47              :         else
      48              :         {
      49            1 :             RIALTO_COMMON_LOG_SYS_WARN(errno, "Failed to determine ownerId for '%s'", fileOwner.c_str());
      50              :         }
      51              :     }
      52           20 :     return ownerId;
      53              : }
      54              : 
      55           20 : gid_t getFileGroupId(const std::string &fileGroup)
      56              : {
      57           20 :     gid_t groupId = kNoGroupChange;
      58           20 :     const size_t kBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX);
      59           20 :     if (!fileGroup.empty() && kBufferSize > 0)
      60              :     {
      61            1 :         errno = 0;
      62            1 :         group groupStruct{};
      63            1 :         group *groupResult = nullptr;
      64            1 :         char buffer[kBufferSize];
      65            1 :         int result = getgrnam_r(fileGroup.c_str(), &groupStruct, buffer, kBufferSize, &groupResult);
      66            1 :         if (result == 0 && groupResult)
      67              :         {
      68            0 :             groupId = groupResult->gr_gid;
      69              :         }
      70              :         else
      71              :         {
      72            1 :             RIALTO_COMMON_LOG_SYS_WARN(errno, "Failed to determine groupId for '%s'", fileGroup.c_str());
      73              :         }
      74              :     }
      75           20 :     return groupId;
      76              : }
      77              : } // namespace
      78              : 
      79              : namespace firebolt::rialto::common
      80              : {
      81           20 : bool setFilePermissions(const std::string &filePath, unsigned int filePermissions)
      82              : {
      83           20 :     errno = 0;
      84           20 :     if (chmod(filePath.c_str(), filePermissions) != 0)
      85              :     {
      86           20 :         RIALTO_COMMON_LOG_SYS_WARN(errno, "Failed to change the permissions on %s", filePath.c_str());
      87           20 :         return false;
      88              :     }
      89            0 :     return true;
      90              : }
      91              : 
      92           20 : bool setFileOwnership(const std::string &filePath, const std::string &fileOwner, const std::string &fileGroup)
      93              : {
      94           20 :     uid_t ownerId = getFileOwnerId(fileOwner);
      95           20 :     gid_t groupId = getFileGroupId(fileGroup);
      96              : 
      97           20 :     if (ownerId != kNoOwnerChange || groupId != kNoGroupChange)
      98              :     {
      99            0 :         errno = 0;
     100            0 :         if (chown(filePath.c_str(), ownerId, groupId) != 0)
     101              :         {
     102            0 :             RIALTO_COMMON_LOG_SYS_WARN(errno, "Failed to change the owner/group for %s", filePath.c_str());
     103              :         }
     104              :     }
     105           20 :     return true;
     106              : }
     107              : } // namespace firebolt::rialto::common
        

Generated by: LCOV version 2.0-1