commit dabd5f394f21462910b91be9ae7b1dce4bc4ae62
parent 29f1c4044fa51e46890c6618992c82812b6ec463
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 9 Aug 2018 15:38:09 +0200
Fix several grid issues
Diffstat:
4 files changed, 30 insertions(+), 12 deletions(-)
diff --git a/src/htrdr.c b/src/htrdr.c
@@ -220,7 +220,7 @@ htrdr_init
output_name = "<stdout>";
} else {
res = open_output_stream
- (htrdr, args->output, args->force_overwriting, &htrdr->output);
+ (htrdr, args->output, 0/*read*/, args->force_overwriting, &htrdr->output);
if(res != RES_OK) goto error;
output_name = args->output;
}
@@ -395,24 +395,29 @@ extern LOCAL_SYM res_T
open_output_stream
(struct htrdr* htrdr,
const char* filename,
+ const int read,
int force_overwrite,
FILE** out_fp)
{
FILE* fp = NULL;
int fd = -1;
+ const char* mode;
res_T res = RES_OK;
ASSERT(htrdr && filename && out_fp);
+ mode = read ? "w+" : "w";
+
if(force_overwrite) {
- fp = fopen(filename, "w");
+ fp = fopen(filename, mode);
if(!fp) {
htrdr_log_err(htrdr, "could not open the output file `%s'.\n", filename);
goto error;
}
} else {
- fd = open(filename, O_CREAT|O_WRONLY|O_EXCL|O_TRUNC, S_IRUSR|S_IWUSR);
+ const int access_flags = read ? O_RDWR : O_WRONLY;
+ fd = open(filename, O_CREAT|O_EXCL|O_TRUNC|access_flags, S_IRUSR|S_IWUSR);
if(fd >= 0) {
- fp = fdopen(fd, "w");
+ fp = fdopen(fd, mode);
if(fp == NULL) {
htrdr_log_err(htrdr, "could not open the output file `%s'.\n", filename);
goto error;
diff --git a/src/htrdr.h b/src/htrdr.h
@@ -20,6 +20,15 @@
#include <rsys/ref_count.h>
#include <rsys/str.h>
+/* Helper macro that asserts if the invocation of the htrdr function `Func'
+ * returns an error. One should use this macro on htcp function calls for
+ * which no explicit error checking is performed */
+#ifndef NDEBUG
+ #define HTRDR(Func) ASSERT(htrdr_ ## Func == RES_OK)
+#else
+ #define HTRDR(Func) htrdr_ ## Func
+#endif
+
/* Forward declarations */
struct htrdr_args;
struct htrdr_buffer;
diff --git a/src/htrdr_c.h b/src/htrdr_c.h
@@ -37,6 +37,7 @@ extern LOCAL_SYM res_T
open_output_stream
(struct htrdr* htrdr,
const char* filename,
+ const int read, /* Enable read access */
int force_overwrite,
FILE** out_fp);
diff --git a/src/htrdr_grid.c b/src/htrdr_grid.c
@@ -84,7 +84,7 @@ htrdr_grid_create
long grid_offset;
int n;
res_T res = RES_OK;
- ASSERT(!htrdr || !out_grid || !filename || !definition);
+ ASSERT(htrdr && out_grid && filename && definition);
if(!definition[0] || !definition[1] || !definition[2]) {
htrdr_log_err(htrdr, "%s: invalid definition [%lu, %lu, %lu].\n", FUNC_NAME,
@@ -115,13 +115,13 @@ htrdr_grid_create
grid->pagesize = (size_t)sysconf(_SC_PAGESIZE);
grid->htrdr = htrdr;
- res = open_output_stream(htrdr, filename, force_overwrite, &grid->fp);
+ res = open_output_stream(htrdr, filename, 1, force_overwrite, &grid->fp);
if(res != RES_OK) goto error;
#define WRITE(Var, N, Name) { \
if(fwrite((Var), sizeof(*(Var)), (N), grid->fp) != (N)) { \
- htrdr_log_err(htrdr, "%s:%s: could not write `%s'.\n", \
- FUNC_NAME, filename, (Name)); \
+ htrdr_log_err(htrdr, "%s:%s: could not write `%s' -- %s.\n", \
+ FUNC_NAME, filename, (Name), strerror(errno)); \
res = RES_IO_ERR; \
goto error; \
} \
@@ -159,12 +159,15 @@ htrdr_grid_create
}
/* Write one char at the end of the file to position the EOF indicator */
- CHK(fseek(grid->fp, -1, SEEK_CUR) == 1);
+ CHK(fseek(grid->fp, -1, SEEK_CUR) != -1);
WRITE(&byte, 1, "Dummy Byte");
#undef WRITE
+ /* Avoid to be positionned on the EOF */
+ rewind(grid->fp);
+
grid->data = mmap(NULL, grid_sz, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_POPULATE, fileno(grid->fp), grid_offset);
+ MAP_SHARED|MAP_POPULATE, fileno(grid->fp), grid_offset);
if(grid->data == MAP_FAILED) {
htrdr_log_err(htrdr, "%s:%s: could not map the grid data -- %s.\n",
FUNC_NAME, filename, strerror(errno));
@@ -239,7 +242,7 @@ htrdr_grid_open
goto error;
}
- READ(&grid->definition, 3, "definition");
+ READ(grid->definition, 3, "definition");
if(!grid->definition[0] || !grid->definition[1] || !grid->definition[2]) {
htrdr_log_err(htrdr, "%s:%s: invalid definition [%lu, %lu, %lu].\n",
FUNC_NAME, filename,
@@ -259,7 +262,7 @@ htrdr_grid_open
grid_sz = ALIGN_SIZE(grid_sz, grid->pagesize);
grid->data = mmap(NULL, grid_sz, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_POPULATE, fileno(grid->fp), (off_t)grid_offset);
+ MAP_SHARED|MAP_POPULATE, fileno(grid->fp), (off_t)grid_offset);
if(grid->data == MAP_FAILED) {
htrdr_log_err(htrdr, "%s:%s: could not map the grid data -- %s.\n",