diff options
Diffstat (limited to 'system_part.c')
-rw-r--r-- | system_part.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/system_part.c b/system_part.c new file mode 100644 index 0000000..aaf2301 --- /dev/null +++ b/system_part.c @@ -0,0 +1,121 @@ +#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,0770); + 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|O_CREAT); + 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"); + + 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,0770)==-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; + unlinkat(branch_dir,filename,0); + fd=openat(branch_dir,filename,O_CREAT|O_RDWR,0660); + if(fd==-1) + die("Could not open the file '%s' for one of the branches\n",filename); + return fdopen(fd,"w"); +} +int create_files_dir(int branch_dir) +{ + int ret; + + ret=openat(branch_dir,"files",O_DIRECTORY|O_RDONLY); + if(ret!=-1) + return ret; + + ret=mkdirat(branch_dir,"files",0770); + if(ret==-1) + die("Could not create the 'files' subdirectory of one of the branch dirs\n"); + + return ret; +} +int create_diff_dir(int branch_dir) +{ + int ret; + + ret=openat(branch_dir,"diffs",O_DIRECTORY|O_RDONLY); + if(ret!=-1) + return ret; + mkdirat(branch_dir,"diffs",0770); + ret=openat(branch_dir,"diffs",O_DIRECTORY|O_RDONLY); + if(ret==-1) + die("Could not create the 'diffs' 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); +} +#endif |