aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2013-02-22 18:12:20 +0100
committerThomas White <taw@physics.org>2013-02-22 18:12:20 +0100
commit2c439230bad3907f5fc93b3e69ade0d5a5acc4b4 (patch)
tree7fea1fe4854fff36709d202b4c6192d72f24e7ce
parentfe8352bf60638f0da55669bd9e55a75ad7e300e4 (diff)
Avoid the use of stdio for writing to final stream
Something really weird goes on when using stdio
-rw-r--r--src/im-sandbox.c35
-rw-r--r--src/im-sandbox.h2
-rw-r--r--src/indexamajig.c11
3 files changed, 29 insertions, 19 deletions
diff --git a/src/im-sandbox.c b/src/im-sandbox.c
index 1ba6b2ce..787e43f9 100644
--- a/src/im-sandbox.c
+++ b/src/im-sandbox.c
@@ -74,8 +74,8 @@ struct sb_reader
FILE **fhs;
int *fds;
- /* Final output file handle */
- FILE *ofh;
+ /* Final output fd */
+ int ofd;
};
@@ -277,8 +277,17 @@ static time_t get_monotonic_seconds()
#endif
+size_t vol = 0;
-static int pump_chunk(FILE *fh, FILE *ofh)
+
+static ssize_t lwrite(int fd, const char *a)
+{
+ size_t l = strlen(a);
+ return write(fd, a, l);
+}
+
+
+static int pump_chunk(FILE *fh, int ofd)
{
int chunk_started = 0;
@@ -294,25 +303,23 @@ static int pump_chunk(FILE *fh, FILE *ofh)
/* Whoops, connection lost */
if ( chunk_started ) {
ERROR("EOF during chunk!\n");
- fprintf(ofh, "Chunk is unfinished!\n");
- fprintf(ofh, CHUNK_END_MARKER"\n");
+ lwrite(ofd, "Unfinished chunk!\n");
+ lwrite(ofd, CHUNK_END_MARKER"\n");
} /* else normal end of output */
return 1;
- } else {
- ERROR("fgets() failed: %s\n", strerror(errno));
}
- break;
+
+ ERROR("fgets() failed: %s\n", strerror(errno));
+ return 1;
}
if ( strcmp(line, "FLUSH\n") == 0 ) break;
-
- fprintf(ofh, "%s", line);
+ lwrite(ofd, line);
if ( strcmp(line, CHUNK_END_MARKER"\n") == 0 ) break;
if ( strcmp(line, CHUNK_START_MARKER"\n") == 0 ) break;
-
} while ( 1 );
return 0;
}
@@ -427,7 +434,7 @@ static void *run_reader(void *rdv)
/* If the chunk cannot be read, assume the connection
* is broken and that the process will die soon. */
- if ( pump_chunk(rd->fhs[i], rd->ofh) ) {
+ if ( pump_chunk(rd->fhs[i], rd->ofd) ) {
/* remove_pipe() assumes that the caller is
* holding rd->lock ! */
remove_pipe(rd, i);
@@ -632,7 +639,7 @@ static void handle_zombie(struct sandbox *sb)
void create_sandbox(struct index_args *iargs, int n_proc, char *prefix,
int config_basename, FILE *fh, char *use_this_one_instead,
- FILE *ofh, int argc, char *argv[])
+ int ofd, int argc, char *argv[])
{
int i;
int allDone;
@@ -669,7 +676,7 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix,
sb->reader->fds = NULL;
sb->reader->fhs = NULL;
- sb->reader->ofh = ofh;
+ sb->reader->ofd = ofd;
sb->stream_pipe_write = calloc(n_proc, sizeof(int));
if ( sb->stream_pipe_write == NULL ) {
diff --git a/src/im-sandbox.h b/src/im-sandbox.h
index 7df41738..9248b226 100644
--- a/src/im-sandbox.h
+++ b/src/im-sandbox.h
@@ -38,5 +38,5 @@
extern void create_sandbox(struct index_args *iargs, int n_proc, char *prefix,
int config_basename, FILE *fh,
- char *use_this_one_instead, FILE *stream,
+ char *use_this_one_instead, int streamfd,
int argc, char *argv[]);
diff --git a/src/indexamajig.c b/src/indexamajig.c
index b88469b8..41662d5f 100644
--- a/src/indexamajig.c
+++ b/src/indexamajig.c
@@ -44,6 +44,9 @@
#include <getopt.h>
#include <hdf5.h>
#include <gsl/gsl_errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
@@ -158,7 +161,7 @@ int main(int argc, char *argv[])
char *filename = NULL;
char *outfile = NULL;
FILE *fh;
- FILE *ofh;
+ int ofd;
char *rval = NULL;
int config_checkprefix = 1;
int config_basename = 0;
@@ -510,8 +513,8 @@ int main(int argc, char *argv[])
iargs.cell = NULL;
}
- ofh = fopen(outfile, "w");
- if ( ofh == NULL ) {
+ ofd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
+ if ( ofd == -1 ) {
ERROR("Failed to open stream '%s'\n", outfile);
return 1;
}
@@ -553,7 +556,7 @@ int main(int argc, char *argv[])
iargs.ipriv = ipriv;
create_sandbox(&iargs, n_proc, prefix, config_basename, fh,
- use_this_one_instead, ofh, argc, argv);
+ use_this_one_instead, ofd, argc, argv);
free(prefix);