kernel+libc+stat: Add support for file times
All checks were successful
continuous-integration/drone/push Build is passing

The modification time is not updated though.
This commit is contained in:
apio 2023-08-08 13:33:40 +02:00
parent 159c05c064
commit 4195e7f206
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 34 additions and 9 deletions

View File

@ -2,7 +2,7 @@
#include <os/FileSystem.h>
#include <os/Mode.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
static const char* file_type(mode_t mode)
{
@ -36,10 +36,20 @@ Result<int> luna_main(int argc, char** argv)
char buf[11];
os::format_mode(st.st_mode, buf);
printf(" File: %s\n", path.chars());
printf(" Size: %zu (%s)\n", st.st_size, file_type(st.st_mode));
printf("Inode: %lu Links: %lu\n", st.st_ino, st.st_nlink);
printf(" Mode: (%#o/%s) UID: %u GID: %u\n", st.st_mode & ~S_IFMT, buf, st.st_uid, st.st_gid);
char atime[256];
strftime(atime, sizeof(atime), "%Y-%m-%d %H:%M:%S", gmtime(&st.st_atim.tv_sec));
char mtime[256];
strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", gmtime(&st.st_mtim.tv_sec));
char ctime[256];
strftime(ctime, sizeof(ctime), "%Y-%m-%d %H:%M:%S", gmtime(&st.st_ctim.tv_sec));
printf(" File: %s\n", path.chars());
printf(" Size: %zu (%s)\n", st.st_size, file_type(st.st_mode));
printf(" Inode: %lu Links: %lu\n", st.st_ino, st.st_nlink);
printf(" Mode: (%#o/%s) UID: %u GID: %u\n", st.st_mode & ~S_IFMT, buf, st.st_uid, st.st_gid);
printf("Access: %s.%.9ld\n", atime, st.st_atim.tv_nsec);
printf("Modify: %s.%.9ld\n", mtime, st.st_mtim.tv_nsec);
printf("Change: %s.%.9ld\n", ctime, st.st_ctim.tv_nsec);
return 0;
}

View File

@ -57,6 +57,9 @@ namespace Ext2
inode->m_metadata.nlinks = inode->m_raw_inode.nlinks;
inode->m_metadata.uid = inode->m_raw_inode.uid;
inode->m_metadata.gid = inode->m_raw_inode.gid;
inode->m_metadata.atime = { .tv_sec = inode->m_raw_inode.atime, .tv_nsec = 0 };
inode->m_metadata.mtime = { .tv_sec = inode->m_raw_inode.mtime, .tv_nsec = 0 };
inode->m_metadata.ctime = { .tv_sec = inode->m_raw_inode.create_time, .tv_nsec = 0 };
#ifdef EXT2_DEBUG
kdbgln("ext2: Read inode %lu with mode %#x (%#x + %#o), size %lu", inum, inode->m_raw_inode.mode,

View File

@ -1,4 +1,5 @@
#include "fs/tmpfs/FileSystem.h"
#include "arch/Timer.h"
#include "fs/devices/DeviceRegistry.h"
#include "fs/tmpfs/Inode.h"
#include <luna/Alloc.h>
@ -26,6 +27,7 @@ namespace TmpFS
inode->set_fs(*this, {});
inode->set_inode_number(m_next_inode_number++, {});
inode->m_metadata.mode = mode;
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
return (SharedPtr<VFS::Inode>)inode;
}
@ -35,6 +37,7 @@ namespace TmpFS
inode->set_fs(*this, {});
TRY(inode->set_link(link, {}));
inode->set_inode_number(m_next_inode_number++, {});
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
return (SharedPtr<VFS::Inode>)inode;
}
@ -49,6 +52,7 @@ namespace TmpFS
inode->set_fs(*this, {});
inode->set_inode_number(m_next_inode_number++, {});
inode->m_metadata.mode = mode;
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
return (SharedPtr<VFS::Inode>)inode;
}
@ -65,6 +69,7 @@ namespace TmpFS
// device ID atm.
inode->set_device_id(luna_dev_makedev(major, minor), {});
inode->m_metadata.mode = mode;
inode->m_metadata.atime = inode->m_metadata.ctime = inode->m_metadata.mtime = *Timer::realtime_clock();
return (SharedPtr<VFS::Inode>)inode;
}

View File

@ -48,6 +48,9 @@ Result<u64> sys_fstatat(Registers*, SyscallArgs args)
kstat.st_size = metadata.size;
kstat.st_dev = inode->fs() ? inode->fs()->host_device_id() : 0;
kstat.st_rdev = metadata.devid;
kstat.st_atim = metadata.atime;
kstat.st_mtim = metadata.mtime;
kstat.st_ctim = metadata.ctime;
if (!MemoryManager::copy_to_user_typed(st, &kstat)) return err(EFAULT);

View File

@ -3,6 +3,7 @@
#ifndef _BITS_STRUCT_STAT_H
#define _BITS_STRUCT_STAT_H
#include <bits/timespec.h>
#include <sys/types.h>
struct stat
@ -15,10 +16,13 @@ struct stat
gid_t st_gid;
off_t st_size;
dev_t st_rdev;
// FIXME: Actually fill these fields in.
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
#define st_atime st_atim.tv_sec
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
#endif