Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
temp_file.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17#include <string>
18
19#include "absl/status/statusor.h"
20#include "absl/strings/str_format.h"
21#include "absl/strings/string_view.h"
22#include "absl/time/clock.h"
24
25#if !defined(__PORTABLE_PLATFORM__)
26#if !defined(_MSC_VER)
27#include <unistd.h>
28#endif
29#endif // !defined(__PORTABLE_PLATFORM__)
30
31namespace file {
32
33::absl::StatusOr<std::string> MakeTempFilename(absl::string_view directory,
34 absl::string_view file_prefix) {
35 std::string filename;
36#if defined(__PORTABLE_PLATFORM__)
37 filename = "Temporary files are not implemented for this platform.";
38 LOG(ERROR) << filename;
39 return absl::UnavailableError(filename);
40#endif // !defined(__PORTABLE_PLATFORM__)
41
42#if defined(__linux__)
43 int32_t tid = static_cast<int32_t>(pthread_self());
44#else // defined(__linux__)
45 int32_t tid = 123;
46#endif // defined(__linux__)
47#if !defined(_MSC_VER)
48 int32_t pid = static_cast<int32_t>(getpid());
49#else // _MSC_VER
50 int32_t pid = 456;
51#endif // _MSC_VER
52 int64_t now = absl::GetCurrentTimeNanos();
53
54 if (directory.empty()) {
55 directory = "/tmp";
56 }
57
58 if (file_prefix.empty()) {
59 file_prefix = "tempfile";
60 }
61
62 filename = absl::StrFormat("%s/%s-%x-%d-%llx", directory, file_prefix, tid,
63 pid, now);
64 return filename;
65}
66
67} // namespace file
Definition file.cc:327
::absl::StatusOr< std::string > MakeTempFilename(absl::string_view directory, absl::string_view file_prefix)
Definition temp_file.cc:33