2 * Copyright (c) 2016-present, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
11 #include "utils/Range.h"
16 #include <system_error>
18 // A small subset of `std::filesystem`.
19 // `std::filesystem` should be a drop in replacement.
20 // See http://en.cppreference.com/w/cpp/filesystem for documentation.
24 // using file_status = ... causes gcc to emit a false positive warning
26 typedef struct ::_stat64 file_status;
28 typedef struct ::stat file_status;
31 /// http://en.cppreference.com/w/cpp/filesystem/status
32 inline file_status status(StringPiece path, std::error_code& ec) noexcept {
35 const auto error = ::_stat64(path.data(), &status);
37 const auto error = ::stat(path.data(), &status);
40 ec.assign(errno, std::generic_category());
47 /// http://en.cppreference.com/w/cpp/filesystem/is_regular_file
48 inline bool is_regular_file(file_status status) noexcept {
50 return S_ISREG(status.st_mode);
51 #elif !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
52 return (status.st_mode & S_IFMT) == S_IFREG;
54 static_assert(false, "No POSIX stat() support.");
58 /// http://en.cppreference.com/w/cpp/filesystem/is_regular_file
59 inline bool is_regular_file(StringPiece path, std::error_code& ec) noexcept {
60 return is_regular_file(status(path, ec));
63 /// http://en.cppreference.com/w/cpp/filesystem/is_directory
64 inline bool is_directory(file_status status) noexcept {
66 return S_ISDIR(status.st_mode);
67 #elif !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
68 return (status.st_mode & S_IFMT) == S_IFDIR;
70 static_assert(false, "NO POSIX stat() support.");
74 /// http://en.cppreference.com/w/cpp/filesystem/is_directory
75 inline bool is_directory(StringPiece path, std::error_code& ec) noexcept {
76 return is_directory(status(path, ec));
79 /// http://en.cppreference.com/w/cpp/filesystem/file_size
80 inline std::uintmax_t file_size(
82 std::error_code& ec) noexcept {
83 auto stat = status(path, ec);
87 if (!is_regular_file(stat)) {
88 ec.assign(ENOTSUP, std::generic_category());