From e99ffb4b0b09d883666e621eba0bb41ac4233d9f Mon Sep 17 00:00:00 2001
From: "Suren A. Chilingaryan" <csa@dside.dyndns.org>
Date: Tue, 13 Dec 2011 20:37:47 +0100
Subject: Properly detect /dev/null as raw device and do not set DIRECT flag on
 raw devices

---
 default.c    | 13 ++++++++++---
 fastwriter.c |  3 +--
 sysinfo.c    |  6 +++---
 sysinfo.h    |  4 ++--
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/default.c b/default.c
index 02b9f6a..6f9d1cb 100644
--- a/default.c
+++ b/default.c
@@ -61,7 +61,7 @@ int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags
     
     fastwriter_default_t *ctx;
 
-    err = get_file_fs(name, sizeof(fs) - 1, fs);
+    err = fastwriter_get_file_fs(name, sizeof(fs) - 1, fs);
     if (err) return err;
     
     ctx = (fastwriter_default_t*)malloc(sizeof(fastwriter_default_t));
@@ -72,7 +72,6 @@ int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags
     fw->ctx = ctx;
 
 #ifdef SYNC_MODE
-    open_flags |= O_DIRECT;
     ctx->sync_mode = 1;
 #endif /* SYNC_MODE */
 
@@ -82,6 +81,9 @@ int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags
 	ctx->wr_block = EXT4_WRITEBLOCK;
 	ctx->pa_block = 0;
 	ctx->prior_size = (size_t)-1;
+#ifdef SYNC_MODE
+	ctx->sync_mode = 0;
+#endif /* SYNC_MODE */
     } else if (!strcmp(fs, "ext4")) {
 	ctx->wr_block = EXT4_WRITEBLOCK;
 	ctx->pa_block = EXT4_PREALLOCATE;
@@ -96,6 +98,12 @@ int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags
 	ctx->pa_block = 0;
     }
     
+#ifdef SYNC_MODE
+    if (ctx->sync_mode) {
+	open_flags |= O_DIRECT;
+    }
+#endif /* SYNC_MODE */
+
     if (flags&FASTWRITER_FLAGS_OVERWRITE)
 	open_flags |= O_TRUNC;
 
@@ -180,7 +188,6 @@ int fastwriter_default_write(fastwriter_t *fw, fastwriter_write_flags_t flags, s
     
     do {
 	res = write(ctx->fd, data + sum, size + delta - sum);
-//	printf("%i %i %p %zu %i\n", res, ctx->fd, data, size, delta);
 	if (res < 0) {
 	    *written = sum;
 	    return errno;
diff --git a/fastwriter.c b/fastwriter.c
index 529acd6..84ef022 100644
--- a/fastwriter.c
+++ b/fastwriter.c
@@ -57,7 +57,7 @@ int fastwriter_open(fastwriter_t *ctx, const char *name, fastwriter_flags_t flag
         ctx->size = FASTWRITER_DEFAULT_BUFFER_SIZE;
 	break;
      case FASTWRITER_BUFFER_MAX:
-        ctx->size = get_free_memory();
+        ctx->size = fastwriter_get_free_memory();
 
 	if ((ctx->size - FASTWRITER_RESERVE_MEMORY) < FASTWRITER_DEFAULT_BUFFER_SIZE)
     	    ctx->size = FASTWRITER_DEFAULT_BUFFER_SIZE;
@@ -266,7 +266,6 @@ int fastwriter_push(fastwriter_t *ctx, size_t size, const void *data) {
     if (ctx->pos < ctx->tail) end = ctx->tail;
     else end = ctx->size;
 
-
     part1 = end - ctx->pos;
 
     if (part1 < size) {
diff --git a/sysinfo.c b/sysinfo.c
index 3805dcc..52354e7 100644
--- a/sysinfo.c
+++ b/sysinfo.c
@@ -48,7 +48,7 @@ static int compare_mem_table_structs(const void *a, const void *b){
   return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
 }
 
-size_t get_free_memory(void){
+size_t fastwriter_get_free_memory(void){
   char buf[4096];
   unsigned long kb_main_buffers, kb_main_cached, kb_main_free;
   char namebuf[16]; /* big enough to hold any row name */
@@ -92,7 +92,7 @@ nextline:
 }
 
 
-int get_file_fs(const char *fname, size_t size, char *fs) {
+int fastwriter_get_file_fs(const char *fname, size_t size, char *fs) {
   int err = 0;
   char buf[4096];
   char *fn;
@@ -115,7 +115,7 @@ int get_file_fs(const char *fname, size_t size, char *fs) {
   }
   
   if (!stat(fn, &st)) {
-    if (S_ISBLK(st.st_mode)) {
+    if (!S_ISREG(st.st_mode)) {
 	strcpy(fs, "raw");
 	goto clean;
     }
diff --git a/sysinfo.h b/sysinfo.h
index d5636a7..7fe367c 100644
--- a/sysinfo.h
+++ b/sysinfo.h
@@ -1,7 +1,7 @@
 #ifndef _PCITOOL_SYSINFO_H
 #define _PCITOOL_SYSINFO_H
 
-size_t get_free_memory();
-int get_file_fs(const char *fname, size_t size, char *fs);
+size_t fastwriter_get_free_memory();
+int fastwriter_get_file_fs(const char *fname, size_t size, char *fs);
 
 #endif /* _PCITOOL_SYSINFO_H */
-- 
cgit v1.2.3