1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +00:00

LibCore: Use utimensat() in Core::File to preserve nanosecond timestamps

This takes care of two FIXMEs! :^)
This commit is contained in:
Andreas Kling 2022-11-22 21:07:51 +01:00
parent a9d55ddf57
commit 376b3c95f7

View file

@ -1,10 +1,11 @@
/* /*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/Platform.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
#include <LibCore/File.h> #include <LibCore/File.h>
@ -453,11 +454,16 @@ ErrorOr<void, File::CopyError> File::copy_file(String const& dst_path, struct st
} }
if (has_flag(preserve_mode, PreserveMode::Timestamps)) { if (has_flag(preserve_mode, PreserveMode::Timestamps)) {
// FIXME: Implement utimens() and use it here. struct timespec times[2] = {
struct utimbuf timbuf; #ifdef AK_OS_MACOS
timbuf.actime = src_stat.st_atime; src_stat.st_atimespec,
timbuf.modtime = src_stat.st_mtime; src_stat.st_mtimespec,
if (utime(dst_path.characters(), &timbuf) < 0) #else
src_stat.st_atim,
src_stat.st_mtim,
#endif
};
if (utimensat(AT_FDCWD, dst_path.characters(), times, 0) < 0)
return CopyError { errno, false }; return CopyError { errno, false };
} }
@ -503,11 +509,16 @@ ErrorOr<void, File::CopyError> File::copy_directory(String const& dst_path, Stri
} }
if (has_flag(preserve_mode, PreserveMode::Timestamps)) { if (has_flag(preserve_mode, PreserveMode::Timestamps)) {
// FIXME: Implement utimens() and use it here. struct timespec times[2] = {
struct utimbuf timbuf; #ifdef AK_OS_MACOS
timbuf.actime = src_stat.st_atime; src_stat.st_atimespec,
timbuf.modtime = src_stat.st_atime; src_stat.st_mtimespec,
if (utime(dst_path.characters(), &timbuf) < 0) #else
src_stat.st_atim,
src_stat.st_mtim,
#endif
};
if (utimensat(AT_FDCWD, dst_path.characters(), times, 0) < 0)
return CopyError { errno, false }; return CopyError { errno, false };
} }