Skip to content

MPP_FileTools

SweerItTer edited this page Feb 1, 2026 · 3 revisions

FileTools API 文档

概述

FileTools 是 utilsCore MPP 模块的工具函数集合,提供文件和目录相关的工具函数。

职责

  • 检查目录是否存在

适用场景

  • 文件路径验证
  • 目录检查
  • 文件系统操作

依赖关系

  • 依赖: sys/stat.h
  • 被依赖: JpegEncoder 等模块

静态方法

dirExists() - 检查目录是否存在

inline bool dirExists(const std::string& path);

参数说明:

  • path (输入): 目录路径

返回值:

  • true: 目录存在
  • false: 目录不存在或发生错误

所有权归属:

  • 无所有权转移

注意事项:

  1. 使用 stat() 函数检查路径状态
  2. 只检查目录,不检查文件
  3. 路径不存在时返回 false

使用例程:

// 检查目录是否存在
if (dirExists("/tmp/jpeg")) {
    printf("Directory exists\n");
} else {
    printf("Directory does not exist\n");
}

// 在保存文件前检查目录
std::string save_dir = "/tmp/jpeg";
if (!dirExists(save_dir)) {
    // 创建目录
    mkdir(save_dir.c_str(), 0755);
}

实现细节

使用 stat() 函数

struct stat st;
if (0 != stat(path.c_str(), &st)) {
    return false;
}
return S_ISDIR(st.st_mode);

说明:

  1. 使用 stat() 获取路径状态
  2. stat() 失败(返回非零)时返回 false
  3. 使用 S_ISDIR() 检查是否为目录

典型使用场景

场景 1: 保存文件前检查目录

JpegEncoder::Config cfg;
cfg.save_dir = "/tmp/jpeg";

// 检查目录是否存在
if (!dirExists(cfg.save_dir)) {
    // 创建目录
    mkdir(cfg.save_dir.c_str(), 0755);
}

JpegEncoder encoder(cfg);

场景 2: 条件创建目录

std::string output_dir = get_output_dir();

if (dirExists(output_dir)) {
    printf("Directory already exists\n");
} else {
    printf("Creating directory...\n");
    mkdir(output_dir.c_str(), 0755);
}

场景 3: 目录验证

std::vector<std::string> dirs = {
    "/tmp/jpeg",
    "/tmp/video",
    "/tmp/data"
};

for (const auto& dir : dirs) {
    if (!dirExists(dir)) {
        printf("Directory %s does not exist\n", dir.c_str());
        // 创建目录
        mkdir(dir.c_str(), 0755);
    }
}

注意事项

  1. 仅检查目录: 只检查目录,不检查文件
  2. 路径权限: 需要有访问路径的权限
  3. 路径格式: 使用绝对路径或相对路径
  4. stat() 失败: stat() 失败时返回 false(包括路径不存在)
  5. 线程安全: 函数是线程安全的
  6. 性能: 每次调用都会执行 stat() 系统调用

相关文档


参考资料

主页

API 文档

DMA 模块

DRM 模块

NET 模块

V4L2 模块

V4L2Param 模块

RGA 模块

MPP 模块

Sys 模块

Mouse 模块

Utils 模块

Clone this wiki locally