Linux实用功能代码集(2) —— 获得机器文件大小和MD5值
在开发中经常会与文件打交道而获得文件大小以及MD5值则也是非常常用的功能。下面就给出获取文件大小以及计算其MD5值的代码。代码如下#include stdio.h #include stdlib.h #include string.h #include unistd.h #include sys/stat.h #include fcntl.h #include openssl/md5.h long get_file_size(const char *file_name) { struct stat file_stat; if (!file_name) { printf(parameter cant be null\n); return -1; } if (stat(file_name, file_stat) -1) { perror(stat failed); return -1; } return file_stat.st_size; } int get_file_md5(const char *file_name, char *md5_result) { if (!file_name || !md5_result) { printf(parameter cant be null\n); return -1; } FILE *fp fopen(file_name, rb); if (!fp) { perror(fopen failed); return -1; } MD5_CTX ctx; MD5_Init(ctx); unsigned char buf[4096] {0}; size_t len; while ((len fread(buf, 1, sizeof(buf), fp)) 0) MD5_Update(ctx, buf, len); unsigned char digest[MD5_DIGEST_LENGTH] {0}; MD5_Final(digest, ctx); memcpy(md5_result, digest, MD5_DIGEST_LENGTH); fclose(fp); return 0; } int main(int argc, char *argv[]) { if (argc ! 2) { printf(usage: %s file name\n, argv[0]); return -1; } const char *file_name argv[1]; struct stat st; if (stat(file_name, st) -1) { perror(stat failed); return -1; } int file_size st.st_size; printf(file size is: %d bytes\n, file_size); unsigned char md5[16] {0}; get_file_md5(file_name, md5); for (int i 0; i MD5_DIGEST_LENGTH; i) printf(%02x, md5[i]); printf(\n); return 0; }main函数中分为了两部分获取文件大小这通过get_file_sizee函数实现计算文件MD5值这通过get_file_md5函数实现。下边依次对这两个函数进行讲解。1. get_file_size函数get_file_size函数比较简单主要就是调用stat函数让内核填充struct stat file_stat并且返回file_stat.st_size。if (stat(file_name, file_stat) -1) { perror(stat failed); return -1; } return file_stat.st_size;stat函数说明原型#include sys/stat.h #include unistd.h int stat(const char *pathname, struct stat *statbuf);参数const char pathname文件路径字符串。struct stat *statbuf输出参数内核把文件信息写入这个结构体。返回值成功返回0失败返回-1并且错误码errno已正确设置。struct stat的定义在sys/stat.h中如下struct stat { dev_t st_dev; // 文件所在设备号 ino_t st_ino; // inode 号文件唯一ID mode_t st_mode; // 文件类型 权限 nlink_t st_nlink; // 硬链接数 uid_t st_uid; // 所有者用户ID gid_t st_gid; // 所有者组ID dev_t st_rdev; // 设备号特殊文件 off_t st_size; // **文件大小字节最重要** time_t st_atime; // 最后访问时间 time_t st_mtime; // 最后修改时间 time_t st_ctime; // 最后状态改变时间 };内核通过此结构体把文件所有信息返回给用户。除了通过stat系统调用获取文件大小还可以通过fseek ftell方式、lseek方式实现相同功能。示例代码如下fseek ftell方式#include stdio.h long get_opened_file_size(FILE *fp) { long size; fseek(fp, 0, SEEK_END); // 跳到文件末尾 size ftell(fp); // 获取当前偏移 文件大小 rewind(fp); // 回到文件开头 return size; }lseek方式#include unistd.h long get_fd_file_size(int fd) { return lseek(fd, 0, SEEK_END); }三种方式比较方法优点适用场景stat()最快、不用打开文件直接获取文件大小推荐fseek() ftell()标准C跨平台已用fopen打开文件lseek()系统调用速度快已用open打开文件fd2. get_file_md5函数get_file_md5函数主要基于Linux自带的OpenSSL MD5接口实现。主要步骤为1打开要计算MD5值的文件2分块读取文件每次4096字节并计算MD53生成整个文件的MD54关闭文件5返回MD5值。对于OpenSSL MD5相关接口说明如下内容引自豆包注1要使用OpenSSL MD5接口需要先通过apt安装libssl-dev。sudo apt install libssl-dev2编译的时候必须加入-lcrypto。对以上代码进行编译实际命令及结果如下笔者的环境为VMWare虚拟机Ubuntu20.04$ gcc file_size_md5.c -lcrypto -o get_file_size_md5 $实际执行结果为$ ./get_file_size_md5 file_size_md5.c file size is: 1450 bytes 9430ca7bd6b8148b2725033b4f328762与通过系统命令ls-l以及md5sum命令得到的结果一致$ ls -l file_size_md5.c -rw-rw-r-- 1 habit habit 1450 Mar 21 17:47 file_size_md5.c $ $ md5sum file_size_md5.c 9430ca7bd6b8148b2725033b4f328762 file_size_md5.c完整工程代码已上传至
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2437140.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!