Turi Create  4.0
file_ownership_handle.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_FILEIO_FILE_OWNERSHIP_HANDLE_HPP
7 #define TURI_FILEIO_FILE_OWNERSHIP_HANDLE_HPP
8 #include <vector>
9 #include <string>
10 #include <core/logging/logger.hpp>
11 #include <core/storage/fileio/fs_utils.hpp>
12 namespace turi {
13 namespace fileio {
14 /**
15  * \ingroup fileio
16  * A simple RAII class which manages the lifespan of one file.
17  * On destruction, this file is deleted if marked for deletion.
18  */
20  file_ownership_handle() = default;
21  // deleted copy constructor
23  // deleted assignment operator
24  file_ownership_handle& operator=(const file_ownership_handle&) = delete;
25 
26  /// move constructor
28  m_file = other.m_file;
29  other.m_file = std::string();
30  m_delete_on_destruction = other.m_delete_on_destruction;
31  m_recursive_deletion = other.m_recursive_deletion;
32  }
33 
34  /// move assignment
36  m_file = other.m_file;
37  other.m_file = std::string();
38  m_delete_on_destruction = other.m_delete_on_destruction;
39  m_recursive_deletion = other.m_recursive_deletion;
40  return (*this);
41  }
42 
43  /// construct from one file
44  inline file_ownership_handle(const std::string& file,
45  bool delete_on_destruction = true,
46  bool recursive_deletion = false) {
47  m_file = file;
48  this->m_delete_on_destruction = delete_on_destruction;
49  this->m_recursive_deletion = recursive_deletion;
50  }
51 
52  void delete_on_destruction() {
53  m_delete_on_destruction = true;
54  }
55 
56  void do_not_delete_on_destruction() {
57  m_delete_on_destruction = false;
58  }
59 
60  /// Destructor deletes the owned file if delete_on_destruction is true
62  if (m_delete_on_destruction) {
63  try {
64  if (!m_file.empty()) {
65  if (m_recursive_deletion) {
66  logstream(LOG_DEBUG) << "deleting directory " << m_file << std::endl;
68  } else {
69  logstream(LOG_DEBUG) << "deleting file " << m_file << std::endl;
71  }
72  }
73  } catch (...) {
74  logstream(LOG_ERROR) << "Exception on attempted deletion of " << m_file << std::endl;
75  }
76  }
77  }
78 
79  std::string m_file;
80  bool m_delete_on_destruction = false;
81  bool m_recursive_deletion = false;
82 }; // file_ownership_handle
83 } // namespace fileio
84 } // namespace turi
85 #endif
#define logstream(lvl)
Definition: logger.hpp:276
file_ownership_handle(file_ownership_handle &&other)
move constructor
bool delete_path_recursive(const std::string &path)
bool delete_path_impl(const std::string &path, file_status status=file_status::FS_UNAVAILABLE)
file_ownership_handle(const std::string &file, bool delete_on_destruction=true, bool recursive_deletion=false)
construct from one file
#define LOG_DEBUG
Definition: logger.hpp:102
~file_ownership_handle()
Destructor deletes the owned file if delete_on_destruction is true.
#define LOG_ERROR
Definition: logger.hpp:97
file_ownership_handle & operator=(file_ownership_handle &&other)
move assignment