Skip to content

Commit

Permalink
优化时间戳设置
Browse files Browse the repository at this point in the history
  • Loading branch information
loogg authored and mysterywolf committed Jul 12, 2024
1 parent 3ad0910 commit 52add2a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions dfs_lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static int _dfs_lfs_stat(struct dfs_filesystem* dfs, const char* path, struct st
return _lfs_result_to_dfs(result);
}

time_t mtime;
time_t mtime = 0;
lfs_getattr(&dfs_lfs->lfs, path, ATTR_TIMESTAMP, &mtime, sizeof(time_t));

_dfs_lfs_tostat(st, &info, mtime);
Expand Down Expand Up @@ -595,6 +595,11 @@ static int _dfs_lfs_open(struct dfs_file* file)
{
goto _error_dir;
}
else
{
time_t now = time(RT_NULL);
lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
}
}

result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->vnode->path);
Expand Down Expand Up @@ -671,6 +676,7 @@ static int _dfs_lfs_close(struct dfs_file* file)
{
int result;
dfs_lfs_fd_t* dfs_lfs_fd;
uint8_t need_time_update;
RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

Expand All @@ -688,12 +694,15 @@ static int _dfs_lfs_close(struct dfs_file* file)
}
else
{
need_time_update = (dfs_lfs_fd->u.file.flags & LFS_F_DIRTY) || (dfs_lfs_fd->u.file.flags & LFS_F_WRITING);
result = lfs_file_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
if (result == LFS_ERR_OK && need_time_update)
{
time_t now = time(RT_NULL);
lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
}
}

time_t now = time(RT_NULL);
lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));

rt_free(dfs_lfs_fd);

return _lfs_result_to_dfs(result);
Expand Down Expand Up @@ -786,12 +795,19 @@ static int _dfs_lfs_flush(struct dfs_file* file)
{
int result;
dfs_lfs_fd_t* dfs_lfs_fd;
uint8_t need_time_update;

RT_ASSERT(file != RT_NULL);
RT_ASSERT(file->data != RT_NULL);

dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
need_time_update = (dfs_lfs_fd->u.file.flags & LFS_F_DIRTY) || (dfs_lfs_fd->u.file.flags & LFS_F_WRITING);
result = lfs_file_sync(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
if (result == LFS_ERR_OK && need_time_update)
{
time_t now = time(RT_NULL);
lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
}

return _lfs_result_to_dfs(result);
}
Expand Down

0 comments on commit 52add2a

Please sign in to comment.