diff options
author | Jeremias Stotter <jeremias@stotter.eu> | 2022-04-18 00:56:19 +0200 |
---|---|---|
committer | Jeremias Stotter <jeremias@stotter.eu> | 2022-04-18 00:56:19 +0200 |
commit | dd0640bf637a74ebfeb4773b82ef1a2bdaa3ba59 (patch) | |
tree | e7c7e1420e6c6b259b4f87604201bfebd2370c0b | |
parent | ddd301d3bfd80e7008e9fbd5bab4d6bf904d5bc6 (diff) | |
download | JBlog-dd0640bf637a74ebfeb4773b82ef1a2bdaa3ba59.tar.gz JBlog-dd0640bf637a74ebfeb4773b82ef1a2bdaa3ba59.tar.bz2 JBlog-dd0640bf637a74ebfeb4773b82ef1a2bdaa3ba59.zip |
Added O_TRUNC to fix a bug and made code more compact
-rw-r--r-- | admin.c | 50 |
1 files changed, 19 insertions, 31 deletions
@@ -411,51 +411,39 @@ int post_admin(char* buffer, size_t buffer_size, char* req_content, size_t req_c char index_dtypes[128] = ""; sprintf(index_dtypes, "%s %s", dtype_entry ? "Entry" : "", dtype_index ? "Index" : ""); sanitize_path(publish_to); + char write_path[PATH_MAX] = ""; + bool save = false; if(strcmp(sub_str, "Process") == 0) { // Process a file process_blog_text(buffer, buffer_size, md_input, title, author, date_str, time_str); return 0; } else if(strcmp(sub_str, "Save") == 0) { // Save a file + save = true; if(adm_save_dir == NULL) { strncpy(buffer, "<p>No admin save directory specified!</p>", buffer_size); return 1; } - char save_file_path[PATH_MAX] = ""; - snprintf(save_file_path, PATH_MAX, "%s/%s", adm_save_dir, publish_to); - int save_file = open(save_file_path, O_CREAT | O_RDWR, 0644); - if(save_file < 0) { - snprintf(buffer, buffer_size, "<p><b>Error saving \"%s\": %s</b></p>", publish_to, strerror(errno)); - return 1; - } - if(dprintf(save_file, JBLOG_FILEPATTERN, date_str, time_str, author, title, blog_type, index_dtypes, index_depth, md_input) < 0) { - snprintf(buffer, buffer_size, "<p><b>Error saving \"%s\": %s</b></p>", publish_to, strerror(errno)); - close(save_file); - return 1; - } - - snprintf(buffer, buffer_size, "<p><b>Saved successfully to %s</b></p>", publish_to); - close(save_file); - return 1; + snprintf(write_path, PATH_MAX, "%s/%s", adm_save_dir, publish_to); } else if(strcmp(sub_str, "Publish") == 0) { // Publish a file - char publish_path[PATH_MAX] = ""; - snprintf(publish_path, PATH_MAX, "%s/%s", base_path, publish_to); - int publish_file = open(publish_path, O_CREAT | O_RDWR, 0644); - if(publish_file < 0) { - snprintf(buffer, buffer_size, "<p><b>Error publishing \"%s\": %s</b></p>", publish_to, strerror(errno)); - return 1; - } - if(dprintf(publish_file, JBLOG_FILEPATTERN, date_str, time_str, author, title, blog_type, index_dtypes, index_depth, md_input) < 0) { - snprintf(buffer, buffer_size, "<p><b>Error saving \"%s\": %s</b></p>", publish_to, strerror(errno)); - close(publish_file); - return 1; - } - - snprintf(buffer, buffer_size, "<p><b>Published successfully to <a target=\"_blank\" href=\"%s/!\">%s</a> (Click to refresh cache)</b></p>", publish_to, publish_to); - close(publish_file); + snprintf(write_path, PATH_MAX, "%s/%s", base_path, publish_to); + } + // Actually write the file to disk + int write_fd = open(write_path, O_CREAT | O_RDWR | O_TRUNC, 0644); + if(write_fd < 0) { + snprintf(buffer, buffer_size, "<p><b>Error saving \"%s\": %s</b></p>", publish_to, strerror(errno)); + return 1; + } + if(dprintf(write_fd, JBLOG_FILEPATTERN, date_str, time_str, author, title, blog_type, index_dtypes, index_depth, md_input) < 0) { + snprintf(buffer, buffer_size, "<p><b>Error saving \"%s\": %s</b></p>", publish_to, strerror(errno)); + close(write_fd); return 1; } + snprintf(buffer, buffer_size, "<p><b>%s successfully to <a target=\"_blank\" href=\"%s/!\">%s</a> (Click to refresh cache)</b></p>", + save ? "Saved" : "Published" , publish_to, publish_to); + close(write_fd); + return 1; } if(folder_name && sub_str && folder_name) { |