C语言解析软链接,获得真实路径

C语言解析软链接路径,获取真实路径

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int resolve_symlink(const char *symlink_path, char *resolved_path, size_t size) {
    if (realpath(symlink_path, resolved_path) == NULL) {
        perror("realpath");
        return -1;
    }
    return 0;
}

int main() {
    const char *symlink_path = "/data/wwwroot/TeamSpiritnode/1235.php";
    char resolved_path[PATH_MAX];

    if (resolve_symlink(symlink_path, resolved_path, sizeof(resolved_path)) == 0) {
        printf("Resolved path: %s\n", resolved_path);
    } else {
        printf("Failed to resolve symlink: %s\n", symlink_path);
    }

    return 0;
}

gcc -o test111 test1111.c
./test111

值得注意:readlink只能获取单个的软链接,不能获取嵌套的

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int resolve_symlink(const char *symlink_path, char *resolved_path, size_t size) {
    ssize_t len = readlink(symlink_path, resolved_path, size - 1);
    if (len == -1) {
        perror("readlink");
        return -1;
    }
    resolved_path[len] = '\0'; // Null-terminate the string
    return 0;
}

int main() {
    const char *symlink_path = "/data/wwwroot/111/111.php";
    char resolved_path[PATH_MAX];

    if (resolve_symlink(symlink_path, resolved_path, sizeof(resolved_path)) == 0) {
        printf("Resolved path: %s\n", resolved_path);
    } else {
        printf("Failed to resolve symlink: %s\n", symlink_path);
    }

    return 0;
}

内核态实现,只能4.17.x版本之前可用

复制代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/dcache.h>
#include <linux/namei.h>
#include <linux/slab.h>

static int resolve_symlink(const char *path, char *resolved_path, size_t size) {
    struct path kern_path;
    char *tmp;
    int err;

    err = kern_path(path, LOOKUP_FOLLOW, &kern_path);
    if (err) {
        printk(KERN_ERR "Failed to resolve path: %s\n", path);
        return err;
    }

    tmp = dentry_path_raw(kern_path.dentry, resolved_path, size);
    if (IS_ERR(tmp)) {
        printk(KERN_ERR "Failed to get real path: %s\n", path);
        path_put(&kern_path);
        return PTR_ERR(tmp);
    }

    path_put(&kern_path);
    return 0;
}

static int __init my_module_init(void) {
    char resolved_path[PATH_MAX];
    const char *symlink_path = "/data/wwwroot/111/111.php";

    if (resolve_symlink(symlink_path, resolved_path, sizeof(resolved_path)) == 0) {
        printk(KERN_INFO "Resolved path: %s\n", resolved_path);
    } else {
        printk(KERN_ERR "Failed to resolve symlink: %s\n", symlink_path);
    }

    return 0;
}

static void __exit my_module_exit(void) {
    printk(KERN_INFO "Module exiting\n");
}

module_init(my_module_init);
module_exit(my_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A module to resolve symlink paths");
相关推荐
毅炼1 分钟前
Java 基础常见问题总结(1)
开发语言·python
fengxin_rou10 分钟前
【黑马点评实战篇|第一篇:基于Redis实现登录】
java·开发语言·数据库·redis·缓存
心前阳光14 分钟前
Unity 模拟父子关系
android·unity·游戏引擎
2501_9151063218 分钟前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview
数智工坊20 分钟前
【数据结构-栈】3.1栈的顺序存储-链式存储
java·开发语言·数据结构
R-G-B28 分钟前
python 验证每次操作图片处理的顺序是否一致,按序号打上标签,图片重命名
开发语言·python·图片重命名·按序号打上标签·验证图片处理的顺序
小二·33 分钟前
Go 语言系统编程与云原生开发实战(第10篇)性能调优实战:Profiling × 内存优化 × 高并发压测(万级 QPS 实录)
开发语言·云原生·golang
多多*36 分钟前
2月3日面试题整理 字节跳动后端开发相关
android·java·开发语言·网络·jvm·adb·c#
v_for_van1 小时前
力扣刷题记录4(无算法背景,纯C语言)
c语言·算法·leetcode
xyq20241 小时前
jEasyUI 自定义分页
开发语言