aboutsummaryrefslogtreecommitdiff
path: root/src/main.cc
blob: 7e849fd93b5ae49bdeca1ec74eff881ad05d05bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/uio.h>

#include "actual.h"

#include "utility/io.h"
#include "utility/logger.h"

#include "tracking/path_matcher.h"
#include "tracking/change_tracker.h"

// `true`  signals the interposed functions to execute tracking logic
// `false` signals the interposed functions to do plain forwarding
static bool enabled = false;

static std::unique_ptr<utility::FileDescriptorGuard> fd_guard;
static std::unique_ptr<utility::Logger>              logger;
static std::unique_ptr<tracking::PathMatcher>        matcher;
static std::unique_ptr<tracking::ChangeTracker>      tracker;

void initialize() __attribute__ ((constructor));
void initialize() {
	if ( getenv("CHANGE_LOG_TARGET") != NULL ) {
		fd_guard = std::make_unique<utility::FileDescriptorGuard>(
			getenv("CHANGE_LOG_TARGET")
		);
		logger   = std::make_unique<utility::Logger>(*fd_guard);
	} else {
		logger   = std::make_unique<utility::Logger>(STDERR_FILENO);
	}

	if ( getenv("CHANGE_LOG_DIFF_CMD") != NULL ) {
		tracker = std::make_unique<tracking::ChangeTracker>(
			logger.get(), getenv("CHANGE_LOG_DIFF_CMD")
		);
	} else {
		tracker = std::make_unique<tracking::ChangeTracker>(logger.get());
	}

	if ( getenv("CHANGE_LOG_IGNORE_PATTERN_PATH") != NULL ) {
		matcher = std::make_unique<tracking::PathMatcher>(
			getenv("CHANGE_LOG_IGNORE_PATTERN_PATH")
		);
	} else {
		matcher = std::make_unique<tracking::PathMatcher>();
	}

	// tracking is only enabled when everything is initialized as both
	// the actual tracking and the decision if a event should be tracked
	// depend on `logger` and `tracker` being fully instantiated.
	// In most cases this library will work correctly without delayed
	// activation but e.g. `nvim` crashes as it performs tracked syscalls
	// before `init` has been called.
	enabled = true;
}

inline void track_write(const int fd) {
	if ( enabled && fd != *fd_guard && utility::is_regular_file(fd) ) {
		const auto path = utility::get_file_path(fd);

		if ( !matcher->isMatching(path) ) {
			tracker->track(path);
		}
	}
}

inline void track_write(const std::string& path) {
	if ( enabled && utility::is_regular_file(path.c_str()) ) {
		if ( !matcher->isMatching(path) ) {
			tracker->track(path);
		}
	}
}

inline void track_rename(
	const std::string& old_path, const std::string& new_path) {
	if ( enabled ) {
		if ( !matcher->isMatching(old_path) ) {
			tracker->track(old_path);

			if ( !matcher->isMatching(new_path) ) {
				logger->append("renamed '", old_path, "' to '", new_path, "'");
			}
		}
	}
}

inline void track_remove(const std::string& path) {
	if ( enabled && utility::is_regular_file(path.c_str()) ) {
		if ( !matcher->isMatching(path) ) {
			logger->append("removed '", path, "'");
		}
	}
}

extern "C" {

int open(const char* path, int flags, mode_t mode) {
	static actual::ptr<int, const char*, int, mode_t> actual_open{};

	if ( !actual_open ) {
		actual_open = actual::get_ptr<decltype(actual_open)>("open");
	}

	// `open` may reset the file contents when used with the `O_TRUNC` flag.
	// e.g. this is how _emacs_ clears the file content prior to writing the
	// new content.
	// Normally `O_TRUNC` is defined in `fcntl.h` which we can not include
	// as it defines the very c-function we are currently _overriding_.
	//
	if ( flags & 01000 ) {
		track_write(path);
	}

	return actual_open(path, flags, mode);
}

ssize_t write(int fd, const void* buffer, size_t count) {
	static actual::ptr<ssize_t, int, const void*, size_t> actual_write{};

	if ( !actual_write ) {
		actual_write = actual::get_ptr<decltype(actual_write)>("write");
	}

	track_write(fd);

	return actual_write(fd, buffer, count);
}

ssize_t writev(int fd, const iovec* iov, int iovcnt) {
	static actual::ptr<ssize_t, int, const iovec*, int> actual_writev{};

	if ( !actual_writev ) {
		actual_writev = actual::get_ptr<decltype(actual_writev)>("writev");
	}

	track_write(fd);

	return actual_writev(fd, iov, iovcnt);
}

void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
	static actual::ptr<void*, void*, size_t, int, int, int, off_t> actual_mmap{};

	if ( !actual_mmap ) {
		actual_mmap = actual::get_ptr<decltype(actual_mmap)>("mmap");
	}

	if ( prot & PROT_WRITE ) {
		track_write(fd);
	}

	return actual_mmap(addr, length, prot, flags, fd, offset);
}

int rename(const char* old_path, const char* new_path) {
	static actual::ptr<int, const char*, const char*> actual_rename{};

	if ( !actual_rename ) {
		actual_rename = actual::get_ptr<decltype(actual_rename)>("rename");
	}

	track_rename(old_path, new_path);

	return actual_rename(old_path, new_path);
}

int rmdir(const char* path) {
	static actual::ptr<int, const char*> actual_rmdir{};

	if ( !actual_rmdir ) {
		actual_rmdir = actual::get_ptr<decltype(actual_rmdir)>("rmdir");
	}

	track_remove(path);

	return actual_rmdir(path);
}

int unlink(const char* path) {
	static actual::ptr<int, const char*> actual_unlink{};

	if ( !actual_unlink ) {
		actual_unlink = actual::get_ptr<decltype(actual_unlink)>("unlink");
	}

	track_remove(path);

	return actual_unlink(path);
}

int unlinkat(int dirfd, const char* path, int flags) {
	static actual::ptr<int, int, const char*, int> actual_unlinkat{};

	if ( !actual_unlinkat ) {
		actual_unlinkat = actual::get_ptr<decltype(actual_unlinkat)>("unlinkat");
	}

	// Normally `AT_FDCWD` is defined in `fcntl.h` which we can not include
	// as it defines `open` which this library also aims to override.
	//
	if ( dirfd == -100 ) {
		track_remove(path);
	} else {
		track_remove(utility::get_file_path(dirfd) + path);
	}

	return actual_unlinkat(dirfd, path, flags);
}

}