mirror of https://github.com/grpc/grpc.git
parent
486989f250
commit
d5d35650b0
23 changed files with 357 additions and 5 deletions
@ -0,0 +1,38 @@ |
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#ifndef GRPC_CORE_LIB_GPRPP_STAT_H |
||||
#define GRPC_CORE_LIB_GPRPP_STAT_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stdio.h> |
||||
#include <time.h> |
||||
|
||||
#include "absl/status/status.h" |
||||
#include "absl/strings/string_view.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
// Gets the last-modified timestamp of a file or a directory.
|
||||
// On success, the correct timestamp will be filled with an StatusCode::kOk
|
||||
// returned. Otherwise, timestamp will be untouched and an
|
||||
// StatusCode::kInternal will be returned.
|
||||
absl::Status GetFileModificationTime(const char* filename, time_t* timestamp); |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_GPRPP_STAT_H
|
@ -0,0 +1,49 @@ |
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#ifdef GPR_POSIX_STAT |
||||
|
||||
#include <errno.h> |
||||
#include <sys/stat.h> |
||||
#include <sys/types.h> |
||||
#include <unistd.h> |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gprpp/stat.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
absl::Status GetFileModificationTime(const char* filename, time_t* timestamp) { |
||||
GPR_ASSERT(filename != nullptr); |
||||
GPR_ASSERT(timestamp != nullptr); |
||||
struct stat buf; |
||||
if (stat(filename, &buf) != 0) { |
||||
const char* error_msg = strerror(errno); |
||||
gpr_log(GPR_ERROR, "stat failed for filename %s with error %s.", filename, |
||||
error_msg); |
||||
return absl::Status(absl::StatusCode::kInternal, error_msg); |
||||
} |
||||
// Last file/directory modification time.
|
||||
*timestamp = buf.st_mtime; |
||||
return absl::OkStatus(); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GPR_POSIX_STAT
|
@ -0,0 +1,48 @@ |
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#ifdef GPR_WINDOWS_STAT |
||||
|
||||
#include <errno.h> |
||||
#include <sys/stat.h> |
||||
#include <sys/types.h> |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gprpp/stat.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
absl::Status GetFileModificationTime(const char* filename, time_t* timestamp) { |
||||
GPR_ASSERT(filename != nullptr); |
||||
GPR_ASSERT(timestamp != nullptr); |
||||
struct _stat buf; |
||||
if (_stat(filename, &buf) != 0) { |
||||
const char* error_msg = strerror(errno); |
||||
gpr_log(GPR_ERROR, "_stat failed for filename %s with error %s.", filename, |
||||
error_msg); |
||||
return absl::Status(absl::StatusCode::kInternal, error_msg); |
||||
} |
||||
// Last file/directory modification time.
|
||||
*timestamp = buf.st_mtime; |
||||
return absl::OkStatus(); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GPR_WINDOWS_STAT
|
@ -0,0 +1,74 @@ |
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <gmock/gmock.h> |
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/slice.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/tmpfile.h" |
||||
#include "src/core/lib/gprpp/stat.h" |
||||
#include "src/core/lib/iomgr/load_file.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
TEST(STAT, GetTimestampOnTmpFile) { |
||||
// Create a temporary empty file.
|
||||
FILE* tmp = nullptr; |
||||
char* tmp_name; |
||||
tmp = gpr_tmpfile("prefix", &tmp_name); |
||||
ASSERT_NE(tmp_name, nullptr); |
||||
ASSERT_NE(tmp, nullptr); |
||||
fclose(tmp); |
||||
// Check the last modified date is correctly set.
|
||||
time_t timestamp = 0; |
||||
absl::Status status = |
||||
grpc_core::GetFileModificationTime(tmp_name, ×tamp); |
||||
EXPECT_EQ(status.code(), absl::StatusCode::kOk); |
||||
EXPECT_GT(timestamp, 0); |
||||
// Clean up.
|
||||
remove(tmp_name); |
||||
gpr_free(tmp_name); |
||||
} |
||||
|
||||
TEST(STAT, GetTimestampOnFailure) { |
||||
time_t timestamp = 0; |
||||
absl::Status status = |
||||
grpc_core::GetFileModificationTime("/DOES_NOT_EXIST", ×tamp); |
||||
EXPECT_EQ(status.code(), absl::StatusCode::kInternal); |
||||
// Check the last modified date is not set.
|
||||
EXPECT_EQ(timestamp, 0); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc::testing::TestEnvironment env(argc, argv); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue