xref: /aosp_15_r20/system/server_configurable_flags/aconfigd/aconfigd_util.h (revision 207333786ba243bc7d4d69ef6b05487aa7071806)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <string>
20 #include <sys/stat.h>
21 
22 #include <android-base/result.h>
23 #include <android-base/file.h>
24 
25 #define RETURN_IF_ERROR(RESULT, ERROR) \
26 if (!RESULT.ok()) {\
27   return android::base::Error() << ERROR << ": " << RESULT.error();\
28 }
29 
30 namespace android {
31   namespace aconfigd {
32 
33   /// Remove files in a dir
34   base::Result<void> RemoveFilesInDir(const std::string& dir);
35 
36   /// Copy file
37   base::Result<void> CopyFile(const std::string& src,
38                               const std::string& dst,
39                               mode_t mode);
40 
41   /// Get a file's timestamp in nano second
42   base::Result<uint64_t> GetFileTimeStamp(const std::string& file);
43 
44   /// Check if file exists
45   bool FileExists(const std::string& file);
46 
47   /// Check if file exists and has non zero size
48   bool FileNonZeroSize(const std::string& file);
49 
50   /// Get file digest
51   base::Result<std::string> GetFilesDigest(const std::vector<std::string>& files);
52 
53   /// Read protobuf from file
54   template <typename T>
ReadPbFromFile(const std::string & pb_file)55   base::Result<T> ReadPbFromFile(const std::string& pb_file) {
56     auto pb = T();
57     if (FileExists(pb_file)) {
58       auto content = std::string();
59       if (!base::ReadFileToString(pb_file, &content)) {
60         return base::ErrnoError() << "ReadFileToString() failed";
61       }
62 
63       if (!pb.ParseFromString(content)) {
64         return base::ErrnoError() << "Unable to parse to protobuf";
65       }
66     }
67     return pb;
68   }
69 
70   /// Write protobuf to file
71   template <typename T>
72   base::Result<void> WritePbToFile(const T& pb,
73                                    const std::string& file_name,
74                                    mode_t mode = 0644) {
75     auto content = std::string();
76     if (!pb.SerializeToString(&content)) {
77       return base::ErrnoError() << "Unable to serialize protobuf to string";
78     }
79 
80     if (!base::WriteStringToFile(content, file_name)) {
81       return base::ErrnoError() << "WriteStringToFile() failed";
82     }
83 
84     if (chmod(file_name.c_str(), mode) == -1) {
85       return base::ErrnoError() << "chmod() failed";
86     };
87 
88     return {};
89   }
90 
91   /// convert override type enum to string
92   std::string OverrideTypeToStr(const StorageRequestMessage::FlagOverrideType&);
93 
94   }// namespace aconfig
95 } // namespace android
96