base: add temp_file
port: rework file
This commit is contained in:
@@ -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(
|
cc_library(
|
||||||
name = "temp_path",
|
name = "temp_path",
|
||||||
srcs = ["temp_path.cc"],
|
srcs = ["temp_path.cc"],
|
||||||
|
|||||||
@@ -11,8 +11,9 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#ifndef OR_TOOLS_BASE_FILE_H_
|
// emulates g3/file/base/file.h
|
||||||
#define OR_TOOLS_BASE_FILE_H_
|
#ifndef ORTOOLS_BASE_FILE_H_
|
||||||
|
#define ORTOOLS_BASE_FILE_H_
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -145,4 +146,4 @@ absl::Status SetBinaryProto(absl::string_view file_name,
|
|||||||
|
|
||||||
} // namespace file
|
} // namespace file
|
||||||
|
|
||||||
#endif // OR_TOOLS_BASE_FILE_H_
|
#endif // ORTOOLS_BASE_FILE_H_
|
||||||
|
|||||||
@@ -11,8 +11,8 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#ifndef THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
#ifndef ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
||||||
#define THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
#define ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -31,4 +31,4 @@ inline std::string StringCopy(const std::string& str) { return str; }
|
|||||||
|
|
||||||
} // namespace google::protobuf
|
} // namespace google::protobuf
|
||||||
|
|
||||||
#endif // THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
#endif // ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
|
||||||
|
|||||||
67
ortools/base/temp_file.cc
Normal file
67
ortools/base/temp_file.cc
Normal file
@@ -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 <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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 <unistd.h>
|
||||||
|
#endif
|
||||||
|
#endif // !defined(__PORTABLE_PLATFORM__)
|
||||||
|
|
||||||
|
namespace file {
|
||||||
|
|
||||||
|
::absl::StatusOr<std::string> 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<int32_t>(pthread_self());
|
||||||
|
#else // defined(__linux__)
|
||||||
|
int32_t tid = 123;
|
||||||
|
#endif // defined(__linux__)
|
||||||
|
#if !defined(_MSC_VER)
|
||||||
|
int32_t pid = static_cast<int32_t>(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
|
||||||
38
ortools/base/temp_file.h
Normal file
38
ortools/base/temp_file.h
Normal file
@@ -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 <string>
|
||||||
|
|
||||||
|
#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<std::string> MakeTempFilename(absl::string_view directory,
|
||||||
|
absl::string_view file_prefix);
|
||||||
|
|
||||||
|
} // namespace file
|
||||||
|
|
||||||
|
#endif // ORTOOLS_BASE_TEMP_FILE_H_
|
||||||
@@ -11,8 +11,9 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#ifndef OR_TOOLS_BASE_TEMP_PATH_H_
|
// emulates g3/file/util/temp_path.h
|
||||||
#define OR_TOOLS_BASE_TEMP_PATH_H_
|
#ifndef ORTOOLS_BASE_TEMP_PATH_H_
|
||||||
|
#define ORTOOLS_BASE_TEMP_PATH_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -59,4 +60,4 @@ class TempPath {
|
|||||||
|
|
||||||
namespace file {} // namespace file
|
namespace file {} // namespace file
|
||||||
|
|
||||||
#endif // OR_TOOLS_BASE_TEMP_PATH_H_
|
#endif // ORTOOLS_BASE_TEMP_PATH_H_
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
|
#include "absl/strings/string_view.h"
|
||||||
#include "ortools/base/logging.h"
|
#include "ortools/base/logging.h"
|
||||||
|
|
||||||
#if !defined(__PORTABLE_PLATFORM__)
|
#if !defined(__PORTABLE_PLATFORM__)
|
||||||
@@ -25,7 +26,6 @@
|
|||||||
|
|
||||||
#include "absl/strings/str_format.h"
|
#include "absl/strings/str_format.h"
|
||||||
#include "absl/time/clock.h"
|
#include "absl/time/clock.h"
|
||||||
#include "ortools/base/file.h"
|
|
||||||
#include "ortools/base/helpers.h"
|
#include "ortools/base/helpers.h"
|
||||||
#include "ortools/base/options.h"
|
#include "ortools/base/options.h"
|
||||||
#endif // !defined(__PORTABLE_PLATFORM__)
|
#endif // !defined(__PORTABLE_PLATFORM__)
|
||||||
@@ -52,29 +52,6 @@ namespace operations_research {
|
|||||||
#endif // !defined(__PORTABLE_PLATFORM__)
|
#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<int32_t>(pthread_self());
|
|
||||||
#else // defined(__linux__)
|
|
||||||
int32_t tid = 123;
|
|
||||||
#endif // defined(__linux__)
|
|
||||||
#if !defined(_MSC_VER)
|
|
||||||
int32_t pid = static_cast<int32_t>(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) {
|
::absl::Status PortableDeleteFile(absl::string_view file_name) {
|
||||||
#if defined(__PORTABLE_PLATFORM__)
|
#if defined(__PORTABLE_PLATFORM__)
|
||||||
return absl::Status(absl::StatusCode::kUnimplemented,
|
return absl::Status(absl::StatusCode::kUnimplemented,
|
||||||
|
|||||||
@@ -29,10 +29,6 @@ namespace operations_research {
|
|||||||
|
|
||||||
::absl::Status PortableDeleteFile(absl::string_view file_name);
|
::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
|
} // namespace operations_research
|
||||||
|
|
||||||
#endif // OR_TOOLS_PORT_FILE_H_
|
#endif // OR_TOOLS_PORT_FILE_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user