Subversion Repositories RepoView

Rev

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