Kernel/BAN: move unix time conversion to BAN and add stat to Shell

This commit is contained in:
Bananymous
2023-03-27 00:44:57 +03:00
parent e55860eb6b
commit 666051fd34
7 changed files with 119 additions and 45 deletions

View File

@@ -6,6 +6,5 @@ namespace RTC
{
BAN::Time get_current_time();
uint64_t get_unix_time();
}

View File

@@ -334,7 +334,7 @@ namespace Kernel
if (error_or.error().get_error_code() != ENOENT)
return error_or.error();
uint64_t current_time = RTC::get_unix_time();
uint64_t current_time = BAN::to_unix_time(RTC::get_current_time());
Ext2::Inode ext2_inode;
ext2_inode.mode = mode;

View File

@@ -87,41 +87,4 @@ namespace RTC
return time;
}
static bool is_leap_year(uint64_t year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static uint64_t leap_days_since_epoch(const BAN::Time& time)
{
uint64_t leap_years = 0;
for (int year = 1970; year < time.year; year++)
if (is_leap_year(year))
leap_years++;
if (is_leap_year(time.year))
if (time.month >= 3 || (time.month == 2 && time.day == 29))
leap_years++;
return leap_years;
}
uint64_t get_unix_time()
{
auto time = get_current_time();
uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
uint64_t years = time.year - 1970;
uint64_t days = years * 365 + month_days[time.month - 1] + time.day + leap_days_since_epoch(time) - 1;
uint64_t hours = days * 24 + time.hour;
uint64_t minutes = hours * 60 + time.minute;
uint64_t seconds = minutes * 60 + time.second;
return seconds;
}
}

View File

@@ -425,6 +425,43 @@ argument_done:
}
TTY_PRINTLN("");
}
else if (arguments.front() == "stat")
{
if (arguments.size() != 2)
return BAN::Error::from_c_string("usage: 'stat path'");
stat st;
TRY(Process::current()->stat(arguments[1], &st));
auto mode_string = [](mode_t mode)
{
static char buffer[11] {};
buffer[0] = (mode & Inode::Mode::IFDIR) ? 'd' : '-';
buffer[1] = (mode & Inode::Mode::IRUSR) ? 'r' : '-';
buffer[2] = (mode & Inode::Mode::IWUSR) ? 'w' : '-';
buffer[3] = (mode & Inode::Mode::IXUSR) ? 'x' : '-';
buffer[4] = (mode & Inode::Mode::IRGRP) ? 'r' : '-';
buffer[5] = (mode & Inode::Mode::IWGRP) ? 'w' : '-';
buffer[6] = (mode & Inode::Mode::IXGRP) ? 'x' : '-';
buffer[7] = (mode & Inode::Mode::IROTH) ? 'r' : '-';
buffer[8] = (mode & Inode::Mode::IWOTH) ? 'w' : '-';
buffer[9] = (mode & Inode::Mode::IXOTH) ? 'x' : '-';
return (const char*)buffer;
};
const char* type =
(st.st_mode & Inode::Mode::IFREG) ? "regular file" :
(st.st_mode & Inode::Mode::IFDIR) ? "directory" :
"other";
TTY_PRINTLN(" File: {}", arguments[1]);
TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type);
TTY_PRINTLN("Device: {},{}\tInode: {}\tLinks: {}", st.st_dev, st.st_rdev, st.st_ino, st.st_nlink);
TTY_PRINTLN("Access: ({}/{})\tUid: {}\tGid: {}", st.st_mode, mode_string(st.st_mode), st.st_uid, st.st_gid);
TTY_PRINTLN("Access: {}", BAN::from_unix_time(st.st_atime));
TTY_PRINTLN("Modify: {}", BAN::from_unix_time(st.st_mtime));
TTY_PRINTLN("Change: {}", BAN::from_unix_time(st.st_ctime));
}
else if (arguments.front() == "cd")
{
if (arguments.size() > 2)