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");
相关推荐
程序猿阿伟几秒前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
傻啦嘿哟18 分钟前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光23 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用23 分钟前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
诸神黄昏EX27 分钟前
Android 分区相关介绍
android
爱摸鱼的孔乙己38 分钟前
【数据结构】链表(leetcode)
c语言·数据结构·c++·链表·csdn
Dola_Pan40 分钟前
C语言:数组转换指针的时机
c语言·开发语言·算法
ExiFengs40 分钟前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
paj12345678942 分钟前
JDK1.8新增特性
java·开发语言
IT古董1 小时前
【人工智能】Python在机器学习与人工智能中的应用
开发语言·人工智能·python·机器学习