diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2019-10-19 12:43:39 +0200 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2019-10-19 12:50:46 +0200 |
commit | 8bf69a1dd9dec1d3f8bcd161b50c86924b9bc464 (patch) | |
tree | 28854fff9e441b7f2300ae2e44c229508af5fde1 /src/Stat.cpp | |
parent | b5b234ea33849358ad0605ba5d9127e6e8adba84 (diff) | |
download | ccache-8bf69a1dd9dec1d3f8bcd161b50c86924b9bc464.tar.gz ccache-8bf69a1dd9dec1d3f8bcd161b50c86924b9bc464.tar.bz2 ccache-8bf69a1dd9dec1d3f8bcd161b50c86924b9bc464.zip |
Improve functions related to (l)stat-ing
Introduced a Stat class that represents a “struct stat”. The class
replaces the utility functions x_lstat, x_stat, file_size_on_disk and
is_symlink, and it provides an easier to use interface than the macros
associated with stat structs.
Diffstat (limited to 'src/Stat.cpp')
-rw-r--r-- | src/Stat.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Stat.cpp b/src/Stat.cpp new file mode 100644 index 00000000..ebb47a09 --- /dev/null +++ b/src/Stat.cpp @@ -0,0 +1,45 @@ +// Copyright (C) 2019 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include "Stat.hpp" + +#include "ccache.hpp" + +#include "third_party/fmt/core.h" + +Stat::Stat(StatFunction stat_function, + const std::string& path, + Stat::OnError on_error) +{ + int result = stat_function(path.c_str(), &m_stat); + if (result == 0) { + m_errno = 0; + } else { + m_errno = errno; + if (on_error == OnError::throw_error) { + throw Error(fmt::format("failed to stat {}: {}", path, strerror(errno))); + } + if (on_error == OnError::log) { + cc_log("Failed to stat %s: %s", path.c_str(), strerror(errno)); + } + + // The file is missing, so just zero fill the stat structure. This will + // make e.g. the is_*() methods return false and mtime() will be 0, etc. + memset(&m_stat, '\0', sizeof(m_stat)); + } +} |