Subversion Repositories RepoView

Rev

Rev 12 | Rev 15 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 nishi 1
/* $Id: repo.c 14 2024-08-21 14:24:05Z nishi $ */
2
 
3
#include "rv_repo.h"
11 nishi 4
 
5
#include "../config.h"
6
 
7
#include "rv_util.h"
8
 
9
#include <stdbool.h>
10
#include <unistd.h>
11
#include <stdlib.h>
12
#include <dirent.h>
13
#include <stdio.h>
14
#include <string.h>
15
#include <fcntl.h>
16
#include <sys/stat.h>
17
#include <sys/wait.h>
12 nishi 18
#include <signal.h>
11 nishi 19
 
20
char* rv_construct_repouser(const char* reponame, const char* username) {
21
	char cbuf[2];
22
	cbuf[0] = REPO_USER_DELIM;
23
	cbuf[1] = 0;
24
	return rv_strcat3(reponame, cbuf, username);
25
}
26
 
27
bool rv_repo_exists(const char* repouser) {
28
	char* path = rv_strcat3(SVN_ROOT, "/", repouser);
29
	if(access(path, F_OK) == 0) {
30
		free(path);
31
		return true;
32
	}
33
	return false;
34
}
35
 
36
void rv_repo_list(const char* username, void (*handler)(const char* name, const char* rev)) {
37
	struct dirent** nl;
38
	int n = scandir(SVN_ROOT, &nl, NULL, alphasort);
39
	if(n < 0) return;
40
	int i;
41
	for(i = 0; i < n; i++) {
42
		if(strcmp(nl[i]->d_name, "..") != 0 && strcmp(nl[i]->d_name, ".") != 0) {
43
			char* tmp = rv_strcat3(SVN_ROOT, "/", nl[i]->d_name);
44
			char* path = rv_strcat(tmp, "/db/current");
45
			free(tmp);
46
			char* str = rv_strdup(nl[i]->d_name);
47
			int j;
48
			for(j = 0; str[j] != 0; j++) {
49
				if(str[j] == REPO_USER_DELIM) {
50
					str[j] = 0;
51
					if(strcmp(str + j + 1, username) == 0) {
52
						struct stat s;
53
						char* rev = rv_strdup("???");
54
						if(stat(path, &s) == 0) {
55
							free(rev);
56
							rev = malloc(s.st_size + 1);
57
							FILE* f = fopen(path, "r");
58
							fread(rev, 1, s.st_size, f);
59
							fclose(f);
60
							rev[s.st_size] = 0;
61
						}
62
						handler(str, rev);
63
						free(rev);
64
					}
65
					break;
66
				}
67
			}
68
			free(path);
69
			free(str);
70
		}
71
		free(nl[i]);
72
	}
73
	free(nl);
74
}
75
 
76
void null_exec(char** cmd) {
77
	pid_t pid = fork();
78
	if(pid == 0) {
79
		int null = open("/dev/null", O_RDWR);
80
		dup2(STDOUT_FILENO, null);
81
		execvp(cmd[0], cmd);
82
		_exit(0);
83
	} else {
84
		waitpid(pid, 0, 0);
85
	}
86
}
87
 
88
void rv_create_repo(const char* repouser) {
89
	char* user = rv_strdup(repouser);
90
	int i;
91
	for(i = 0; user[i] != 0; i++) {
92
		if(user[i] == REPO_USER_DELIM) {
93
			user[i] = 0;
94
			break;
95
		}
96
	}
97
	char* path = rv_strcat3(SVN_ROOT, "/", repouser);
98
	char* cmd[] = {"svnadmin", "create", path, NULL};
99
	null_exec(cmd);
100
	free(path);
101
	FILE* f = fopen(APACHE_AUTHZ, "r+");
102
	lockf(fileno(f), F_LOCK, 0);
103
 
104
	fseek(f, 0, SEEK_END);
105
 
106
	fprintf(f, "#%%START %s\n", repouser);
107
	fprintf(f, "* = r\n");
108
	fprintf(f, "%s = r\n", user);
109
	fprintf(f, "#%%END\n");
110
 
111
	lockf(fileno(f), F_ULOCK, 0);
112
	free(user);
113
}
114
 
115
char* rv_get_readme(const char* repouser) {
116
	char* tmp = rv_strcat3(SVN_ROOT, "/", repouser);
117
	char* path = rv_strcat(tmp, "/README.txt");
118
	free(tmp);
119
	struct stat s;
120
	if(stat(path, &s) == 0) {
121
		FILE* f = fopen(path, "r");
122
		char* buf = malloc(s.st_size + 1);
123
		fread(buf, 1, s.st_size, f);
124
		fclose(f);
125
		buf[s.st_size] = 0;
126
		return buf;
127
	}
128
	return NULL;
129
}
130
 
131
long long rv_get_filesize(const char* repouser, const char* path) {
132
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
133
	int pipes[2];
134
	pipe(pipes);
135
	pid_t pid = fork();
136
	if(pid == 0) {
137
		close(pipes[0]);
138
		dup2(pipes[1], STDOUT_FILENO);
139
		char* cmd[] = {"svnlook", "filesize", svnpath, (char*)path, NULL};
140
		execvp("svnlook", cmd);
141
		_exit(0);
142
	} else {
143
		close(pipes[1]);
144
		char cbuf[2];
145
		cbuf[1] = 0;
146
		char* d = malloc(1);
147
		d[0] = 0;
148
		while(1) {
149
			int n = read(pipes[0], cbuf, 1);
150
			if(n == 0) break;
151
			char* tmp = d;
152
			d = rv_strcat(tmp, cbuf);
153
			free(tmp);
154
		}
155
		int status;
156
		waitpid(pid, &status, 0);
157
		if(WEXITSTATUS(status) != 0) {
158
			free(d);
159
			free(svnpath);
160
			return -1;
161
		}
162
		long long sz = atoll(d);
163
		free(svnpath);
164
		free(d);
165
		return sz;
166
	}
167
}
168
 
14 nishi 169
void rv_remove_repo(const char* repouser) {
170
	printf("");
171
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
172
	pid_t pid = fork();
173
	if(pid == 0) {
174
		char* cmd[] = {"rm", "-rf", svnpath, NULL};
175
		execvp("rm", cmd);
176
		_exit(0);
177
	} else {
178
		waitpid(pid, 0, 0);
179
	}
180
	free(svnpath);
181
 
182
	FILE* f = fopen(APACHE_AUTHZ, "r+");
183
	lockf(fileno(f), F_LOCK, 0);
184
 
185
	fseek(f, 0, SEEK_SET);
186
	struct stat s;
187
	stat(APACHE_AUTHZ, &s);
188
	char* buffer = malloc(s.st_size + 1);
189
	fread(buffer, 1, s.st_size, f);
190
	buffer[s.st_size] = 0;
191
 
192
	f = freopen(APACHE_AUTHZ, "w+", f);
193
	lockf(fileno(f), F_LOCK, 0);
194
	int incr = 0;
195
	int i;
196
	char* start = rv_strcat("#%START ", repouser);
197
	bool discard = false;
198
	for(i = 0;; i++) {
199
		if(buffer[i] == '\n' || buffer[i] == 0) {
200
			char oldc = buffer[i];
201
			buffer[i] = 0;
202
 
203
			char* line = buffer + incr;
204
			if(strcmp(line, start) == 0) {
205
				discard = true;
206
			} else if(discard && strcmp(line, "#%END") == 0) {
207
				discard = false;
208
			} else if(!discard) {
209
				fprintf(f, "%s\n", line);
210
			}
211
 
212
			incr = i + 1;
213
			if(oldc == 0) break;
214
		}
215
	}
216
 
217
	lockf(fileno(f), F_ULOCK, 0);
218
	fclose(f);
219
}
220
 
11 nishi 221
bool rv_get_list(const char* repouser, const char* path, void (*handler)(const char* pathname), int* isdir) {
222
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
223
	int pipes[2];
224
	*isdir = 0;
225
	pipe(pipes);
226
	pid_t pid = fork();
227
	if(pid == 0) {
228
		close(pipes[0]);
229
		dup2(pipes[1], STDOUT_FILENO);
230
		char* cmd[] = {"svnlook", "-N", "tree", svnpath, (char*)path, NULL};
231
		execvp("svnlook", cmd);
232
		_exit(0);
233
	} else {
234
		close(pipes[1]);
235
		char cbuf[2];
236
		cbuf[1] = 0;
237
		char* d = malloc(1);
238
		d[0] = 0;
239
		while(1) {
240
			int n = read(pipes[0], cbuf, 1);
241
			if(n == 0) break;
242
			char* tmp = d;
243
			d = rv_strcat(tmp, cbuf);
244
			free(tmp);
245
		}
246
		int status;
247
		waitpid(pid, &status, 0);
248
		if(WEXITSTATUS(status) != 0) {
249
			free(d);
250
			free(svnpath);
251
			return false;
252
		}
253
		int count = 0;
254
		int incr = 0;
255
		int i;
256
		int phase = 0;
257
	repeat:
258
		for(i = 0;; i++) {
259
			if(d[i] == '\r') {
260
				d[i] = 0;
261
			} else if(d[i] == '\n' || d[i] == 0) {
262
				char oldc = d[i];
263
				d[i] = 0;
264
				count++;
12 nishi 265
				if(count > 1 && d[incr] != 0 && strlen(d + incr + 1) > 0) {
11 nishi 266
					char* pathname = d + incr + 1;
267
					if(phase == 0 && pathname[strlen(pathname) - 1] == '/') {
268
						handler(d + incr + 1);
269
					} else if(phase == 1 && pathname[strlen(pathname) - 1] != '/') {
270
						handler(d + incr + 1);
271
					}
272
				} else {
273
					char* pathname = d + incr;
274
					if(pathname[strlen(pathname) - 1] == '/') *isdir = 1;
275
				}
276
				d[i] = oldc;
277
				incr = i + 1;
278
				if(oldc == 0) break;
279
			}
280
		}
281
		phase++;
12 nishi 282
		incr = 0;
283
		count = 0;
11 nishi 284
		if(phase == 1) goto repeat;
285
		free(d);
286
	}
287
	free(svnpath);
288
	return true;
289
}
290
 
291
char* rv_read_file(const char* repouser, char* path) {
12 nishi 292
	if(rv_get_filesize(repouser, path) > 1024 * 128) return NULL;
11 nishi 293
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
294
	int pipes[2];
295
	pipe(pipes);
296
	pid_t pid = fork();
297
	if(pid == 0) {
298
		close(pipes[0]);
299
		dup2(pipes[1], STDOUT_FILENO);
300
		char* cmd[] = {"svnlook", "cat", svnpath, (char*)path, NULL};
301
		execvp("svnlook", cmd);
302
		_exit(0);
303
	} else {
304
		close(pipes[1]);
12 nishi 305
		char cbuf[1024];
306
		char* d = malloc(1024 * 128);
307
		int incr = 0;
308
		bool bin = false;
11 nishi 309
		while(1) {
12 nishi 310
			int n = read(pipes[0], cbuf, 1024);
311
			if(cbuf[0] == 0) {
312
				bin = true;
313
				kill(pid, SIGKILL);
314
				break;
315
			}
11 nishi 316
			if(n == 0) break;
12 nishi 317
			memcpy(d + incr, cbuf, n);
318
			d[incr + n] = 0;
319
			incr += n;
11 nishi 320
		}
321
		int status;
322
		waitpid(pid, &status, 0);
323
		if(WEXITSTATUS(status) != 0) {
324
			free(d);
325
			free(svnpath);
326
			return NULL;
327
		}
12 nishi 328
		if(bin) {
329
			free(d);
330
			return NULL;
331
		}
11 nishi 332
		return d;
333
	}
334
}