You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sysrepo sr_install_module()未生效search_dirs参数问题咨询

问题描述

尝试使用sr_install_module()安装Sysrepo模块,调用以下代码时,函数未正确处理search_dirs参数,报错:libyang[0]: Failed to open file "iana-if-type.yang" (No such file or directory)。

#include "sysrepo.h"
#include "libyang/libyang.h"

sr_conn_ctx_t *srContext;
sr_connect(SR_CONN_CACHE_RUNNING, &srContext);


char *moduleName = "ietf-interfaces.yang";
char *searchDir = "/path/to/dir"
sr_install_module(srContext, moduleName, searchDir, NULL);

模块存在于"/path/to/dir"路径下,但调用失败;若将moduleName设为"/path/to/dir/ietf-interfaces.yang"则可正常找到模块。

问题原因

核心问题是依赖模块的搜索路径未同步配置:

  • 当传入模块全路径时,libyang会自动将该文件所在目录(/path/to/dir)加入自身搜索路径,因此能找到同目录下的依赖模块iana-if-type.yang。
  • 当仅传入模块名+searchDir参数时,searchDir仅被Sysrepo用来定位主模块ietf-interfaces.yang,但解析模块时,libyang的搜索路径并未包含/path/to/dir,导致找不到依赖的iana-if-type.yang。
解决方法

方法一:手动添加libyang搜索路径

在调用sr_install_module()前,通过Sysrepo获取libyang上下文,将目标目录添加到搜索路径中:

#include "sysrepo.h"
#include "libyang/libyang.h"

sr_conn_ctx_t *srContext;
sr_connect(SR_CONN_CACHE_RUNNING, &srContext);

// 获取libyang上下文并添加搜索路径
ly_ctx_t *ly_ctx = sr_get_context(srContext);
ly_ctx_set_search_path(ly_ctx, "/path/to/dir", 0);

char *moduleName = "ietf-interfaces.yang";
char *searchDir = "/path/to/dir"; // 补全语法错误:添加分号
sr_install_module(srContext, moduleName, searchDir, NULL);

方法二:统一模块存放路径

将ietf-interfaces.yang和依赖的iana-if-type.yang都放到libyang的默认搜索路径中(通常为/usr/share/yang或Sysrepo配置的模块目录),无需额外指定searchDir即可直接安装。

方法三:验证参数与依赖位置

确保iana-if-type.yang与ietf-interfaces.yang在同一目录,补全代码中的语法错误(char *searchDir = "/path/to/dir"后缺少分号)后重试,部分Sysrepo版本中searchDir会被传递给libyang用于依赖搜索。


内容的提问来源于stack exchange,提问作者Klickmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:46:23