LCOV - code coverage report
Current view: top level - adump/common - path.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 3 3
Test Date: 2026-08-12 11:04:53 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #ifndef ADUMP_COMMON_PATH_H
      11              : #define ADUMP_COMMON_PATH_H
      12              : 
      13              : #include <string>
      14              : #include "mmpa_api.h"
      15              : 
      16              : namespace Adx {
      17              : class Path {
      18              : public:
      19          232 :     Path() noexcept : path_() {}
      20       140934 :     explicit Path(const std::string &path) : path_(path) {}
      21       141538 :     ~Path() = default;
      22              : 
      23              :     Path &operator = (const std::string &path);
      24              :     bool operator == (const Path &other) const;
      25              :     Path &operator += (const std::string &path);
      26              :     Path &Assign(const std::string &path);
      27              :     Path &Append(const std::string &path);
      28              :     Path &Concat(const std::string &path);
      29              :     Path &AddExtension(const std::string &extension);
      30              :     std::string GetExtension() const;
      31              :     std::string GetFileName() const;
      32              :     bool Empty() const;
      33              :     bool Exist() const;
      34              :     bool Asccess(mmMode_t mode) const;
      35              :     bool IsDirectory() const;
      36              :     bool RealPath();
      37              :     Path ParentPath() const;
      38              :     bool CreateDirectory(bool recursion = false) const;
      39              :     std::string GetString() const;
      40              :     const char *GetCString() const;
      41              :     /*
      42              :      * @brief: 将相对文件 relativeFile 拼接到 rootPath 下,生成规范化的绝对文件路径。处理包括:
      43              :      *         1) 校验 relativeFile 不含 '..' 路径段(防路径穿越);
      44              :      *         2) 递归创建父目录;
      45              :      *         3) 对父目录做 RealPath 解析并校验其确为目录;
      46              :      *         4) 纵深防御:校验解析后的父目录仍位于 rootPath 之内(防落盘根目录下的软链接指向外部)。
      47              :      * @param [in]  rootPath      根路径(限定的落盘根目录)
      48              :      * @param [in]  relativeFile  相对文件路径(不能为空,不允许包含 '..' 段;前导 '/' 会被 Concat 剥离)
      49              :      * @param [out] canonicalFile 规范化后的绝对文件路径(父目录已 RealPath 解析)
      50              :      * @return true: 合法且父目录就绪; false: 入参非法、路径穿越、创建目录失败、父目录非法或逃逸出 rootPath
      51              :      */
      52              :     static bool BuildFullPathUnderRoot(const std::string &rootPath, const std::string &relativeFile,
      53              :         std::string &canonicalFile);
      54              : 
      55              :     /*
      56              :      * @brief: 判断 realSubPath 是否位于 realDirPath 目录之内(含相等)。两个入参都必须是已 RealPath 解析
      57              :      *         的规范化绝对路径。按 '/' 路径段比较,避免 /root_evil 被 /root 误判为子路径。
      58              :      * @param [in] realDirPath 规范化后的目录路径
      59              :      * @param [in] realSubPath 规范化后的待判断路径
      60              :      * @return true: realSubPath 在 realDirPath 之内; false: 不在其内或入参为空
      61              :      */
      62              :     static bool IsUnderDirectory(const std::string &realDirPath, const std::string &realSubPath);
      63              : 
      64              :     /*
      65              :      * @brief: 判断相对路径中是否存在 '..' 路径段(按 '/' 分段判断,避免误杀 my..file.bin 这类合法文件名)。
      66              :      * @param [in] path 待判断的相对路径
      67              :      * @return true: 存在 '..' 路径段(可能导致路径穿越); false: 不存在
      68              :      */
      69              :     static bool HasParentDirSegment(const std::string &path);
      70              : 
      71              : private:
      72              :     void AppendPath(const std::string &path);
      73              :     void AddSeperator();
      74              :     std::string path_;
      75              : };
      76              : } // namespace Adx
      77              : 
      78              : #endif // ADUMP_COMMON_PATH_H
        

Generated by: LCOV version 2.0-1