| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include <string> |
| 20 |
|
| 21 |
// IWYU pragma: no_include <bits/struct_stat.h> |
| 22 |
|
| 23 |
#include "absl/status/status.h" |
| 24 |
#include "absl/strings/string_view.h" |
| 25 |
|
| 26 |
#ifdef GPR_POSIX_STAT |
| 27 |
|
| 28 |
#include <errno.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
|
| 31 |
#include <grpc/support/log.h> |
| 32 |
|
| 33 |
#include "src/core/lib/gprpp/stat.h" |
| 34 |
#include "src/core/lib/gprpp/strerror.h" |
| 35 |
|
| 36 |
namespace grpc_core { |
| 37 |
|
| 38 |
absl::Status GetFileModificationTime(const char* filename, time_t* timestamp) { |
| 39 |
GPR_ASSERT(filename != nullptr); |
| 40 |
GPR_ASSERT(timestamp != nullptr); |
| 41 |
struct stat buf; |
| 42 |
if (stat(filename, &buf) != 0) { |
| 43 |
std::string error_msg = StrError(errno); |
| 44 |
gpr_log(GPR_ERROR, "stat failed for filename %s with error %s.", filename, |
| 45 |
error_msg.c_str()); |
| 46 |
return absl::Status(absl::StatusCode::kInternal, error_msg); |
| 47 |
} |
| 48 |
// Last file/directory modification time. |
| 49 |
*timestamp = buf.st_mtime; |
| 50 |
return absl::OkStatus(); |
| 51 |
} |
| 52 |
|
| 53 |
} // namespace grpc_core |
| 54 |
|
| 55 |
#endif // GPR_POSIX_STAT |