-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
temp_path.cc
82 lines (68 loc) · 2.2 KB
/
temp_path.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2010-2024 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_path.h"
#include <utility>
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "absl/time/time.h"
#include "ortools/base/filesystem.h"
namespace file {
std::string TempFile(absl::string_view prefix) {
std::string path;
if (prefix.empty()) {
path = absl::StrCat(absl::ToUnixMicros(absl::Now()));
} else {
path = absl::StrCat(prefix, "_", absl::ToUnixMicros(absl::Now()));
}
return path;
}
} // namespace file
TempPath::TempPath(absl::string_view prefix) : path_(file::TempFile(prefix)) {
CHECK_OK(Init(kDefaultMode));
}
TempPath::TempPath(absl::string_view prefix, absl::Status* status)
: path_(file::TempFile(prefix)) {
*status = Init(kDefaultMode);
}
TempPath::TempPath(TempPath && rhs) : path_(std::move(rhs.path_)) {}
TempPath& TempPath::operator=(TempPath&& rhs) {
TempPath tmp(std::move(*this));
path_ = std::move(rhs.path_);
return *this;
}
TempPath::~TempPath() {}
TempPath* TempPath::Create(Location location) {
std::string dirname;
switch (location) {
case Local:
dirname = file::TempFile("");
}
if (dirname.empty()) {
return nullptr;
}
absl::Status status;
TempPath* temp_path = new TempPath(dirname, &status);
if (!status.ok()) {
delete temp_path;
return nullptr;
}
return temp_path;
}
TempPath::TempPath(const std::string& dirname, file::Options options,
absl::Status* status)
: path_(dirname) {
*status = Init(options);
}
absl::Status TempPath::Init(file::Options options) {
return file::RecursivelyCreateDir(path(), options);
}