Add mtime/ctime/atime support with system time
This commit is contained in:
@@ -27,4 +27,5 @@ extern uint64_t system_time;
|
||||
extern time_t system_boot_time;
|
||||
|
||||
time_t mktime(struct tm *tm);
|
||||
time_t time(void);
|
||||
#endif
|
||||
|
||||
@@ -171,9 +171,9 @@ static int devfs_vnode_stat(struct vnode *node, struct stat *st) {
|
||||
st->st_blocks = 0;
|
||||
st->st_ino = 0;
|
||||
|
||||
st->st_atime = 0;
|
||||
st->st_mtime = 0;
|
||||
st->st_ctime = 0;
|
||||
st->st_atime = system_boot_time;
|
||||
st->st_mtime = system_boot_time;
|
||||
st->st_ctime = system_boot_time;
|
||||
|
||||
st->st_dev = 0;
|
||||
st->st_rdev = 0;
|
||||
|
||||
@@ -27,6 +27,10 @@ static int tm_check(const struct tm *tm) {
|
||||
tm->tm_sec < 0 || tm->tm_sec > 59;
|
||||
}
|
||||
|
||||
time_t time(void) {
|
||||
return system_boot_time + (time_t) (system_time / 1000000000ULL);
|
||||
}
|
||||
|
||||
time_t mktime(struct tm *tm) {
|
||||
if (tm_check(tm) != 0) {
|
||||
return 0;
|
||||
|
||||
+16
-13
@@ -9,6 +9,7 @@
|
||||
#include "sys/assert.h"
|
||||
#include "sys/panic.h"
|
||||
#include "sys/errno.h"
|
||||
#include "sys/time.h"
|
||||
#include "sys/heap.h"
|
||||
|
||||
// #include <string.h>
|
||||
@@ -189,10 +190,10 @@ static int ext2_vnode_mkdir(struct vnode *at, const char *name, uid_t uid, gid_t
|
||||
ent_inode->acl = 0;
|
||||
ent_inode->os_value_1 = 0;
|
||||
memset(ent_inode->os_value_2, 0, sizeof(ent_inode->os_value_2));
|
||||
// TODO: time support in kernel
|
||||
ent_inode->atime = 0;
|
||||
ent_inode->mtime = 0;
|
||||
ent_inode->ctime = 0;
|
||||
time_t cur_time = time();
|
||||
ent_inode->atime = cur_time;
|
||||
ent_inode->mtime = cur_time;
|
||||
ent_inode->ctime = cur_time;
|
||||
ent_inode->dtime = 0;
|
||||
|
||||
memset(ent_inode->direct_blocks, 0, sizeof(ent_inode->direct_blocks));
|
||||
@@ -272,10 +273,11 @@ static int ext2_vnode_creat(struct vnode *at, const char *name, uid_t uid, gid_t
|
||||
ent_inode->acl = 0;
|
||||
ent_inode->os_value_1 = 0;
|
||||
memset(ent_inode->os_value_2, 0, sizeof(ent_inode->os_value_2));
|
||||
// TODO: time support in kernel
|
||||
ent_inode->atime = 0;
|
||||
ent_inode->mtime = 0;
|
||||
ent_inode->ctime = 0;
|
||||
|
||||
time_t cur_time = time();
|
||||
ent_inode->atime = cur_time;
|
||||
ent_inode->mtime = cur_time;
|
||||
ent_inode->ctime = cur_time;
|
||||
ent_inode->dtime = 0;
|
||||
|
||||
memset(ent_inode->direct_blocks, 0, sizeof(ent_inode->direct_blocks));
|
||||
@@ -355,6 +357,10 @@ static ssize_t ext2_vnode_write(struct ofile *fd, const void *buf, size_t count)
|
||||
size_t written = 0;
|
||||
size_t remaining = count;
|
||||
|
||||
// Update mtime on writes
|
||||
// TODO: something like nomtime option
|
||||
inode->mtime = time();
|
||||
|
||||
if (can_write) {
|
||||
size_t can_write_blocks = (can_write + sb->block_size - 1) / sb->block_size;
|
||||
|
||||
@@ -425,13 +431,10 @@ static ssize_t ext2_vnode_write(struct ofile *fd, const void *buf, size_t count)
|
||||
fd->pos += need_write;
|
||||
remaining -= need_write;
|
||||
}
|
||||
} else {
|
||||
if (written) {
|
||||
// Flush inode struct to disk - size has changed
|
||||
ext2_write_inode(ext2, inode, vn->ino);
|
||||
}
|
||||
}
|
||||
|
||||
ext2_write_inode(ext2, inode, vn->ino);
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
|
||||
+19
-7
@@ -35,7 +35,7 @@ struct tar {
|
||||
uint32_t size;
|
||||
uint64_t __pad3;
|
||||
uint32_t mtime;
|
||||
uint64_t __pad4;
|
||||
uint32_t ctime;
|
||||
// The rest is irrelevant
|
||||
} __attribute__((packed)) meta;
|
||||
};
|
||||
@@ -202,6 +202,8 @@ static int tar_init(struct fs *tar, const char *opt) {
|
||||
gid_t gid = tarfs_octal(hdr->meta_oct.gid, 8);
|
||||
mode_t mode = tarfs_octal(hdr->meta_oct.mode, 8) & 0x1FF;
|
||||
|
||||
time_t mtime = tarfs_octal(hdr->meta_oct.mtime, 12);
|
||||
|
||||
if (hdr->meta_oct.typeflag[0] == '\0' || hdr->meta_oct.typeflag[0] == '0') {
|
||||
node_size = tarfs_octal(hdr->meta_oct.size, 12);
|
||||
mode |= S_IFREG;
|
||||
@@ -248,6 +250,8 @@ static int tar_init(struct fs *tar, const char *opt) {
|
||||
hdr->meta.gid = gid;
|
||||
hdr->meta.mode = mode;
|
||||
hdr->meta.size = node_size;
|
||||
hdr->meta.mtime = mtime;
|
||||
hdr->meta.ctime = mtime;
|
||||
|
||||
node->uid = hdr->meta.uid;
|
||||
node->gid = hdr->meta.gid;
|
||||
@@ -291,9 +295,9 @@ static int tarfs_vnode_stat(struct vnode *vn, struct stat *st) {
|
||||
st->st_gid = vn->gid;
|
||||
st->st_mode = vn->mode | S_IFDIR;
|
||||
|
||||
st->st_atime = 0;
|
||||
st->st_mtime = 0;
|
||||
st->st_ctime = 0;
|
||||
st->st_atime = system_boot_time;
|
||||
st->st_mtime = system_boot_time;
|
||||
st->st_ctime = system_boot_time;
|
||||
|
||||
st->st_nlink = 1;
|
||||
st->st_rdev = 0;
|
||||
@@ -316,9 +320,9 @@ static int tarfs_vnode_stat(struct vnode *vn, struct stat *st) {
|
||||
st->st_rdev = 0;
|
||||
st->st_dev = 0;
|
||||
|
||||
st->st_mtime = 0;
|
||||
st->st_ctime = 0;
|
||||
st->st_atime = 0;
|
||||
st->st_mtime = hdr->meta.mtime;
|
||||
st->st_ctime = hdr->meta.ctime;
|
||||
st->st_atime = st->st_mtime;
|
||||
|
||||
st->st_nlink = 1;
|
||||
|
||||
@@ -339,6 +343,9 @@ static int tarfs_vnode_mkdir(struct vnode *at, const char *name, uid_t uid, gid_
|
||||
hdr->meta.gid = gid;
|
||||
hdr->meta.mode = mode & 0x1FF;
|
||||
hdr->meta.size = 0;
|
||||
time_t cur_time = time();
|
||||
hdr->meta.mtime = cur_time;
|
||||
hdr->meta.ctime = cur_time;
|
||||
|
||||
node->uid = uid;
|
||||
node->gid = gid;
|
||||
@@ -367,6 +374,9 @@ static int tarfs_vnode_creat(struct vnode *at, const char *name, uid_t uid, gid_
|
||||
hdr->meta.gid = gid;
|
||||
hdr->meta.mode = mode & 0x1FF;
|
||||
hdr->meta.size = 0;
|
||||
time_t cur_time = time();
|
||||
hdr->meta.mtime = cur_time;
|
||||
hdr->meta.ctime = cur_time;
|
||||
|
||||
for (size_t i = 0; i < TAR_DIRECT_BLOCKS; ++i) {
|
||||
hdr->direct_blocks[i] = 0;
|
||||
@@ -422,6 +432,8 @@ static ssize_t tarfs_vnode_write(struct ofile *fd, const void *buf, size_t count
|
||||
_assert(node);
|
||||
struct tar *hdr = node->fs_data;
|
||||
_assert(hdr);
|
||||
// Update mtime on writes
|
||||
hdr->meta.mtime = time();
|
||||
|
||||
if (fd->pos > hdr->meta.size) {
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user