Skip to content

Commit

Permalink
Merge pull request #298 from tim-griesbach/fix-ftell-usage
Browse files Browse the repository at this point in the history
Ensure C standard compliant `fseek` usage
  • Loading branch information
cburstedde authored Jun 3, 2024
2 parents 95b9ed6 + 6b43371 commit 12ca178
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/p4est.c
Original file line number Diff line number Diff line change
Expand Up @@ -3477,21 +3477,23 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
size_t data_size, qbuf_size, comb_size, head_count;
size_t zz, zcount;
uint64_t *u64a;
FILE *file;
#ifdef P4EST_MPIIO_WRITE
MPI_File mpifile;
MPI_Offset mpipos;
MPI_Offset mpithis;
#else
FILE *file;
long fthis;
#endif
p4est_topidx_t jt, num_trees;
p4est_gloidx_t *pertree;
p4est_tree_t *tree;
p4est_quadrant_t *q;
char *lbuf, *bp;
char nul[2] = "\0";
p4est_qcoord_t *qpos;
sc_array_t *tquadrants;
sc_io_sink_t *sink = NULL;

P4EST_GLOBAL_PRODUCTIONF ("Into " P4EST_STRING "_save %s\n", filename);
p4est_log_indent_push ();
Expand Down Expand Up @@ -3519,22 +3521,17 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
p4est_comm_count_pertree (p4est, pertree);

if (rank == 0) {
p4est_connectivity_save (filename, p4est->connectivity);

/* open file after writing connectivity to it */
file = fopen (filename, "ab");
SC_CHECK_ABORT (file != NULL, "file open");

/* explicitly seek to end to avoid bad ftell return value on Windows */
retval = fseek (file, 0, SEEK_END);
SC_CHECK_ABORT (retval == 0, "file seek");
sink = sc_io_sink_new (SC_IO_TYPE_FILENAME, SC_IO_MODE_WRITE,
SC_IO_ENCODE_NONE, filename);
SC_CHECK_ABORT (sink != NULL, "file open");
p4est_connectivity_sink (p4est->connectivity, sink);

/* align the start of the header */
fpos = ftell (file);
fpos = sink->bytes_out;
SC_CHECK_ABORT (fpos > 0, "first file tell");
while (fpos % align != 0) {
retval = fputc ('\0', file);
SC_CHECK_ABORT (retval == 0, "first file align");
retval = sc_io_sink_write (sink, nul, 1);
SC_CHECK_ABORT (retval == SC_IO_ERROR_NONE, "first file align");
++fpos;
}

Expand All @@ -3559,31 +3556,28 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
for (jt = 0; jt < num_trees; ++jt) {
u64a[headc + save_num_procs + jt] = (uint64_t) pertree[jt + 1];
}
sc_fwrite (u64a, sizeof (uint64_t), head_count,
file, "write header information");
retval = sc_io_sink_write (sink, u64a, sizeof (uint64_t) * head_count);
SC_CHECK_ABORT (retval == SC_IO_ERROR_NONE, "write header information");
P4EST_FREE (u64a);
fpos += head_count * sizeof (uint64_t);

/* align the start of the quadrants */
fpos = ftell (file);
fpos = sink->bytes_out;
SC_CHECK_ABORT (fpos > 0, "second file tell");
while (fpos % align != 0) {
retval = fputc ('\0', file);
SC_CHECK_ABORT (retval == 0, "second file align");
retval = sc_io_sink_write (sink, nul, 1);
SC_CHECK_ABORT (retval == SC_IO_ERROR_NONE, "second file align");
++fpos;
}

#ifdef P4EST_MPIIO_WRITE
/* we will close the sequential access to the file */
sc_fflush_fsync_fclose (file);
file = NULL;
sc_io_sink_destroy (sink);
sink = NULL;
#else
/* file is still open for sequential write mode */
#endif
}
else {
file = NULL;
}
P4EST_FREE (pertree);

#ifndef P4EST_MPIIO_WRITE
Expand All @@ -3599,6 +3593,9 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
file = fopen (filename, "rb+");
SC_CHECK_ABORT (file != NULL, "file open");
}
else {
file = NULL;
}
#else
/* Every core opens the file in append mode -- file must exist */
mpiret = sc_MPI_Barrier (p4est->mpicomm);
Expand Down Expand Up @@ -3649,7 +3646,13 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
bp += comb_size;
}
#ifndef P4EST_MPIIO_WRITE
sc_fwrite (lbuf, comb_size, zcount, file, "write quadrants");
if (p4est->mpirank == 0) {
retval = sc_io_sink_write (sink, lbuf, comb_size * zcount);
SC_CHECK_ABORT (retval == SC_IO_ERROR_NONE, "write quadrants");
}
else {
sc_fwrite (lbuf, comb_size, zcount, file, "write quadrants");
}
#else
sc_mpi_write (mpifile, lbuf, comb_size * zcount, MPI_BYTE,
"write quadrants");
Expand All @@ -3658,8 +3661,14 @@ p4est_save_ext (const char *filename, p4est_t * p4est,
}

#ifndef P4EST_MPIIO_WRITE
sc_fflush_fsync_fclose (file);
file = NULL;
if (p4est->mpirank == 0) {
sc_io_sink_destroy (sink);
sink = NULL;
}
else {
sc_fflush_fsync_fclose (file);
file = NULL;
}

/* initiate sequential synchronization */
#ifdef P4EST_ENABLE_MPI
Expand Down

0 comments on commit 12ca178

Please sign in to comment.