#include #include #include #include #include #include #include #include #include "cms_build.h" #include "cms_err.h" #include "cms_files.h" #include "cms_util.h" #include "cms_page.h" #include "cms_post.h" #include cms_err* cms_build_write_files(cms_page* root, char* path) { char* dirpath = cms_util_path_join(path, root->slug); if (dirpath == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); if (mkdir(dirpath, 077) == -1 && errno != EEXIST) return cms_err_from_std_err(errno); //Go through each post in the page, writing it to disk size_t i; for (i = 0; i < root->numposts; ++i) { cms_post post = root->posts[i]; char* postdirpath = cms_util_path_join(dirpath, post.slug); if (postdirpath == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); if (mkdir(postdirpath, 0777) == -1 && errno != EEXIST) return cms_err_from_std_err(errno); char* filepath = cms_util_path_join(postdirpath, CMS_FILE_INDEX); if (postdirpath == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); int file = open(filepath, O_WRONLY | O_CREAT, 0777); if (file == -1) return cms_err_from_std_err(errno); write(file, post.html, strlen(post.html)); close(file); free(filepath); free(postdirpath); } //Go through each sub in the page recursively for (i = 0; i < root->numsubs; ++i) { cms_page sub = root->subs[i]; char* subdirpath = cms_util_path_join(dirpath, sub.slug); if (subdirpath == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); cms_build_write_files(&sub, subdirpath); free(subdirpath); } free(dirpath); } cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname) { struct dirent** namelist; int n = scandir(path, &namelist, 0, alphasort); if (n == -1) return cms_err_from_std_err(errno); struct dirent* ep; struct stat* st = malloc(sizeof(struct stat)); if (st == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); for (int i = 0; i < n; ++i) { ep = namelist[i]; if (ep->d_name[0] == '.') continue; char* entpath = cms_util_path_join(path, ep->d_name); stat(entpath, st); //Entry is directory, recurse if (S_ISDIR(st->st_mode)) { cms_page* sub = cms_page_create(); if (sub == NULL) return cms_err_create(CMS_ERR_ALLOC, NULL); cms_err* err; err = cms_build_make_tree(sub, entpath, ep->d_name); if (err) return err; err = cms_page_add_sub(root, sub); if (err) return err; } //Entry is the file which contains metadata //about a page, parse else if (strcmp(ep->d_name, CMS_FILE_PAGE) == 0) { char* content = cms_util_file_read(entpath); if (content == NULL) return cms_err_create(CMS_ERR_FILEREAD, entpath); cms_err* err = cms_page_parse(root, content, dirname); free(content); if (err) return err; } //Entry is a post, read it and add to page else { cms_post* post = cms_post_create(); char* content = cms_util_file_read(entpath); if (content == NULL) return cms_err_create(CMS_ERR_FILEREAD, entpath); cms_err* err; err = cms_post_parse(post, content, ep->d_name); free(content); if (err) return err; err = cms_page_add_post(root, post); if (err) return err; } free(entpath); } free(st); free(namelist); return cms_err_create(CMS_ERR_NONE, NULL); }