Subversion Repositories RepoView

Rev

Rev 14 | Rev 16 | 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 15 2024-08-21 15:36:37Z 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);
15 nishi 107
	fprintf(f, "[%s:/]\n", repouser);
11 nishi 108
	fprintf(f, "* = r\n");
15 nishi 109
	fprintf(f, "%s = rw\n", user);
11 nishi 110
	fprintf(f, "#%%END\n");
111
 
112
	lockf(fileno(f), F_ULOCK, 0);
113
	free(user);
114
}
115
 
116
char* rv_get_readme(const char* repouser) {
117
	char* tmp = rv_strcat3(SVN_ROOT, "/", repouser);
118
	char* path = rv_strcat(tmp, "/README.txt");
119
	free(tmp);
120
	struct stat s;
121
	if(stat(path, &s) == 0) {
122
		FILE* f = fopen(path, "r");
123
		char* buf = malloc(s.st_size + 1);
124
		fread(buf, 1, s.st_size, f);
125
		fclose(f);
126
		buf[s.st_size] = 0;
127
		return buf;
128
	}
129
	return NULL;
130
}
131
 
132
long long rv_get_filesize(const char* repouser, const char* path) {
133
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
134
	int pipes[2];
135
	pipe(pipes);
136
	pid_t pid = fork();
137
	if(pid == 0) {
138
		close(pipes[0]);
139
		dup2(pipes[1], STDOUT_FILENO);
140
		char* cmd[] = {"svnlook", "filesize", svnpath, (char*)path, NULL};
141
		execvp("svnlook", cmd);
142
		_exit(0);
143
	} else {
144
		close(pipes[1]);
145
		char cbuf[2];
146
		cbuf[1] = 0;
147
		char* d = malloc(1);
148
		d[0] = 0;
149
		while(1) {
150
			int n = read(pipes[0], cbuf, 1);
151
			if(n == 0) break;
152
			char* tmp = d;
153
			d = rv_strcat(tmp, cbuf);
154
			free(tmp);
155
		}
156
		int status;
157
		waitpid(pid, &status, 0);
158
		if(WEXITSTATUS(status) != 0) {
159
			free(d);
160
			free(svnpath);
161
			return -1;
162
		}
163
		long long sz = atoll(d);
164
		free(svnpath);
165
		free(d);
166
		return sz;
167
	}
168
}
169
 
14 nishi 170
void rv_remove_repo(const char* repouser) {
171
	printf("");
172
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
173
	pid_t pid = fork();
174
	if(pid == 0) {
175
		char* cmd[] = {"rm", "-rf", svnpath, NULL};
176
		execvp("rm", cmd);
177
		_exit(0);
178
	} else {
179
		waitpid(pid, 0, 0);
180
	}
181
	free(svnpath);
182
 
183
	FILE* f = fopen(APACHE_AUTHZ, "r+");
184
	lockf(fileno(f), F_LOCK, 0);
185
 
186
	fseek(f, 0, SEEK_SET);
187
	struct stat s;
188
	stat(APACHE_AUTHZ, &s);
189
	char* buffer = malloc(s.st_size + 1);
190
	fread(buffer, 1, s.st_size, f);
191
	buffer[s.st_size] = 0;
192
 
193
	f = freopen(APACHE_AUTHZ, "w+", f);
194
	lockf(fileno(f), F_LOCK, 0);
195
	int incr = 0;
196
	int i;
197
	char* start = rv_strcat("#%START ", repouser);
198
	bool discard = false;
199
	for(i = 0;; i++) {
200
		if(buffer[i] == '\n' || buffer[i] == 0) {
201
			char oldc = buffer[i];
202
			buffer[i] = 0;
203
 
204
			char* line = buffer + incr;
205
			if(strcmp(line, start) == 0) {
206
				discard = true;
207
			} else if(discard && strcmp(line, "#%END") == 0) {
208
				discard = false;
15 nishi 209
			} else if(strcmp(line, "") == 0) {
14 nishi 210
			} else if(!discard) {
211
				fprintf(f, "%s\n", line);
212
			}
213
 
214
			incr = i + 1;
215
			if(oldc == 0) break;
216
		}
217
	}
218
 
219
	lockf(fileno(f), F_ULOCK, 0);
220
	fclose(f);
221
}
222
 
15 nishi 223
void rv_set_readme(const char* repouser, const char* readme) {
224
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
225
	char* path = rv_strcat(svnpath, "/README.txt");
226
	FILE* f = fopen(path, "w");
227
	if(f != NULL) {
228
		fwrite(readme, 1, strlen(readme), f);
229
		fclose(f);
230
	}
231
	free(path);
232
	free(svnpath);
233
}
234
 
11 nishi 235
bool rv_get_list(const char* repouser, const char* path, void (*handler)(const char* pathname), int* isdir) {
236
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
237
	int pipes[2];
238
	*isdir = 0;
239
	pipe(pipes);
240
	pid_t pid = fork();
241
	if(pid == 0) {
242
		close(pipes[0]);
243
		dup2(pipes[1], STDOUT_FILENO);
244
		char* cmd[] = {"svnlook", "-N", "tree", svnpath, (char*)path, NULL};
245
		execvp("svnlook", cmd);
246
		_exit(0);
247
	} else {
248
		close(pipes[1]);
249
		char cbuf[2];
250
		cbuf[1] = 0;
251
		char* d = malloc(1);
252
		d[0] = 0;
253
		while(1) {
254
			int n = read(pipes[0], cbuf, 1);
255
			if(n == 0) break;
256
			char* tmp = d;
257
			d = rv_strcat(tmp, cbuf);
258
			free(tmp);
259
		}
260
		int status;
261
		waitpid(pid, &status, 0);
262
		if(WEXITSTATUS(status) != 0) {
263
			free(d);
264
			free(svnpath);
265
			return false;
266
		}
267
		int count = 0;
268
		int incr = 0;
269
		int i;
270
		int phase = 0;
271
	repeat:
272
		for(i = 0;; i++) {
273
			if(d[i] == '\r') {
274
				d[i] = 0;
275
			} else if(d[i] == '\n' || d[i] == 0) {
276
				char oldc = d[i];
277
				d[i] = 0;
278
				count++;
12 nishi 279
				if(count > 1 && d[incr] != 0 && strlen(d + incr + 1) > 0) {
11 nishi 280
					char* pathname = d + incr + 1;
281
					if(phase == 0 && pathname[strlen(pathname) - 1] == '/') {
282
						handler(d + incr + 1);
283
					} else if(phase == 1 && pathname[strlen(pathname) - 1] != '/') {
284
						handler(d + incr + 1);
285
					}
286
				} else {
287
					char* pathname = d + incr;
288
					if(pathname[strlen(pathname) - 1] == '/') *isdir = 1;
289
				}
290
				d[i] = oldc;
291
				incr = i + 1;
292
				if(oldc == 0) break;
293
			}
294
		}
295
		phase++;
12 nishi 296
		incr = 0;
297
		count = 0;
11 nishi 298
		if(phase == 1) goto repeat;
299
		free(d);
300
	}
301
	free(svnpath);
302
	return true;
303
}
304
 
305
char* rv_read_file(const char* repouser, char* path) {
12 nishi 306
	if(rv_get_filesize(repouser, path) > 1024 * 128) return NULL;
11 nishi 307
	char* svnpath = rv_strcat3(SVN_ROOT, "/", repouser);
308
	int pipes[2];
309
	pipe(pipes);
310
	pid_t pid = fork();
311
	if(pid == 0) {
312
		close(pipes[0]);
313
		dup2(pipes[1], STDOUT_FILENO);
314
		char* cmd[] = {"svnlook", "cat", svnpath, (char*)path, NULL};
315
		execvp("svnlook", cmd);
316
		_exit(0);
317
	} else {
318
		close(pipes[1]);
12 nishi 319
		char cbuf[1024];
320
		char* d = malloc(1024 * 128);
321
		int incr = 0;
322
		bool bin = false;
11 nishi 323
		while(1) {
12 nishi 324
			int n = read(pipes[0], cbuf, 1024);
325
			if(cbuf[0] == 0) {
326
				bin = true;
327
				kill(pid, SIGKILL);
328
				break;
329
			}
11 nishi 330
			if(n == 0) break;
12 nishi 331
			memcpy(d + incr, cbuf, n);
332
			d[incr + n] = 0;
333
			incr += n;
11 nishi 334
		}
335
		int status;
336
		waitpid(pid, &status, 0);
337
		if(WEXITSTATUS(status) != 0) {
338
			free(d);
339
			free(svnpath);
340
			return NULL;
341
		}
12 nishi 342
		if(bin) {
343
			free(d);
344
			return NULL;
345
		}
11 nishi 346
		return d;
347
	}
348
}