From f5fabb01ba27fa223252926a4c3a4ec018da71ad Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 18 Nov 2010 13:54:35 +0100 Subject: [PATCH] Determine realpath of path --- ft/document.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- ft/document.h | 2 +- ft/pdf/pdf.c | 36 +++++++++++++++++++++++++++++++++++- ft/pdf/pdf.h | 6 ++++++ 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/ft/document.c b/ft/document.c index 387be7c..925791a 100644 --- a/ft/document.c +++ b/ft/document.c @@ -1,8 +1,13 @@ /* See LICENSE file for license and copyright information */ +#define _BSD_SOURCE +#define _XOPEN_SOURCE 500 +// TODO: Implement realpath + #include #include #include +#include #include "document.h" #include "../utils.h" @@ -32,13 +37,33 @@ zathura_document_open(const char* path, const char* password) return NULL; } - zathura_document_t* document = malloc(sizeof(zathura_document_t)); - if(!document) { + /* determine real path */ + size_t path_max; +#ifdef PATH_MAX + path_max = PATH_MAX; +#else + path_max = pathconf(path,_PC_PATH_MAX); + if(path_max <= 0) + path_max = 4096; +#endif + + char* real_path = malloc(sizeof(char) * path_max); + if(!real_path) { return NULL; } + if(!realpath(path, real_path)) { + free(real_path); + return NULL; + } - document->file_path = path; + zathura_document_t* document = malloc(sizeof(zathura_document_t)); + if(!document) { + free(real_path); + return NULL; + } + + document->file_path = real_path; document->password = password; document->current_page_number = 0; document->number_of_pages = 0; @@ -64,6 +89,11 @@ zathura_document_open(const char* path, const char* password) if(zathura_document_plugins[i].open_function) { if(zathura_document_plugins[i].open_function(document)) { return document; + } else { + fprintf(stderr, "error: could not open file\n"); + free(real_path); + free(document); + return NULL; } } } @@ -71,6 +101,9 @@ zathura_document_open(const char* path, const char* password) fprintf(stderr, "error: unknown file type\n"); + free(real_path); + free(document); + return NULL; } @@ -83,12 +116,21 @@ zathura_document_free(zathura_document_t* document) if(!document->functions.document_free) { fprintf(stderr, "error: %s not implemented\n", __FUNCTION__); + + if(document->file_path) { + free(document->file_path); + } + free(document); return true; } bool r = document->functions.document_free(document); + if(document->file_path) { + free(document->file_path); + } + free(document); return r; diff --git a/ft/document.h b/ft/document.h index 9976639..5defeb5 100644 --- a/ft/document.h +++ b/ft/document.h @@ -70,7 +70,7 @@ typedef struct zathura_page_s struct zathura_document_s { - const char* file_path; + char* file_path; const char* password; unsigned int current_page_number; unsigned int number_of_pages; diff --git a/ft/pdf/pdf.c b/ft/pdf/pdf.c index 35b0995..130db05 100644 --- a/ft/pdf/pdf.c +++ b/ft/pdf/pdf.c @@ -21,13 +21,47 @@ pdf_document_open(zathura_document_t* document) document->functions.page_render = pdf_page_render; document->functions.page_free = pdf_page_free; + document->data = malloc(sizeof(pdf_document_t)); + if(!document->data) { + return false; + } + + /* format path */ + GError* error = NULL; + char* file_uri = g_filename_to_uri(document->file_path, NULL, &error); + + if(!file_uri) { + fprintf(stderr, "error: could not open file: %s\n", error->message); + g_error_free(error); + free(document->data); + return false; + } + + pdf_document_t* pdf_document = (pdf_document_t*) document->data; + pdf_document->document = poppler_document_new_from_file(file_uri, document->password, &error); + + if(!pdf_document->document) { + fprintf(stderr, "error: could not open file: %s\n", error->message); + g_error_free(error); + free(document->data); + return false; + } + return true; } bool pdf_document_free(zathura_document_t* document) { - return false; + if(!document) { + return false; + } + + if(document->data) { + free(document->data); + } + + return true; } zathura_list_t* diff --git a/ft/pdf/pdf.h b/ft/pdf/pdf.h index 1d7563d..971bedc 100644 --- a/ft/pdf/pdf.h +++ b/ft/pdf/pdf.h @@ -4,9 +4,15 @@ #define PDF_H #include +#include #include "../document.h" +typedef struct pdf_document_s +{ + PopplerDocument *document; +} pdf_document_t; + bool pdf_document_open(zathura_document_t* document); bool pdf_document_free(zathura_document_t* document); zathura_list_t* pdf_document_index_generate(zathura_document_t* document);