Home | History | Annotate | Download | only in ext4_utils
      1 #include <string>
      2 #include <fstream>
      3 
      4 // key names for properties we use
      5 namespace properties {
      6     extern const char* key;
      7     extern const char* ref;
      8     extern const char* props;
      9     extern const char* is_default;
     10 }
     11 
     12 /**
     13  * Class to store data on the unencrypted folder of a device.
     14  * Note that the folder must exist before this class is constructed.
     15  * All names must be valid single level (no '/') file or directory names
     16  * Data is organized hierarchically so we can get a child folder
     17  */
     18 class UnencryptedProperties
     19 {
     20 public:
     21     // Get path of folder. Must create before using any properties
     22     // This is to allow proper setting of SELinux policy
     23     static std::string GetPath(const char* device);
     24 
     25     // Opens properties folder on named device.
     26     // If folder does not exist, OK will return false, all
     27     // getters will return default properties and setters will fail.
     28     UnencryptedProperties(const char* device);
     29 
     30     // Get named object. Return default if object does not exist or error.
     31     template<typename t> t Get(const char* name, t default_value = t()) const;
     32 
     33     // Set named object. Return true if success, false otherwise
     34     template<typename t> bool Set(const char* name, t const& value);
     35 
     36     // Get child properties
     37     UnencryptedProperties GetChild(const char* name) const;
     38 
     39     // Remove named object
     40     bool Remove(const char* name);
     41 
     42     // Does folder exist?
     43     bool OK() const;
     44 
     45 private:
     46     UnencryptedProperties();
     47     std::string folder_;
     48 };
     49 
     50 
     51 template<typename t> t UnencryptedProperties::Get(const char* name,
     52                                                   t default_value) const
     53 {
     54     if (!OK()) return default_value;
     55     t value = default_value;
     56     std::ifstream(folder_ + "/" + name) >> value;
     57     return value;
     58 }
     59 
     60 template<typename t> bool UnencryptedProperties::Set(const char* name,
     61                                                      t const& value)
     62 {
     63     if (!OK()) return false;
     64     std::ofstream o(folder_ + "/" + name);
     65     o << value;
     66     return !o.fail();
     67 }
     68 
     69 // Specialized getters/setters for strings
     70 template<> std::string UnencryptedProperties::Get(const char* name,
     71                                       std::string default_value) const;
     72 
     73 template<> bool UnencryptedProperties::Set(const char* name,
     74                                            std::string const& value);
     75