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");
相关推荐
蓝婷儿3 分钟前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟19 分钟前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
学习噢学个屁40 分钟前
基于STM32语音识别柔光台灯
c语言·stm32·单片机·嵌入式硬件·语音识别
糯米导航41 分钟前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计
糯米导航44 分钟前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计
MessiGo1 小时前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙1 小时前
nonlocal 与global关键字
开发语言·python
galaxy_strive1 小时前
绘制饼图详细过程
开发语言·c++·qt
androidwork2 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空2 小时前
Android第十三次面试总结基础
android·面试·职场和发展