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 #include <string>
18 #include <sys/stat.h>
19 
20 #include <android-base/result.h>
21 #include <android-base/file.h>
22 
23 #define RETURN_IF_ERROR(RESULT, ERROR) \
24 if (!RESULT.ok()) {\
25   return android::base::Error() << ERROR << ": " << RESULT.error();\
26 }
27 
28 namespace android {
29   namespace aconfigd {
30 
31   /// Remove files in a dir
32   base::Result<void> RemoveFilesInDir(const std::string& dir);
33 
34   /// Copy file
35   base::Result<void> CopyFile(const std::string& src,
36                               const std::string& dst,
37                               mode_t mode);
38 
39   /// Get a file's timestamp in nano second
40   base::Result<uint64_t> GetFileTimeStamp(const std::string& file);
41 
42   /// Check if file exists
43   bool FileExists(const std::string& file);
44 
45   /// Check if file exists and has non zero size
46   bool FileNonZeroSize(const std::string& file);
47 
48   /// Get file digest
49   base::Result<std::string> GetFilesDigest(const std::vector<std::string>& files);
50 
51   /// Read protobuf from file
52   template <typename T>
ReadPbFromFile(const std::string & pb_file)53   base::Result<T> ReadPbFromFile(const std::string& pb_file) {
54     auto pb = T();
55     if (FileExists(pb_file)) {
56       auto content = std::string();
57       if (!base::ReadFileToString(pb_file, &content)) {
58         return base::ErrnoError() << "ReadFileToString() failed";
59       }
60 
61       if (!pb.ParseFromString(content)) {
62         return base::ErrnoError() << "Unable to parse to protobuf";
63       }
64     }
65     return pb;
66   }
67 
68   /// Write protobuf to file
69   template <typename T>
70   base::Result<void> WritePbToFile(const T& pb,
71                                    const std::string& file_name,
72                                    mode_t mode = 0644) {
73     auto content = std::string();
74     if (!pb.SerializeToString(&content)) {
75       return base::ErrnoError() << "Unable to serialize protobuf to string";
76     }
77 
78     if (!base::WriteStringToFile(content, file_name)) {
79       return base::ErrnoError() << "WriteStringToFile() failed";
80     }
81 
82     if (chmod(file_name.c_str(), mode) == -1) {
83       return base::ErrnoError() << "chmod() failed";
84     };
85 
86     return {};
87   }
88 
89   }// namespace aconfig
90 } // namespace android
91