[Bf-blender-cvs] [9a116c7c2d9] master: Cleanup: 64 bit file IO on windows.

Ray Molenkamp noreply at git.blender.org
Wed Mar 18 19:13:09 CET 2020


Commit: 9a116c7c2d957e0e10a926eccc38dcb28cfec6e4
Author: Ray Molenkamp
Date:   Wed Mar 18 12:13:03 2020 -0600
Branches: master
https://developer.blender.org/rB9a116c7c2d957e0e10a926eccc38dcb28cfec6e4

Cleanup: 64 bit file IO on windows.

Unlike Linux where fseek/tell will be either 32 or 64 bit
depending on the target platform, it will always be 32 bit
on windows.

We had some macro magic in BLI_winstuff.h that substituted
them for 64 bit versions, but that is upsetting the system
headers if they get included after BLI_winstuff.h which
is problematic for D6811.

This diff adds proper functions in blenlib and updates
all calls that were using the BLI_winstuff.h header to
gain 64 bit file IO.

note: Anything that was using the 32 bit versions (ie not
including BLI_winstuff.h) will still be using the 32 bit
versions, which is perhaps a good code quality Friday project.

Differential Revision: https://developer.blender.org/D7160

Reviewers: brecht dfelinto

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

M	source/blender/blenkernel/intern/pointcache.c
M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/BLI_winstuff.h
M	source/blender/blenlib/intern/storage.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/io/avi/intern/avi.c
M	source/blender/io/avi/intern/avi_options.c
M	source/blender/modifiers/intern/MOD_meshcache_mdd.c
M	source/blender/modifiers/intern/MOD_meshcache_pc2.c

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

diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c
index b0163436cc4..19d77fcd6db 100644
--- a/source/blender/blenkernel/intern/pointcache.c
+++ b/source/blender/blenkernel/intern/pointcache.c
@@ -2385,7 +2385,7 @@ static int ptcache_file_header_begin_read(PTCacheFile *pf)
 
   /* if there was an error set file as it was */
   if (error) {
-    fseek(pf->fp, 0, SEEK_SET);
+    BLI_fseek(pf->fp, 0, SEEK_SET);
   }
 
   return !error;
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index d908c47b400..8596a60bc6a 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -70,6 +70,10 @@ typedef struct stat BLI_stat_t;
 
 int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
+int64_t BLI_ftell(FILE *stream) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
+int BLI_fseek(FILE *stream, int64_t offset, int whence);
+int64_t BLI_lseek(int fd, int64_t offset, int whence);
+
 #ifdef WIN32
 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer);
 #endif
diff --git a/source/blender/blenlib/BLI_winstuff.h b/source/blender/blenlib/BLI_winstuff.h
index eddb69e84ec..08d29a328b4 100644
--- a/source/blender/blenlib/BLI_winstuff.h
+++ b/source/blender/blenlib/BLI_winstuff.h
@@ -80,13 +80,6 @@ extern "C" {
 
 typedef unsigned int mode_t;
 
-/* use functions that take a 64 bit offset for files larger than 4GB */
-#include <stdio.h>
-#define fseek(stream, offset, origin) _fseeki64(stream, offset, origin)
-#define ftell(stream) _ftelli64(stream)
-#define lseek(fd, offset, origin) _lseeki64(fd, offset, origin)
-#define tell(fd) _telli64(fd)
-
 #ifndef _SSIZE_T_
 #  define _SSIZE_T_
 /* python uses HAVE_SSIZE_T */
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index 7743ced1344..de16960eb85 100644
--- a/source/blender/blenlib/intern/storage.c
+++ b/source/blender/blenlib/intern/storage.c
@@ -175,6 +175,33 @@ double BLI_dir_free_space(const char *dir)
 #endif
 }
 
+int64_t BLI_ftell(FILE *stream)
+{
+#ifdef WIN32
+  return _ftelli64(stream);
+#else
+  return ftell(stream);
+#endif
+}
+
+int BLI_fseek(FILE *stream, int64_t offset, int whence)
+{
+#ifdef WIN32
+  return _fseeki64(stream, offset, whence);
+#else
+  return fseek(stream, offset, whence);
+#endif
+}
+
+int64_t BLI_lseek(int fd, int64_t offset, int whence)
+{
+#ifdef WIN32
+  return _lseeki64(fd, offset, whence);
+#else
+  return lseek(fd, offset, whence);
+#endif
+}
+
 /**
  * Returns the file size of an opened file descriptor.
  */
@@ -383,15 +410,15 @@ static void *file_read_data_as_mem_impl(FILE *fp,
   if (S_ISDIR(st.st_mode)) {
     return NULL;
   }
-  if (fseek(fp, 0L, SEEK_END) == -1) {
+  if (BLI_fseek(fp, 0L, SEEK_END) == -1) {
     return NULL;
   }
   /* Don't use the 'st_size' because it may be the symlink. */
-  const long int filelen = ftell(fp);
+  const long int filelen = BLI_ftell(fp);
   if (filelen == -1) {
     return NULL;
   }
-  if (fseek(fp, 0L, SEEK_SET) == -1) {
+  if (BLI_fseek(fp, 0L, SEEK_SET) == -1) {
     return NULL;
   }
 
@@ -519,9 +546,9 @@ LinkNode *BLI_file_read_as_lines(const char *name)
     return NULL;
   }
 
-  fseek(fp, 0, SEEK_END);
-  size = (size_t)ftell(fp);
-  fseek(fp, 0, SEEK_SET);
+  BLI_fseek(fp, 0, SEEK_END);
+  size = (size_t)BLI_ftell(fp);
+  BLI_fseek(fp, 0, SEEK_SET);
 
   if (UNLIKELY(size == (size_t)-1)) {
     fclose(fp);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 941783d1bec..f7c995d598e 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -1171,7 +1171,7 @@ static int fd_read_data_from_file(FileData *filedata,
 
 static off64_t fd_seek_data_from_file(FileData *filedata, off64_t offset, int whence)
 {
-  filedata->file_offset = lseek(filedata->filedes, offset, whence);
+  filedata->file_offset = BLI_lseek(filedata->filedes, offset, whence);
   return filedata->file_offset;
 }
 
@@ -1349,7 +1349,7 @@ static FileData *blo_filedata_from_file_descriptor(const char *filepath,
     return NULL;
   }
   else {
-    lseek(file, 0, SEEK_SET);
+    BLI_lseek(file, 0, SEEK_SET);
   }
 
   /* Regular file. */
diff --git a/source/blender/io/avi/intern/avi.c b/source/blender/io/avi/intern/avi.c
index 22eb0be0cc0..0901d03a90f 100644
--- a/source/blender/io/avi/intern/avi.c
+++ b/source/blender/io/avi/intern/avi.c
@@ -249,7 +249,7 @@ bool AVI_is_avi(const char *name)
   movie.header->Reserved[2] = GET_FCC(movie.fp);
   movie.header->Reserved[3] = GET_FCC(movie.fp);
 
-  fseek(movie.fp, movie.header->size - 14 * 4, SEEK_CUR);
+  BLI_fseek(movie.fp, movie.header->size - 14 * 4, SEEK_CUR);
 
   /* Limit number of streams to some reasonable amount to prevent
    * buffer overflow vulnerabilities. */
@@ -312,7 +312,7 @@ bool AVI_is_avi(const char *name)
     movie.streams[temp].sh.right = GET_TCC(movie.fp);
     movie.streams[temp].sh.bottom = GET_TCC(movie.fp);
 
-    fseek(movie.fp, movie.streams[temp].sh.size - 14 * 4, SEEK_CUR);
+    BLI_fseek(movie.fp, movie.streams[temp].sh.size - 14 * 4, SEEK_CUR);
 
     if (GET_FCC(movie.fp) != FCC("strf")) {
       DEBUG_PRINT("no stream format information\n");
@@ -362,27 +362,27 @@ bool AVI_is_avi(const char *name)
         }
       }
       if (j > 0) {
-        fseek(movie.fp, j, SEEK_CUR);
+        BLI_fseek(movie.fp, j, SEEK_CUR);
       }
     }
     else {
-      fseek(movie.fp, movie.streams[temp].sf_size, SEEK_CUR);
+      BLI_fseek(movie.fp, movie.streams[temp].sf_size, SEEK_CUR);
     }
 
     /* Walk to the next LIST */
     while (GET_FCC(movie.fp) != FCC("LIST")) {
       temp = GET_FCC(movie.fp);
-      if (temp < 0 || ftell(movie.fp) > movie.size) {
+      if (temp < 0 || BLI_ftell(movie.fp) > movie.size) {
         DEBUG_PRINT("incorrect size in header or error in AVI\n");
 
         MEM_freeN(movie.streams);
         fclose(movie.fp);
         return 0;
       }
-      fseek(movie.fp, temp, SEEK_CUR);
+      BLI_fseek(movie.fp, temp, SEEK_CUR);
     }
 
-    fseek(movie.fp, -4L, SEEK_CUR);
+    BLI_fseek(movie.fp, -4L, SEEK_CUR);
   }
 
   MEM_freeN(movie.streams);
@@ -437,7 +437,7 @@ AviError AVI_open_movie(const char *name, AviMovie *movie)
   movie->header->Reserved[2] = GET_FCC(movie->fp);
   movie->header->Reserved[3] = GET_FCC(movie->fp);
 
-  fseek(movie->fp, movie->header->size - 14 * 4, SEEK_CUR);
+  BLI_fseek(movie->fp, movie->header->size - 14 * 4, SEEK_CUR);
 
   /* Limit number of streams to some reasonable amount to prevent
    * buffer overflow vulnerabilities. */
@@ -493,7 +493,7 @@ AviError AVI_open_movie(const char *name, AviMovie *movie)
     movie->streams[temp].sh.right = GET_TCC(movie->fp);
     movie->streams[temp].sh.bottom = GET_TCC(movie->fp);
 
-    fseek(movie->fp, movie->streams[temp].sh.size - 14 * 4, SEEK_CUR);
+    BLI_fseek(movie->fp, movie->streams[temp].sh.size - 14 * 4, SEEK_CUR);
 
     if (GET_FCC(movie->fp) != FCC("strf")) {
       DEBUG_PRINT("no stream format information\n");
@@ -540,24 +540,24 @@ AviError AVI_open_movie(const char *name, AviMovie *movie)
         }
       }
       if (j > 0) {
-        fseek(movie->fp, j, SEEK_CUR);
+        BLI_fseek(movie->fp, j, SEEK_CUR);
       }
     }
     else {
-      fseek(movie->fp, movie->streams[temp].sf_size, SEEK_CUR);
+      BLI_fseek(movie->fp, movie->streams[temp].sf_size, SEEK_CUR);
     }
 
     /* Walk to the next LIST */
     while (GET_FCC(movie->fp) != FCC("LIST")) {
       temp = GET_FCC(movie->fp);
-      if (temp < 0 || ftell(movie->fp) > movie->size) {
+      if (temp < 0 || BLI_ftell(movie->fp) > movie->size) {
         DEBUG_PRINT("incorrect size in header or error in AVI\n");
         return AVI_ERROR_FORMAT;
       }
-      fseek(movie->fp, temp, SEEK_CUR);
+      BLI_fseek(movie->fp, temp, SEEK_CUR);
     }
 
-    fseek(movie->fp, -4L, SEEK_CUR);
+    BLI_fseek(movie->fp, -4L, SEEK_CUR);
   }
 
   while (1) {
@@ -573,24 +573,24 @@ AviError AVI_open_movie(const char *name, AviMovie *movie)
         break;
       }
       else {
-        fseek(movie->fp, size - 4, SEEK_CUR);
+        BLI_fseek(movie->fp, size - 4, SEEK_CUR);
       }
     }
     else {
-      fseek(movie->fp, size, SEEK_CUR);
+      BLI_fseek(movie->fp, size, SEEK_CUR);
     }
-    if (ftell(movie->fp) > movie->size) {
+    if (BLI_ftell(movie->fp) > movie->size) {
       DEBUG_PRINT("incorrect size in header or error in AVI\n");
       return AVI_ERROR_FORMAT;
     }
   }
 
-  movie->movi_offset = ftell(movie->fp);
+  movie->movi_offset = BLI_ftell(movie->fp);
   movie->read_offset = movie->movi_offset;
 
   /* Read in the index if the file has one, otherwise create one */
   if (movie->header->Flags & AVIF_HASINDEX) {
-    fseek(movie->fp, size - 4, SEEK_CUR);
+    BLI_fseek(movie->fp, size - 4, SEEK_CUR);
 
     if (GET_FCC(movie->fp) != FCC("idx1")) {
       DEBUG_PRINT("bad index informatio\n");
@@ -669,7 +669,7 @@ void *AVI_read_frame(AviMovie *movie, AviFormat format, int frame, int stream)
     return NULL;
   }
 
-  fseek(movie->fp, movie->read_offset + movie->entries[i - 1].Offset, SEEK_SET);
+  BLI_fseek(movie->fp, movie->read_offset + movie->entries[i - 1].Offset, SEEK_SET);
 
   size_t size = GET_FCC(movie->fp);
   buffer = MEM_mallocN(size, "readbuffer");
@@ -834,9 +834,9 @@ AviError AVI_open_compress(char *name, AviMovie *movie, int streams, ...)
 
   awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST);
 
-  header_pos1 = ftell(movie->fp);
+  header_pos1 = BLI_ftell(movie->fp);
 
-  movie->offset_table[0] = ftell(movie->fp);
+  movie->offset_table[0] = BLI_ftell(movie->fp);
 
   awrite(movie, movie->header, 1, sizeof(AviMainHeader), movie->fp, AVI_MAINH);
 
@@ -847,24 +847,24 @@ AviError AVI_open_compress(char *name, AviMovie *movie, int streams, ...)
 
     awrite(movie, &list, 1, sizeof(AviList), movie->fp, AVI_LIST);
 
-    stream_pos1 = ftell(movie->fp);
+    stream_pos1 = BLI_ftell(movie->fp);
 
-    movie->offset_table[1 + i * 2] = ftell(movie->fp);
+    movie->offset_table[1 + i * 2] = BLI_ftell(m

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list