fd to filename

本文详细解析了如何通过进程ID和文件描述符获取文件路径及名称,并使用readlink函数实现路径解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

char path[256] = { 0 };
    char name[256] = { 0 };
    snprintf(path, sizeof(path)-1, "/proc/%d/fd/%d", getpid(), fd);
    memset(name, 0, sizeof(name));
    readlink(path, name, 255);
    ALOGD("fd %d -> %s", fd, name);
static void send_file(int client_socket, const char *filename) { /** * file_stat Store file metadata * dir A directory flow pointer that is used to iterate \ * through the contents of the catalog * response HTML response buffer * entry Catalog entry information * length Records the number of bytes that have been \ * written to the buffer at the moment * path Temporarily stores the full path to the directory entry * entry_stat Stores the metadata of individual files within a directory * size_str File size formatting string * fd File descriptor * buffer File read/write buffer * bytes_read Records the number of bytes per read() read */ struct stat file_stat; DIR *dir = NULL; char response[BUFFER_SIZE * 2]; struct dirent *entry; int length = 0; char path[1024]; struct stat entry_stat; char size_str[20]; int fd; char buffer[BUFFER_SIZE]; ssize_t bytes_read; if (0 > stat(filename, &file_stat)) { send_error(client_socket, 404, "Not Found", "File not found"); return; } if (S_ISDIR(file_stat.st_mode)) { /* If it's a catalog, list the contents of the catalog */ dir = opendir(filename); if (!dir) { send_error(client_socket, 403, "Forbidden", "Cannot list directory"); return; } length = snprintf(response, sizeof(response), "<html><head><title>Index of %s</title></head>" "<body><h1>Index of %s</h1><ul>", filename, filename); while (NULL != (entry = readdir(dir))) { /* Skip hidden files and current/parent directories */ if ('.' == entry->d_name[0]) { continue; } snprintf(path, sizeof(path), "%s/%s", filename, entry->d_name); stat(path, &entry_stat); if (S_ISDIR(entry_stat.st_mode)) { strcpy(size_str, "<DIR>"); } else { snprintf(size_str, sizeof(size_str), "%ld bytes", entry_stat.st_size); } length += snprintf(response + length, sizeof(response) - length, "<li><a href=\"%s\">%s</a> - %s</li>", entry->d_name, entry->d_name, size_str); } length += snprintf(response + length, sizeof(response) - length, "</ul></body></html>"); closedir(dir); send_headers(client_socket, 200, "OK", "text/html", length); send(client_socket, response, length, 0); return; } /* Send the file content */ fd = open(filename, O_RDONLY); if (0 > fd) { send_error(client_socket, 403, "Forbidden", "Cannot open file"); return; } /* Send HTTP headers */ const char *mime_type = get_mime_type(filename); send_headers(client_socket, 200, "OK", mime_type, file_stat.st_size); /* Send the file content */ while (0 < (bytes_read = read(fd, buffer, sizeof(buffer)))) { send(client_socket, buffer, bytes_read, 0); } close(fd); }以上代码有没有什么问题
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值