From fcf4bd181edc88b348d871f0c01d63599e426815 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 20 Aug 2025 11:24:30 +0200 Subject: [PATCH] base: add temp_file port: rework file --- ortools/base/BUILD.bazel | 13 ++++++ ortools/base/file.h | 7 +-- ortools/base/string_view_migration.h | 6 +-- ortools/base/temp_file.cc | 67 ++++++++++++++++++++++++++++ ortools/base/temp_file.h | 38 ++++++++++++++++ ortools/base/temp_path.h | 7 +-- ortools/port/file.cc | 25 +---------- ortools/port/file.h | 4 -- 8 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 ortools/base/temp_file.cc create mode 100644 ortools/base/temp_file.h diff --git a/ortools/base/BUILD.bazel b/ortools/base/BUILD.bazel index 4576ad9bbb..c918f4021f 100644 --- a/ortools/base/BUILD.bazel +++ b/ortools/base/BUILD.bazel @@ -634,6 +634,19 @@ cc_library( ], ) +cc_library( + name = "temp_file", + srcs = ["temp_file.cc"], + hdrs = ["temp_file.h"], + deps = [ + ":base", + ":file", + "@abseil-cpp//absl/status", + "@abseil-cpp//absl/strings", + "@abseil-cpp//absl/time", + ], +) + cc_library( name = "temp_path", srcs = ["temp_path.cc"], diff --git a/ortools/base/file.h b/ortools/base/file.h index c53ae28458..75b67ca501 100644 --- a/ortools/base/file.h +++ b/ortools/base/file.h @@ -11,8 +11,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef OR_TOOLS_BASE_FILE_H_ -#define OR_TOOLS_BASE_FILE_H_ +// emulates g3/file/base/file.h +#ifndef ORTOOLS_BASE_FILE_H_ +#define ORTOOLS_BASE_FILE_H_ #include #include @@ -145,4 +146,4 @@ absl::Status SetBinaryProto(absl::string_view file_name, } // namespace file -#endif // OR_TOOLS_BASE_FILE_H_ +#endif // ORTOOLS_BASE_FILE_H_ diff --git a/ortools/base/string_view_migration.h b/ortools/base/string_view_migration.h index df76521299..85f5b1f495 100644 --- a/ortools/base/string_view_migration.h +++ b/ortools/base/string_view_migration.h @@ -11,8 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ -#define THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ +#ifndef ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ +#define ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ #include @@ -31,4 +31,4 @@ inline std::string StringCopy(const std::string& str) { return str; } } // namespace google::protobuf -#endif // THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ +#endif // ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_ diff --git a/ortools/base/temp_file.cc b/ortools/base/temp_file.cc new file mode 100644 index 0000000000..e43d41071e --- /dev/null +++ b/ortools/base/temp_file.cc @@ -0,0 +1,67 @@ +// Copyright 2010-2025 Google LLC +// 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 "ortools/base/temp_file.h" + +#include +#include + +#include "absl/status/statusor.h" +#include "absl/strings/str_format.h" +#include "absl/strings/string_view.h" +#include "absl/time/clock.h" +#include "ortools/base/logging.h" + +#if !defined(__PORTABLE_PLATFORM__) +#if !defined(_MSC_VER) +#include +#endif +#endif // !defined(__PORTABLE_PLATFORM__) + +namespace file { + +::absl::StatusOr MakeTempFilename(absl::string_view directory, + absl::string_view file_prefix) { + std::string filename; +#if defined(__PORTABLE_PLATFORM__) + filename = "Temporary files are not implemented for this platform."; + LOG(ERROR) << filename; + return absl::UnavailableError(filename); +#endif // !defined(__PORTABLE_PLATFORM__) + +#if defined(__linux__) + int32_t tid = static_cast(pthread_self()); +#else // defined(__linux__) + int32_t tid = 123; +#endif // defined(__linux__) +#if !defined(_MSC_VER) + int32_t pid = static_cast(getpid()); +#else // _MSC_VER + int32_t pid = 456; +#endif // _MSC_VER + int64_t now = absl::GetCurrentTimeNanos(); + + if (directory.empty()) { + directory = "/tmp"; + } + + if (file_prefix.empty()) { + file_prefix = "tempfile"; + } + + filename = absl::StrFormat("%s/%s-%x-%d-%llx", directory, file_prefix, tid, + pid, now); + return filename; +} + +} // namespace file diff --git a/ortools/base/temp_file.h b/ortools/base/temp_file.h new file mode 100644 index 0000000000..0e5b9f3f28 --- /dev/null +++ b/ortools/base/temp_file.h @@ -0,0 +1,38 @@ +// Copyright 2010-2025 Google LLC +// 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. + +// emulates g3/file/util/temp_file.h +#ifndef ORTOOLS_BASE_TEMP_FILE_H_ +#define ORTOOLS_BASE_TEMP_FILE_H_ + +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace file { + +// The following functions return a temporary-looking filename. +// Note that the pattern of the generated filenames is subject to change. + +// Returns a unique filename in 'directory' (which can be followed or not by a +// "/"). Unique filenames begin with the given file_prefix followed by a unique +// string. +// If 'directory' is empty a local scratch directory is selected. +// If 'file_prefix' is empty a default prefix is used. +::absl::StatusOr MakeTempFilename(absl::string_view directory, + absl::string_view file_prefix); + +} // namespace file + +#endif // ORTOOLS_BASE_TEMP_FILE_H_ diff --git a/ortools/base/temp_path.h b/ortools/base/temp_path.h index 568268d749..e561bc458f 100644 --- a/ortools/base/temp_path.h +++ b/ortools/base/temp_path.h @@ -11,8 +11,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef OR_TOOLS_BASE_TEMP_PATH_H_ -#define OR_TOOLS_BASE_TEMP_PATH_H_ +// emulates g3/file/util/temp_path.h +#ifndef ORTOOLS_BASE_TEMP_PATH_H_ +#define ORTOOLS_BASE_TEMP_PATH_H_ #include @@ -59,4 +60,4 @@ class TempPath { namespace file {} // namespace file -#endif // OR_TOOLS_BASE_TEMP_PATH_H_ +#endif // ORTOOLS_BASE_TEMP_PATH_H_ diff --git a/ortools/port/file.cc b/ortools/port/file.cc index d895064e1e..c32ecad2e1 100644 --- a/ortools/port/file.cc +++ b/ortools/port/file.cc @@ -16,6 +16,7 @@ #include #include "absl/status/status.h" +#include "absl/strings/string_view.h" #include "ortools/base/logging.h" #if !defined(__PORTABLE_PLATFORM__) @@ -25,7 +26,6 @@ #include "absl/strings/str_format.h" #include "absl/time/clock.h" -#include "ortools/base/file.h" #include "ortools/base/helpers.h" #include "ortools/base/options.h" #endif // !defined(__PORTABLE_PLATFORM__) @@ -52,29 +52,6 @@ namespace operations_research { #endif // !defined(__PORTABLE_PLATFORM__) } -bool PortableTemporaryFile(const char* directory_prefix, - std::string* filename_out) { -#if defined(__PORTABLE_PLATFORM__) - LOG(ERROR) << "Temporary files are not implemented for this platform."; - return false; -#else // defined(__PORTABLE_PLATFORM__) -#if defined(__linux) - int32_t tid = static_cast(pthread_self()); -#else // defined(__linux__) - int32_t tid = 123; -#endif // defined(__linux__) -#if !defined(_MSC_VER) - int32_t pid = static_cast(getpid()); -#else // _MSC_VER - int32_t pid = 456; -#endif // _MSC_VER - int64_t now = absl::GetCurrentTimeNanos(); - std::string filename = - absl::StrFormat("/tmp/parameters-tempfile-%x-%d-%llx", tid, pid, now); - return true; -#endif // !defined(__PORTABLE_PLATFORM__) -} - ::absl::Status PortableDeleteFile(absl::string_view file_name) { #if defined(__PORTABLE_PLATFORM__) return absl::Status(absl::StatusCode::kUnimplemented, diff --git a/ortools/port/file.h b/ortools/port/file.h index b2bac036e4..ccdcf402a9 100644 --- a/ortools/port/file.h +++ b/ortools/port/file.h @@ -29,10 +29,6 @@ namespace operations_research { ::absl::Status PortableDeleteFile(absl::string_view file_name); -// Returns true if successful. Outputs temp file to filename. -bool PortableTemporaryFile(const char* directory_prefix, - std::string* filename_out); - } // namespace operations_research #endif // OR_TOOLS_PORT_FILE_H_