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");
相关推荐
朱一头zcy7 分钟前
C语言复习第9章 字符串/字符/内存函数
c语言
此生只爱蛋10 分钟前
【手撕排序2】快速排序
c语言·c++·算法·排序算法
blammmp17 分钟前
Java:数据结构-枚举
java·开发语言·数据结构
何曾参静谧30 分钟前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
暗黑起源喵36 分钟前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong40 分钟前
Java反射
java·开发语言·反射
Troc_wangpeng42 分钟前
R language 关于二维平面直角坐标系的制作
开发语言·机器学习
努力的家伙是不讨厌的43 分钟前
解析json导出csv或者直接入库
开发语言·python·json
萌面小侠Plus43 分钟前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农44 分钟前
Android Profiler 内存分析
android