From eb3621c118d57c485c23d63bfdb005d05bb73e11 Mon Sep 17 00:00:00 2001 From: tradke Date: Wed, 30 Jun 2004 14:16:34 +0000 Subject: Use the new routine IOUtil_AssembleFilename() to generate filenames for checkpoint/filereader files. This closes PR CactusPUGHIO 1061: "IOHDF5/DumpGH.c/Checkpoint writes to strings without checking the string lengths". git-svn-id: http://svn.cactuscode.org/arrangements/CactusPUGHIO/IOFlexIO/trunk@327 ebee0441-1374-4afa-a3b5-247f3ba15b9a --- src/DumpGH.c | 38 +++++++++++++++++++++----------------- src/RecoverGH.c | 21 ++++++++++++--------- src/ioFlexGH.h | 2 +- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/DumpGH.c b/src/DumpGH.c index 4693549..a4039e3 100644 --- a/src/DumpGH.c +++ b/src/DumpGH.c @@ -275,7 +275,7 @@ void IOFlexIOi_DumpGHExtensions (const cGH *GH, IOFile file) a new checkpoint file and then dumps away all grid variables. @enddesc - @calls IOUtil_PrepareFilename + @calls IOUtil_AssembleFilename IOUtil_PrintTimings IOFlexIOi_DumpParameters IOFlexIOi_DumpGHExtensions @@ -315,7 +315,6 @@ void IOFlexIOi_DumpGHExtensions (const cGH *GH, IOFile file) @@*/ static int Checkpoint (const cGH *GH, int called_from) { - char filename[1024], tmpfilename[1024]; IOFile file; int dim, groupvarsize, myproc, first_vindex, gindex, retval; cGroup gdata; @@ -323,12 +322,12 @@ static int Checkpoint (const cGH *GH, int called_from) ioRequest *request; flexioGH *myGH; const ioGH *ioUtilGH; - char *fullname; + char *filename, *tempname, *fullname; static char **cp_filenames = NULL; static int cp_fileindex = 0; - const char *timer_descriptions[3] = {"Time to dump parameters: ", - "Time to dump datasets: ", - "Total time to checkpoint:"}; + const char *timer_descriptions[] = {"Time to dump parameters: ", + "Time to dump datasets: ", + "Total time to checkpoint:"}; DECLARE_CCTK_PARAMETERS @@ -347,29 +346,30 @@ static int Checkpoint (const cGH *GH, int called_from) CCTK_TimerStartI (myGH->timers[2]); } - /* get the base filename ... */ myproc = CCTK_MyProc (GH); ioUtilGH = CCTK_GHExtension (GH, "IO"); - IOUtil_PrepareFilename (GH, NULL, filename, called_from, - myproc / ioUtilGH->ioproc_every, ioUtilGH->unchunked); - /* ... and append the extension */ - sprintf (tmpfilename, "%s.tmp.ieee", filename); - sprintf (filename, "%s.ieee", filename); + /* get the filenames for both the temporary and final checkpoint file */ + filename = IOUtil_AssembleFilename (GH, NULL, "", ".ieee", called_from, + myproc / ioUtilGH->ioproc_every, + ioUtilGH->unchunked); + tempname = IOUtil_AssembleFilename (GH, NULL, ".tmp", ".ieee", called_from, + myproc / ioUtilGH->ioproc_every, + ioUtilGH->unchunked); /* now open the file */ if (myproc == ioUtilGH->ioproc) { if (CCTK_Equals (verbose, "full")) { - CCTK_VInfo (CCTK_THORNSTRING, "Creating file '%s'", tmpfilename); + CCTK_VInfo (CCTK_THORNSTRING, "Creating file '%s'", tempname); } - file = IEEEopen (tmpfilename, "w"); + file = IEEEopen (tempname, "w"); if (! IOisValid (file)) { CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, "Can't open checkpoint file '%s'. Checkpointing is skipped", - tmpfilename); + tempname); return (-1); } } @@ -511,11 +511,11 @@ static int Checkpoint (const cGH *GH, int called_from) unlink an existing file */ unlink (filename); #endif - if (rename (tmpfilename, filename)) + if (rename (tempname, filename)) { CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING, "Could not rename temporary checkpoint file %s to %s", - tmpfilename, filename); + tempname, filename); } } @@ -543,5 +543,9 @@ static int Checkpoint (const cGH *GH, int called_from) /* save the iteration number of this checkpoint */ myGH->last_checkpoint_iteration = GH->cctk_iteration; + /* free allocated resources */ + free (tempname); + free (filename); + return (retval); } diff --git a/src/RecoverGH.c b/src/RecoverGH.c index c2de6c1..be1a3a4 100644 --- a/src/RecoverGH.c +++ b/src/RecoverGH.c @@ -117,6 +117,7 @@ int IOFlexIO_Recover (cGH *GH, const char *basefilename, int called_from) called_from == FILEREADER_DATA || (GH && (GH->cctk_levfac[0] > 1 || GH->cctk_convlevel > 0))) { + memset (&file, 0, sizeof (file)); if (OpenFile (GH, basefilename, called_from, &file) < 0) { return (-1); @@ -282,9 +283,9 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, we need to try both file names. */ /* at first try with unchunked mode */ file->unchunked = 1; - IOUtil_PrepareFilename (GH, basefilename, file->filename, called_from, 0, - file->unchunked); - strcat (file->filename, ".ieee"); + free (file->filename); + file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee", + called_from, 0, file->unchunked); if (myproc == 0) { @@ -303,9 +304,9 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, /* now try with chunked mode */ file->unchunked = 0; - IOUtil_PrepareFilename (GH, basefilename, file->filename, called_from, 0, - file->unchunked); - strcat (file->filename, ".ieee"); + free (file->filename); + file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee", + called_from, 0,file->unchunked); if (CCTK_Equals (verbose, "full")) { @@ -439,9 +440,11 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, /* Determine the IO processors for each node and the corresponding checkpoint file */ file->ioproc = myproc - (myproc % file->ioproc_every); - IOUtil_PrepareFilename (GH, basefilename, file->filename, called_from, - file->ioproc/file->ioproc_every, file->unchunked); - strcat (file->filename, ".ieee"); + free (file->filename); + file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee", + called_from, + file->ioproc / file->ioproc_every, + file->unchunked); /* Open chunked files on other IO processors */ if (myproc == file->ioproc && myproc != 0) diff --git a/src/ioFlexGH.h b/src/ioFlexGH.h index 5edf70a..d707288 100644 --- a/src/ioFlexGH.h +++ b/src/ioFlexGH.h @@ -97,7 +97,7 @@ typedef struct { int is_IEEEIO_file; /* flag indicating valid file info */ IOFile fid; /* IEEEIO file handle */ - char filename[1024]; /* complete file name for recovery */ + char *filename; /* complete file name for recovery */ CCTK_INT nprocs; /* number of total processors */ CCTK_INT ioproc; /* the associated IO processor */ CCTK_INT ioproc_every; /* how many IO processors there are */ -- cgit v1.2.3