ArmNN
 24.02
armnnUtils::Filesystem Namespace Reference

Typedefs

using FileContents = std::string
 

Functions

fs::path NamedTempFile (const char *fileName)
 Returns a path to a file in the system temporary folder. If the file existed it will be deleted. More...
 
std::string CreateDirectory (std::string sPath)
 Returns full path to temporary folder. More...
 
FileContents ReadFileContentsIntoString (const std::string &path)
 
void RemoveDirectoryAndContents (const std::string &path)
 Remove a directory and its contents. More...
 

Typedef Documentation

◆ FileContents

using FileContents = std::string

Definition at line 24 of file Filesystem.hpp.

Function Documentation

◆ CreateDirectory()

std::string CreateDirectory ( std::string  path)

Returns full path to temporary folder.

Construct a temporary directory.

Given a specified directory name construct a path in the system temporary directory. If the directory already exists, it is deleted, otherwise create it. This could throw filesystem_error exceptions.

Parameters
pathis the path required in the temporary directory.
Returns
path consisting of system temporary directory.
Exceptions
RuntimeExceptionif the directory cannot be created or exists but cannot be removed.

Definition at line 47 of file Filesystem.cpp.

48 {
49  // This line is very unlikely to throw an exception.
50  fs::path tmpDir = fs::temp_directory_path();
51  std::string full_path = tmpDir.generic_string() + path;
52  // This could throw a file permission exception.
53  RemoveDirectoryAndContents(full_path);
54 #if defined(_WIN32)
55  result = _mkdir(full_path.c_str()); // can be used on Windows
56  armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory");
57 #else
58  try
59  {
60  if(!fs::create_directory(full_path))
61  {
62  throw armnn::RuntimeException("Unable to create directory: " + full_path);
63  }
64  }
65  catch (const std::system_error& e)
66  {
67  std::string error = "Unable to create directory. Reason: ";
68  error.append(e.what());
69  throw armnn::RuntimeException(error);
70  }
71 #endif
72 
73  return full_path + "/";
74 }

References armnn::error, and RemoveDirectoryAndContents().

Referenced by armnn::Optimize().

◆ NamedTempFile()

fs::path NamedTempFile ( const char *  fileName)

Returns a path to a file in the system temporary folder. If the file existed it will be deleted.

Construct a temporary file name.

Given a specified file name construct a path to that file in the system temporary directory. If the file already exists it is deleted. This could throw filesystem_error exceptions.

Parameters
fileNamethe file name required in the temporary directory.
Returns
path consisting of system temporary directory and file name.

Definition at line 25 of file Filesystem.cpp.

26 {
27  fs::path tmpDir = fs::temp_directory_path();
28  fs::path namedTempFile{tmpDir / fileName};
29  if (fs::exists(namedTempFile))
30  {
31  fs::remove(namedTempFile);
32  }
33  return namedTempFile;
34 }

Referenced by TEST_SUITE().

◆ ReadFileContentsIntoString()

FileContents ReadFileContentsIntoString ( const std::string &  path)

Definition at line 102 of file Filesystem.cpp.

102  {
103  if (!fs::exists(path))
104  {
105  throw armnn::RuntimeException("Path does not exist: " + path);
106  }
107  std::ifstream input_file(path);
108  armnn::ConditionalThrow<armnn::RuntimeException>((input_file.is_open()), "Could not read file contents");
109  return FileContents((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
110 }

◆ RemoveDirectoryAndContents()

void RemoveDirectoryAndContents ( const std::string &  path)

Remove a directory and its contents.

Given a directory path delete it's contents and the directory. If the specified directory doesn't exist this does nothing. If any item cannot be removed this will throw a RuntimeException.

Parameters
full_path

Definition at line 84 of file Filesystem.cpp.

85 {
86  if (fs::exists(path))
87  {
88  try
89  {
90  // This could throw an exception on a multi-user system.
91  fs::remove_all(path);
92  }
93  catch (const std::system_error& e)
94  {
95  std::string error = "Directory exists and cannot be removed. Reason: ";
96  error.append(e.what());
97  throw armnn::RuntimeException(error);
98  }
99  }
100 }

References armnn::error.

Referenced by CreateDirectory().

armnn::BoostLogSeverityMapping::error
@ error
armnnUtils::Filesystem::FileContents
std::string FileContents
Definition: Filesystem.hpp:24
armnn::RuntimeException
Definition: Exceptions.hpp:120
armnnUtils::Filesystem::RemoveDirectoryAndContents
void RemoveDirectoryAndContents(const std::string &path)
Remove a directory and its contents.
Definition: Filesystem.cpp:84