blob: 1577d2d07c7e94905c0ceb42dcd920796e4757ea [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Jim Flynn6da6a452020-07-14 14:26:27 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Rob Hughesbdee4262020-01-07 17:05:24 +00003// SPDX-License-Identifier: MIT
4//
Jim Flynn870b96c2022-03-25 21:24:56 +00005#if !defined(ARMNN_DISABLE_FILESYSTEM)
Rob Hughesbdee4262020-01-07 17:05:24 +00006
Rob Hughes9542f902021-07-14 09:48:54 +01007#include <armnnUtils/Filesystem.hpp>
Keith Davis15f9c682022-10-14 15:50:33 +01008#include "armnn/Exceptions.hpp"
Rob Hughesbdee4262020-01-07 17:05:24 +00009
Rob Hughesbdee4262020-01-07 17:05:24 +000010namespace armnnUtils
11{
12namespace Filesystem
13{
14
Francis Murtagh532a29d2020-06-29 11:50:01 +010015/**
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 */
25fs::path NamedTempFile(const char* fileName)
Rob Hughesbdee4262020-01-07 17:05:24 +000026{
Francis Murtagh532a29d2020-06-29 11:50:01 +010027 fs::path tmpDir = fs::temp_directory_path();
28 fs::path namedTempFile{tmpDir / fileName};
29 if (fs::exists(namedTempFile))
Rob Hughesbdee4262020-01-07 17:05:24 +000030 {
Francis Murtagh532a29d2020-06-29 11:50:01 +010031 fs::remove(namedTempFile);
Rob Hughesbdee4262020-01-07 17:05:24 +000032 }
Francis Murtagh532a29d2020-06-29 11:50:01 +010033 return namedTempFile;
Rob Hughesbdee4262020-01-07 17:05:24 +000034}
35
Keith Davis15f9c682022-10-14 15:50:33 +010036/**
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.
45 */
46std::string CreateDirectory(std::string path)
47{
48 fs::path tmpDir = fs::temp_directory_path();
49 mode_t permissions = 0733;
50 int result = 0;
51
52 std::string full_path = tmpDir.generic_string() + path;
53 if (fs::exists(full_path))
54 {
55 fs::remove_all(full_path);
56 }
57
58#if defined(_WIN32)
59 result = _mkdir(full_path.c_str()); // can be used on Windows
60 armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory");
61#else
62 result = mkdir(full_path.c_str(), permissions);
63 armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory");
64#endif
65
66 return full_path + "/";
67}
68
69FileContents ReadFileContentsIntoString(const std::string path) {
70 std::ifstream input_file(path);
71 armnn::ConditionalThrow<armnn::RuntimeException>((input_file.is_open()), "Could not read file contents");
72 return FileContents((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
73}
74
Jim Flynn870b96c2022-03-25 21:24:56 +000075} // namespace armnnUtils
76} // namespace Filesystem
77
78#endif // !defined(ARMNN_DISABLE_FILESYSTEM)