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
24using namespace std;
25
26struct 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 */
46struct 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 */
63void get_directory_contents(const string& dir, map<string,FileInfo>* results);
64
65bool directory_contents_differ(const map<string,FileInfo>& before,
66        const map<string,FileInfo>& after);
67
68string escape_quotes(const char* str);
69
70string escape_for_commandline(const char* str);
71
72string trim(const string& trim);
73
74bool starts_with(const string& str, const string& prefix);
75
76bool ends_with(const string& str, const string& suffix);
77
78void split_lines(vector<string>* result, const string& str);
79
80string read_file(const string& filename);
81
82#endif // UTIL_H
83
84