fixed case of pre-allocated files (reported size of the file might be greater than...
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 5 Feb 2017 17:49:51 +0000 (18:49 +0100)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 5 Feb 2017 17:49:51 +0000 (18:49 +0100)
src/lib/pio.c
src/lib/pio.h

index 7bb3d7c..1942348 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2016 jeanfi@gmail.com
+ * Copyright (C) 2010-2017 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -150,11 +150,13 @@ void paths_free(char **paths)
 
 char *file_get_content(const char *fpath)
 {
-       long size;
-
+       long size, n;
        char *page;
 
+       log_fct_enter();
+
        size = file_get_size(fpath);
+
        if (size == -1) {
                page = NULL;
 
@@ -167,19 +169,27 @@ char *file_get_content(const char *fpath)
 
                if (fp) {
                        page = malloc(size + 1);
-                       if (!page || size != fread(page, 1, size, fp)) {
-                               free(page);
-                               page = NULL;
-                       } else {
-                               *(page + size) = '\0';
+
+                       if (page) {
+                               clearerr(fp);
+                               n = fread(page, 1, size, fp);
+                               if (n != size && ferror(fp)) {
+                                       free(page);
+                                       page = NULL;
+                               } else {
+                                       *(page + n) = '\0';
+                               }
                        }
 
                        fclose(fp);
                } else {
+                       log_debug("failed to open %s", fpath);
                        page = NULL;
                }
        }
 
+       log_fct_exit();
+
        return page;
 }
 
index b7b0652..018706b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2010-2016 jeanfi@gmail.com
+ *  Copyright (C) 2010-2017 jeanfi@gmail.com
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -20,7 +20,7 @@
 #ifndef _P_IO_H
 #define _P_IO_H
 
-#define P_IO_VER 6
+#define P_IO_VER 7
 
 #include <sys/types.h>