Compare commits

...

2 Commits

Author SHA1 Message Date
e640c6e245 Kernel, libc, userspace: Add file timestamps (atime,ctime,mtime) 2022-10-30 09:57:17 +01:00
8d46c9bbe2 Kernel, libc: Fix a big bug in printf()
Every time printf flushes the buffer to us in sprintf() or snprintf(), we call strncat to append the data.

But we want to start from the beginning in the first flush. What if there was data already there?
Well, we just append to the old data. Which is not good, and breaks snprintf()'s maximum size policy.

This fix sets the first byte of str to NULL, to avoid this.
2022-10-30 09:53:23 +01:00
16 changed files with 64 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
const char* mode_to_string(mode_t mode)
{
@ -55,6 +56,10 @@ int main(int argc, char** argv)
printf("Owned by: %s\n", own->pw_name);
printf("Mode: %s\n", mode_to_string(st.st_mode));
printf("Accessed on: %s", ctime(&st.st_atime));
printf("Modified on: %s", ctime(&st.st_mtime));
printf("Changed on: %s", ctime(&st.st_ctime));
endpwent();
return EXIT_SUCCESS;

View File

@ -41,6 +41,9 @@ namespace VFS
Node* link;
int uid;
int gid;
uint64_t atime;
uint64_t ctime;
uint64_t mtime;
mode_t mode;
};

View File

@ -5,6 +5,8 @@
#include "std/stdlib.h"
#include "std/string.h"
extern uint64_t clock_boot();
VFS::Node* ConsoleDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -16,6 +18,7 @@ VFS::Node* ConsoleDevice::create_new(const char* devname)
dev->tty = 1;
dev->uid = dev->gid = 0;
dev->mode = 0222;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -16,6 +16,8 @@ VFS::Node* devfs_root = nullptr;
VFS::Node* devfs_files[DEVFS_MAX_FILES];
int devfs_file_count = 0;
extern uint64_t clock_boot();
VFS::Node* DeviceFS::get()
{
if (devfs_root) return devfs_root;
@ -27,6 +29,7 @@ VFS::Node* DeviceFS::get()
devfs_root->readdir_func = DeviceFS::readdir;
devfs_root->mode = 0755;
devfs_root->uid = devfs_root->gid = 0;
devfs_root->atime = devfs_root->ctime = devfs_root->mtime = clock_boot();
strncpy(devfs_root->name, "dev", sizeof(devfs_root->name));
devfs_files[devfs_file_count++] = VersionDevice::create_new("version");

View File

@ -16,6 +16,8 @@ int KeyboardDevice::would_block(VFS::Node*)
return kbd_bufsize == 0;
}
extern uint64_t clock_boot();
VFS::Node* KeyboardDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -28,6 +30,7 @@ VFS::Node* KeyboardDevice::create_new(const char* devname)
dev->tty = 1;
dev->uid = dev->gid = 0;
dev->mode = 0444;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -3,6 +3,8 @@
#include "std/stdlib.h"
#include "std/string.h"
extern uint64_t clock_boot();
VFS::Node* NullDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -14,6 +16,7 @@ VFS::Node* NullDevice::create_new(const char* devname)
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0666;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -6,6 +6,8 @@
#include "std/stdlib.h"
#include "std/string.h"
extern uint64_t clock_boot();
VFS::Node* RandomDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -16,6 +18,7 @@ VFS::Node* RandomDevice::create_new(const char* devname)
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0444;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -5,6 +5,8 @@
#include "std/stdlib.h"
#include "std/string.h"
extern uint64_t clock_boot();
VFS::Node* SerialDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -15,6 +17,7 @@ VFS::Node* SerialDevice::create_new(const char* devname)
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0200;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -4,6 +4,8 @@
#include "std/string.h"
#include "thread/PIT.h"
extern uint64_t clock_boot();
VFS::Node* UptimeDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -14,6 +16,7 @@ VFS::Node* UptimeDevice::create_new(const char* devname)
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0444;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -4,6 +4,8 @@
#include "std/stdlib.h"
#include "std/string.h"
extern uint64_t clock_boot();
VFS::Node* VersionDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
@ -14,6 +16,7 @@ VFS::Node* VersionDevice::create_new(const char* devname)
dev->flags = 0;
dev->uid = dev->gid = 0;
dev->mode = 0444;
dev->atime = dev->ctime = dev->mtime = clock_boot();
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}

View File

@ -18,6 +18,9 @@ static bool initrd_initialized = false;
static VFS::Node initrd_root;
extern uint64_t clock_boot(); // defined in sys/clock.cpp
extern uint64_t clock_now();
bool InitRD::is_initialized()
{
return initrd_initialized;
@ -234,6 +237,7 @@ int initrd_mkdir(VFS::Node* node, const char* name, mode_t mode) // FIXME: Retur
new_node.type = VFS_DIRECTORY;
new_node.mode = mode;
new_node.uid = new_node.gid = 0;
new_node.atime = new_node.ctime = new_node.mtime = clock_now();
strncpy(new_node.name, name, sizeof(new_node.name));
InitRD::Directory dir;
strncpy(dir.name, name, sizeof(dir.name));
@ -293,6 +297,7 @@ static bool initrd_register_dir(InitRD::Directory& dir, uint64_t inode)
node.length = 0;
node.mode = 0755;
node.uid = node.gid = 0;
node.atime = node.ctime = node.mtime = clock_boot();
strncpy(node.name, buffer, sizeof(node.name));
strncpy(dir.name, buffer, sizeof(dir.name));
@ -354,6 +359,7 @@ static bool initrd_register_file(InitRD::File& f, uint64_t inode)
node.type = VFS_FILE;
node.mode = f.mode & 07555; // don't allow writing
node.uid = node.gid = 0;
node.atime = node.ctime = node.mtime = clock_boot();
strncpy(node.name, buffer, sizeof(node.name));
strncpy(f.name, buffer, sizeof(f.name));
@ -398,6 +404,7 @@ static void initrd_initialize_root()
initrd_root.type |= VFS_DIRECTORY;
initrd_root.mode = 0755;
initrd_root.uid = initrd_root.gid = 0;
initrd_root.atime = initrd_root.ctime = initrd_root.mtime = clock_boot();
InitRD::Directory& root = dirs[0];
total_dirs++;
strncpy(initrd_root.name, "initrd", sizeof(initrd_root.name));

View File

@ -228,6 +228,7 @@ int sprintf(char* __s, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (__s) *__s = 0;
int written = internal_printf(
fmt,
[&](const char* s) {
@ -242,6 +243,7 @@ int snprintf(char* __s, size_t max, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (__s && max) *__s = 0;
int written = internal_printf(
fmt,
[&](const char* s) {
@ -266,6 +268,7 @@ int vkprintf(const char* fmt, va_list ap)
int vsprintf(char* __s, const char* fmt, va_list ap)
{
*__s = 0;
return internal_printf(
fmt,
[&](const char* s) {
@ -276,6 +279,7 @@ int vsprintf(char* __s, const char* fmt, va_list ap)
int vsnprintf(char* __s, size_t max, const char* fmt, va_list ap)
{
if (max) *__s = 0;
return internal_printf(
fmt,
[&](const char* s) {

View File

@ -70,4 +70,14 @@ extern BOOTBOOT bootboot;
void clock_init()
{
unix_boot_time = unix_boottime(bootboot.datetime);
}
uint64_t clock_now()
{
return unix_boot_time + (PIT::ms_since_boot / 1000);
}
uint64_t clock_boot()
{
return unix_boot_time;
}

View File

@ -16,9 +16,9 @@ struct stat // FIXME: This struct is quite stubbed out.
int st_dev; // FIXME: Implement this.
uid_t st_uid;
gid_t st_gid;
time_t st_atime; // Not implemented.
time_t st_mtime; // Not implemented.
time_t st_ctime; // Not implemented.
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
};
void do_stat(Context* context, VFS::Node* node, struct stat* buf)
@ -34,6 +34,9 @@ void do_stat(Context* context, VFS::Node* node, struct stat* buf)
kstat->st_size = node->length;
kstat->st_uid = node->uid;
kstat->st_gid = node->gid;
kstat->st_atime = node->atime;
kstat->st_ctime = node->ctime;
kstat->st_mtime = node->mtime;
release_user_ref(kstat);
context->rax = 0;
}

View File

@ -12,9 +12,9 @@ struct stat // FIXME: This struct is quite stubbed out.
int st_dev; // Not implemented.
uid_t st_uid;
gid_t st_gid;
time_t st_atime; // Not implemented.
time_t st_mtime; // Not implemented.
time_t st_ctime; // Not implemented.
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
};
/* Type of file. */

View File

@ -261,6 +261,7 @@ extern "C"
int vsprintf(char* str, const char* format, va_list ap)
{
if (str) *str = 0; // so strncat starts from the beginning
return internal_printf(
format,
[&](const char* s) {
@ -271,6 +272,7 @@ extern "C"
int vsnprintf(char* str, size_t max, const char* format, va_list ap)
{
if (max && str) *str = 0; // so strncat starts from the beginning
return internal_printf(
format,
[&](const char* s) {