OR-Tools  9.0
port/file.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 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 
14 #include "ortools/port/file.h"
15 
16 #include "absl/status/status.h"
17 #include "ortools/base/logging.h"
18 
19 #if !defined(__PORTABLE_PLATFORM__)
20 #if !defined(_MSC_VER)
21 #include <unistd.h>
22 #endif
23 
24 #include "absl/strings/str_format.h"
25 #include "absl/time/clock.h"
26 #include "ortools/base/file.h"
27 #endif // !defined(__PORTABLE_PLATFORM__)
28 
29 namespace operations_research {
30 
31 ::absl::Status PortableFileSetContents(absl::string_view file_name,
32  absl::string_view content) {
33 #if defined(__PORTABLE_PLATFORM__)
34  return absl::Status(absl::StatusCode::kUnimplemented,
35  "File io is not implemented for this platform.");
36 #else // defined(__PORTABLE_PLATFORM__)
37  return file::SetContents(file_name, content, file::Defaults());
38 #endif // !defined(__PORTABLE_PLATFORM__)
39 }
40 
41 ::absl::Status PortableFileGetContents(absl::string_view file_name,
42  std::string* output) {
43 #if defined(__PORTABLE_PLATFORM__)
44  return absl::Status(absl::StatusCode::kUnimplemented,
45  "File io is not implemented for this platform.");
46 #else // defined(__PORTABLE_PLATFORM__)
47  return file::GetContents(file_name, output, file::Defaults());
48 #endif // !defined(__PORTABLE_PLATFORM__)
49 }
50 
51 bool PortableTemporaryFile(const char* directory_prefix,
52  std::string* filename_out) {
53 #if defined(__PORTABLE_PLATFORM__)
54  LOG(ERROR) << "Temporary files are not implemented for this platform.";
55  return false;
56 #else // defined(__PORTABLE_PLATFORM__)
57 #if defined(__linux)
58  int32_t tid = static_cast<int32_t>(pthread_self());
59 #else // defined(__linux__)
60  int32_t tid = 123;
61 #endif // defined(__linux__)
62 #if !defined(_MSC_VER)
63  int32_t pid = static_cast<int32_t>(getpid());
64 #else // _MSC_VER
65  int32_t pid = 456;
66 #endif // _MSC_VER
67  int64_t now = absl::GetCurrentTimeNanos();
68  std::string filename =
69  absl::StrFormat("/tmp/parameters-tempfile-%x-%d-%llx", tid, pid, now);
70  return true;
71 #endif // !defined(__PORTABLE_PLATFORM__)
72 }
73 
74 ::absl::Status PortableDeleteFile(absl::string_view file_name) {
75 #if defined(__PORTABLE_PLATFORM__)
76  return absl::Status(absl::StatusCode::kUnimplemented,
77  "File io is not implemented for this platform.");
78 #else // defined(__PORTABLE_PLATFORM__)
79  return file::Delete(file_name, file::Defaults());
80 #endif // !defined(__PORTABLE_PLATFORM__)
81 }
82 } // namespace operations_research
#define LOG(severity)
Definition: base/logging.h:423
const int ERROR
Definition: log_severity.h:32
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: base/file.cc:163
int Defaults()
Definition: base/file.h:119
absl::Status Delete(const absl::string_view &path, int flags)
Definition: base/file.cc:305
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: base/file.cc:196
Collection of objects used to extend the Constraint Solver library.
::absl::Status PortableDeleteFile(absl::string_view file_name)
Definition: port/file.cc:74
bool PortableTemporaryFile(const char *directory_prefix, std::string *filename_out)
Definition: port/file.cc:51
::absl::Status PortableFileSetContents(absl::string_view file_name, absl::string_view content)
Definition: port/file.cc:31
::absl::Status PortableFileGetContents(absl::string_view file_name, std::string *output)
Definition: port/file.cc:41