blob: 8c66911b3c48aad412c75a39399504d2bdc8637b [file] [log] [blame]
Joe Onorato0578cbc2016-10-19 17:03:06 -07001/*
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
Colin Cross7cdbd682021-09-13 16:30:12 -070020#include <sys/types.h>
21
Joe Onorato0578cbc2016-10-19 17:03:06 -070022#include <map>
23#include <string>
24#include <vector>
25
26using namespace std;
27
28struct 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 */
48struct 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 */
65void get_directory_contents(const string& dir, map<string,FileInfo>* results);
66
67bool directory_contents_differ(const map<string,FileInfo>& before,
68 const map<string,FileInfo>& after);
69
70string escape_quotes(const char* str);
71
72string escape_for_commandline(const char* str);
73
74string trim(const string& trim);
75
76bool starts_with(const string& str, const string& prefix);
77
78bool ends_with(const string& str, const string& suffix);
79
80void split_lines(vector<string>* result, const string& str);
81
82string read_file(const string& filename);
83
Joe Onorato6c97f492019-02-27 20:42:37 -050084bool is_executable(const string& filename);
85
86string dirname(const string& filename);
87string leafname(const string& filename);
88
Joe Onorato0578cbc2016-10-19 17:03:06 -070089#endif // UTIL_H
90