farm-ng-core
filesystem.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
18 
19 #include <unistd.h>
20 
21 #include <filesystem>
22 #include <optional>
23 
24 namespace farm_ng {
25 
26 /// Given a `target_path` name "foo", this function will create a version folder
27 /// "foo.xy".
28 ///
29 /// First, it attempt to create folder "foo.00".
30 /// If the path "foo.00" already exits, it attempts to create "foo.01", etc.
31 ///
32 /// Precondition: `parent_path` of the versioned folder must exist
33 ///
34 /// Returns nullopt if for unexpected reason creation of the folder fails.
35 /// Otherwise, it returns the path of the created directory.
36 inline std::optional<std::filesystem::path> createVersionedFolder(
37  std::filesystem::path const& target_path) {
38  /* the following removes trailing '/', only if it is there. */
39  std::filesystem::path clean_path =
40  (std::filesystem::path(target_path) / "foo").parent_path();
41 
42  if (!std::filesystem::exists(clean_path.parent_path())) {
43  return std::nullopt;
44  }
45 
46  int count = 0;
47  std::filesystem::path path_attempt;
48  do {
49  path_attempt = FARM_FORMAT("{}.{:02d}", clean_path.string(), count++);
50  } while (std::filesystem::exists(path_attempt));
51  if (!std::filesystem::create_directory(path_attempt)) {
52  return std::nullopt;
53  }
54  return path_attempt;
55 }
56 
57 /// Creates a unique temporary directory with the prefix "/tmp/farm-ng-"
58 ///
59 /// Returns nullopt if for unexpected reason creation of the folder fails.
60 /// Otherwise, it returns the path of the created directory.
61 inline std::optional<std::filesystem::path> createUniqueTemporaryDirectory() {
62  char template_str[] = "/tmp/farm-ng-XXXXXX";
63  char* directory_str = mkdtemp(template_str);
64  if (directory_str == nullptr) {
65  return std::nullopt;
66  }
67  return directory_str;
68 }
69 
70 } // namespace farm_ng
FARM_FORMAT
#define FARM_FORMAT(...)
Formats the cstr using the libfmt library.
Definition: format.h:125
farm_ng
Definition: backtrace.cpp:102
farm_ng::createVersionedFolder
std::optional< std::filesystem::path > createVersionedFolder(std::filesystem::path const &target_path)
Given a target_path name "foo", this function will create a version folder "foo.xy".
Definition: filesystem.h:36
format.h
farm_ng::createUniqueTemporaryDirectory
std::optional< std::filesystem::path > createUniqueTemporaryDirectory()
Creates a unique temporary directory with the prefix "/tmp/farm-ng-".
Definition: filesystem.h:61
sophus::count
auto count(ImageViewBool mask, bool truth_value) -> int
Returns number of pixels equal truth_value in mask.
Definition: image_types.cpp:13