Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
文件系统学习营
文件系统学习 + Eric + 修改时间戳
发布于 2018-07-18 15:50:11 浏览:1749
订阅该版
* 本帖最后由 EricYYG 于 2018-7-18 15:52 编辑 * 篡改文件时间戳:1.txt文本创建时间为2018年07月12日,修改为2018年07月01日注意需要打开menuconfig中的RTC![4.png](/uploads/201807/18/155202m5i3ip1ii81359ig.png) 先做一个加强版的ls-->ll,能打印更多的信息,用于观察文件的修改时间,并添加到msh中。代码如下:void ll(void){ struct stat buf; DIR*dirp; structdirent *d; /* 打开 path 目录*/ dirp = opendir("."); if (dirp == RT_NULL) { //rt_kprintf("input invalid path\"%s\"directory!
", path); return; } else { /* 在这儿进行读取目录相关操作*/ while((d = readdir(dirp)) != RT_NULL) { stat(d->d_name,&buf); rt_kprintf("|%-15s|%-8s|%-8s|%-8s|%-15s
","file", "uid", "gid", "size","time"); rt_kprintf("|%-15s|%-8d|%-8d|%-8d|%-15s",d->d_name, buf.st_uid, buf.st_gid, buf.st_size, ctime(&buf.st_mtime)); } /* 关闭目录 */ closedir(dirp); } }/* 导出到 msh 命令列表中 */MSH_CMD_EXPORT(ll, show current dirinformation); 运行如下:![1.png](/uploads/201807/18/154308ft2zucs5ubvvc5mt.png) 接下来进入主题,修改时间戳的难点在于,修改时间函数应该在什么地方添加!我们先跟踪一下stat这个函数的实现:int stat(const char *file, struct stat*buf){ int result; result = dfs_file_stat(file, buf); if (result < 0) { rt_set_errno(result); return -1; } return result;}int dfs_file_stat(const char *path, structstat *buf){ int result; char *fullpath; struct dfs_filesystem *fs; fullpath = dfs_normalize_path(NULL, path); if (fullpath == NULL) { return -1; } if ((fs = dfs_filesystem_lookup(fullpath)) == NULL) { dbg_log(DBG_ERROR, "can't find mountedfilesystem on this path:%s
", fullpath); rt_free(fullpath); return -ENOENT; } if ((fullpath[0] == '/' && fullpath[1] == '\0') || (dfs_subdir(fs->path, fullpath) == NULL)) { /* it's the root directory */ buf->st_dev = 0; buf->st_mode = S_IRUSR |S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP |S_IWOTH; buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; buf->st_size = 0; buf->st_mtime = 0; /* release full path */ rt_free(fullpath); return RT_EOK; } else { if (fs->ops->stat == NULL) { rt_free(fullpath); dbg_log(DBG_ERROR, "the filesystem didn'timplement this function
"); return -ENOSYS; } /* get the real file path and get file stat */ if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) result = fs->ops->stat(fs, fullpath, buf); else result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath),buf); } rt_free(fullpath); return result;}fs->ops->stat(fs, dfs_subdir(fs->path,fullpath), buf)是一个结构体里面的函数指针struct dfs_filesystem_ops{ char *name; uint32_t flags; /* flags forfile system operations */ /* operations for file */ const struct dfs_file_ops *fops; /* mount and unmount file system */ int (*mount) (structdfs_filesystem *fs, unsigned long rwflag, const void *data); int (*unmount) (structdfs_filesystem *fs); /* make a file system */ int (*mkfs) (rt_device_tdevid); int (*statfs) (structdfs_filesystem *fs, struct statfs *buf); int (*unlink) (structdfs_filesystem *fs, const char *pathname); int (*stat) (structdfs_filesystem *fs, const char *filename, struct stat *buf); int (*rename) (structdfs_filesystem *fs, const char *oldpath, const char *newpath);};这个函数指针最后跑到这个地方来了:int dfs_elm_stat(struct dfs_filesystem *fs,const char *path, struct stat *st) 上面的联系是怎么实现的呢?我个人分析应该是通过下面这个函数初始化关联起来的:int elm_init(void){ /* register fatfs file system */ dfs_register(&dfs_elm); return 0;} 命名大概原理了,那么我们开始增加一个属于修改时间戳的函数utime()int utime(const char *file, int year, intmonth, int day, int hour, int minute, int second){ int result; result = dfs_file_utime(file, year, month, day, hour, minute, second); if (result < 0) { rt_set_errno(result); return -1; } return result;}RTM_EXPORT(utime); int dfs_file_utime(const char *path, intyear, int month, int day, int hour, int minute, int second){ int result; char *fullpath; struct dfs_filesystem *fs; fullpath = dfs_normalize_path(NULL, path); if (fullpath == NULL) { return -1; } if ((fs = dfs_filesystem_lookup(fullpath)) == NULL) { dbg_log(DBG_ERROR, "can't find mountedfilesystem on this path:%s
", fullpath); rt_free(fullpath); return -ENOENT; } if ((fullpath[0] == '/' && fullpath[1] == '\0') || (dfs_subdir(fs->path, fullpath) == NULL)) { /* it's the root directory */ /*do nothing */ /* release full path */ rt_free(fullpath); return RT_EOK; } else { if (fs->ops->utime == NULL) { rt_free(fullpath); dbg_log(DBG_ERROR, "the filesystem didn'timplement this function
"); return -ENOSYS; } /* get the real file path and get file stat */ if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) result = fs->ops->utime(fs, fullpath, year, month, day, hour,minute, second); else result = fs->ops->utime(fs, dfs_subdir(fs->path, fullpath),year, month, day, hour, minute, second); } rt_free(fullpath); return result;} struct dfs_filesystem_ops{ char *name; uint32_t flags; /* flags forfile system operations */ /* operations for file */ const struct dfs_file_ops *fops; /* mount and unmount file system */ int (*mount) (structdfs_filesystem *fs, unsigned long rwflag, const void *data); int (*unmount) (structdfs_filesystem *fs); /* make a file system */ int (*mkfs) (rt_device_tdevid); int (*statfs) (structdfs_filesystem *fs, struct statfs *buf); int (*unlink) (structdfs_filesystem *fs, const char *pathname); int (*stat) (structdfs_filesystem *fs, const char *filename, struct stat *buf); int (*rename) (structdfs_filesystem *fs, const char *oldpath, const char *newpath); int (*utime) (structdfs_filesystem *fs, const char *filename, int year, int month, int day, inthour, int minute, int second);}; int dfs_elm_utime(struct dfs_filesystem*fs, const char *path, int year, int month, int day, int hour, int minute, intsecond){ rt_kprintf("dfs_elm_utime%s %d %d %d %d %d %d
", path, year, month, day, hour, minute, second); FILINFO fno; fno.fdate= (WORD)(((year - 1980) * 512U) | (month * 32U) | day); fno.ftime= (WORD)(hour * 2048U | minute * 32U | second / 2U); returnf_utime(path, &fno);}utime()在fatfs官网上有这个函数,编译时候注意这两个宏要打开_USE_CHMOD 和FS_READONLY打开方法:在menuconfig中取消下图红色区域![2.png](/uploads/201807/18/154956bpbpkqezppbbdbbd.png)另一个在代码中修改:#define _USE_CHMOD 1 /* This option switches attributemanipulation functions, f_chmod() and f_utime()./ (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable thisoption. */ 测试时间情况:int main(void){ printf("hello rt-thread
"); utime("hello.txt",2018, 7, 17, 7, 7, 7); ll(); utime("hello.txt",2018, 7, 12, 1, 1, 1); ll(); return 0;}运行效果:![3.png](/uploads/201807/18/155007nxu438ci7c7us6ku.png)
查看更多
0
个回答
默认排序
按发布时间排序
暂无答案,快来添加答案吧
撰写答案
登录
注册新账号
关注者
0
被浏览
1.7k
关于作者
EricYYG
这家伙很懒,什么也没写!
提问
13
回答
9
被采纳
0
关注TA
发私信
相关问题
1
【文件系统学习】+DMY+任务1sample例程
2
【文件系统学习】skawu的文件系统跑起来了
3
【文件系统学习】bin5219在qemu-vexpress-a9 中运行一个.c
4
【文件系统学习】skawu之文件匹配学习
5
【文件系统学习】+海中陆地+成功运行文件系统,并运行文...
6
【文件系统学习】+清石+运行文件系统sample例程
7
【文件系统学习】+小燕+运行文件系统sample例程
8
【文件系统学习】+阿暖+运行文件系统 sample 例程
9
【文件系统学习】+小燕+一系列文件中找出指定文件
10
【文件系统学习】+小燕+文件数据替换
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
2
Rt-thread中OTA下载后,bootloader不搬程序
3
ulog 日志 LOG_HEX 输出时间改为本地日期时间
4
在RT-Thread Studio中构建前执行python命令
5
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
9
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
4
次点赞
Ghost_Girls
1
篇文章
7
次点赞
xiaorui
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部