xref: /openbmc/estoraged/include/filesystemInterface.hpp (revision 2443a0217d6ffe4cec3226992a49b45d37bbb53d)
1b810c926SJohn Wedig #pragma once
2b810c926SJohn Wedig 
3b810c926SJohn Wedig #include <sys/mount.h>
4b810c926SJohn Wedig 
5b810c926SJohn Wedig #include <filesystem>
6b810c926SJohn Wedig #include <string>
7b810c926SJohn Wedig 
8b810c926SJohn Wedig namespace estoraged
9b810c926SJohn Wedig {
10b810c926SJohn Wedig 
11b810c926SJohn Wedig /** @class FilesystemInterface
12b810c926SJohn Wedig  *  @brief Interface to the filesystem operations that eStoraged needs.
13b810c926SJohn Wedig  *  @details This class is used to mock out the filesystem operations.
14b810c926SJohn Wedig  */
15b810c926SJohn Wedig class FilesystemInterface
16b810c926SJohn Wedig {
17b810c926SJohn Wedig   public:
18b810c926SJohn Wedig     virtual ~FilesystemInterface() = default;
19b810c926SJohn Wedig 
2082897c35SEd Tanous     FilesystemInterface() = default;
2182897c35SEd Tanous     FilesystemInterface(const FilesystemInterface&) = delete;
2282897c35SEd Tanous     FilesystemInterface& operator=(const FilesystemInterface&) = delete;
2382897c35SEd Tanous 
2482897c35SEd Tanous     FilesystemInterface(FilesystemInterface&&) = delete;
2582897c35SEd Tanous     FilesystemInterface& operator=(FilesystemInterface&&) = delete;
2682897c35SEd Tanous 
27b810c926SJohn Wedig     /** @brief Runs the mkfs command to create the filesystem.
28b810c926SJohn Wedig      *  @details Used for mocking purposes.
29b810c926SJohn Wedig      *
30*2443a021SJohn Wedig      *  @param[in] logicalVolumePath - path to the mapped LUKS device.
31b810c926SJohn Wedig      *
32b810c926SJohn Wedig      *  @returns 0 on success, nonzero on failure.
33b810c926SJohn Wedig      */
34*2443a021SJohn Wedig     virtual int runMkfs(const std::string& logicalVolumePath) = 0;
35b810c926SJohn Wedig 
36b810c926SJohn Wedig     /** @brief Wrapper around mount().
37b810c926SJohn Wedig      *  @details Used for mocking purposes.
38b810c926SJohn Wedig      *
39b810c926SJohn Wedig      *  @param[in] source - device where the filesystem is located.
40b810c926SJohn Wedig      *  @param[in] target - path to where the filesystem should be mounted.
41b810c926SJohn Wedig      *  @param[in] filesystemType - (e.g. "ext4").
42b810c926SJohn Wedig      *  @param[in] mountflags - flags bit mask (see mount() documentation).
43b810c926SJohn Wedig      *  @param[in] data - options for specific filesystem type, can be NULL
44b810c926SJohn Wedig      *    (see mount() documentation).
45b810c926SJohn Wedig      *
46b810c926SJohn Wedig      *  @returns On success, zero is returned.  On error, -1 is returned, and
47b810c926SJohn Wedig      *    errno is set to indicate the error.
48b810c926SJohn Wedig      */
49b810c926SJohn Wedig     virtual int doMount(const char* source, const char* target,
50b810c926SJohn Wedig                         const char* filesystemtype, unsigned long mountflags,
51b810c926SJohn Wedig                         const void* data) = 0;
52b810c926SJohn Wedig 
53b810c926SJohn Wedig     /** @brief Wrapper around umount().
54b810c926SJohn Wedig      *  @details Used for mocking purposes.
55b810c926SJohn Wedig      *
56b810c926SJohn Wedig      *  @param[in] target - path location where the filesystem is mounted.
57b810c926SJohn Wedig      *
58b810c926SJohn Wedig      *  @returns On success, zero is returned.  On error, -1 is returned, and
59b810c926SJohn Wedig      *    errno is set to indicate the error.
60b810c926SJohn Wedig      */
61b810c926SJohn Wedig     virtual int doUnmount(const char* target) = 0;
62b810c926SJohn Wedig 
63b810c926SJohn Wedig     /** @brief Wrapper around std::filesystem::create_directory.
64b810c926SJohn Wedig      *  @details Used for mocking purposes.
65b810c926SJohn Wedig      *
66b810c926SJohn Wedig      *  @param[in] p - path to directory that should be created.
67b810c926SJohn Wedig      *
68b810c926SJohn Wedig      *  @returns true on success, false otherwise.
69b810c926SJohn Wedig      */
70b810c926SJohn Wedig     virtual bool createDirectory(const std::filesystem::path& p) = 0;
71b810c926SJohn Wedig 
72b810c926SJohn Wedig     /** @brief Wrapper around std::filesystem::remove.
73b810c926SJohn Wedig      *  @details Used for mocking purposes.
74b810c926SJohn Wedig      *
75b810c926SJohn Wedig      *  @param[in] p - path to directory that should be removed.
76b810c926SJohn Wedig      *
77b810c926SJohn Wedig      *  @returns true on success, false otherwise.
78b810c926SJohn Wedig      */
79b810c926SJohn Wedig     virtual bool removeDirectory(const std::filesystem::path& p) = 0;
80b17f8251SJohn Wedig 
81b17f8251SJohn Wedig     /** @brief Wrapper around std::filesystem::is_directory
82b17f8251SJohn Wedig      *
83b17f8251SJohn Wedig      *  @param[in] p - path to directory that we want to query.
84b17f8251SJohn Wedig      *
85b17f8251SJohn Wedig      *  @returns true if the path exists and represents a directory.
86b17f8251SJohn Wedig      */
87b17f8251SJohn Wedig     virtual bool directoryExists(const std::filesystem::path& p) = 0;
88b810c926SJohn Wedig };
89b810c926SJohn Wedig 
90b810c926SJohn Wedig /** @class Filesystem
91b810c926SJohn Wedig  *  @brief Implements FilesystemInterface
92b810c926SJohn Wedig  */
93b810c926SJohn Wedig class Filesystem : public FilesystemInterface
94b810c926SJohn Wedig {
95b810c926SJohn Wedig   public:
9682897c35SEd Tanous     ~Filesystem() override = default;
9782897c35SEd Tanous     Filesystem() = default;
9882897c35SEd Tanous     Filesystem(const Filesystem&) = delete;
9982897c35SEd Tanous     Filesystem& operator=(const Filesystem&) = delete;
10082897c35SEd Tanous 
10182897c35SEd Tanous     Filesystem(Filesystem&&) = delete;
10282897c35SEd Tanous     Filesystem& operator=(Filesystem&&) = delete;
103b810c926SJohn Wedig 
runMkfs(const std::string & logicalVolumePath)104*2443a021SJohn Wedig     int runMkfs(const std::string& logicalVolumePath) override
105b810c926SJohn Wedig     {
106*2443a021SJohn Wedig         std::string mkfsCommand("mkfs.ext4 " + logicalVolumePath);
10782897c35SEd Tanous         // calling 'system' uses a command processor //NOLINTNEXTLINE
108b810c926SJohn Wedig         return system(mkfsCommand.c_str());
109b810c926SJohn Wedig     }
110b810c926SJohn Wedig 
doMount(const char * source,const char * target,const char * filesystemtype,unsigned long mountflags,const void * data)111b810c926SJohn Wedig     int doMount(const char* source, const char* target,
112b810c926SJohn Wedig                 const char* filesystemtype, unsigned long mountflags,
113b810c926SJohn Wedig                 const void* data) override
114b810c926SJohn Wedig     {
115b810c926SJohn Wedig         return mount(source, target, filesystemtype, mountflags, data);
116b810c926SJohn Wedig     }
117b810c926SJohn Wedig 
doUnmount(const char * target)118b810c926SJohn Wedig     int doUnmount(const char* target) override
119b810c926SJohn Wedig     {
120b810c926SJohn Wedig         return umount(target);
121b810c926SJohn Wedig     }
122b810c926SJohn Wedig 
createDirectory(const std::filesystem::path & p)123b810c926SJohn Wedig     bool createDirectory(const std::filesystem::path& p) override
124b810c926SJohn Wedig     {
125b810c926SJohn Wedig         return std::filesystem::create_directory(p);
126b810c926SJohn Wedig     }
127b810c926SJohn Wedig 
removeDirectory(const std::filesystem::path & p)128b810c926SJohn Wedig     bool removeDirectory(const std::filesystem::path& p) override
129b810c926SJohn Wedig     {
130b810c926SJohn Wedig         return std::filesystem::remove(p);
131b810c926SJohn Wedig     }
132b17f8251SJohn Wedig 
directoryExists(const std::filesystem::path & p)133b17f8251SJohn Wedig     bool directoryExists(const std::filesystem::path& p) override
134b17f8251SJohn Wedig     {
135b17f8251SJohn Wedig         return std::filesystem::is_directory(std::filesystem::status(p));
136b17f8251SJohn Wedig     }
137b810c926SJohn Wedig };
138b810c926SJohn Wedig } // namespace estoraged
139