diff options
author | Colm Donelan <colm.donelan@arm.com> | 2023-01-16 16:45:08 +0000 |
---|---|---|
committer | Colm Donelan <colm.donelan@arm.com> | 2023-01-18 11:45:06 +0000 |
commit | e27983ccfc9f73e2ec69863dcc5d9812fba7f5ef (patch) | |
tree | 48da418fa5adad68ef95a486cfb76f7b93bd9de6 /src/armnnUtils | |
parent | 23a6d00339f2310dd2973e47d95b9c0d7b8d9bde (diff) | |
download | armnn-e27983ccfc9f73e2ec69863dcc5d9812fba7f5ef.tar.gz |
IVGCVSW-7405 Improving error handling around creating directories.
The -F execute network option creates a directory to print intermediate
tensors but minor problems caused serious failures. This attempts
to clean up the error handling.
Signed-off-by: Colm Donelan <colm.donelan@arm.com>
Change-Id: Ia44c008919b1bee299b43a672235b1fcc25bf1bd
Diffstat (limited to 'src/armnnUtils')
-rw-r--r-- | src/armnnUtils/Filesystem.cpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/armnnUtils/Filesystem.cpp b/src/armnnUtils/Filesystem.cpp index 1577d2d07c..78f928a874 100644 --- a/src/armnnUtils/Filesystem.cpp +++ b/src/armnnUtils/Filesystem.cpp @@ -1,11 +1,11 @@ // -// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// Copyright © 2020,2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #if !defined(ARMNN_DISABLE_FILESYSTEM) +#include <armnn/Exceptions.hpp> #include <armnnUtils/Filesystem.hpp> -#include "armnn/Exceptions.hpp" namespace armnnUtils { @@ -42,31 +42,54 @@ fs::path NamedTempFile(const char* fileName) * * @param path is the path required in the temporary directory. * @return path consisting of system temporary directory. + * @throws RuntimeException if the directory cannot be created or exists but cannot be removed. */ std::string CreateDirectory(std::string path) { + // This line is very unlikely to throw an exception. fs::path tmpDir = fs::temp_directory_path(); - mode_t permissions = 0733; - int result = 0; - std::string full_path = tmpDir.generic_string() + path; if (fs::exists(full_path)) { - fs::remove_all(full_path); + try + { + // This could throw an exception on a multi-user system. + fs::remove_all(full_path); + } + catch (const std::system_error& e) + { + std::string error = "Directory exists and cannot be removed. Reason: "; + error.append(e.what()); + throw armnn::RuntimeException(error); + } } - #if defined(_WIN32) result = _mkdir(full_path.c_str()); // can be used on Windows armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory"); #else - result = mkdir(full_path.c_str(), permissions); - armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory"); + try + { + if(!fs::create_directory(full_path)) + { + throw armnn::RuntimeException("Unable to create directory: " + full_path); + } + } + catch (const std::system_error& e) + { + std::string error = "Unable to create directory. Reason: "; + error.append(e.what()); + throw armnn::RuntimeException(error); + } #endif return full_path + "/"; } FileContents ReadFileContentsIntoString(const std::string path) { + if (!fs::exists(path)) + { + throw armnn::RuntimeException("Path does not exist: " + path); + } std::ifstream input_file(path); armnn::ConditionalThrow<armnn::RuntimeException>((input_file.is_open()), "Could not read file contents"); return FileContents((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>()); |