gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

fileblob.c (3200B)


      1 #include "writer/gopher/fileblob.h"
      2 
      3 #include <assert.h>
      4 #include <err.h>
      5 #include <libgen.h>
      6 #include <limits.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 #include "format.h"
     12 #include "utils.h"
     13 #include "writer/gopher/page.h"
     14 
     15 struct GopherFileBlob {
     16   const GitRepo* repo;
     17   const FileSystem* fs;
     18   FILE* out;
     19   GopherPage* page;
     20 };
     21 
     22 GopherFileBlob* gopher_fileblob_create(const GitRepo* repo,
     23                                        const FileSystem* fs,
     24                                        const char* path) {
     25   assert(repo != NULL);
     26   assert(fs != NULL);
     27   assert(path != NULL);
     28   if (!is_safe_repo_path(path)) {
     29     errx(1, "unsafe path: %s", path);
     30   }
     31   GopherFileBlob* blob = ecalloc(1, sizeof(GopherFileBlob));
     32   blob->repo = repo;
     33   blob->fs = fs;
     34 
     35   // Create directories.
     36   char filename_buffer[PATH_MAX];
     37   int r = snprintf(filename_buffer, sizeof(filename_buffer), "%s.gph", path);
     38   if (r < 0 || (size_t)r >= sizeof(filename_buffer)) {
     39     errx(1, "snprintf: filename truncated or error");
     40   }
     41 
     42   char* out_path = path_concat("file", filename_buffer);
     43   char* dir_copy = estrdup(out_path);
     44   const char* d = dirname(dir_copy);
     45   if (!d) {
     46     err(1, "dirname");
     47   }
     48   fs->mkdirp(d);
     49   blob->out = fs->fopen(out_path, "w");
     50   if (!blob->out) {
     51     err(1, "fopen: %s", out_path);
     52   }
     53 
     54   // Compute the relative path.
     55   char* relpath = relpath_from_dir(d);
     56   free(dir_copy);
     57 
     58   char* path_copy = estrdup(path);
     59   const char* title = basename(path_copy);
     60   if (!title) {
     61     err(1, "basename");
     62   }
     63   blob->page = gopher_page_create(blob->out, repo, fs, title, relpath);
     64 
     65   free(out_path);
     66   free(path_copy);
     67   free(relpath);
     68 
     69   return blob;
     70 }
     71 
     72 void gopher_fileblob_free(GopherFileBlob* blob) {
     73   if (!blob) {
     74     return;
     75   }
     76   blob->fs->fclose(blob->out);
     77   gopher_page_free(blob->page);
     78   free(blob);
     79 }
     80 
     81 void gopher_fileblob_begin(GopherFileBlob* blob) {
     82   assert(blob != NULL);
     83   gopher_page_begin(blob->page);
     84 }
     85 
     86 void gopher_fileblob_add_file(GopherFileBlob* blob, const GitFile* file) {
     87   assert(blob != NULL);
     88   assert(file != NULL);
     89   FILE* out = blob->out;
     90 
     91   char path[PATH_MAX];
     92   estrlcpy(path, file->repo_path, sizeof(path));
     93   const char* filename = basename(path);
     94   if (!filename) {
     95     err(1, "basename");
     96   }
     97   print_gopher_text(out, filename, false);
     98   fprintf(out, " (%zdB)\n", file->size_bytes);
     99   fprintf(out, "---\n");
    100 
    101   ssize_t size_lines = file->size_lines;
    102   if (size_lines == -1) {
    103     fprintf(out, "Binary file.\n");
    104     return;
    105   }
    106   if (size_lines == -2) {
    107     fprintf(out, "File too large to display.\n");
    108     return;
    109   }
    110 
    111   assert(file->content != NULL);
    112   size_t i = 0;
    113   const char* end = file->content + file->size_bytes;
    114   const char* cur_line = file->content;
    115   while (cur_line < end) {
    116     const char* next_line = memchr(cur_line, '\n', end - cur_line);
    117     size_t len = (next_line ? next_line : end) - cur_line;
    118 
    119     i++;
    120     fprintf(out, "%6zu ", i);
    121     print_gopher_text_len(out, cur_line, len, false);
    122     fprintf(out, "\n");
    123 
    124     if (next_line) {
    125       cur_line = next_line + 1;
    126     } else {
    127       break;
    128     }
    129   }
    130 }
    131 
    132 void gopher_fileblob_end(GopherFileBlob* blob) {
    133   assert(blob != NULL);
    134   gopher_page_end(blob->page);
    135 }