1 /*
2  * Copyright (C) 2016 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 #ifndef UTIL_H
18 #define UTIL_H
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 using namespace std;
25 
26 struct FileInfo
27 {
28     bool exists;
29     time_t mtime;
30     time_t ctime;
31     off_t size;
32 
33     FileInfo();
34     FileInfo(const FileInfo& that);
35     explicit FileInfo(const string& filename);
36     ~FileInfo();
37 
38     bool operator==(const FileInfo& that) const;
39     bool operator!=(const FileInfo& that) const;
40 };
41 
42 
43 /**
44  * Record for a file that we are watching
45  */
46 struct TrackedFile {
47     string filename;
48     FileInfo fileInfo;
49 
50     TrackedFile();
51     TrackedFile(const TrackedFile& that);
52     explicit TrackedFile(const string& filename);
53     ~TrackedFile();
54 
55     // Returns if the file has changed. If it doesn't currently exist, returns true.
56     bool HasChanged() const;
57 };
58 
59 /**
60  * Get FileInfo structures recursively for all the files and symlinks in a directory.
61  * Does not traverse symlinks, but it does record them.
62  */
63 void get_directory_contents(const string& dir, map<string,FileInfo>* results);
64 
65 bool directory_contents_differ(const map<string,FileInfo>& before,
66         const map<string,FileInfo>& after);
67 
68 string escape_quotes(const char* str);
69 
70 string escape_for_commandline(const char* str);
71 
72 string trim(const string& trim);
73 
74 bool starts_with(const string& str, const string& prefix);
75 
76 bool ends_with(const string& str, const string& suffix);
77 
78 void split_lines(vector<string>* result, const string& str);
79 
80 string read_file(const string& filename);
81 
82 #endif // UTIL_H
83 
84