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