Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
temp_path.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <utility>
17
18#include "absl/log/check.h"
19#include "absl/strings/str_cat.h"
20#include "absl/time/time.h"
22
23namespace file {
24
25std::string TempFile(absl::string_view prefix) {
26 std::string path;
27 if (prefix.empty()) {
28 path = absl::StrCat(absl::ToUnixMicros(absl::Now()));
29 } else {
30 path = absl::StrCat(prefix, "_", absl::ToUnixMicros(absl::Now()));
31 }
32 return path;
33}
34
35} // namespace file
36
37TempPath::TempPath(absl::string_view prefix) : path_(file::TempFile(prefix)) {
38 CHECK_OK(Init(kDefaultMode));
39}
40
41TempPath::TempPath(absl::string_view prefix, absl::Status* status)
42 : path_(file::TempFile(prefix)) {
43 *status = Init(kDefaultMode);
44}
45
46 TempPath::TempPath(TempPath && rhs) : path_(std::move(rhs.path_)) {}
47
49 TempPath tmp(std::move(*this));
50 path_ = std::move(rhs.path_);
51 return *this;
52 }
53
55
57 std::string dirname;
58 switch (location) {
59 case Local:
60 dirname = file::TempFile("");
61 }
62 if (dirname.empty()) {
63 return nullptr;
64 }
65 absl::Status status;
66 TempPath* temp_path = new TempPath(dirname, &status);
67 if (!status.ok()) {
68 delete temp_path;
69 return nullptr;
70 }
71 return temp_path;
72 }
73
74 TempPath::TempPath(const std::string& dirname, file::Options options,
75 absl::Status* status)
76 : path_(dirname) {
77 *status = Init(options);
78 }
79
80 absl::Status TempPath::Init(file::Options options) {
81 return file::RecursivelyCreateDir(path(), options);
82 }
static TempPath * Create(Location location)
Definition temp_path.cc:56
static constexpr int kDefaultMode
default mode to create directories (a+rwx):
Definition temp_path.h:26
TempPath(absl::string_view prefix)
Definition temp_path.cc:37
std::string path() const
Returns the path which was created by this object.
Definition temp_path.h:40
TempPath & operator=(TempPath &&rhs)
Definition temp_path.cc:48
absl::Status status
Definition g_gurobi.cc:44
Definition file.cc:169
int Options
Definition file.h:107
absl::Status RecursivelyCreateDir(std::string_view path, const file::Options &options)
Definition filesystem.cc:74
std::string TempFile(absl::string_view prefix)
Definition temp_path.cc:25
STL namespace.