From e81e16ef8df3930fe2a9f51cf5d3c7a308711489 Mon Sep 17 00:00:00 2001 From: Dave Hansen Date: Thu, 5 Feb 2009 12:50:25 -0800 Subject: [PATCH] Replace O_DIRECT with posix_fadvise(). This is easier to compile and does not draw Linus's ire http://lkml.org/lkml/2007/1/10/233. --- eyefi-config.c | 20 ++++++++++---------- eyefi-config.h | 4 ++-- eyefi-linux.c | 24 ++++++++++++------------ eyefi-unix.c | 1 + 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/eyefi-config.c b/eyefi-config.c index a4ddd86..f4518fa 100755 --- a/eyefi-config.c +++ b/eyefi-config.c @@ -185,7 +185,7 @@ static char *eyefi_file(enum eyefi_file file) void read_from(enum eyefi_file __file) { - int ret, retcntl; + int ret; int fd; char *file = eyefi_file(__file); @@ -195,11 +195,7 @@ retry: fd = open(file, O_RDONLY); if (fd < 0) open_error(file, fd); - retcntl = fd_dont_cache(fd); - if (retcntl < 0) { - perror("bad fcntl"); - exit(1); - } + fd_flush(fd); ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE); if (eyefi_debug_level > 3) dumpbuf(eyefi_buf, 128); @@ -209,7 +205,7 @@ retry: goto retry; exit(1); } - debug_printf(4, "read '%s': bytes: %d fcntl: %d\n", file, ret, retcntl); + debug_printf(4, "read '%s': bytes: %d\n", file, ret); /* * There was a time when I was carefully recording how each response * looked, and I counted the zeros in each response. I don't care @@ -253,12 +249,14 @@ void write_to(enum eyefi_file __file, void *stuff, int len) fd = open(file, O_RDWR|O_CREAT, 0600); if (fd < 0 ) open_error(file, fd); - ret = fd_dont_cache(fd); - if (ret < 0) - open_error(file, ret); if (eyefi_debug_level > 3) dumpbuf(eyefi_buf, 128); ret = write(fd, eyefi_buf, EYEFI_BUF_SIZE); + if (ret < 0) + open_error(file, ret); + ret = fd_flush(fd); + if (ret < 0) + open_error(file, ret); close(fd); debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", ret, file, len); if (ret < 0) { @@ -522,11 +520,13 @@ void testit0(void) perror("fdout"); if (fdin <= 0 || fdout <= 0) exit(1); + fd_flush(fdin); i = read(fdin, &fwbuf[0], 524288); perror("read"); if (i != 524288) exit(2); i = write(fdout, &fwbuf[0], 524288); + fd_flush(fdout); perror("write"); if (i != 524288) exit(3); diff --git a/eyefi-config.h b/eyefi-config.h index 27c0927..1306a50 100644 --- a/eyefi-config.h +++ b/eyefi-config.h @@ -27,7 +27,7 @@ extern int eyefi_debug_level; #define exit(i) return #define perror(i) do{}while(0) #define system(i) do{}while(0) -#define fd_dont_cache(fd) (0) +#define fd_flush(fd) (0) #define assert(x) do{}while(0) #define output_flush() do{}while(0) @@ -65,7 +65,7 @@ extern int eyefi_printf(const char *fmt, ...); /* * These have to be created by the unix variants */ -extern int fd_dont_cache(int); +extern int fd_flush(int); /* * Do some kernel-style types to make diff --git a/eyefi-linux.c b/eyefi-linux.c index 8f5cd94..224358c 100644 --- a/eyefi-linux.c +++ b/eyefi-linux.c @@ -1,12 +1,7 @@ #include "eyefi-config.h" -// Geez there has to be a better way to do this - -#ifdef __i386 -#define O_DIRECT 00040000 /* direct disk access hint */ -#else -#define O_DIRECT 0200000 /* direct disk access hint - currently ignored */ -#endif +#include +#include static int atoo(char o) { @@ -59,9 +54,13 @@ static char *replace_escapes(char *str) return str; } -int fd_dont_cache(int fd) +int fd_flush(int fd) { - return fcntl(fd, F_SETFL, O_DIRECT); + int ret; + ret = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); + if (ret) + perror("posix_fadvise() failed"); + return ret; } @@ -116,9 +115,10 @@ char *locate_eyefi_mount(void) return &eyefi_mount[0]; debug_printf(0, "unable to locate Eye-Fi card\n"); - if (eyefi_debug_level < 5) - debug_printf(0, "please run with '-d5' option and report the output\n"); - else { + if (eyefi_debug_level < 5) { + debug_printf(0, "Please check that your card is inserted and mounted\n"); + debug_printf(0, "If you still have issues, please re-run with the '-d5' option and report the output\n"); + } else { debug_printf(0, "----------------------------------------------\n"); debug_printf(0, "Debug information:\n"); system("cat /proc/mounts >&2"); diff --git a/eyefi-unix.c b/eyefi-unix.c index c7abe2e..a11926a 100755 --- a/eyefi-unix.c +++ b/eyefi-unix.c @@ -119,6 +119,7 @@ int try_connection_to(char *essid, char *ascii_password) u8 last_rsp = -1; char rsp = '\0'; + ret = -1; for (i=0; i < 200; i++) { struct byte_response *r; issue_noarg_command('s'); -- 2.45.0