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