更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
vfs_utime.c 文件参考

浏览源代码.

函数

int utime (const char *path, const struct utimbuf *ptimes)
 

函数说明

◆ utime()

int utime ( const char *  path,
const struct utimbuf *  ptimes 
)

在文件 vfs_utime.c50 行定义.

51{
52 int ret;
53 char *fullpath = NULL;
54 struct Vnode *vnode = NULL;
55 time_t cur_sec;
56 struct IATTR attr = {0};
57
58 /* Sanity checks *///健全性检查
59
60
61 if (path == NULL) {
62 ret = -EINVAL;
63 goto errout;
64 }
65
66 if (!path[0]) {
67 ret = -ENOENT;
68 goto errout;
69 }
70 //找到绝对路径
71 ret = vfs_normalize_path((const char *)NULL, path, &fullpath);
72 if (ret < 0) {
73 goto errout;
74 }
75
76 /* Get the vnode for this file */
77 VnodeHold();
78 ret = VnodeLookup(fullpath, &vnode, 0);//找到vnode节点
79 if (ret != LOS_OK) {
80 VnodeDrop();
81 goto errout_with_path;
82 }
83
84 if ((vnode->originMount) && (vnode->originMount->mountFlags & MS_RDONLY)) {
85 VnodeDrop();
86 ret = -EROFS;
87 goto errout_with_path;
88 }
89
90 if (vnode->vop && vnode->vop->Chattr) {//验证接口都已经实现
91 if (ptimes == NULL) {//参数没有给时间
92 /* get current seconds */
93 cur_sec = time(NULL);//获取当前秒
94 attr.attr_chg_atime = cur_sec;//更新访问时间
95 attr.attr_chg_mtime = cur_sec;//更新修改时间
96 } else {//采用参数时间
97 attr.attr_chg_atime = ptimes->actime;//更新访问时间
98 attr.attr_chg_mtime = ptimes->modtime;
99 }
100 attr.attr_chg_valid = CHG_ATIME | CHG_MTIME;//更新了两个时间
101 ret = vnode->vop->Chattr(vnode, &attr);//调用接口更新数据
102 if (ret != OK) {
103 VnodeDrop();
104 goto errout_with_path;
105 }
106 } else {
107 ret = -ENOSYS;
108 VnodeDrop();
109 goto errout_with_path;
110 }
111 VnodeDrop();
112
113 /* Successfully stat'ed the file */
114 free(fullpath);
115
116 return OK;
117
118 /* Failure conditions always set the errno appropriately */
119
120errout_with_path:
121 free(fullpath);
122errout:
123
124 if (ret != 0) {
125 set_errno(-ret);
126 }
127 return VFS_ERROR;
128}
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
Definition: fullpath.c:245
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
此结构用于记录 vnode 的属性
Definition: vnode.h:81
unsigned attr_chg_atime
节点最近访问时间
Definition: vnode.h:89
unsigned attr_chg_mtime
节点对应的文件内容被修改时间
Definition: vnode.h:90
unsigned int attr_chg_valid
节点改变有效性 (CHG_MODE | CHG_UID | ... )
Definition: vnode.h:83
unsigned long mountFlags
Definition: mount.h:80
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
struct VnodeOps * vop
Definition: vnode.h:174
struct Mount * originMount
Definition: vnode.h:180
int(* Chattr)(struct Vnode *vnode, struct IATTR *attr)
改变节点属性(change attr)
Definition: vnode.h:214
time_t time(time_t *t)
Definition: time.c:1224
int VnodeDrop(void)
归还锁
Definition: vnode.c:292
int VnodeHold(void)
拿锁,封装互斥量
Definition: vnode.c:283
int VnodeLookup(const char *path, struct Vnode **vnode, uint32_t flags)
通过路径查询vnode节点
Definition: vnode.c:491
函数调用图:
这是这个函数的调用关系图: