Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 2 | // Copyright © 2020,2023 Arm Ltd and Contributors. All rights reserved. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 5 | #if !defined(ARMNN_DISABLE_FILESYSTEM) |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 6 | |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 7 | #include <armnn/Exceptions.hpp> |
Rob Hughes | 9542f90 | 2021-07-14 09:48:54 +0100 | [diff] [blame] | 8 | #include <armnnUtils/Filesystem.hpp> |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 9 | |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 10 | namespace armnnUtils |
| 11 | { |
| 12 | namespace Filesystem |
| 13 | { |
| 14 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 15 | /** |
| 16 | * @brief Construct a temporary file name. |
| 17 | * |
| 18 | * Given a specified file name construct a path to that file in the |
| 19 | * system temporary directory. If the file already exists it is deleted. This |
| 20 | * could throw filesystem_error exceptions. |
| 21 | * |
| 22 | * @param fileName the file name required in the temporary directory. |
| 23 | * @return path consisting of system temporary directory and file name. |
| 24 | */ |
| 25 | fs::path NamedTempFile(const char* fileName) |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 26 | { |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 27 | fs::path tmpDir = fs::temp_directory_path(); |
| 28 | fs::path namedTempFile{tmpDir / fileName}; |
| 29 | if (fs::exists(namedTempFile)) |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 30 | { |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 31 | fs::remove(namedTempFile); |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 32 | } |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 33 | return namedTempFile; |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 36 | /** |
| 37 | * @brief Construct a temporary directory |
| 38 | * |
| 39 | * Given a specified directory name construct a path in the |
| 40 | * system temporary directory. If the directory already exists, it is deleted, |
| 41 | * otherwise create it. This could throw filesystem_error exceptions. |
| 42 | * |
| 43 | * @param path is the path required in the temporary directory. |
| 44 | * @return path consisting of system temporary directory. |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 45 | * @throws RuntimeException if the directory cannot be created or exists but cannot be removed. |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 46 | */ |
| 47 | std::string CreateDirectory(std::string path) |
| 48 | { |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 49 | // This line is very unlikely to throw an exception. |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 50 | fs::path tmpDir = fs::temp_directory_path(); |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 51 | std::string full_path = tmpDir.generic_string() + path; |
Colm Donelan | b5ea589 | 2023-02-10 15:19:46 +0000 | [diff] [blame] | 52 | // This could throw a file permission exception. |
| 53 | RemoveDirectoryAndContents(full_path); |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 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 |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 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 | } |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | return full_path + "/"; |
| 74 | } |
| 75 | |
Colm Donelan | b5ea589 | 2023-02-10 15:19:46 +0000 | [diff] [blame] | 76 | /** |
| 77 | * @brief Remove a directory and its contents. |
| 78 | * |
| 79 | * Given a directory path delete it's contents and the directory. If the specified directory doesn't exist this |
| 80 | * does nothing. If any item cannot be removed this will throw a RuntimeException. |
| 81 | * |
| 82 | * @param full_path |
| 83 | */ |
| 84 | void RemoveDirectoryAndContents(const std::string& path) |
| 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 | } |
| 101 | |
| 102 | FileContents ReadFileContentsIntoString(const std::string& path) { |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 103 | if (!fs::exists(path)) |
| 104 | { |
| 105 | throw armnn::RuntimeException("Path does not exist: " + path); |
| 106 | } |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 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 | } |
| 111 | |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 112 | } // namespace armnnUtils |
| 113 | } // namespace Filesystem |
| 114 | |
| 115 | #endif // !defined(ARMNN_DISABLE_FILESYSTEM) |