summaryrefslogtreecommitdiffstats
path: root/system_part.c
blob: 04ea1555f7f0a392b96ff049f93a6e79a76d0df2 (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
#ifndef VOLGIT_SYSTEM_PART_C
#define VOLGIT_SYSTEM_PART_C VOLGIT_SYSTEM_PART_C
#include <system_part.h>

const char *default_header="<!DOCTYPE html><html><head><title>GIT</title></head><body>";
const char *default_footer="</body></html>";

struct Volgit_Options options;

void parse_options(int argc,char **argv)
{

	options.header=default_header;
	options.footer=default_footer;
	switch(argc)
	{
		case 5:
			options.header=read_file(argv[3]);
			options.footer=read_file(argv[4]);
		/*falltrough*/
		case 3:
			options.input_path=argv[1];
			options.output_path=argv[2];
			break;
		default:
			die("Usage: volgit source destination [ file-containint-header file-containing-footer ]\n");
	}

	options.output_dir_fd=open(options.output_path,O_RDONLY|O_DIRECTORY);
	if(options.output_dir_fd==-1)
	{
		mkdir(options.output_path,0775);
		options.output_dir_fd=open(options.output_path,O_RDONLY|O_DIRECTORY);
		if(options.output_dir_fd==-1)
			die("Could not open output dir\n");
	}
}
char* read_file(const char *filename)
{
	long file_size;
	int file;
	char *ret;
	struct stat file_stats;

	file=open(filename,O_RDONLY);
	if(file==-1)
		die("Could not open file %s\n",filename);

	fstat(file,&file_stats);

	ret=malloc(file_stats.st_size+1);

	if(ret==NULL)
		die("Ran out of memory!\n");

	read(file,ret,file_stats.st_size);

	close(file);
	return ret;
}

int create_branch_dir(const char *branch_name)
{
	int ret=0;

	ret=openat(options.output_dir_fd,branch_name,O_DIRECTORY|O_RDONLY);	
	if(ret==-1)
	{
		if(mkdirat(options.output_dir_fd,branch_name,0775)==-1)
			die("Could not create branch directory for %s\n",branch_name);

		ret=openat(options.output_dir_fd,branch_name,O_DIRECTORY|O_RDONLY);	
		if(ret==-1)
			die("Could not open newly created directory for branch %s\n",branch_name);
	}

	return ret;
}
FILE* create_file(int branch_dir,const char *filename)
{
	int fd;
	char *new_filename;
	size_t old_filename_len;
	FILE *ret;

	old_filename_len=strlen(filename);
	new_filename=calloc(old_filename_len+sizeof(".html"),1);
	if(new_filename==NULL)
		die("Ran out of memory!\n");

	memcpy(new_filename,filename,old_filename_len);
	memcpy(new_filename+old_filename_len,".html",sizeof(".html"));

	unlinkat(branch_dir,new_filename,0);
	fd=openat(branch_dir,new_filename,O_CREAT|O_RDWR,0664);
	if(fd==-1)
		die("Could not open the file '%s' for one of the branches\n",filename);

	free(new_filename);

	ret=fdopen(fd,"w");

	fprintf(ret,"%s",options.header);
	return ret;
}
void close_file(FILE *file)
{
	fprintf(file,"%s",options.footer);
	fclose(file);
}
int create_dir(int base_dir,const char *dir_name)
{
	int ret;
	ret=openat(base_dir,dir_name,O_DIRECTORY|O_RDONLY);	
	if(ret!=-1)
		return ret;

	mkdirat(base_dir,dir_name,0775);
	ret=openat(base_dir,dir_name,O_DIRECTORY|O_RDONLY);	
	if(ret==-1)
		die("Could not create the 'files' subdirectory of one of the branch dirs\n");

	return ret;
}

void die(const char *format, ...)
{
	va_list args;
	va_start(args,format);
	vfprintf(stderr,format,args);
	exit(1);
}
void push_html_link_for_blob(FILE *out,const char *filename,const char *oid)
{
	fprintf(out,"<a href=\"%s.html\" class=\"blob_entry\">%s</a>\n",oid,filename);	
}
void push_html_link_for_tree(FILE *out,const char *filename,const char *oid)
{
	fprintf(out,"<b><a href=\"%s/index.html\" class=\"tree_entry\">%s</a></b>\n",oid,filename);	
}
#endif