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");
相关推荐
范纹杉想快点毕业14 分钟前
以项目的方式学QT开发C++(二)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·windows·vscode·qt·visual studio
limingade20 分钟前
手机打电话时如何将通话对方的声音在手机上识别成文字
android·智能手机·语音识别·funasr·蓝牙电话·ai电话机器人·funasr安卓移植和部署
多多*27 分钟前
Spring之Bean的初始化 Bean的生命周期 全站式解析
java·开发语言·前端·数据库·后端·spring·servlet
努力学习的小廉37 分钟前
深入了解linux系统—— 基础IO(上)
android·linux·运维
少了一只鹅43 分钟前
c语言内存函数
c语言·开发语言
じ☆ve 清风°1 小时前
滑动窗口算法详解与C++实现
开发语言·c++·算法
苕皮蓝牙土豆1 小时前
C++ map & multimap 容器:赋值、排序、大小与删除操作
开发语言·c++
tmacfrank1 小时前
Android 性能优化入门(一)—— 数据结构优化
android·数据结构·性能优化
Villiam_AY1 小时前
Go 后端中双 token 的实现模板
开发语言·后端·golang
东风西巷1 小时前
Screen Mirroring App:轻松实现手机与电视的无缝投屏
android·智能手机·生活·软件需求