Commit: 20dfa8aa2856a4cd35e76010dcd5023d4c7dbe82
Author: Campbell Barton
Date:   Sun Feb 24 23:42:05 2019 +1100
Branches: master
https://developer.blender.org/rB20dfa8aa2856a4cd35e76010dcd5023d4c7dbe82

readfile: minor changes to support other compression formats

Allow different readers to re-use the file descriptor.

===================================================================

M       source/blender/blenloader/intern/readfile.c

===================================================================

diff --git a/source/blender/blenloader/intern/readfile.c 
b/source/blender/blenloader/intern/readfile.c
index 6fc96c88d2d..bb7ff4efaa5 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -1271,28 +1271,20 @@ static FileData *blo_decode_and_check(FileData *fd, 
ReportList *reports)
        return fd;
 }
 
-static FileData *blo_filedata_from_file_open(const char *filepath, ReportList 
*reports)
+static FileData *blo_filedata_from_file_descriptor(const char *filepath, 
ReportList *reports, int file)
 {
        FileDataReadFn *read_fn = NULL;
        FileDataSeekFn *seek_fn = NULL;  /* Optional. */
 
-       int file = -1;
        gzFile gzfile = (gzFile)Z_NULL;
 
        char header[7];
 
        /* Regular file. */
        errno = 0;
-       file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
-       if (file == -1) {
-               BKE_reportf(reports, RPT_WARNING, "Unable to open '%s': %s",
-                           filepath, errno ? strerror(errno) : TIP_("unknown 
error reading file"));
-               return NULL;
-       }
-       else if (read(file, header, sizeof(header)) != sizeof(header)) {
+       if (read(file, header, sizeof(header)) != sizeof(header)) {
                BKE_reportf(reports, RPT_WARNING, "Unable to read '%s': %s",
                            filepath, errno ? strerror(errno) : 
TIP_("insufficient content"));
-               close(file);
                return NULL;
        }
        else {
@@ -1304,10 +1296,6 @@ static FileData *blo_filedata_from_file_open(const char 
*filepath, ReportList *r
                read_fn = fd_read_data_from_file;
                seek_fn = fd_seek_data_from_file;
        }
-       else {
-               close(file);
-               file = -1;
-       }
 
        /* Gzip file. */
        errno = 0;
@@ -1324,6 +1312,8 @@ static FileData *blo_filedata_from_file_open(const char 
*filepath, ReportList *r
                else {
                        /* 'seek_fn' is too slow for gzip, don't set it. */
                        read_fn = fd_read_gzip_from_file;
+                       /* Caller must close. */
+                       file = -1;
                }
        }
 
@@ -1343,6 +1333,22 @@ static FileData *blo_filedata_from_file_open(const char 
*filepath, ReportList *r
        return fd;
 }
 
+static FileData *blo_filedata_from_file_open(const char *filepath, ReportList 
*reports)
+{
+       errno = 0;
+       const int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
+       if (file == -1) {
+               BKE_reportf(reports, RPT_WARNING, "Unable to open '%s': %s",
+                           filepath, errno ? strerror(errno) : TIP_("unknown 
error reading file"));
+               return NULL;
+       }
+       FileData *fd = blo_filedata_from_file_descriptor(filepath, reports, 
file);
+       if ((fd == NULL) || (fd->filedes == -1)) {
+               close(file);
+       }
+       return fd;
+}
+
 /* cannot be called with relative paths anymore! */
 /* on each new library added, it now checks for the current FileData and 
expands relativeness */
 FileData *blo_filedata_from_file(const char *filepath, ReportList *reports)

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to