This commit is contained in:
parent
a92077d311
commit
37e9b25b62
@ -46,3 +46,4 @@ luna_app(socket-test.cpp socket-test)
|
||||
luna_app(socket-client.cpp socket-client)
|
||||
luna_app(input.cpp input)
|
||||
luna_app(shmem-test.cpp shmem-test)
|
||||
luna_app(touch.cpp touch)
|
||||
|
47
apps/touch.cpp
Normal file
47
apps/touch.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <errno.h>
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/File.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
Vector<StringView> files;
|
||||
|
||||
bool only_atime { false };
|
||||
bool only_mtime { false };
|
||||
bool no_create { false };
|
||||
bool no_dereference { false };
|
||||
|
||||
os::ArgumentParser parser;
|
||||
parser.add_description("Update the access and modification times of files."_sv);
|
||||
parser.add_system_program_info("touch"_sv);
|
||||
parser.set_vector_argument(files, "files", true);
|
||||
parser.add_switch_argument(only_atime, 'a', ""_sv, "change only the access time"_sv);
|
||||
parser.add_switch_argument(no_create, 'c', "no-create"_sv, "do not create new files"_sv);
|
||||
parser.add_switch_argument(no_dereference, 'h', "no-dereference"_sv, "do not follow symbolic links"_sv);
|
||||
parser.add_switch_argument(only_mtime, 'm', ""_sv, "change only the modification time"_sv);
|
||||
TRY(parser.parse(argc, argv));
|
||||
|
||||
if (only_atime && only_mtime)
|
||||
{
|
||||
os::eprintln("%s: only one of -a and -m can be specified.", argv[0]);
|
||||
parser.short_usage(argv[0]);
|
||||
}
|
||||
|
||||
struct timespec times[2] = {
|
||||
{ .tv_sec = 0, .tv_nsec = only_mtime ? UTIME_OMIT : UTIME_NOW },
|
||||
{ .tv_sec = 0, .tv_nsec = only_atime ? UTIME_OMIT : UTIME_NOW },
|
||||
};
|
||||
|
||||
for (auto& filename : files)
|
||||
{
|
||||
SharedPtr<os::File> file;
|
||||
if (no_create) file = TRY(os::File::open(filename, os::File::ReadOnly));
|
||||
else
|
||||
file = TRY(os::File::open_or_create(filename, os::File::ReadOnly));
|
||||
|
||||
if (futimens(file->fd(), times, no_dereference ? AT_SYMLINK_NOFOLLOW : 0) < 0) return err(errno);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -300,14 +300,20 @@ Result<u64> sys_utimensat(Registers*, SyscallArgs args)
|
||||
if (ktimes[0].tv_nsec != UTIME_OMIT)
|
||||
{
|
||||
if (ktimes[0].tv_nsec == UTIME_NOW) metadata.atime = *Timer::realtime_clock();
|
||||
if (ktimes[0].tv_nsec < 0 || ktimes[0].tv_nsec > 999'999'999) return err(EINVAL);
|
||||
metadata.atime = ktimes[0];
|
||||
else
|
||||
{
|
||||
if (ktimes[0].tv_nsec < 0 || ktimes[0].tv_nsec > 999'999'999) return err(EINVAL);
|
||||
metadata.atime = ktimes[0];
|
||||
}
|
||||
}
|
||||
if (ktimes[1].tv_nsec != UTIME_OMIT)
|
||||
{
|
||||
if (ktimes[1].tv_nsec == UTIME_NOW) metadata.mtime = *Timer::realtime_clock();
|
||||
if (ktimes[1].tv_nsec < 0 || ktimes[1].tv_nsec > 999'999'999) return err(EINVAL);
|
||||
metadata.mtime = ktimes[1];
|
||||
else
|
||||
{
|
||||
if (ktimes[1].tv_nsec < 0 || ktimes[1].tv_nsec > 999'999'999) return err(EINVAL);
|
||||
metadata.mtime = ktimes[1];
|
||||
}
|
||||
}
|
||||
TRY(inode->set_metadata(metadata));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user