Move truncate() wrapper to futil.h
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 19 Oct 2014 09:36:02 +0000 (12:36 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Thu, 23 Oct 2014 13:51:28 +0000 (16:51 +0300)
- Isolate platform-dependent file handling code better, removing
  some platform-dependent #ifdefs and #includes from higher-level files.
- Make 'gmx trjconv -trunc' independent of the platform (should now also
  work on Windows).
- Don't leak file handles when truncating on Windows.

Change-Id: I66162978c7b29499c8fc796d15b5f2649abf59dd

src/gromacs/gmxana/gmx_trjconv.c
src/gromacs/gmxlib/checkpoint.cpp
src/gromacs/utility/futil.cpp
src/gromacs/utility/futil.h

index 9e4833c783f4aa4d92636c50a12ffb396273e485..242a6031b075c824f6ca6373e441350295213026 100644 (file)
  */
 #include "gmxpre.h"
 
-#include "config.h"
-
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/confio.h"
 #include "gromacs/fileio/gmxfio.h"
@@ -475,7 +469,6 @@ void check_trn(const char *fn)
     }
 }
 
-#ifndef GMX_NATIVE_WINDOWS
 void do_trunc(const char *fn, real t0)
 {
     t_fileio        *in;
@@ -531,7 +524,7 @@ void do_trunc(const char *fn, real t0)
             {
                 fprintf(stderr, "Once again, I'm gonna DO this...\n");
                 close_trn(in);
-                if (0 != truncate(fn, fpos))
+                if (0 != gmx_truncate(fn, fpos))
                 {
                     gmx_fatal(FARGS, "Error truncating file %s", fn);
                 }
@@ -548,7 +541,6 @@ void do_trunc(const char *fn, real t0)
         }
     }
 }
-#endif
 
 /*! \brief Read a full molecular topology if useful and available.
  *
@@ -828,11 +820,9 @@ int gmx_trjconv(int argc, char *argv[])
           { &bVels }, "Read and write velocities if possible" },
         { "-force", FALSE, etBOOL,
           { &bForce }, "Read and write forces if possible" },
-#ifndef GMX_NATIVE_WINDOWS
         { "-trunc", FALSE, etTIME,
           { &ttrunc },
           "Truncate input trajectory file after this time (%t)" },
-#endif
         { "-exec", FALSE, etSTR,
           { &exec_command },
           "Execute command for every output frame with the "
@@ -940,9 +930,7 @@ int gmx_trjconv(int argc, char *argv[])
 
     if (ttrunc != -1)
     {
-#ifndef GMX_NATIVE_WINDOWS
         do_trunc(in_file, ttrunc);
-#endif
     }
     else
     {
index a9e1ae0f7013a5bcce8e4b5626b5e66cbbaa4842..cf722fcff0d48e57c71ad052615dafd5743cb2c6 100644 (file)
 #include <string.h>
 
 #include <fcntl.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #ifdef GMX_NATIVE_WINDOWS
-/* _chsize_s */
 #include <io.h>
 #include <sys/locking.h>
 #endif
@@ -153,33 +148,6 @@ const char *edfh_names[edfhNR] =
     "accumulated_plus", "accumulated_minus", "accumulated_plus_2",  "accumulated_minus_2", "Tij", "Tij_empirical"
 };
 
-#ifdef GMX_NATIVE_WINDOWS
-static int
-gmx_wintruncate(const char *filename, __int64 size)
-{
-#ifdef GMX_FAHCORE
-    /*we do this elsewhere*/
-    return 0;
-#else
-    FILE *fp;
-
-    fp = fopen(filename, "rb+");
-
-    if (fp == NULL)
-    {
-        return -1;
-    }
-
-#ifdef _MSC_VER
-    return _chsize_s( fileno(fp), size);
-#else
-    return _chsize( fileno(fp), size);
-#endif
-#endif
-}
-#endif
-
-
 enum {
     ecprREAL, ecprRVEC, ecprMATRIX
 };
@@ -2240,15 +2208,14 @@ static void read_checkpoint(const char *fn, FILE **pfplog,
 
             if (i != 0) /*log file is already seeked to correct position */
             {
-#ifdef GMX_NATIVE_WINDOWS
-                rc = gmx_wintruncate(outputfiles[i].filename, outputfiles[i].offset);
-#else
-                rc = truncate(outputfiles[i].filename, outputfiles[i].offset);
-#endif
+#if !defined(GMX_NATIVE_WINDOWS) || !defined(GMX_FAHCORE)
+                /* For FAHCORE, we do this elsewhere*/
+                rc = gmx_truncate(outputfiles[i].filename, outputfiles[i].offset);
                 if (rc != 0)
                 {
                     gmx_fatal(FARGS, "Truncation of file %s failed. Cannot do appending because of this failure.", outputfiles[i].filename);
                 }
+#endif
             }
         }
     }
index 546b875808bc1d69105bab65ac8a1fefb263a057..c59c4dffb0b03b5a396959c1b8fd23a6878ada76 100644 (file)
 /* POSIX */
 #include <dirent.h>
 #endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-
 #ifdef GMX_NATIVE_WINDOWS
 #include <direct.h>
 #include <io.h>
 #include <windows.h>
 #endif
 
-/* Windows file stuff, only necessary for visual studio */
-#ifdef _MSC_VER
-#include <windows.h>
-#endif
-
 #include "thread_mpi/threads.h"
 
 #include "gromacs/utility/cstringutil.h"
@@ -246,6 +239,26 @@ gmx_off_t gmx_ftell(FILE *stream)
 #endif
 }
 
+int gmx_truncate(const char *filename, gmx_off_t length)
+{
+#ifdef GMX_NATIVE_WINDOWS
+    FILE *fp = fopen(filename, "rb+");
+    if (fp == NULL)
+    {
+        return -1;
+    }
+#ifdef _MSC_VER
+    int rc = _chsize_s(fileno(fp), length);
+#else
+    int rc = _chsize(fileno(fp), length);
+#endif
+    fclose(fp);
+    return rc;
+#else
+    return truncate(filename, length);
+#endif
+}
+
 static FILE *uncompress(const char *fn, const char *mode)
 {
     FILE *fp;
index 8e669e09d301516d7f99fee65f0732651dd84371..46a0bc1c75358fc2529b421d83823680518eebc7 100644 (file)
@@ -149,6 +149,9 @@ int gmx_fseek(FILE *stream, gmx_off_t offset, int whence);
 /** OS-independent 64-bit ftell(). */
 gmx_off_t gmx_ftell(FILE *stream);
 
+/** OS-independent truncate(). */
+int gmx_truncate(const char *filename, gmx_off_t length);
+
 /*! \brief
  * Finds full path for a library file.
  *